stringFunctions.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. //------------------------------------------------------------------------------
  43. // standard string functions [defined in platformString.cpp]
  44. // Buffer size bounds checking "safe" versions of strcat and strcpy. Ideally you
  45. // should use these and check if they return >= dstSize and throw an error if so.
  46. extern S32 dStrlcat(char *dst, const char *src, dsize_t dstSize);
  47. extern S32 dStrlcpy(char *dst, const char *src, dsize_t dstSize);
  48. #ifdef UNSAFE_STRING_FUNCTIONS
  49. /// @deprecated Use dStrcat(char *, const char *, dsize_t) instead
  50. inline char *dStrcat(char *dst, const char *src)
  51. {
  52. AssertFatal(false, "dStrcat without length is deprecated");
  53. return strcat(dst,src);
  54. }
  55. #endif
  56. /// Concatenate strings.
  57. /// @note The third parameter is the size of the destination buffer like strlcat
  58. /// instead of the number of characters to copy like strncat. This is done
  59. /// under the assumption that being easier to use will make this safer.
  60. /// If you want the original behavior use dStrncat.
  61. inline char *dStrcat(char *dst, const char *src, dsize_t dstSize)
  62. {
  63. dStrlcat(dst, src, dstSize);
  64. return dst;
  65. }
  66. inline char *dStrncat(char *dst, const char *src, dsize_t len)
  67. {
  68. return strncat(dst, src, len);
  69. }
  70. inline S32 dStrcmp(const char *str1, const char *str2)
  71. {
  72. return strcmp(str1, str2);
  73. }
  74. inline bool dStrIsEmpty(const char *src)
  75. {
  76. return src == 0 || src[0] == '\0';
  77. }
  78. inline S32 dStrncmp(const char *str1, const char *str2, dsize_t len)
  79. {
  80. return strncmp(str1, str2, len);
  81. }
  82. inline S32 dStricmp(const char *str1, const char *str2)
  83. {
  84. return strcasecmp( str1, str2 );
  85. }
  86. inline S32 dStrnicmp(const char *str1, const char *str2, dsize_t len)
  87. {
  88. return strncasecmp( str1, str2, len );
  89. }
  90. #ifdef UNSAFE_STRING_FUNCTIONS
  91. /// @deprecated Use strcpy(char *, const char *, dsize_t) instead
  92. inline char *dStrcpy(char *dst, const char *src)
  93. {
  94. AssertFatal(false, "dStrcpy without length is deprecated");
  95. return strcpy(dst,src);
  96. }
  97. #endif
  98. inline char *dStrcpy(char *dst, const char *src, dsize_t dstSize)
  99. {
  100. dStrlcpy(dst, src, dstSize);
  101. return dst;
  102. }
  103. inline char *dStrncpy(char *dst, const char *src, dsize_t len)
  104. {
  105. return strncpy(dst,src,len);
  106. }
  107. inline dsize_t dStrlen(const char *str)
  108. {
  109. return strlen(str);
  110. }
  111. inline char *dStrchr(char *str, S32 c)
  112. {
  113. return strchr(str,c);
  114. }
  115. inline const char *dStrchr(const char *str, S32 c)
  116. {
  117. return strchr(str,c);
  118. }
  119. inline char *dStrrchr(char *str, S32 c)
  120. {
  121. return strrchr(str,c);
  122. }
  123. inline const char *dStrrchr(const char *str, S32 c)
  124. {
  125. return strrchr(str,c);
  126. }
  127. inline dsize_t dStrspn(const char *str, const char *set)
  128. {
  129. return strspn(str, set);
  130. }
  131. inline dsize_t dStrcspn(const char *str, const char *set)
  132. {
  133. return strcspn(str, set);
  134. }
  135. inline char *dStrstr(const char *str1, const char *str2)
  136. {
  137. return strstr((char *)str1,str2);
  138. }
  139. const char* dStristr( const char* str1, const char* str2 );
  140. char* dStristr( char* str1, const char* str2 );
  141. inline char *dStrtok(char *str, const char *sep)
  142. {
  143. return strtok(str, sep);
  144. }
  145. inline S32 dAtoi(const char *str)
  146. {
  147. return strtol(str, NULL, 10);
  148. }
  149. inline U32 dAtoui(const char *str, U32 base = 10)
  150. {
  151. return strtoul(str, NULL, base);
  152. }
  153. inline U16 dAtous(const char *str, U32 base = 10)
  154. {
  155. return strtoul(str, NULL, base);
  156. }
  157. inline F32 dAtof(const char *str)
  158. {
  159. return strtof(str, NULL);
  160. }
  161. inline F64 dAtod(const char *str)
  162. {
  163. return strtod(str, NULL);
  164. }
  165. inline S64 dAtol(const char* str)
  166. {
  167. return strtol(str, NULL, 10);
  168. }
  169. inline char dToupper(const char c)
  170. {
  171. return toupper( c );
  172. }
  173. inline char dTolower(const char c)
  174. {
  175. return tolower( c );
  176. }
  177. inline bool dIsalnum(const char c)
  178. {
  179. return isalnum(c);
  180. }
  181. inline bool dIsalpha(const char c)
  182. {
  183. return isalpha(c);
  184. }
  185. inline bool dIsspace(const char c)
  186. {
  187. return isspace(c);
  188. }
  189. inline bool dIsdigit(const char c)
  190. {
  191. return isdigit(c);
  192. }
  193. inline bool dIsquote(const char c)
  194. {
  195. return ( c == '\"' );
  196. }
  197. //------------------------------------------------------------------------------
  198. // non-standard string functions [defined in stringFunctions.cpp]
  199. #define dStrdup(x) dStrdup_r(x, __FILE__, __LINE__)
  200. extern char *dStrdup_r(const char *src, const char*, dsize_t);
  201. extern char *dStrcpyl(char *dst, dsize_t dstSize, ...);
  202. extern char *dStrcatl(char *dst, dsize_t dstSize, ...);
  203. extern char *dStrupr(char *str);
  204. extern char *dStrlwr(char *str);
  205. extern char* dStrichr( char* str, char ch );
  206. extern const char* dStrichr( const char* str, char ch );
  207. extern S32 dStrcmp(const UTF16 *str1, const UTF16 *str2);
  208. extern S32 dStrnatcmp( const char* str1, const char* str2 );
  209. extern S32 dStrnatcasecmp( const char* str1, const char* str2 );
  210. inline bool dAtob(const char *str)
  211. {
  212. return !dStricmp(str, "true") || dAtof(str);
  213. }
  214. bool dStrEqual(const char* str1, const char* str2);
  215. bool dStrStartsWith(const char* str1, const char* str2);
  216. bool dStrEndsWith(const char* str1, const char* str2);
  217. char* dStripPath(const char* filename);
  218. int dStrrev(char* str);
  219. int dItoa(int n, char s[]);
  220. //------------------------------------------------------------------------------
  221. // standard I/O functions [defined in platformString.cpp]
  222. extern void dPrintf(const char *format, ...);
  223. extern S32 dVprintf(const char *format, va_list arglist);
  224. extern S32 dSprintf(char *buffer, U32 bufferSize, const char *format, ...);
  225. extern S32 dVsprintf(char *buffer, U32 bufferSize, const char *format, va_list arglist);
  226. extern S32 dSscanf(const char *buffer, const char *format, ...);
  227. #endif