stringFunctions.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _STRINGFUNCTIONS_H_
  23. #define _STRINGFUNCTIONS_H_
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <cstdarg>
  28. #ifndef _TORQUE_TYPES_H_
  29. #include "platform/types.h"
  30. #endif
  31. #ifndef _PLATFORMASSERT_H_
  32. #include "platform/platformAssert.h"
  33. #endif
  34. #if defined(TORQUE_OS_WIN)
  35. // These standard functions are not defined on Win32 and other Microsoft platforms...
  36. #define strcasecmp _stricmp
  37. #define strncasecmp _strnicmp
  38. #if _MSC_VER < 1800
  39. #define strtof (float)strtod
  40. #endif // _MSC_VER < 1800
  41. #endif // defined(TORQUE_OS_WIN)
  42. #define DEBUG_CHECK_OVERFLOW 1
  43. //------------------------------------------------------------------------------
  44. // standard string functions [defined in platformString.cpp]
  45. /// @deprecated Use dStrcat(char *, const char *, dsize_t) instead
  46. inline char *dStrcat(char *dst, const char *src)
  47. {
  48. AssertFatal(false, "dStrcat without length is deprecated");
  49. return strcat(dst,src);
  50. }
  51. inline char *dStrcat(char *dst, const char *src, dsize_t len)
  52. {
  53. #ifdef DEBUG_CHECK_OVERFLOW
  54. if (strlen(src) >= len) {
  55. AssertWarn(false, "dStrcat out of range");
  56. }
  57. #endif
  58. return strncat(dst,src,len - 1); //Safety because strncat copies at most len+1 characters
  59. }
  60. inline char *dStrncat(char *dst, const char *src, dsize_t len)
  61. {
  62. return dStrcat(dst, src, len);
  63. }
  64. inline S32 dStrcmp(const char *str1, const char *str2)
  65. {
  66. return strcmp(str1, str2);
  67. }
  68. inline bool dStrIsEmpty(const char *src)
  69. {
  70. return src == 0 || src[0] == '\0';
  71. }
  72. inline S32 dStrncmp(const char *str1, const char *str2, dsize_t len)
  73. {
  74. return strncmp(str1, str2, len);
  75. }
  76. inline S32 dStricmp(const char *str1, const char *str2)
  77. {
  78. return strcasecmp( str1, str2 );
  79. }
  80. inline S32 dStrnicmp(const char *str1, const char *str2, dsize_t len)
  81. {
  82. return strncasecmp( str1, str2, len );
  83. }
  84. /// @deprecated Use strcpy(char *, const char *, dsize_t) instead
  85. inline char *dStrcpy(char *dst, const char *src)
  86. {
  87. AssertFatal(false, "dStrcpy without length is deprecated");
  88. return strcpy(dst,src);
  89. }
  90. inline char *dStrcpy(char *dst, const char *src, dsize_t len)
  91. {
  92. #ifdef DEBUG_CHECK_OVERFLOW
  93. if (strlen(src) >= len) {
  94. AssertWarn(false, "dStrcpy out of range");
  95. }
  96. #endif
  97. return strncpy(dst,src,len);
  98. }
  99. inline char *dStrncpy(char *dst, const char *src, dsize_t len)
  100. {
  101. return strncpy(dst,src,len);
  102. }
  103. inline dsize_t dStrlen(const char *str)
  104. {
  105. return strlen(str);
  106. }
  107. inline char *dStrchr(char *str, S32 c)
  108. {
  109. return strchr(str,c);
  110. }
  111. inline const char *dStrchr(const char *str, S32 c)
  112. {
  113. return strchr(str,c);
  114. }
  115. inline char *dStrrchr(char *str, S32 c)
  116. {
  117. return strrchr(str,c);
  118. }
  119. inline const char *dStrrchr(const char *str, S32 c)
  120. {
  121. return strrchr(str,c);
  122. }
  123. inline dsize_t dStrspn(const char *str, const char *set)
  124. {
  125. return strspn(str, set);
  126. }
  127. inline dsize_t dStrcspn(const char *str, const char *set)
  128. {
  129. return strcspn(str, set);
  130. }
  131. inline char *dStrstr(const char *str1, const char *str2)
  132. {
  133. return strstr((char *)str1,str2);
  134. }
  135. const char* dStristr( const char* str1, const char* str2 );
  136. char* dStristr( char* str1, const char* str2 );
  137. inline char *dStrtok(char *str, const char *sep)
  138. {
  139. return strtok(str, sep);
  140. }
  141. inline S32 dAtoi(const char *str)
  142. {
  143. return strtol(str, NULL, 10);
  144. }
  145. inline U32 dAtoui(const char *str, U32 base = 10)
  146. {
  147. return strtoul(str, NULL, base);
  148. }
  149. inline U16 dAtous(const char *str, U32 base = 10)
  150. {
  151. return strtoul(str, NULL, base);
  152. }
  153. inline F32 dAtof(const char *str)
  154. {
  155. return strtof(str, NULL);
  156. }
  157. inline F64 dAtod(const char *str)
  158. {
  159. return strtod(str, NULL);
  160. }
  161. inline char dToupper(const char c)
  162. {
  163. return toupper( c );
  164. }
  165. inline char dTolower(const char c)
  166. {
  167. return tolower( c );
  168. }
  169. inline bool dIsalnum(const char c)
  170. {
  171. return isalnum(c);
  172. }
  173. inline bool dIsalpha(const char c)
  174. {
  175. return isalpha(c);
  176. }
  177. inline bool dIsspace(const char c)
  178. {
  179. return isspace(c);
  180. }
  181. inline bool dIsdigit(const char c)
  182. {
  183. return isdigit(c);
  184. }
  185. inline bool dIsquote(const char c)
  186. {
  187. return ( c == '\"' );
  188. }
  189. //------------------------------------------------------------------------------
  190. // non-standard string functions [defined in stringFunctions.cpp]
  191. #define dStrdup(x) dStrdup_r(x, __FILE__, __LINE__)
  192. extern char *dStrdup_r(const char *src, const char*, dsize_t);
  193. extern char *dStrcpyl(char *dst, dsize_t dstSize, ...);
  194. extern char *dStrcatl(char *dst, dsize_t dstSize, ...);
  195. extern char *dStrupr(char *str);
  196. extern char *dStrlwr(char *str);
  197. extern char* dStrichr( char* str, char ch );
  198. extern const char* dStrichr( const char* str, char ch );
  199. extern S32 dStrcmp(const UTF16 *str1, const UTF16 *str2);
  200. extern S32 dStrnatcmp( const char* str1, const char* str2 );
  201. extern S32 dStrnatcasecmp( const char* str1, const char* str2 );
  202. inline bool dAtob(const char *str)
  203. {
  204. return !dStricmp(str, "true") || dAtof(str);
  205. }
  206. bool dStrEqual(const char* str1, const char* str2);
  207. bool dStrStartsWith(const char* str1, const char* str2);
  208. bool dStrEndsWith(const char* str1, const char* str2);
  209. char* dStripPath(const char* filename);
  210. int dStrrev(char* str);
  211. int dItoa(int n, char s[]);
  212. //------------------------------------------------------------------------------
  213. // standard I/O functions [defined in platformString.cpp]
  214. extern void dPrintf(const char *format, ...);
  215. extern S32 dVprintf(const char *format, va_list arglist);
  216. extern S32 dSprintf(char *buffer, U32 bufferSize, const char *format, ...);
  217. extern S32 dVsprintf(char *buffer, U32 bufferSize, const char *format, va_list arglist);
  218. extern S32 dSscanf(const char *buffer, const char *format, ...);
  219. #endif