Line data Source code
1 : //========= Copyright Valve Corporation ============//
2 : #pragma once
3 :
4 : #include <string>
5 : #include <stdint.h>
6 : #include <sys/types.h>
7 :
8 : /** returns true if the string has the prefix */
9 : bool StringHasPrefix( const std::string & sString, const std::string & sPrefix );
10 : bool StringHasPrefixCaseSensitive( const std::string & sString, const std::string & sPrefix );
11 :
12 : /** returns if the string has the suffix */
13 : bool StringHasSuffix( const std::string &sString, const std::string &sSuffix );
14 : bool StringHasSuffixCaseSensitive( const std::string &sString, const std::string &sSuffix );
15 :
16 : /** converts a UTF-16 string to a UTF-8 string */
17 : std::string UTF16to8(const wchar_t * in);
18 :
19 : /** converts a UTF-8 string to a UTF-16 string */
20 : std::wstring UTF8to16(const char * in);
21 : #define Utf16FromUtf8 UTF8to16
22 :
23 : /** safely copy a string into a buffer */
24 : void strcpy_safe( char *pchBuffer, size_t unBufferSizeBytes, const char *pchSource );
25 : template< size_t bufferSize >
26 : void strcpy_safe( char (& buffer) [ bufferSize ], const char *pchSource )
27 : {
28 : strcpy_safe( buffer, bufferSize, pchSource );
29 : }
30 :
31 :
32 : /** converts a string to upper case */
33 : std::string StringToUpper( const std::string & sString );
34 :
35 : /** converts a string to lower case */
36 : std::string StringToLower( const std::string & sString );
37 :
38 : // we stricmp (from WIN) but it isn't POSIX - OSX/LINUX have strcasecmp so just inline bridge to it
39 : #if defined( OSX ) || defined( LINUX )
40 : #include <strings.h>
41 0 : inline int stricmp(const char *pStr1, const char *pStr2) { return strcasecmp(pStr1,pStr2); }
42 : #define _stricmp stricmp
43 0 : inline int strnicmp( const char *pStr1, const char *pStr2, size_t unBufferLen ) { return strncasecmp( pStr1,pStr2, unBufferLen ); }
44 : #define _strnicmp strnicmp
45 :
46 : #define _vsnprintf_s vsnprintf
47 :
48 : #define _TRUNCATE ((size_t)-1)
49 :
50 : #endif
51 :
52 : #if defined( OSX )
53 : // behaviors ensure NULL-termination at least as well as _TRUNCATE does, but
54 : // wcsncpy_s/strncpy_s can non-NULL-terminate, wcslcpy/strlcpy can not.
55 : /*
56 : // Commented out by Mozilla, please see README.mozilla
57 : inline errno_t wcsncpy_s(wchar_t *strDest, size_t numberOfElements, const wchar_t *strSource, size_t count)
58 : {
59 : return wcslcpy(strDest, strSource, numberOfElements);
60 : }
61 :
62 : inline errno_t strncpy_s(char *strDest, size_t numberOfElements, const char *strSource, size_t count)
63 : {
64 : return strlcpy(strDest, strSource, numberOfElements);
65 : }
66 : */
67 : #endif
68 :
69 : #if defined( LINUX )
70 : // this implementation does not return whether or not the destination was
71 : // truncated, but that is straightforward to fix if anybody actually needs the
72 : // return code.
73 : #include "string.h"
74 : inline void wcsncpy_s(wchar_t *strDest, size_t numberOfElements, const wchar_t *strSource, size_t count)
75 : {
76 : wcsncpy(strDest, strSource, numberOfElements);
77 : strDest[numberOfElements-1] = '\0';
78 : }
79 :
80 : inline void strncpy_s(char *strDest, size_t numberOfElements, const char *strSource, size_t count)
81 : {
82 : strncpy(strDest, strSource, numberOfElements);
83 : strDest[numberOfElements-1] = '\0';
84 : }
85 :
86 : #endif
87 :
88 : #if defined( _WIN32 ) && _MSC_VER < 1800
89 : inline uint64_t strtoull(const char *str, char **endptr, int base) { return _strtoui64( str, endptr, base ); }
90 : #endif
91 :
92 : /* Handles copying a std::string into a buffer as would be provided in an API */
93 : uint32_t ReturnStdString( const std::string & sValue, char *pchBuffer, uint32_t unBufferLen );
94 :
95 : /* Handles copying a buffer into an std::string and auto adds null terminator */
96 : void BufferToStdString( std::string & sDest, const char *pchBuffer, uint32_t unBufferLen );
97 :
98 : /** Returns a std::string from a uint64_t */
99 : // std::string Uint64ToString( uint64_t ulValue );
100 : // Commented out by Mozilla, please see README.mozilla
101 :
102 : /** returns a uint64_t from a string */
103 : uint64_t StringToUint64( const std::string & sValue );
104 :
105 : //-----------------------------------------------------------------------------
106 : // Purpose: Encodes a string (or binary data) from URL encoding format, see rfc1738 section 2.2.
107 : // This version of the call isn't a strict RFC implementation, but uses + for space as is
108 : // the standard in HTML form encoding, despite it not being part of the RFC.
109 : //
110 : // Dest buffer should be at least as large as source buffer to guarantee room for decode.
111 : //-----------------------------------------------------------------------------
112 : void V_URLEncode( char *pchDest, int nDestLen, const char *pchSource, int nSourceLen );
113 :
114 : //-----------------------------------------------------------------------------
115 : // Purpose: Decodes a string (or binary data) from URL encoding format, see rfc1738 section 2.2.
116 : // This version of the call isn't a strict RFC implementation, but uses + for space as is
117 : // the standard in HTML form encoding, despite it not being part of the RFC.
118 : //
119 : // Dest buffer should be at least as large as source buffer to guarantee room for decode.
120 : // Dest buffer being the same as the source buffer (decode in-place) is explicitly allowed.
121 : //-----------------------------------------------------------------------------
122 : size_t V_URLDecode( char *pchDecodeDest, int nDecodeDestLen, const char *pchEncodedSource, int nEncodedSourceLen );
123 :
124 : //-----------------------------------------------------------------------------
125 : // Purpose: strip extension from a path
126 : //-----------------------------------------------------------------------------
127 : void V_StripExtension( std::string &in );
128 :
129 :
|