stringFunctions.h 5.6 KB

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