std_string.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "std_string.h"
  2. #include <bfc/assert.h>
  3. int WCSICOLL(const wchar_t *str1, const wchar_t *str2)
  4. {
  5. return lstrcmpiW(str1, str2);
  6. }
  7. int WCSICMP(const wchar_t *str1, const wchar_t *str2)
  8. {
  9. // WCSICMP is supposed to be used for string-lookup kinds of code, so we need to make sure it's done in a non-locale-aware way
  10. return CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, str1, -1, str2, -1)-2;
  11. //return _wcsicmp(str1, str2);
  12. }
  13. int WCSNICMP(const wchar_t *str1, const wchar_t *str2, size_t len)
  14. {
  15. return _wcsnicmp(str1, str2, len);
  16. }
  17. wchar_t *WCSTOK(wchar_t *str, const wchar_t *sep, wchar_t **last)
  18. {
  19. return wcstok(str, sep);
  20. }
  21. bool ISALPHA(wchar_t alpha)
  22. {
  23. return IsCharAlphaW(alpha)==TRUE;
  24. }
  25. bool ISDIGIT(wchar_t digit)
  26. {
  27. WORD type=0;
  28. GetStringTypeExW(LOCALE_USER_DEFAULT, CT_CTYPE1, &digit, 1, &type);
  29. return !!(type&C1_DIGIT);
  30. }
  31. bool ISSPACE(wchar_t space)
  32. {
  33. WORD type=0;
  34. GetStringTypeExW(LOCALE_USER_DEFAULT, CT_CTYPE1, &space, 1, &type);
  35. return !!(type&C1_SPACE);
  36. }
  37. bool ISPUNCT(wchar_t punct)
  38. {
  39. WORD type=0;
  40. GetStringTypeExW(LOCALE_USER_DEFAULT, CT_CTYPE1, &punct, 1, &type);
  41. return !!(type&C1_PUNCT);
  42. }
  43. char *STRSTR(const char *str1, const char *str2) {
  44. ASSERT(str1!=NULL);
  45. ASSERT(str2!=NULL);
  46. ASSERT(str1 != str2);
  47. return const_cast<char *>(strstr(str1, str2));
  48. }
  49. wchar_t *WCSCASESTR(const wchar_t *str1, const wchar_t *str2)
  50. {
  51. ASSERT(str1!=NULL);
  52. ASSERT(str2!=NULL);
  53. wchar_t *p;
  54. size_t len = wcslen(str2);
  55. for (p = (wchar_t *)str1; *p; p++)
  56. {
  57. if (!_wcsnicmp(p, str2, len))
  58. return p;
  59. }
  60. return NULL;
  61. }
  62. void STRCPY(char *dest, const char *src) {
  63. ASSERT(dest != NULL);
  64. ASSERT(src != NULL);
  65. ASSERT(dest != src);
  66. strcpy(dest, src);
  67. //INLINE
  68. }
  69. void STRNCPY(char *dest, const char *src, int maxchar) {
  70. ASSERT(dest != NULL);
  71. ASSERT(src != NULL);
  72. strncpy(dest, src, maxchar);
  73. //INLINE
  74. }
  75. void WCSCPYN(wchar_t *dest, const wchar_t *src, size_t maxchar)
  76. {
  77. ASSERT(dest != NULL);
  78. ASSERT(src != NULL);
  79. StringCchCopyW(dest, maxchar, src);
  80. }
  81. char *STRCHR(const char *str, int c) {
  82. ASSERT(str != NULL);
  83. return const_cast<char *>(strchr(str, c));
  84. //INLINE
  85. }
  86. void STRCAT(char *dest, const char *append) {
  87. ASSERT(dest != NULL);
  88. ASSERT(append != NULL);
  89. ASSERT(dest != append);
  90. strcat(dest, append);
  91. }
  92. unsigned long STRTOUL(const char *s, char **p, int rx) {
  93. ASSERT(s != NULL);
  94. ASSERT(p != NULL);
  95. return strtoul(s,p,rx);
  96. }
  97. wchar_t *WCSDUP(const wchar_t *ptr)
  98. {
  99. if (ptr == NULL) return NULL;
  100. size_t size = wcslen(ptr);
  101. wchar_t *ret = (wchar_t *)MALLOC((size + 1) * sizeof(wchar_t));
  102. if (ret != NULL)
  103. {
  104. WCSCPYN(ret, ptr, size+1);
  105. }
  106. return ret;
  107. }
  108. int STRLEN(const char *str) {
  109. ASSERT(str != NULL);
  110. return (int)strlen(str);
  111. }
  112. int STRCMP(const char *str1, const char *str2) {
  113. ASSERT(str1!=NULL);
  114. ASSERT(str2!=NULL);
  115. return strcmp(str1, str2);
  116. }
  117. int STRCMPSAFE(const char *str1, const char *str2, const char *defval1, const char *defval2) {
  118. if (str1 == NULL) str1 = defval1;
  119. if (str2 == NULL) str2 = defval2;
  120. return STRCMP(str1, str2);
  121. }
  122. int WCSCMPSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1, const wchar_t *defval2) {
  123. if (str1 == NULL) str1 = defval1;
  124. if (str2 == NULL) str2 = defval2;
  125. return wcscmp(str1, str2);
  126. }
  127. int STRICMP(const char *str1, const char *str2) {
  128. ASSERT(str1!=NULL);
  129. ASSERT(str2!=NULL);
  130. #if defined(WIN32)
  131. return _stricmp(str1, str2);
  132. #elif defined(LINUX)
  133. return strcasecmp(str1, str2);
  134. #endif
  135. }
  136. int STRICMPSAFE(const char *str1, const char *str2, const char *defval1, const char *defval2) {
  137. if (str1 == NULL) str1 = defval1;
  138. if (str2 == NULL) str2 = defval2;
  139. return STRICMP(str1, str2);
  140. }
  141. int WCSICMPSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1, const wchar_t *defval2)
  142. {
  143. if (str1 == NULL) str1 = defval1;
  144. if (str2 == NULL) str2 = defval2;
  145. return WCSICMP(str1, str2);
  146. }
  147. int WCSEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1, const wchar_t *defval2) {
  148. return !WCSCMPSAFE(str1, str2, defval1, defval2);
  149. }
  150. int STRCASEEQLSAFE(const char *str1, const char *str2, const char *defval1, const char *defval2) {
  151. return !STRICMPSAFE(str1, str2, defval1, defval2);
  152. }
  153. int WCSCASEEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1, const wchar_t *defval2)
  154. {
  155. return !WCSICMPSAFE(str1, str2, defval1, defval2);
  156. }
  157. //FG> sorry brennan, this need to not be a const :)
  158. void STRTOUPPER(char *p)
  159. {
  160. CharUpperA(p);
  161. }
  162. void STRTOLOWER(char *p)
  163. {
  164. CharLowerA(p);
  165. }
  166. void WCSTOUPPER(wchar_t *p)
  167. {
  168. CharUpperW(p);
  169. }
  170. void KEYWORDUPPER(wchar_t *p)
  171. {/*
  172. if (p)
  173. {
  174. while (p && *p)
  175. {
  176. *p = towupper(*p);
  177. p++;
  178. }
  179. }*/
  180. int l = (int)wcslen(p);
  181. // from MSDN - If LCMAP_UPPERCASE or LCMAP_LOWERCASE is set, the lpSrcStr and lpDestStr pointers can be the same.
  182. LCMapStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), LCMAP_UPPERCASE, p, l, p, l);
  183. }
  184. void WCSTOLOWER(wchar_t *p)
  185. {
  186. CharLowerW(p);
  187. }
  188. int STRNICMP(const char *str1, const char *str2, int l) {
  189. ASSERT(str1!=NULL);
  190. ASSERT(str2!=NULL);
  191. ASSERT(l>=0);
  192. while (TOUPPER(*str1) == TOUPPER(*str2) && *str1 != 0 && *str2 != 0 && l--)
  193. str1++, str2++;
  194. if (l == 0) return 0;
  195. return (*str2 - *str1);
  196. }
  197. int STRNCASEEQL(const char *str1, const char *str2, int l) {
  198. ASSERT(str1!=NULL);
  199. ASSERT(str2!=NULL);
  200. return STRNICMP(str1, str2, l) == 0;
  201. }
  202. int STREQL(const char *str1, const char *str2) {
  203. ASSERT(str1!=NULL);
  204. ASSERT(str2!=NULL);
  205. return (strcmp(str1, str2) == 0);
  206. }
  207. int STRCASEEQL(const char *str1, const char *str2) {
  208. ASSERT(str1!=NULL);
  209. ASSERT(str2!=NULL);
  210. #ifdef WIN32
  211. return (_stricmp(str1, str2)==0);
  212. #else
  213. return (strcasecmp(str1, str2)==0);
  214. #endif
  215. }