char *_Q_strstr(const char* file, int line, const char *s1, const char *search ) { AssertValidStringPtr( s1 ); AssertValidStringPtr( search ); return (char *)strstr( s1, search ); }
char* V_stristr( char* pStr, char const* pSearch ) { AssertValidStringPtr( pStr ); AssertValidStringPtr( pSearch ); return (char*)V_stristr( (char const*)pStr, pSearch ); }
int V_strncasecmp (const char *s1, const char *s2, int n) { Assert( n >= 0 ); AssertValidStringPtr( s1 ); AssertValidStringPtr( s2 ); while ( n-- > 0 ) { int c1 = *s1++; int c2 = *s2++; if (c1 != c2) { if (c1 >= 'a' && c1 <= 'z') c1 -= ('a' - 'A'); if (c2 >= 'a' && c2 <= 'z') c2 -= ('a' - 'A'); if (c1 != c2) return c1 < c2 ? -1 : 1; } if ( c1 == '\0' ) return 0; // null terminator hit - strings the same } return 0; // n characters compared the same }
int V_strcasecmp( const char *s1, const char *s2 ) { AssertValidStringPtr( s1 ); AssertValidStringPtr( s2 ); return stricmp( s1, s2 ); }
int Q_strcasecmp (const char *s1, const char *s2) { AssertValidStringPtr(s1); AssertValidStringPtr(s2); return Q_strncasecmp (s1, s2, 99999); }
int Q_strncasecmp (const char *s1, const char *s2, int n) { Assert( n >= 0 ); AssertValidStringPtr( s1 ); AssertValidStringPtr( s2 ); int c1, c2; while (1) { c1 = *s1++; c2 = *s2++; if (!n--) return 0; // strings are equal until end point if (c1 != c2) { if (c1 >= 'a' && c1 <= 'z') c1 -= ('a' - 'A'); if (c2 >= 'a' && c2 <= 'z') c2 -= ('a' - 'A'); if (c1 != c2) return -1; // strings not equal } if (!c1) return 0; // strings are equal // s1++; // s2++; } return -1; }
//----------------------------------------------------------------------------- // Purpose: If COPY_ALL_CHARACTERS == max_chars_to_copy then we try to add the whole pSrc to the end of pDest, otherwise // we copy only as many characters as are specified in max_chars_to_copy (or the # of characters in pSrc if thats's less). // Input : *pDest - destination buffer // *pSrc - string to append // destBufferSize - sizeof the buffer pointed to by pDest // max_chars_to_copy - COPY_ALL_CHARACTERS in pSrc or max # to copy // Output : char * the copied buffer //----------------------------------------------------------------------------- char *V_strncat(char *pDest, const char *pSrc, size_t destBufferSize, int max_chars_to_copy ) { size_t charstocopy = (size_t)0; Assert( destBufferSize >= 0 ); AssertValidStringPtr( pDest); AssertValidStringPtr( pSrc ); size_t len = strlen(pDest); size_t srclen = strlen( pSrc ); if ( max_chars_to_copy <= COPY_ALL_CHARACTERS ) { charstocopy = srclen; } else { charstocopy = (size_t)min( max_chars_to_copy, (int)srclen ); } if ( len + charstocopy >= destBufferSize ) { charstocopy = destBufferSize - len - 1; } if ( !charstocopy ) { return pDest; } char *pOut = strncat( pDest, pSrc, charstocopy ); pOut[destBufferSize-1] = 0; return pOut; }
int _V_stricmp(const char* file, int line, const char *s1, const char *s2 ) { AssertValidStringPtr( s1 ); AssertValidStringPtr( s2 ); return stricmp( s1, s2 ); }
int V_strnicmp (const char *s1, const char *s2, int n) { Assert( n >= 0 ); AssertValidStringPtr(s1); AssertValidStringPtr(s2); return V_strncasecmp( s1, s2, n ); }
void Q_strcat (char *dest, const char *src) { AssertValidStringPtr(dest); AssertValidStringPtr(src); dest += Q_strlen(dest); Q_strcpy (dest, src); }
char *_V_strstr(const char* file, int line, const char *s1, const char *search ) { AssertValidStringPtr( s1 ); AssertValidStringPtr( search ); #if defined( _X360 ) return (char *)strstr( (char *)s1, search ); #else return (char *)strstr( s1, search ); #endif }
const char *StringAfterPrefix( const char *str, const char *prefix ) { AssertValidStringPtr( str ); AssertValidStringPtr( prefix ); do { if ( !*prefix ) return str; } while ( tolower( *str++ ) == tolower( *prefix++ ) ); return NULL; }
const char *StringAfterPrefixCaseSensitive( const char *str, const char *prefix ) { AssertValidStringPtr( str ); AssertValidStringPtr( prefix ); do { if ( !*prefix ) return str; } while ( *str++ == *prefix++ ); return NULL; }
//----------------------------------------------------------------------------- // Finds a string in another string with a case insensitive test w/ length validation //----------------------------------------------------------------------------- char const* V_strnistr( char const* pStr, char const* pSearch, int n ) { AssertValidStringPtr(pStr); AssertValidStringPtr(pSearch); if (!pStr || !pSearch) return 0; char const* pLetter = pStr; // Check the entire string while (*pLetter != 0) { if ( n <= 0 ) return 0; // Skip over non-matches if (tolower(*pLetter) == tolower(*pSearch)) { int n1 = n - 1; // Check for match char const* pMatch = pLetter + 1; char const* pTest = pSearch + 1; while (*pTest != 0) { if ( n1 <= 0 ) return 0; // We've run off the end; don't bother. if (*pMatch == 0) return 0; if (tolower(*pMatch) != tolower(*pTest)) break; ++pMatch; ++pTest; --n1; } // Found a match! if (*pTest == 0) return pLetter; } ++pLetter; --n; } return 0; }
void _V_strcpy (const char* file, int line, char *dest, const char *src) { AssertValidWritePtr(dest); AssertValidStringPtr(src); strcpy( dest, src ); }
char *Q_strncat(char *pDest, const char *pSrc, size_t maxLen) { Assert( maxLen >= 0 ); AssertValidStringPtr( pDest); AssertValidStringPtr( pSrc ); int len = strlen(pDest); maxLen = (maxLen - 1) - len; if ( maxLen <= 0 ) return pDest; char *pOut = strncat( pDest, pSrc, maxLen ); pOut[len + maxLen] = 0; return pOut; }
//----------------------------------------------------------------------------- // Purpose: Converts a UTF8 string into a unicode string //----------------------------------------------------------------------------- int V_UTF8ToUnicode( const char *pUTF8, wchar_t *pwchDest, int cubDestSizeInBytes ) { // pwchDest can be null to allow for getting the length of the string if ( cubDestSizeInBytes > 0 ) { AssertValidWritePtr(pwchDest); pwchDest[0] = 0; } if ( !pUTF8 ) return 0; AssertValidStringPtr(pUTF8); #ifdef _WIN32 int cchResult = MultiByteToWideChar( CP_UTF8, 0, pUTF8, -1, pwchDest, cubDestSizeInBytes / sizeof(wchar_t) ); #elif _LINUX int cchResult = mbstowcs( pwchDest, pUTF8, cubDestSizeInBytes / sizeof(wchar_t) ) + 1; #endif if ( cubDestSizeInBytes > 0 ) { pwchDest[(cubDestSizeInBytes / sizeof(wchar_t)) - 1] = 0; } return cchResult; }
int V_atoi (const char *str) { AssertValidStringPtr( str ); int val; int sign; int c; Assert( str ); if (*str == '-') { sign = -1; str++; } else sign = 1; val = 0; // // check for hex // if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) { str += 2; while (1) { c = *str++; if (c >= '0' && c <= '9') val = (val<<4) + c - '0'; else if (c >= 'a' && c <= 'f') val = (val<<4) + c - 'a' + 10; else if (c >= 'A' && c <= 'F') val = (val<<4) + c - 'A' + 10; else return val*sign; } } // // check for character // if (str[0] == '\'') { return sign * str[1]; } // // assume decimal // while (1) { c = *str++; if (c <'0' || c > '9') return val*sign; val = val*10 + c - '0'; } return 0; }
void WideCharToChar( const wchar_t * lpSrc, char * lpDst, int nMax ) { AssertValidStringPtr( lpDst,nMax ); int nCountSrc = wcslen( lpSrc ) + 1; int nCount = nCountSrc * 2; Assert( nCount < nMax ); if( nCount < nMax ) { #ifdef WIN32 WideCharToMultiByte(CP_ACP, 0, lpSrc, nCountSrc, lpDst, nCount, NULL, NULL ); #else wcstombs(lpDst, lpSrc, nCount); #endif lpDst[nCount] = '\0'; } else { char szLog[512+1] = { 0 }; #ifdef WIN32 WideCharToMultiByte(CP_ACP, 0, lpSrc, nCountSrc, szLog, 512, NULL, NULL ); #else wcstombs(szLog, lpSrc, 512); #endif Log( "WideCharToChar %s", szLog ); lpDst[0] = 0; } }
int V_snprintf( char *pDest, int maxLen, char const *pFormat, ... ) { Assert( maxLen >= 0 ); AssertValidWritePtr( pDest, maxLen ); AssertValidStringPtr( pFormat ); va_list marker; va_start( marker, pFormat ); #ifdef _WIN32 int len = _vsnprintf( pDest, maxLen, pFormat, marker ); #elif _LINUX int len = vsnprintf( pDest, maxLen, pFormat, marker ); #else #error "define vsnprintf type." #endif va_end( marker ); // Len < 0 represents an overflow if( len < 0 ) { len = maxLen; pDest[maxLen-1] = 0; } return len; }
char *V_strnlwr(char *s, size_t count) { Assert( count >= 0 ); AssertValidStringPtr( s, count ); char* pRet = s; if ( !s ) return s; while ( --count >= 0 ) { if ( !*s ) break; *s = tolower( *s ); ++s; } if ( count > 0 ) { s[count-1] = 0; } return pRet; }
int _Q_strcmp (const char* file, int line, const char *s1, const char *s2) { AssertValidStringPtr( s1 ); AssertValidStringPtr( s2 ); while (1) { if (*s1 != *s2) return -1; // strings not equal if (!*s1) return 0; // strings are equal s1++; s2++; } return -1; }
int Q_snprintf( char *pDest, int maxLen, char const *pFormat, ... ) { Assert( maxLen >= 0 ); AssertValidWritePtr( pDest, maxLen ); AssertValidStringPtr( pFormat ); #ifdef FREEBSD const char* bit64 = "I64"; const int formatlen = strlen(pFormat); char pTempFormat[1024] ={0}; int tfidx = 0; for( int mfidx=0; mfidx<formatlen; mfidx++ ) { pTempFormat[tfidx++] = pFormat[mfidx]; if( '%' == pFormat[mfidx] ) { int bit64idx = 0; int scanidx = mfidx + 1; for( ; bit64idx<3; bit64idx++ ) { if( scanidx >= formatlen ) { break; } if( bit64[bit64idx] != pFormat[scanidx] ) { break; } scanidx++; } if( bit64idx == 3 ) { memcpy( pTempFormat+tfidx, "ll", 2 ); tfidx += 2; mfidx += 3; } } } pTempFormat[tfidx] = '\0'; #endif va_list marker; va_start( marker, pFormat ); #ifdef WIN32 int len = _vsnprintf( pDest, maxLen, pFormat, marker ); #else int len = vsnprintf( pDest, maxLen, pTempFormat, marker ); #endif va_end( marker ); // Len < 0 represents an overflow if( len < 0 ) { len = maxLen; pDest[maxLen-1] = 0; } return len; }
char *_V_strrchr(const char* file, int line, const char *s, char c) { AssertValidStringPtr( s ); int len = V_strlen(s); s += len; while (len--) if (*--s == c) return (char *)s; return 0; }
int V_strncmp (const char *s1, const char *s2, int count) { Assert( count >= 0 ); AssertValidStringPtr( s1, count ); AssertValidStringPtr( s2, count ); while ( count-- > 0 ) { if ( *s1 != *s2 ) return *s1 < *s2 ? -1 : 1; // string different if ( *s1 == '\0' ) return 0; // null terminator hit - strings the same s1++; s2++; } return 0; // count characters compared the same }
void Q_strncpy( char *pDest, char const *pSrc, int maxLen ) { Assert( maxLen >= 0 ); AssertValidWritePtr( pDest, maxLen ); AssertValidStringPtr( pSrc ); strncpy( pDest, pSrc, maxLen ); if( maxLen ) pDest[maxLen-1] = 0; }
//----------------------------------------------------------------------------- // 360 spew sizes of dll modules //----------------------------------------------------------------------------- char const* HACK_stristr( char const* pStr, char const* pSearch ) // hack because moved code from above vstdlib { AssertValidStringPtr(pStr); AssertValidStringPtr(pSearch); if (!pStr || !pSearch) return 0; char const* pLetter = pStr; // Check the entire string while (*pLetter != 0) { // Skip over non-matches if (tolower((unsigned char)*pLetter) == tolower((unsigned char)*pSearch)) { // Check for match char const* pMatch = pLetter + 1; char const* pTest = pSearch + 1; while (*pTest != 0) { // We've run off the end; don't bother. if (*pMatch == 0) return 0; if (tolower((unsigned char)*pMatch) != tolower((unsigned char)*pTest)) break; ++pMatch; ++pTest; } // Found a match! if (*pTest == 0) return pLetter; } ++pLetter; } return 0; }
int Q_strncmp (const char *s1, const char *s2, int count) { Assert( count >= 0 ); AssertValidStringPtr( s1, count ); AssertValidStringPtr( s2, count ); while (1) { if (!count--) return 0; if (*s1 != *s2) return -1; // strings not equal if (!*s1) return 0; // strings are equal s1++; s2++; } return -1; }
void V_strncpy( char *pDest, char const *pSrc, int maxLen ) { mxASSERT( maxLen >= 0 ); AssertValidWritePtr( pDest, maxLen ); AssertValidStringPtr( pSrc ); strncpy( pDest, pSrc, maxLen ); if ( maxLen > 0 ) { pDest[maxLen-1] = 0; } }
//----------------------------------------------------------------------------- // Purpose: Converts a unicode string into a UTF8 (standard) string //----------------------------------------------------------------------------- int V_UnicodeToUTF8( const wchar_t *pUnicode, char *pUTF8, int cubDestSizeInBytes ) { AssertValidStringPtr(pUTF8, cubDestSizeInBytes); AssertValidReadPtr(pUnicode); pUTF8[0] = 0; #ifdef _WIN32 int cchResult = WideCharToMultiByte( CP_UTF8, 0, pUnicode, -1, pUTF8, cubDestSizeInBytes, NULL, NULL ); #elif _LINUX int cchResult = wcstombs( pUTF8, pUnicode, cubDestSizeInBytes ); #endif pUTF8[cubDestSizeInBytes - 1] = 0; return cchResult; }