123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #ifndef _STRINGFUNCTIONS_H_
- #define _STRINGFUNCTIONS_H_
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <cstdarg>
- #ifndef _TORQUE_TYPES_H_
- #include "platform/types.h"
- #endif
- #ifndef _PLATFORMASSERT_H_
- #include "platform/platformAssert.h"
- #endif
- #if defined(TORQUE_OS_WIN)
- // These standard functions are not defined on Win32 and other Microsoft platforms...
- #define strcasecmp _stricmp
- #define strncasecmp _strnicmp
- #if _MSC_VER < 1800
- #define strtof (float)strtod
- #endif // _MSC_VER < 1800
- #endif // defined(TORQUE_OS_WIN)
- //------------------------------------------------------------------------------
- // standard string functions [defined in platformString.cpp]
- // Buffer size bounds checking "safe" versions of strcat and strcpy. Ideally you
- // should use these and check if they return >= dstSize and throw an error if so.
- extern S32 dStrlcat(char *dst, const char *src, dsize_t dstSize);
- extern S32 dStrlcpy(char *dst, const char *src, dsize_t dstSize);
- #ifdef UNSAFE_STRING_FUNCTIONS
- /// @deprecated Use dStrcat(char *, const char *, dsize_t) instead
- inline char *dStrcat(char *dst, const char *src)
- {
- AssertFatal(false, "dStrcat without length is deprecated");
- return strcat(dst,src);
- }
- #endif
- /// Concatenate strings.
- /// @note The third parameter is the size of the destination buffer like strlcat
- /// instead of the number of characters to copy like strncat. This is done
- /// under the assumption that being easier to use will make this safer.
- /// If you want the original behavior use dStrncat.
- inline char *dStrcat(char *dst, const char *src, dsize_t dstSize)
- {
- dStrlcat(dst, src, dstSize);
- return dst;
- }
- inline char *dStrncat(char *dst, const char *src, dsize_t len)
- {
- return strncat(dst, src, len);
- }
- inline S32 dStrcmp(const char *str1, const char *str2)
- {
- return strcmp(str1, str2);
- }
- inline bool dStrIsEmpty(const char *src)
- {
- return src == 0 || src[0] == '\0';
- }
- inline S32 dStrncmp(const char *str1, const char *str2, dsize_t len)
- {
- return strncmp(str1, str2, len);
- }
- inline S32 dStricmp(const char *str1, const char *str2)
- {
- return strcasecmp( str1, str2 );
- }
- inline S32 dStrnicmp(const char *str1, const char *str2, dsize_t len)
- {
- return strncasecmp( str1, str2, len );
- }
- #ifdef UNSAFE_STRING_FUNCTIONS
- /// @deprecated Use strcpy(char *, const char *, dsize_t) instead
- inline char *dStrcpy(char *dst, const char *src)
- {
- AssertFatal(false, "dStrcpy without length is deprecated");
- return strcpy(dst,src);
- }
- #endif
- inline char *dStrcpy(char *dst, const char *src, dsize_t dstSize)
- {
- dStrlcpy(dst, src, dstSize);
- return dst;
- }
- inline char *dStrncpy(char *dst, const char *src, dsize_t len)
- {
- return strncpy(dst,src,len);
- }
- inline dsize_t dStrlen(const char *str)
- {
- return strlen(str);
- }
- inline char *dStrchr(char *str, S32 c)
- {
- return strchr(str,c);
- }
- inline const char *dStrchr(const char *str, S32 c)
- {
- return strchr(str,c);
- }
- inline char *dStrrchr(char *str, S32 c)
- {
- return strrchr(str,c);
- }
- inline const char *dStrrchr(const char *str, S32 c)
- {
- return strrchr(str,c);
- }
- inline dsize_t dStrspn(const char *str, const char *set)
- {
- return strspn(str, set);
- }
- inline dsize_t dStrcspn(const char *str, const char *set)
- {
- return strcspn(str, set);
- }
- inline char *dStrstr(const char *str1, const char *str2)
- {
- return strstr((char *)str1,str2);
- }
- const char* dStristr( const char* str1, const char* str2 );
- char* dStristr( char* str1, const char* str2 );
- inline char *dStrtok(char *str, const char *sep)
- {
- return strtok(str, sep);
- }
- inline S32 dAtoi(const char *str)
- {
- return strtol(str, NULL, 10);
- }
- inline U32 dAtoui(const char *str, U32 base = 10)
- {
- return strtoul(str, NULL, base);
- }
- inline U16 dAtous(const char *str, U32 base = 10)
- {
- return strtoul(str, NULL, base);
- }
- inline F32 dAtof(const char *str)
- {
- return strtof(str, NULL);
- }
- inline F64 dAtod(const char *str)
- {
- return strtod(str, NULL);
- }
- inline S64 dAtol(const char* str)
- {
- return strtol(str, NULL, 10);
- }
- inline char dToupper(const char c)
- {
- return toupper( c );
- }
- inline char dTolower(const char c)
- {
- return tolower( c );
- }
- inline bool dIsalnum(const char c)
- {
- return isalnum(c);
- }
- inline bool dIsalpha(const char c)
- {
- return isalpha(c);
- }
- inline bool dIsspace(const char c)
- {
- return isspace(c);
- }
- inline bool dIsdigit(const char c)
- {
- return isdigit(c);
- }
- inline bool dIsquote(const char c)
- {
- return ( c == '\"' );
- }
- //------------------------------------------------------------------------------
- // non-standard string functions [defined in stringFunctions.cpp]
- #define dStrdup(x) dStrdup_r(x, __FILE__, __LINE__)
- extern char *dStrdup_r(const char *src, const char*, dsize_t);
- extern char *dStrcpyl(char *dst, dsize_t dstSize, ...);
- extern char *dStrcatl(char *dst, dsize_t dstSize, ...);
- extern char *dStrupr(char *str);
- extern char *dStrlwr(char *str);
- extern char* dStrichr( char* str, char ch );
- extern const char* dStrichr( const char* str, char ch );
- extern S32 dStrcmp(const UTF16 *str1, const UTF16 *str2);
- extern S32 dStrnatcmp( const char* str1, const char* str2 );
- extern S32 dStrnatcasecmp( const char* str1, const char* str2 );
- inline bool dAtob(const char *str)
- {
- return !dStricmp(str, "true") || dAtof(str);
- }
- bool dStrEqual(const char* str1, const char* str2);
- bool dStrStartsWith(const char* str1, const char* str2);
- bool dStrEndsWith(const char* str1, const char* str2);
- char* dStripPath(const char* filename);
- int dStrrev(char* str);
- int dItoa(int n, char s[]);
- //------------------------------------------------------------------------------
- // standard I/O functions [defined in platformString.cpp]
- extern void dPrintf(const char *format, ...);
- extern S32 dVprintf(const char *format, va_list arglist);
- extern S32 dSprintf(char *buffer, U32 bufferSize, const char *format, ...);
- extern S32 dVsprintf(char *buffer, U32 bufferSize, const char *format, va_list arglist);
- extern S32 dSscanf(const char *buffer, const char *format, ...);
- #endif
|