stringFunctions.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /// @deprecated Use dStrcat(char *, const char *, dsize_t) instead
  45. inline char *dStrcat(char *dst, const char *src)
  46. {
  47. AssertFatal(false, "dStrcat without length is deprecated");
  48. return strcat(dst,src);
  49. }
  50. inline char *dStrcat(char *dst, const char *src, dsize_t len)
  51. {
  52. return strncat(dst,src,len - 1); //Safety because strncat copies at most len+1 characters
  53. }
  54. inline char *dStrncat(char *dst, const char *src, dsize_t len)
  55. {
  56. AssertFatal(false, "Use dStrcat with length");
  57. return dStrcat(dst, src, len);
  58. }
  59. inline S32 dStrcmp(const char *str1, const char *str2)
  60. {
  61. return strcmp(str1, str2);
  62. }
  63. inline bool dStrIsEmpty(const char *src)
  64. {
  65. return src == 0 || src[0] == '\0';
  66. }
  67. inline S32 dStrncmp(const char *str1, const char *str2, dsize_t len)
  68. {
  69. return strncmp(str1, str2, len);
  70. }
  71. inline S32 dStricmp(const char *str1, const char *str2)
  72. {
  73. return strcasecmp( str1, str2 );
  74. }
  75. inline S32 dStrnicmp(const char *str1, const char *str2, dsize_t len)
  76. {
  77. return strncasecmp( str1, str2, len );
  78. }
  79. inline char *dStrcpy(char *dst, const char *src)
  80. {
  81. return strcpy(dst,src);
  82. }
  83. inline char *dStrncpy(char *dst, const char *src, dsize_t len)
  84. {
  85. return strncpy(dst,src,len);
  86. }
  87. inline dsize_t dStrlen(const char *str)
  88. {
  89. return strlen(str);
  90. }
  91. inline char *dStrchr(char *str, S32 c)
  92. {
  93. return strchr(str,c);
  94. }
  95. inline const char *dStrchr(const char *str, S32 c)
  96. {
  97. return strchr(str,c);
  98. }
  99. inline char *dStrrchr(char *str, S32 c)
  100. {
  101. return strrchr(str,c);
  102. }
  103. inline const char *dStrrchr(const char *str, S32 c)
  104. {
  105. return strrchr(str,c);
  106. }
  107. inline dsize_t dStrspn(const char *str, const char *set)
  108. {
  109. return strspn(str, set);
  110. }
  111. inline dsize_t dStrcspn(const char *str, const char *set)
  112. {
  113. return strcspn(str, set);
  114. }
  115. inline char *dStrstr(const char *str1, const char *str2)
  116. {
  117. return strstr((char *)str1,str2);
  118. }
  119. const char* dStristr( const char* str1, const char* str2 );
  120. char* dStristr( char* str1, const char* str2 );
  121. inline char *dStrtok(char *str, const char *sep)
  122. {
  123. return strtok(str, sep);
  124. }
  125. inline S32 dAtoi(const char *str)
  126. {
  127. return strtol(str, NULL, 10);
  128. }
  129. inline U32 dAtoui(const char *str, U32 base = 10)
  130. {
  131. return strtoul(str, NULL, base);
  132. }
  133. inline U16 dAtous(const char *str, U32 base = 10)
  134. {
  135. return strtoul(str, NULL, base);
  136. }
  137. inline F32 dAtof(const char *str)
  138. {
  139. return strtof(str, NULL);
  140. }
  141. inline F64 dAtod(const char *str)
  142. {
  143. return strtod(str, NULL);
  144. }
  145. inline char dToupper(const char c)
  146. {
  147. return toupper( c );
  148. }
  149. inline char dTolower(const char c)
  150. {
  151. return tolower( c );
  152. }
  153. inline bool dIsalnum(const char c)
  154. {
  155. return isalnum(c);
  156. }
  157. inline bool dIsalpha(const char c)
  158. {
  159. return isalpha(c);
  160. }
  161. inline bool dIsspace(const char c)
  162. {
  163. return isspace(c);
  164. }
  165. inline bool dIsdigit(const char c)
  166. {
  167. return isdigit(c);
  168. }
  169. inline bool dIsquote(const char c)
  170. {
  171. return ( c == '\"' );
  172. }
  173. //------------------------------------------------------------------------------
  174. // non-standard string functions [defined in stringFunctions.cpp]
  175. #define dStrdup(x) dStrdup_r(x, __FILE__, __LINE__)
  176. extern char *dStrdup_r(const char *src, const char*, dsize_t);
  177. extern char *dStrcpyl(char *dst, dsize_t dstSize, ...);
  178. extern char *dStrcatl(char *dst, dsize_t dstSize, ...);
  179. extern char *dStrupr(char *str);
  180. extern char *dStrlwr(char *str);
  181. extern char* dStrichr( char* str, char ch );
  182. extern const char* dStrichr( const char* str, char ch );
  183. extern S32 dStrcmp(const UTF16 *str1, const UTF16 *str2);
  184. extern S32 dStrnatcmp( const char* str1, const char* str2 );
  185. extern S32 dStrnatcasecmp( const char* str1, const char* str2 );
  186. inline bool dAtob(const char *str)
  187. {
  188. return !dStricmp(str, "true") || dAtof(str);
  189. }
  190. bool dStrEqual(const char* str1, const char* str2);
  191. bool dStrStartsWith(const char* str1, const char* str2);
  192. bool dStrEndsWith(const char* str1, const char* str2);
  193. char* dStripPath(const char* filename);
  194. int dStrrev(char* str);
  195. int dItoa(int n, char s[]);
  196. //------------------------------------------------------------------------------
  197. // standard I/O functions [defined in platformString.cpp]
  198. extern void dPrintf(const char *format, ...);
  199. extern S32 dVprintf(const char *format, va_list arglist);
  200. extern S32 dSprintf(char *buffer, U32 bufferSize, const char *format, ...);
  201. extern S32 dVsprintf(char *buffer, U32 bufferSize, const char *format, va_list arglist);
  202. extern S32 dSscanf(const char *buffer, const char *format, ...);
  203. #endif