Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
12 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef UTLSTRING_H
  7. #define UTLSTRING_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlmemory.h"
  12. #include "tier1/strtools.h"
  13. #include "limits.h"
  14. //-----------------------------------------------------------------------------
  15. // Base class, containing simple memory management
  16. //-----------------------------------------------------------------------------
  17. class CUtlBinaryBlock
  18. {
  19. public:
  20. CUtlBinaryBlock( int growSize = 0, int initSize = 0 );
  21. ~CUtlBinaryBlock()
  22. {
  23. #ifdef _DEBUG
  24. m_nActualLength = 0x7BADF00D;
  25. #else
  26. m_nActualLength = 0;
  27. #endif
  28. }
  29. // NOTE: nInitialLength indicates how much of the buffer starts full
  30. CUtlBinaryBlock( void* pMemory, int nSizeInBytes, int nInitialLength );
  31. CUtlBinaryBlock( const void* pMemory, int nSizeInBytes );
  32. CUtlBinaryBlock( const CUtlBinaryBlock& src );
  33. void Get( void *pValue, int nMaxLen ) const;
  34. void Set( const void *pValue, int nLen );
  35. const void *Get( ) const;
  36. void *Get( );
  37. unsigned char& operator[]( int i );
  38. const unsigned char& operator[]( int i ) const;
  39. int Length() const;
  40. void SetLength( int nLength ); // Undefined memory will result
  41. bool IsEmpty() const;
  42. void Clear();
  43. void Purge();
  44. bool IsReadOnly() const;
  45. CUtlBinaryBlock &operator=( const CUtlBinaryBlock &src );
  46. // Test for equality
  47. bool operator==( const CUtlBinaryBlock &src ) const;
  48. private:
  49. CUtlMemory<unsigned char> m_Memory;
  50. int m_nActualLength;
  51. };
  52. //-----------------------------------------------------------------------------
  53. // class inlines
  54. //-----------------------------------------------------------------------------
  55. inline const void *CUtlBinaryBlock::Get( ) const
  56. {
  57. return m_Memory.Base();
  58. }
  59. inline void *CUtlBinaryBlock::Get( )
  60. {
  61. return m_Memory.Base();
  62. }
  63. inline int CUtlBinaryBlock::Length() const
  64. {
  65. return m_nActualLength;
  66. }
  67. inline unsigned char& CUtlBinaryBlock::operator[]( int i )
  68. {
  69. return m_Memory[i];
  70. }
  71. inline const unsigned char& CUtlBinaryBlock::operator[]( int i ) const
  72. {
  73. return m_Memory[i];
  74. }
  75. inline bool CUtlBinaryBlock::IsReadOnly() const
  76. {
  77. return m_Memory.IsReadOnly();
  78. }
  79. inline bool CUtlBinaryBlock::IsEmpty() const
  80. {
  81. return Length() == 0;
  82. }
  83. inline void CUtlBinaryBlock::Clear()
  84. {
  85. SetLength( 0 );
  86. }
  87. inline void CUtlBinaryBlock::Purge()
  88. {
  89. SetLength( 0 );
  90. m_Memory.Purge();
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Simple string class.
  94. // NOTE: This is *not* optimal! Use in tools, but not runtime code
  95. //-----------------------------------------------------------------------------
  96. class CUtlString
  97. {
  98. public:
  99. typedef enum
  100. {
  101. PATTERN_NONE = 0x00000000,
  102. PATTERN_DIRECTORY = 0x00000001
  103. } TUtlStringPattern;
  104. public:
  105. CUtlString();
  106. CUtlString( const char *pString );
  107. CUtlString( const CUtlString& string );
  108. // Attaches the string to external memory. Useful for avoiding a copy
  109. CUtlString( void* pMemory, int nSizeInBytes, int nInitialLength );
  110. CUtlString( const void* pMemory, int nSizeInBytes );
  111. const char *Get( ) const;
  112. void Set( const char *pValue );
  113. void Clear() { Set( NULL ); }
  114. // Converts to c-strings
  115. operator const char*() const;
  116. // for compatibility switching items from UtlSymbol
  117. const char *String() const { return Get(); }
  118. // Returns strlen
  119. int Length() const;
  120. bool IsEmpty() const;
  121. // Sets the length (used to serialize into the buffer )
  122. // Note: If nLen != 0, then this adds an extra byte for a null-terminator.
  123. void SetLength( int nLen );
  124. char *Get();
  125. void Purge();
  126. // Case Change
  127. void ToLower( );
  128. void Append( const char *pchAddition );
  129. // Strips the trailing slash
  130. void StripTrailingSlash();
  131. CUtlString &operator=( const CUtlString &src );
  132. CUtlString &operator=( const char *src );
  133. // Test for equality
  134. bool operator==( const CUtlString &src ) const;
  135. bool operator==( const char *src ) const;
  136. bool operator!=( const CUtlString &src ) const { return !operator==( src ); }
  137. bool operator!=( const char *src ) const { return !operator==( src ); }
  138. // If these are not defined, CUtlString as rhs will auto-convert
  139. // to const char* and do logical operations on the raw pointers. Ugh.
  140. inline friend bool operator==( const char *lhs, const CUtlString &rhs ) { return rhs.operator==( lhs ); }
  141. inline friend bool operator!=( const char *lhs, const CUtlString &rhs ) { return rhs.operator!=( lhs ); }
  142. CUtlString &operator+=( const CUtlString &rhs );
  143. CUtlString &operator+=( const char *rhs );
  144. CUtlString &operator+=( char c );
  145. CUtlString &operator+=( int rhs );
  146. CUtlString &operator+=( double rhs );
  147. CUtlString operator+( const char *pOther ) const;
  148. bool MatchesPattern( const CUtlString &Pattern, int nFlags = 0 ) const; // case SENSITIVE, use * for wildcard in pattern string
  149. int Format( const char *pFormat, ... );
  150. void SetDirect( const char *pValue, int nChars );
  151. // Defining AltArgumentType_t hints that associative container classes should
  152. // also implement Find/Insert/Remove functions that take const char* params.
  153. typedef const char *AltArgumentType_t;
  154. // Take a piece out of the string.
  155. // If you only specify nStart, it'll go from nStart to the end.
  156. // You can use negative numbers and it'll wrap around to the start.
  157. CUtlString Slice( int32 nStart=0, int32 nEnd=INT_MAX ) const;
  158. // Grab a substring starting from the left or the right side.
  159. CUtlString Left( int32 nChars ) const;
  160. CUtlString Right( int32 nChars ) const;
  161. // Replace all instances of one character with another.
  162. CUtlString Replace( char cFrom, char cTo ) const;
  163. // Calls right through to V_MakeAbsolutePath.
  164. CUtlString AbsPath( const char *pStartingDir=NULL ) const;
  165. // Gets the filename (everything except the path.. c:\a\b\c\somefile.txt -> somefile.txt).
  166. CUtlString UnqualifiedFilename() const;
  167. // Strips off one directory. Uses V_StripLastDir but strips the last slash also!
  168. CUtlString DirName() const;
  169. // Works like V_ComposeFileName.
  170. static CUtlString PathJoin( const char *pStr1, const char *pStr2 );
  171. // These can be used for utlvector sorts.
  172. static int __cdecl SortCaseInsensitive( const CUtlString *pString1, const CUtlString *pString2 );
  173. static int __cdecl SortCaseSensitive( const CUtlString *pString1, const CUtlString *pString2 );
  174. private:
  175. CUtlBinaryBlock m_Storage;
  176. };
  177. //-----------------------------------------------------------------------------
  178. // Inline methods
  179. //-----------------------------------------------------------------------------
  180. inline bool CUtlString::IsEmpty() const
  181. {
  182. return Length() == 0;
  183. }
  184. inline int __cdecl CUtlString::SortCaseInsensitive( const CUtlString *pString1, const CUtlString *pString2 )
  185. {
  186. return V_stricmp( pString1->String(), pString2->String() );
  187. }
  188. inline int __cdecl CUtlString::SortCaseSensitive( const CUtlString *pString1, const CUtlString *pString2 )
  189. {
  190. return V_strcmp( pString1->String(), pString2->String() );
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Purpose: Implementation of low-level string functionality for character types.
  194. //-----------------------------------------------------------------------------
  195. template < typename T >
  196. class StringFuncs
  197. {
  198. public:
  199. static T *Duplicate( const T *pValue );
  200. static void Copy( T *out_pOut, const T *pIn, int iLength );
  201. static int Compare( const T *pLhs, const T *pRhs );
  202. static int Length( const T *pValue );
  203. static const T *FindChar( const T *pStr, const T cSearch );
  204. static const T *EmptyString();
  205. };
  206. template < >
  207. class StringFuncs<char>
  208. {
  209. public:
  210. static char *Duplicate( const char *pValue ) { return strdup( pValue ); }
  211. static void Copy( char *out_pOut, const char *pIn, int iLength ) { strncpy( out_pOut, pIn, iLength ); }
  212. static int Compare( const char *pLhs, const char *pRhs ) { return strcmp( pLhs, pRhs ); }
  213. static int Length( const char *pValue ) { return strlen( pValue ); }
  214. static const char *FindChar( const char *pStr, const char cSearch ) { return strchr( pStr, cSearch ); }
  215. static const char *EmptyString() { return ""; }
  216. };
  217. template < >
  218. class StringFuncs<wchar_t>
  219. {
  220. public:
  221. static wchar_t *Duplicate( const wchar_t *pValue ) { return wcsdup( pValue ); }
  222. static void Copy( wchar_t *out_pOut, const wchar_t *pIn, int iLength ) { wcsncpy( out_pOut, pIn, iLength ); }
  223. static int Compare( const wchar_t *pLhs, const wchar_t *pRhs ) { return wcscmp( pLhs, pRhs ); }
  224. static int Length( const wchar_t *pValue ) { return wcslen( pValue ); }
  225. static const wchar_t *FindChar( const wchar_t *pStr, const wchar_t cSearch ) { return wcschr( pStr, cSearch ); }
  226. static const wchar_t *EmptyString() { return L""; }
  227. };
  228. //-----------------------------------------------------------------------------
  229. // Dirt-basic auto-release string class. Not intended for manipulation,
  230. // can be stored in a container or forwarded as a functor parameter.
  231. // Note the benefit over CUtlString: sizeof(CUtlConstString) == sizeof(char*).
  232. // Also note: null char* pointers are treated identically to empty strings.
  233. //-----------------------------------------------------------------------------
  234. template < typename T = char >
  235. class CUtlConstStringBase
  236. {
  237. public:
  238. CUtlConstStringBase() : m_pString( NULL ) {}
  239. CUtlConstStringBase( const T *pString ) : m_pString( NULL ) { Set( pString ); }
  240. CUtlConstStringBase( const CUtlConstStringBase& src ) : m_pString( NULL ) { Set( src.m_pString ); }
  241. ~CUtlConstStringBase() { Set( NULL ); }
  242. void Set( const T *pValue );
  243. void Clear() { Set( NULL ); }
  244. const T *Get() const { return m_pString ? m_pString : StringFuncs<T>::EmptyString(); }
  245. operator const T*() const { return m_pString ? m_pString : StringFuncs<T>::EmptyString(); }
  246. bool IsEmpty() const { return m_pString == NULL; } // Note: empty strings are never stored by Set
  247. int Compare( const T *rhs ) const;
  248. // Logical ops
  249. bool operator<( const T *rhs ) const { return Compare( rhs ) < 0; }
  250. bool operator==( const T *rhs ) const { return Compare( rhs ) == 0; }
  251. bool operator!=( const T *rhs ) const { return Compare( rhs ) != 0; }
  252. bool operator<( const CUtlConstStringBase &rhs ) const { return Compare( rhs.m_pString ) < 0; }
  253. bool operator==( const CUtlConstStringBase &rhs ) const { return Compare( rhs.m_pString ) == 0; }
  254. bool operator!=( const CUtlConstStringBase &rhs ) const { return Compare( rhs.m_pString ) != 0; }
  255. // If these are not defined, CUtlConstString as rhs will auto-convert
  256. // to const char* and do logical operations on the raw pointers. Ugh.
  257. inline friend bool operator<( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) > 0; }
  258. inline friend bool operator==( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) == 0; }
  259. inline friend bool operator!=( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) != 0; }
  260. CUtlConstStringBase &operator=( const T *src ) { Set( src ); return *this; }
  261. CUtlConstStringBase &operator=( const CUtlConstStringBase &src ) { Set( src.m_pString ); return *this; }
  262. // Defining AltArgumentType_t is a hint to containers that they should
  263. // implement Find/Insert/Remove functions that take const char* params.
  264. typedef const T *AltArgumentType_t;
  265. protected:
  266. const T *m_pString;
  267. };
  268. template < typename T >
  269. void CUtlConstStringBase<T>::Set( const T *pValue )
  270. {
  271. if ( pValue != m_pString )
  272. {
  273. free( ( void* ) m_pString );
  274. m_pString = pValue && pValue[0] ? StringFuncs<T>::Duplicate( pValue ) : NULL;
  275. }
  276. }
  277. template < typename T >
  278. int CUtlConstStringBase<T>::Compare( const T *rhs ) const
  279. {
  280. if ( m_pString )
  281. {
  282. if ( rhs )
  283. return StringFuncs<T>::Compare( m_pString, rhs );
  284. else
  285. return 1;
  286. }
  287. else
  288. {
  289. if ( rhs )
  290. return -1;
  291. else
  292. return 0;
  293. }
  294. }
  295. typedef CUtlConstStringBase<char> CUtlConstString;
  296. typedef CUtlConstStringBase<wchar_t> CUtlConstWideString;
  297. #endif // UTLSTRING_H