Dict.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __DICT_H__
  21. #define __DICT_H__
  22. /*
  23. ===============================================================================
  24. Key/value dictionary
  25. This is a dictionary class that tracks an arbitrary number of key / value
  26. pair combinations. It is used for map entity spawning, GUI state management,
  27. and other things.
  28. Keys are compared case-insensitive.
  29. Does not allocate memory until the first key/value pair is added.
  30. ===============================================================================
  31. */
  32. class idKeyValue {
  33. friend class idDict;
  34. public:
  35. const idStr & GetKey( void ) const { return *key; }
  36. const idStr & GetValue( void ) const { return *value; }
  37. size_t Allocated( void ) const { return key->Allocated() + value->Allocated(); }
  38. size_t Size( void ) const { return sizeof( *this ) + key->Size() + value->Size(); }
  39. bool operator==( const idKeyValue &kv ) const { return ( key == kv.key && value == kv.value ); }
  40. private:
  41. const idPoolStr * key;
  42. const idPoolStr * value;
  43. };
  44. class idDict {
  45. public:
  46. idDict( void );
  47. idDict( const idDict &other ); // allow declaration with assignment
  48. ~idDict( void );
  49. // set the granularity for the index
  50. void SetGranularity( int granularity );
  51. // set hash size
  52. void SetHashSize( int hashSize );
  53. // clear existing key/value pairs and copy all key/value pairs from other
  54. idDict & operator=( const idDict &other );
  55. // copy from other while leaving existing key/value pairs in place
  56. void Copy( const idDict &other );
  57. // clear existing key/value pairs and transfer key/value pairs from other
  58. void TransferKeyValues( idDict &other );
  59. // parse dict from parser
  60. bool Parse( idParser &parser );
  61. // copy key/value pairs from other dict not present in this dict
  62. void SetDefaults( const idDict *dict );
  63. // clear dict freeing up memory
  64. void Clear( void );
  65. // print the dict
  66. void Print() const;
  67. size_t Allocated( void ) const;
  68. size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
  69. void Set( const char *key, const char *value );
  70. void SetFloat( const char *key, float val );
  71. void SetInt( const char *key, int val );
  72. void SetBool( const char *key, bool val );
  73. void SetVector( const char *key, const idVec3 &val );
  74. void SetVec2( const char *key, const idVec2 &val );
  75. void SetVec4( const char *key, const idVec4 &val );
  76. void SetAngles( const char *key, const idAngles &val );
  77. void SetMatrix( const char *key, const idMat3 &val );
  78. // these return default values of 0.0, 0 and false
  79. const char * GetString( const char *key, const char *defaultString = "" ) const;
  80. float GetFloat( const char *key, const char *defaultString = "0" ) const;
  81. int GetInt( const char *key, const char *defaultString = "0" ) const;
  82. bool GetBool( const char *key, const char *defaultString = "0" ) const;
  83. idVec3 GetVector( const char *key, const char *defaultString = NULL ) const;
  84. idVec2 GetVec2( const char *key, const char *defaultString = NULL ) const;
  85. idVec4 GetVec4( const char *key, const char *defaultString = NULL ) const;
  86. idAngles GetAngles( const char *key, const char *defaultString = NULL ) const;
  87. idMat3 GetMatrix( const char *key, const char *defaultString = NULL ) const;
  88. bool GetString( const char *key, const char *defaultString, const char **out ) const;
  89. bool GetString( const char *key, const char *defaultString, idStr &out ) const;
  90. bool GetFloat( const char *key, const char *defaultString, float &out ) const;
  91. bool GetInt( const char *key, const char *defaultString, int &out ) const;
  92. bool GetBool( const char *key, const char *defaultString, bool &out ) const;
  93. bool GetVector( const char *key, const char *defaultString, idVec3 &out ) const;
  94. bool GetVec2( const char *key, const char *defaultString, idVec2 &out ) const;
  95. bool GetVec4( const char *key, const char *defaultString, idVec4 &out ) const;
  96. bool GetAngles( const char *key, const char *defaultString, idAngles &out ) const;
  97. bool GetMatrix( const char *key, const char *defaultString, idMat3 &out ) const;
  98. int GetNumKeyVals( void ) const;
  99. const idKeyValue * GetKeyVal( int index ) const;
  100. // returns the key/value pair with the given key
  101. // returns NULL if the key/value pair does not exist
  102. const idKeyValue * FindKey( const char *key ) const;
  103. // returns the index to the key/value pair with the given key
  104. // returns -1 if the key/value pair does not exist
  105. int FindKeyIndex( const char *key ) const;
  106. // delete the key/value pair with the given key
  107. void Delete( const char *key );
  108. // finds the next key/value pair with the given key prefix.
  109. // lastMatch can be used to do additional searches past the first match.
  110. const idKeyValue * MatchPrefix( const char *prefix, const idKeyValue *lastMatch = NULL ) const;
  111. // randomly chooses one of the key/value pairs with the given key prefix and returns it's value
  112. const char * RandomPrefix( const char *prefix, idRandom &random ) const;
  113. void WriteToFileHandle( idFile *f ) const;
  114. void ReadFromFileHandle( idFile *f );
  115. // returns a unique checksum for this dictionary's content
  116. int Checksum( void ) const;
  117. static void Init( void );
  118. static void Shutdown( void );
  119. static void ShowMemoryUsage_f( const idCmdArgs &args );
  120. static void ListKeys_f( const idCmdArgs &args );
  121. static void ListValues_f( const idCmdArgs &args );
  122. private:
  123. idList<idKeyValue> args;
  124. idHashIndex argHash;
  125. static idStrPool globalKeys;
  126. static idStrPool globalValues;
  127. };
  128. ID_INLINE idDict::idDict( void ) {
  129. args.SetGranularity( 16 );
  130. argHash.SetGranularity( 16 );
  131. argHash.Clear( 128, 16 );
  132. }
  133. ID_INLINE idDict::idDict( const idDict &other ) {
  134. *this = other;
  135. }
  136. ID_INLINE idDict::~idDict( void ) {
  137. Clear();
  138. }
  139. ID_INLINE void idDict::SetGranularity( int granularity ) {
  140. args.SetGranularity( granularity );
  141. argHash.SetGranularity( granularity );
  142. }
  143. ID_INLINE void idDict::SetHashSize( int hashSize ) {
  144. if ( args.Num() == 0 ) {
  145. argHash.Clear( hashSize, 16 );
  146. }
  147. }
  148. ID_INLINE void idDict::SetFloat( const char *key, float val ) {
  149. Set( key, va( "%f", val ) );
  150. }
  151. ID_INLINE void idDict::SetInt( const char *key, int val ) {
  152. Set( key, va( "%i", val ) );
  153. }
  154. ID_INLINE void idDict::SetBool( const char *key, bool val ) {
  155. Set( key, va( "%i", val ) );
  156. }
  157. ID_INLINE void idDict::SetVector( const char *key, const idVec3 &val ) {
  158. Set( key, val.ToString() );
  159. }
  160. ID_INLINE void idDict::SetVec4( const char *key, const idVec4 &val ) {
  161. Set( key, val.ToString() );
  162. }
  163. ID_INLINE void idDict::SetVec2( const char *key, const idVec2 &val ) {
  164. Set( key, val.ToString() );
  165. }
  166. ID_INLINE void idDict::SetAngles( const char *key, const idAngles &val ) {
  167. Set( key, val.ToString() );
  168. }
  169. ID_INLINE void idDict::SetMatrix( const char *key, const idMat3 &val ) {
  170. Set( key, val.ToString() );
  171. }
  172. ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, const char **out ) const {
  173. const idKeyValue *kv = FindKey( key );
  174. if ( kv ) {
  175. *out = kv->GetValue();
  176. return true;
  177. }
  178. *out = defaultString;
  179. return false;
  180. }
  181. ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, idStr &out ) const {
  182. const idKeyValue *kv = FindKey( key );
  183. if ( kv ) {
  184. out = kv->GetValue();
  185. return true;
  186. }
  187. out = defaultString;
  188. return false;
  189. }
  190. ID_INLINE const char *idDict::GetString( const char *key, const char *defaultString ) const {
  191. const idKeyValue *kv = FindKey( key );
  192. if ( kv ) {
  193. return kv->GetValue();
  194. }
  195. return defaultString;
  196. }
  197. ID_INLINE float idDict::GetFloat( const char *key, const char *defaultString ) const {
  198. return atof( GetString( key, defaultString ) );
  199. }
  200. ID_INLINE int idDict::GetInt( const char *key, const char *defaultString ) const {
  201. return atoi( GetString( key, defaultString ) );
  202. }
  203. ID_INLINE bool idDict::GetBool( const char *key, const char *defaultString ) const {
  204. return ( atoi( GetString( key, defaultString ) ) != 0 );
  205. }
  206. ID_INLINE idVec3 idDict::GetVector( const char *key, const char *defaultString ) const {
  207. idVec3 out;
  208. GetVector( key, defaultString, out );
  209. return out;
  210. }
  211. ID_INLINE idVec2 idDict::GetVec2( const char *key, const char *defaultString ) const {
  212. idVec2 out;
  213. GetVec2( key, defaultString, out );
  214. return out;
  215. }
  216. ID_INLINE idVec4 idDict::GetVec4( const char *key, const char *defaultString ) const {
  217. idVec4 out;
  218. GetVec4( key, defaultString, out );
  219. return out;
  220. }
  221. ID_INLINE idAngles idDict::GetAngles( const char *key, const char *defaultString ) const {
  222. idAngles out;
  223. GetAngles( key, defaultString, out );
  224. return out;
  225. }
  226. ID_INLINE idMat3 idDict::GetMatrix( const char *key, const char *defaultString ) const {
  227. idMat3 out;
  228. GetMatrix( key, defaultString, out );
  229. return out;
  230. }
  231. ID_INLINE int idDict::GetNumKeyVals( void ) const {
  232. return args.Num();
  233. }
  234. ID_INLINE const idKeyValue *idDict::GetKeyVal( int index ) const {
  235. if ( index >= 0 && index < args.Num() ) {
  236. return &args[ index ];
  237. }
  238. return NULL;
  239. }
  240. #endif /* !__DICT_H__ */