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.

519 lines
22 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef KEYVALUES_H
  8. #define KEYVALUES_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #ifndef NULL
  13. #ifdef __cplusplus
  14. #define NULL 0
  15. #else
  16. #define NULL ((void *)0)
  17. #endif
  18. #endif
  19. #include "utlvector.h"
  20. #include "color.h"
  21. #include "exprevaluator.h"
  22. #define FOR_EACH_SUBKEY( kvRoot, kvSubKey ) \
  23. for ( KeyValues * kvSubKey = kvRoot->GetFirstSubKey(); kvSubKey != NULL; kvSubKey = kvSubKey->GetNextKey() )
  24. #define FOR_EACH_TRUE_SUBKEY( kvRoot, kvSubKey ) \
  25. for ( KeyValues * kvSubKey = kvRoot->GetFirstTrueSubKey(); kvSubKey != NULL; kvSubKey = kvSubKey->GetNextTrueSubKey() )
  26. #define FOR_EACH_VALUE( kvRoot, kvValue ) \
  27. for ( KeyValues * kvValue = kvRoot->GetFirstValue(); kvValue != NULL; kvValue = kvValue->GetNextValue() )
  28. class IBaseFileSystem;
  29. class CUtlBuffer;
  30. class Color;
  31. class KeyValues;
  32. class IKeyValuesDumpContext;
  33. typedef void * FileHandle_t;
  34. class CKeyValuesGrowableStringTable;
  35. // single byte identifies a xbox kv file in binary format
  36. // strings are pooled from a searchpath/zip mounted symbol table
  37. #define KV_BINARY_POOLED_FORMAT 0xAA
  38. #define FOR_EACH_SUBKEY( kvRoot, kvSubKey ) \
  39. for ( KeyValues * kvSubKey = kvRoot->GetFirstSubKey(); kvSubKey != NULL; kvSubKey = kvSubKey->GetNextKey() )
  40. #define FOR_EACH_TRUE_SUBKEY( kvRoot, kvSubKey ) \
  41. for ( KeyValues * kvSubKey = kvRoot->GetFirstTrueSubKey(); kvSubKey != NULL; kvSubKey = kvSubKey->GetNextTrueSubKey() )
  42. #define FOR_EACH_VALUE( kvRoot, kvValue ) \
  43. for ( KeyValues * kvValue = kvRoot->GetFirstValue(); kvValue != NULL; kvValue = kvValue->GetNextValue() )
  44. //-----------------------------------------------------------------------------
  45. // Purpose: Simple recursive data access class
  46. // Used in vgui for message parameters and resource files
  47. // Destructor deletes all child KeyValues nodes
  48. // Data is stored in key (string names) - (string/int/float)value pairs called nodes.
  49. //
  50. // About KeyValues Text File Format:
  51. // It has 3 control characters '{', '}' and '"'. Names and values may be quoted or
  52. // not. The quote '"' character must not be used within name or values, only for
  53. // quoting whole tokens. You may use escape sequences wile parsing and add within a
  54. // quoted token a \" to add quotes within your name or token. When using Escape
  55. // Sequence the parser must now that by setting KeyValues::UsesEscapeSequences( true ),
  56. // which it's off by default. Non-quoted tokens ends with a whitespace, '{', '}' and '"'.
  57. // So you may use '{' and '}' within quoted tokens, but not for non-quoted tokens.
  58. // An open bracket '{' after a key name indicates a list of subkeys which is finished
  59. // with a closing bracket '}'. Subkeys use the same definitions recursively.
  60. // Whitespaces are space, return, newline and tabulator. Allowed Escape sequences
  61. // are \n, \t, \\, \n and \". The number character '#' is used for macro purposes
  62. // (eg #include), don't use it as first character in key names.
  63. //-----------------------------------------------------------------------------
  64. class KeyValues
  65. {
  66. public:
  67. // By default, the KeyValues class uses a string table for the key names that is
  68. // limited to 4MB. The game will exit in error if this space is exhausted. In
  69. // general this is preferable for game code for performance and memory fragmentation
  70. // reasons.
  71. //
  72. // If this is not acceptable, you can use this call to switch to a table that can grow
  73. // arbitrarily. This call must be made before any KeyValues objects are allocated or it
  74. // will result in undefined behavior. If you use the growable string table, you cannot
  75. // share KeyValues pointers directly with any other module. You can serialize them across
  76. // module boundaries. These limitations are acceptable in the Steam backend code
  77. // this option was written for, but may not be in other situations. Make sure to
  78. // understand the implications before using this.
  79. static void SetUseGrowableStringTable( bool bUseGrowableTable );
  80. explicit KeyValues( const char *setName );
  81. //
  82. // AutoDelete class to automatically free the keyvalues.
  83. // Simply construct it with the keyvalues you allocated and it will free them when falls out of scope.
  84. // When you decide that keyvalues shouldn't be deleted call Assign(NULL) on it.
  85. // If you constructed AutoDelete(NULL) you can later assign the keyvalues to be deleted with Assign(pKeyValues).
  86. //
  87. class AutoDelete
  88. {
  89. public:
  90. explicit inline AutoDelete( KeyValues *pKeyValues ) : m_pKeyValues( pKeyValues ) {}
  91. explicit inline AutoDelete( const char *pchKVName ) : m_pKeyValues( new KeyValues( pchKVName ) ) {}
  92. inline ~AutoDelete( void ) { if( m_pKeyValues ) m_pKeyValues->deleteThis(); }
  93. inline void Assign( KeyValues *pKeyValues ) { m_pKeyValues = pKeyValues; }
  94. KeyValues *operator->() { return m_pKeyValues; }
  95. operator KeyValues *() { return m_pKeyValues; }
  96. private:
  97. AutoDelete( AutoDelete const &x ); // forbid
  98. AutoDelete & operator= ( AutoDelete const &x ); // forbid
  99. protected:
  100. KeyValues *m_pKeyValues;
  101. };
  102. //
  103. // AutoDeleteInline is useful when you want to hold your keyvalues object inside
  104. // and delete it right after using.
  105. // You can also pass temporary KeyValues object as an argument to a function by wrapping it into KeyValues::AutoDeleteInline
  106. // instance: call_my_function( KeyValues::AutoDeleteInline( new KeyValues( "test" ) ) )
  107. //
  108. class AutoDeleteInline : public AutoDelete
  109. {
  110. public:
  111. explicit inline AutoDeleteInline( KeyValues *pKeyValues ) : AutoDelete( pKeyValues ) {}
  112. inline operator KeyValues *() const { return m_pKeyValues; }
  113. inline KeyValues * Get() const { return m_pKeyValues; }
  114. };
  115. // Quick setup constructors
  116. KeyValues( const char *setName, const char *firstKey, const char *firstValue );
  117. KeyValues( const char *setName, const char *firstKey, const wchar_t *firstValue );
  118. KeyValues( const char *setName, const char *firstKey, int firstValue );
  119. KeyValues( const char *setName, const char *firstKey, const char *firstValue, const char *secondKey, const char *secondValue );
  120. KeyValues( const char *setName, const char *firstKey, int firstValue, const char *secondKey, int secondValue );
  121. // Section name
  122. const char *GetName() const;
  123. void SetName( const char *setName);
  124. // gets the name as a unique int
  125. int GetNameSymbol() const;
  126. int GetNameSymbolCaseSensitive() const;
  127. // File access. Set UsesEscapeSequences true, if resource file/buffer uses Escape Sequences (eg \n, \t)
  128. void UsesEscapeSequences(bool state); // default false
  129. bool LoadFromFile( IBaseFileSystem *filesystem, const char *resourceName, const char *pathID = NULL, GetSymbolProc_t pfnEvaluateSymbolProc = NULL);
  130. bool SaveToFile( IBaseFileSystem *filesystem, const char *resourceName, const char *pathID = NULL);
  131. // Read from a buffer... Note that the buffer must be null terminated
  132. bool LoadFromBuffer( char const *resourceName, const char *pBuffer, IBaseFileSystem* pFileSystem = NULL, const char *pPathID = NULL, GetSymbolProc_t pfnEvaluateSymbolProc = NULL );
  133. // Read from a utlbuffer...
  134. bool LoadFromBuffer( char const *resourceName, CUtlBuffer &buf, IBaseFileSystem* pFileSystem = NULL, const char *pPathID = NULL, GetSymbolProc_t pfnEvaluateSymbolProc = NULL );
  135. // Find a keyValue, create it if it is not found.
  136. // Set bCreate to true to create the key if it doesn't already exist (which ensures a valid pointer will be returned)
  137. KeyValues *FindKey(const char *keyName, bool bCreate = false);
  138. KeyValues *FindKey(int keySymbol) const;
  139. KeyValues *CreateNewKey(); // creates a new key, with an autogenerated name. name is guaranteed to be an integer, of value 1 higher than the highest other integer key name
  140. void AddSubKey( KeyValues *pSubkey ); // Adds a subkey. Make sure the subkey isn't a child of some other keyvalues
  141. void RemoveSubKey(KeyValues *subKey); // removes a subkey from the list, DOES NOT DELETE IT
  142. void InsertSubKey( int nIndex, KeyValues *pSubKey ); // Inserts the given sub-key before the Nth child location
  143. bool ContainsSubKey( KeyValues *pSubKey ); // Returns true if this key values contains the specified sub key, false otherwise.
  144. void SwapSubKey( KeyValues *pExistingSubKey, KeyValues *pNewSubKey ); // Swaps an existing subkey for a new one, DOES NOT DELETE THE OLD ONE but takes ownership of the new one
  145. void ElideSubKey( KeyValues *pSubKey ); // Removes a subkey but inserts all of its children in its place, in-order (flattens a tree, like firing a manager!)
  146. // Key iteration.
  147. //
  148. // NOTE: GetFirstSubKey/GetNextKey will iterate keys AND values. Use the functions
  149. // below if you want to iterate over just the keys or just the values.
  150. //
  151. KeyValues *GetFirstSubKey(); // returns the first subkey in the list
  152. KeyValues *GetNextKey(); // returns the next subkey
  153. void SetNextKey( KeyValues * pDat);
  154. //
  155. // These functions can be used to treat it like a true key/values tree instead of
  156. // confusing values with keys.
  157. //
  158. // So if you wanted to iterate all subkeys, then all values, it would look like this:
  159. // for ( KeyValues *pKey = pRoot->GetFirstTrueSubKey(); pKey; pKey = pKey->GetNextTrueSubKey() )
  160. // {
  161. // Msg( "Key name: %s\n", pKey->GetName() );
  162. // }
  163. // for ( KeyValues *pValue = pRoot->GetFirstValue(); pKey; pKey = pKey->GetNextValue() )
  164. // {
  165. // Msg( "Int value: %d\n", pValue->GetInt() ); // Assuming pValue->GetDataType() == TYPE_INT...
  166. // }
  167. KeyValues* GetFirstTrueSubKey();
  168. KeyValues* GetNextTrueSubKey();
  169. KeyValues* GetFirstValue(); // When you get a value back, you can use GetX and pass in NULL to get the value.
  170. KeyValues* GetNextValue();
  171. // Data access
  172. int GetInt( const char *keyName = NULL, int defaultValue = 0 );
  173. uint64 GetUint64( const char *keyName = NULL, uint64 defaultValue = 0 );
  174. float GetFloat( const char *keyName = NULL, float defaultValue = 0.0f );
  175. const char *GetString( const char *keyName = NULL, const char *defaultValue = "" );
  176. const wchar_t *GetWString( const char *keyName = NULL, const wchar_t *defaultValue = L"" );
  177. void *GetPtr( const char *keyName = NULL, void *defaultValue = (void*)0 );
  178. Color GetColor( const char *keyName = NULL , const Color &defaultColor = Color( 0, 0, 0, 0 ) );
  179. bool GetBool( const char *keyName = NULL, bool defaultValue = false ) { return GetInt( keyName, defaultValue ? 1 : 0 ) ? true : false; }
  180. bool IsEmpty(const char *keyName = NULL);
  181. // Data access
  182. int GetInt( int keySymbol, int defaultValue = 0 );
  183. uint64 GetUint64( int keySymbol, uint64 defaultValue = 0 );
  184. float GetFloat( int keySymbol, float defaultValue = 0.0f );
  185. const char *GetString( int keySymbol, const char *defaultValue = "" );
  186. const wchar_t *GetWString( int keySymbol, const wchar_t *defaultValue = L"" );
  187. void *GetPtr( int keySymbol, void *defaultValue = (void*)0 );
  188. Color GetColor( int keySymbol /* default value is all black */);
  189. bool GetBool( int keySymbol, bool defaultValue = false ) { return GetInt( keySymbol, defaultValue ? 1 : 0 ) ? true : false; }
  190. bool IsEmpty( int keySymbol );
  191. // Key writing
  192. void SetWString( const char *keyName, const wchar_t *value );
  193. void SetString( const char *keyName, const char *value );
  194. void SetInt( const char *keyName, int value );
  195. void SetUint64( const char *keyName, uint64 value );
  196. void SetFloat( const char *keyName, float value );
  197. void SetPtr( const char *keyName, void *value );
  198. void SetColor( const char *keyName, Color value);
  199. void SetBool( const char *keyName, bool value ) { SetInt( keyName, value ? 1 : 0 ); }
  200. // Memory allocation (optimized)
  201. void *operator new( size_t iAllocSize );
  202. void *operator new( size_t iAllocSize, int nBlockUse, const char *pFileName, int nLine );
  203. void operator delete( void *pMem );
  204. void operator delete( void *pMem, int nBlockUse, const char *pFileName, int nLine );
  205. KeyValues& operator=( KeyValues& src );
  206. // Adds a chain... if we don't find stuff in this keyvalue, we'll look
  207. // in the one we're chained to.
  208. void ChainKeyValue( KeyValues* pChain );
  209. void RecursiveSaveToFile( CUtlBuffer& buf, int indentLevel );
  210. bool WriteAsBinary( CUtlBuffer &buffer ) const;
  211. bool ReadAsBinary( CUtlBuffer &buffer );
  212. // Allocate & create a new copy of the keys
  213. KeyValues *MakeCopy( void ) const;
  214. // Make a new copy of all subkeys, add them all to the passed-in keyvalues
  215. void CopySubkeys( KeyValues *pParent ) const;
  216. // Clear out all subkeys, and the current value
  217. void Clear( void );
  218. // Data type
  219. enum types_t
  220. {
  221. TYPE_NONE = 0,
  222. TYPE_STRING,
  223. TYPE_INT,
  224. TYPE_FLOAT,
  225. TYPE_PTR,
  226. TYPE_WSTRING,
  227. TYPE_COLOR,
  228. TYPE_UINT64,
  229. TYPE_COMPILED_INT_BYTE, // hack to collapse 1 byte ints in the compiled format
  230. TYPE_COMPILED_INT_0, // hack to collapse 0 in the compiled format
  231. TYPE_COMPILED_INT_1, // hack to collapse 1 in the compiled format
  232. TYPE_NUMTYPES,
  233. };
  234. types_t GetDataType(const char *keyName = NULL);
  235. // Virtual deletion function - ensures that KeyValues object is deleted from correct heap
  236. void deleteThis();
  237. void SetStringValue( char const *strValue );
  238. // unpack a key values list into a structure
  239. void UnpackIntoStructure( struct KeyValuesUnpackStructure const *pUnpackTable, void *pDest );
  240. // Process conditional keys for widescreen support.
  241. bool ProcessResolutionKeys( const char *pResString );
  242. // Dump keyvalues recursively into a dump context
  243. bool Dump( IKeyValuesDumpContext *pDump, int nIndentLevel = 0 );
  244. // Merge operations describing how two keyvalues can be combined
  245. enum MergeKeyValuesOp_t
  246. {
  247. MERGE_KV_ALL,
  248. MERGE_KV_UPDATE, // update values are copied into storage, adding new keys to storage or updating existing ones
  249. MERGE_KV_DELETE, // update values specify keys that get deleted from storage
  250. MERGE_KV_BORROW, // update values only update existing keys in storage, keys in update that do not exist in storage are discarded
  251. };
  252. void MergeFrom( KeyValues *kvMerge, MergeKeyValuesOp_t eOp = MERGE_KV_ALL );
  253. // Assign keyvalues from a string
  254. static KeyValues * FromString( char const *szName, char const *szStringVal, char const **ppEndOfParse = NULL );
  255. protected:
  256. KeyValues( KeyValues& ); // prevent copy constructor being used
  257. // prevent delete being called except through deleteThis()
  258. ~KeyValues();
  259. KeyValues* CreateKey( const char *keyName );
  260. void RecursiveCopyKeyValues( KeyValues& src );
  261. void RemoveEverything();
  262. // void RecursiveSaveToFile( IBaseFileSystem *filesystem, CUtlBuffer &buffer, int indentLevel );
  263. // void WriteConvertedString( CUtlBuffer &buffer, const char *pszString );
  264. // NOTE: If both filesystem and pBuf are non-null, it'll save to both of them.
  265. // If filesystem is null, it'll ignore f.
  266. void RecursiveSaveToFile( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, int indentLevel );
  267. void WriteConvertedString( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, const char *pszString );
  268. void RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &buf, GetSymbolProc_t pfnEvaluateSymbolProc );
  269. // for handling #include "filename"
  270. void AppendIncludedKeys( CUtlVector< KeyValues * >& includedKeys );
  271. void ParseIncludedKeys( char const *resourceName, const char *filetoinclude,
  272. IBaseFileSystem* pFileSystem, const char *pPathID, CUtlVector< KeyValues * >& includedKeys, GetSymbolProc_t pfnEvaluateSymbolProc );
  273. // For handling #base "filename"
  274. void MergeBaseKeys( CUtlVector< KeyValues * >& baseKeys );
  275. void RecursiveMergeKeyValues( KeyValues *baseKV );
  276. // NOTE: If both filesystem and pBuf are non-null, it'll save to both of them.
  277. // If filesystem is null, it'll ignore f.
  278. void InternalWrite( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, const void *pData, int len );
  279. void Init();
  280. const char * ReadToken( CUtlBuffer &buf, bool &wasQuoted, bool &wasConditional );
  281. void WriteIndents( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, int indentLevel );
  282. void FreeAllocatedValue();
  283. void AllocateValueBlock(int size);
  284. bool ReadAsBinaryPooledFormat( CUtlBuffer &buf, IBaseFileSystem *pFileSystem, unsigned int poolKey, GetSymbolProc_t pfnEvaluateSymbolProc );
  285. bool EvaluateConditional( const char *pExpressionString, GetSymbolProc_t pfnEvaluateSymbolProc );
  286. uint32 m_iKeyName : 24; // keyname is a symbol defined in KeyValuesSystem
  287. uint32 m_iKeyNameCaseSensitive1 : 8; // 1st part of case sensitive symbol defined in KeyValueSystem
  288. // These are needed out of the union because the API returns string pointers
  289. char *m_sValue;
  290. wchar_t *m_wsValue;
  291. // we don't delete these
  292. union
  293. {
  294. int m_iValue;
  295. float m_flValue;
  296. void *m_pValue;
  297. unsigned char m_Color[4];
  298. };
  299. char m_iDataType;
  300. char m_bHasEscapeSequences; // true, if while parsing this KeyValue, Escape Sequences are used (default false)
  301. uint16 m_iKeyNameCaseSensitive2; // 2nd part of case sensitive symbol defined in KeyValueSystem;
  302. KeyValues *m_pPeer; // pointer to next key in list
  303. KeyValues *m_pSub; // pointer to Start of a new sub key list
  304. KeyValues *m_pChain;// Search here if it's not in our list
  305. GetSymbolProc_t m_pExpressionGetSymbolProc;
  306. private:
  307. // Statics to implement the optional growable string table
  308. // Function pointers that will determine which mode we are in
  309. static int (*s_pfGetSymbolForString)( const char *name, bool bCreate );
  310. static const char *(*s_pfGetStringForSymbol)( int symbol );
  311. static CKeyValuesGrowableStringTable *s_pGrowableStringTable;
  312. public:
  313. // Functions that invoke the default behavior
  314. static int GetSymbolForStringClassic( const char *name, bool bCreate = true );
  315. static const char *GetStringForSymbolClassic( int symbol );
  316. // Functions that use the growable string table
  317. static int GetSymbolForStringGrowable( const char *name, bool bCreate = true );
  318. static const char *GetStringForSymbolGrowable( int symbol );
  319. };
  320. typedef KeyValues::AutoDelete KeyValuesAD;
  321. enum KeyValuesUnpackDestinationTypes_t
  322. {
  323. UNPACK_TYPE_FLOAT, // dest is a float
  324. UNPACK_TYPE_VECTOR, // dest is a Vector
  325. UNPACK_TYPE_VECTOR_COLOR, // dest is a vector, src is a color
  326. UNPACK_TYPE_STRING, // dest is a char *. unpacker will allocate.
  327. UNPACK_TYPE_INT, // dest is an int
  328. UNPACK_TYPE_FOUR_FLOATS, // dest is an array of 4 floats. source is a string like "1 2 3 4"
  329. UNPACK_TYPE_TWO_FLOATS, // dest is an array of 2 floats. source is a string like "1 2"
  330. };
  331. #define UNPACK_FIXED( kname, kdefault, dtype, ofs ) { kname, kdefault, dtype, ofs, 0 }
  332. #define UNPACK_VARIABLE( kname, kdefault, dtype, ofs, sz ) { kname, kdefault, dtype, ofs, sz }
  333. #define UNPACK_END_MARKER { NULL, NULL, UNPACK_TYPE_FLOAT, 0 }
  334. struct KeyValuesUnpackStructure
  335. {
  336. char const *m_pKeyName; // null to terminate tbl
  337. char const *m_pKeyDefault; // null ok
  338. KeyValuesUnpackDestinationTypes_t m_eDataType; // UNPACK_TYPE_INT, ..
  339. size_t m_nFieldOffset; // use offsetof to set
  340. size_t m_nFieldSize; // for strings or other variable length
  341. };
  342. //-----------------------------------------------------------------------------
  343. // inline methods
  344. //-----------------------------------------------------------------------------
  345. inline int KeyValues::GetInt( int keySymbol, int defaultValue )
  346. {
  347. KeyValues *dat = FindKey( keySymbol );
  348. return dat ? dat->GetInt( (const char *)NULL, defaultValue ) : defaultValue;
  349. }
  350. inline uint64 KeyValues::GetUint64( int keySymbol, uint64 defaultValue )
  351. {
  352. KeyValues *dat = FindKey( keySymbol );
  353. return dat ? dat->GetUint64( (const char *)NULL, defaultValue ) : defaultValue;
  354. }
  355. inline float KeyValues::GetFloat( int keySymbol, float defaultValue )
  356. {
  357. KeyValues *dat = FindKey( keySymbol );
  358. return dat ? dat->GetFloat( (const char *)NULL, defaultValue ) : defaultValue;
  359. }
  360. inline const char *KeyValues::GetString( int keySymbol, const char *defaultValue )
  361. {
  362. KeyValues *dat = FindKey( keySymbol );
  363. return dat ? dat->GetString( (const char *)NULL, defaultValue ) : defaultValue;
  364. }
  365. inline const wchar_t *KeyValues::GetWString( int keySymbol, const wchar_t *defaultValue )
  366. {
  367. KeyValues *dat = FindKey( keySymbol );
  368. return dat ? dat->GetWString( (const char *)NULL, defaultValue ) : defaultValue;
  369. }
  370. inline void *KeyValues::GetPtr( int keySymbol, void *defaultValue )
  371. {
  372. KeyValues *dat = FindKey( keySymbol );
  373. return dat ? dat->GetPtr( (const char *)NULL, defaultValue ) : defaultValue;
  374. }
  375. inline Color KeyValues::GetColor( int keySymbol )
  376. {
  377. Color defaultValue( 0, 0, 0, 0 );
  378. KeyValues *dat = FindKey( keySymbol );
  379. return dat ? dat->GetColor( ) : defaultValue;
  380. }
  381. inline bool KeyValues::IsEmpty( int keySymbol )
  382. {
  383. KeyValues *dat = FindKey( keySymbol );
  384. return dat ? dat->IsEmpty( ) : true;
  385. }
  386. //
  387. // KeyValuesDumpContext and generic implementations
  388. //
  389. class IKeyValuesDumpContext
  390. {
  391. public:
  392. virtual bool KvBeginKey( KeyValues *pKey, int nIndentLevel ) = 0;
  393. virtual bool KvWriteValue( KeyValues *pValue, int nIndentLevel ) = 0;
  394. virtual bool KvEndKey( KeyValues *pKey, int nIndentLevel ) = 0;
  395. };
  396. class IKeyValuesDumpContextAsText : public IKeyValuesDumpContext
  397. {
  398. public:
  399. virtual bool KvBeginKey( KeyValues *pKey, int nIndentLevel );
  400. virtual bool KvWriteValue( KeyValues *pValue, int nIndentLevel );
  401. virtual bool KvEndKey( KeyValues *pKey, int nIndentLevel );
  402. public:
  403. virtual bool KvWriteIndent( int nIndentLevel );
  404. virtual bool KvWriteText( char const *szText ) = 0;
  405. };
  406. class CKeyValuesDumpContextAsDevMsg : public IKeyValuesDumpContextAsText
  407. {
  408. public:
  409. // Overrides developer level to dump in DevMsg, zero to dump as Msg
  410. CKeyValuesDumpContextAsDevMsg( int nDeveloperLevel = 1 ) : m_nDeveloperLevel( nDeveloperLevel ) {}
  411. public:
  412. virtual bool KvBeginKey( KeyValues *pKey, int nIndentLevel );
  413. virtual bool KvWriteText( char const *szText );
  414. protected:
  415. int m_nDeveloperLevel;
  416. };
  417. inline bool KeyValuesDumpAsDevMsg( KeyValues *pKeyValues, int nIndentLevel = 0, int nDeveloperLevel = 1 )
  418. {
  419. CKeyValuesDumpContextAsDevMsg ctx( nDeveloperLevel );
  420. return pKeyValues->Dump( &ctx, nIndentLevel );
  421. }
  422. #endif // KEYVALUES_H