stringFunctions.h 6.0 KB

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