Dict.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. class idSerializer;
  23. /*
  24. ===============================================================================
  25. Key/value dictionary
  26. This is a dictionary class that tracks an arbitrary number of key / value
  27. pair combinations. It is used for map entity spawning, GUI state management,
  28. and other things.
  29. Keys are compared case-insensitive.
  30. Does not allocate memory until the first key/value pair is added.
  31. ===============================================================================
  32. */
  33. class idKeyValue {
  34. friend class idDict;
  35. public:
  36. const idStr & GetKey() const { return *key; }
  37. const idStr & GetValue() const { return *value; }
  38. size_t Allocated() const { return key->Allocated() + value->Allocated(); }
  39. size_t Size() const { return sizeof( *this ) + key->Size() + value->Size(); }
  40. bool operator==( const idKeyValue &kv ) const { return ( key == kv.key && value == kv.value ); }
  41. private:
  42. const idPoolStr * key;
  43. const idPoolStr * value;
  44. };
  45. /*
  46. ================================================
  47. idSort_KeyValue
  48. ================================================
  49. */
  50. class idSort_KeyValue : public idSort_Quick< idKeyValue, idSort_KeyValue > {
  51. public:
  52. int Compare( const idKeyValue & a, const idKeyValue & b ) const { return a.GetKey().Icmp( b.GetKey() ); }
  53. };
  54. class idDict {
  55. public:
  56. idDict();
  57. idDict( const idDict &other ); // allow declaration with assignment
  58. ~idDict();
  59. // set the granularity for the index
  60. void SetGranularity( int granularity );
  61. // set hash size
  62. void SetHashSize( int hashSize );
  63. // clear existing key/value pairs and copy all key/value pairs from other
  64. idDict & operator=( const idDict &other );
  65. // copy from other while leaving existing key/value pairs in place
  66. void Copy( const idDict &other );
  67. // clear existing key/value pairs and transfer key/value pairs from other
  68. void TransferKeyValues( idDict &other );
  69. // parse dict from parser
  70. bool Parse( idParser &parser );
  71. // copy key/value pairs from other dict not present in this dict
  72. void SetDefaults( const idDict *dict );
  73. // clear dict freeing up memory
  74. void Clear();
  75. // print the dict
  76. void Print() const;
  77. size_t Allocated() const;
  78. size_t Size() const { return sizeof( *this ) + Allocated(); }
  79. void Set( const char *key, const char *value );
  80. void SetFloat( const char *key, float val );
  81. void SetInt( const char *key, int val );
  82. void SetBool( const char *key, bool val );
  83. void SetVector( const char *key, const idVec3 &val );
  84. void SetVec2( const char *key, const idVec2 &val );
  85. void SetVec4( const char *key, const idVec4 &val );
  86. void SetAngles( const char *key, const idAngles &val );
  87. void SetMatrix( const char *key, const idMat3 &val );
  88. // these return default values of 0.0, 0 and false
  89. const char * GetString( const char *key, const char *defaultString = "" ) const;
  90. float GetFloat( const char *key, const char *defaultString ) const;
  91. int GetInt( const char *key, const char *defaultString ) const;
  92. bool GetBool( const char *key, const char *defaultString ) const;
  93. float GetFloat( const char *key, const float defaultFloat = 0.0f ) const;
  94. int GetInt( const char *key, const int defaultInt = 0 ) const;
  95. bool GetBool( const char *key, const bool defaultBool = false ) const;
  96. idVec3 GetVector( const char *key, const char *defaultString = NULL ) const;
  97. idVec2 GetVec2( const char *key, const char *defaultString = NULL ) const;
  98. idVec4 GetVec4( const char *key, const char *defaultString = NULL ) const;
  99. idAngles GetAngles( const char *key, const char *defaultString = NULL ) const;
  100. idMat3 GetMatrix( const char *key, const char *defaultString = NULL ) const;
  101. bool GetString( const char *key, const char *defaultString, const char **out ) const;
  102. bool GetString( const char *key, const char *defaultString, idStr &out ) const;
  103. bool GetFloat( const char *key, const char *defaultString, float &out ) const;
  104. bool GetInt( const char *key, const char *defaultString, int &out ) const;
  105. bool GetBool( const char *key, const char *defaultString, bool &out ) const;
  106. bool GetFloat( const char *key, const float defaultFloat, float &out ) const;
  107. bool GetInt( const char *key, const int defaultInt, int &out ) const;
  108. bool GetBool( const char *key, const bool defaultBool, bool &out ) const;
  109. bool GetVector( const char *key, const char *defaultString, idVec3 &out ) const;
  110. bool GetVec2( const char *key, const char *defaultString, idVec2 &out ) const;
  111. bool GetVec4( const char *key, const char *defaultString, idVec4 &out ) const;
  112. bool GetAngles( const char *key, const char *defaultString, idAngles &out ) const;
  113. bool GetMatrix( const char *key, const char *defaultString, idMat3 &out ) const;
  114. int GetNumKeyVals() const;
  115. const idKeyValue * GetKeyVal( int index ) const;
  116. // returns the key/value pair with the given key
  117. // returns NULL if the key/value pair does not exist
  118. const idKeyValue * FindKey( const char *key ) const;
  119. // returns the index to the key/value pair with the given key
  120. // returns -1 if the key/value pair does not exist
  121. int FindKeyIndex( const char *key ) const;
  122. // delete the key/value pair with the given key
  123. void Delete( const char *key );
  124. // finds the next key/value pair with the given key prefix.
  125. // lastMatch can be used to do additional searches past the first match.
  126. const idKeyValue * MatchPrefix( const char *prefix, const idKeyValue *lastMatch = NULL ) const;
  127. // randomly chooses one of the key/value pairs with the given key prefix and returns it's value
  128. const char * RandomPrefix( const char *prefix, idRandom &random ) const;
  129. void WriteToFileHandle( idFile *f ) const;
  130. void ReadFromFileHandle( idFile *f );
  131. void WriteToIniFile( idFile * f ) const;
  132. bool ReadFromIniFile( idFile * f );
  133. void Serialize( idSerializer & ser );
  134. // returns a unique checksum for this dictionary's content
  135. int Checksum() const;
  136. static void Init();
  137. static void Shutdown();
  138. static void ShowMemoryUsage_f( const idCmdArgs &args );
  139. static void ListKeys_f( const idCmdArgs &args );
  140. static void ListValues_f( const idCmdArgs &args );
  141. private:
  142. idList<idKeyValue> args;
  143. idHashIndex argHash;
  144. static idStrPool globalKeys;
  145. static idStrPool globalValues;
  146. };
  147. ID_INLINE idDict::idDict() {
  148. args.SetGranularity( 16 );
  149. argHash.SetGranularity( 16 );
  150. argHash.Clear( 128, 16 );
  151. }
  152. ID_INLINE idDict::idDict( const idDict &other ) {
  153. *this = other;
  154. }
  155. ID_INLINE idDict::~idDict() {
  156. Clear();
  157. }
  158. ID_INLINE void idDict::SetGranularity( int granularity ) {
  159. args.SetGranularity( granularity );
  160. argHash.SetGranularity( granularity );
  161. }
  162. ID_INLINE void idDict::SetHashSize( int hashSize ) {
  163. if ( args.Num() == 0 ) {
  164. argHash.Clear( hashSize, 16 );
  165. }
  166. }
  167. ID_INLINE void idDict::SetFloat( const char *key, float val ) {
  168. Set( key, va( "%f", val ) );
  169. }
  170. ID_INLINE void idDict::SetInt( const char *key, int val ) {
  171. Set( key, va( "%i", val ) );
  172. }
  173. ID_INLINE void idDict::SetBool( const char *key, bool val ) {
  174. Set( key, va( "%i", val ) );
  175. }
  176. ID_INLINE void idDict::SetVector( const char *key, const idVec3 &val ) {
  177. Set( key, val.ToString() );
  178. }
  179. ID_INLINE void idDict::SetVec4( const char *key, const idVec4 &val ) {
  180. Set( key, val.ToString() );
  181. }
  182. ID_INLINE void idDict::SetVec2( const char *key, const idVec2 &val ) {
  183. Set( key, val.ToString() );
  184. }
  185. ID_INLINE void idDict::SetAngles( const char *key, const idAngles &val ) {
  186. Set( key, val.ToString() );
  187. }
  188. ID_INLINE void idDict::SetMatrix( const char *key, const idMat3 &val ) {
  189. Set( key, val.ToString() );
  190. }
  191. ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, const char **out ) const {
  192. const idKeyValue *kv = FindKey( key );
  193. if ( kv ) {
  194. *out = kv->GetValue();
  195. return true;
  196. }
  197. *out = defaultString;
  198. return false;
  199. }
  200. ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, idStr &out ) const {
  201. const idKeyValue *kv = FindKey( key );
  202. if ( kv ) {
  203. out = kv->GetValue();
  204. return true;
  205. }
  206. out = defaultString;
  207. return false;
  208. }
  209. ID_INLINE const char *idDict::GetString( const char *key, const char *defaultString ) const {
  210. const idKeyValue *kv = FindKey( key );
  211. if ( kv ) {
  212. return kv->GetValue();
  213. }
  214. return defaultString;
  215. }
  216. ID_INLINE float idDict::GetFloat( const char *key, const char *defaultString ) const {
  217. return atof( GetString( key, defaultString ) );
  218. }
  219. ID_INLINE int idDict::GetInt( const char *key, const char *defaultString ) const {
  220. return atoi( GetString( key, defaultString ) );
  221. }
  222. ID_INLINE bool idDict::GetBool( const char *key, const char *defaultString ) const {
  223. return ( atoi( GetString( key, defaultString ) ) != 0 );
  224. }
  225. ID_INLINE float idDict::GetFloat( const char *key, const float defaultFloat ) const {
  226. const idKeyValue *kv = FindKey( key );
  227. if ( kv ) {
  228. return atof( kv->GetValue() );
  229. }
  230. return defaultFloat;
  231. }
  232. ID_INLINE int idDict::GetInt( const char *key, int defaultInt ) const {
  233. const idKeyValue *kv = FindKey( key );
  234. if ( kv ) {
  235. return atoi( kv->GetValue() );
  236. }
  237. return defaultInt;
  238. }
  239. ID_INLINE bool idDict::GetBool( const char *key, const bool defaultBool ) const {
  240. const idKeyValue *kv = FindKey( key );
  241. if ( kv ) {
  242. return atoi( kv->GetValue() ) != 0;
  243. }
  244. return defaultBool;
  245. }
  246. ID_INLINE idVec3 idDict::GetVector( const char *key, const char *defaultString ) const {
  247. idVec3 out;
  248. GetVector( key, defaultString, out );
  249. return out;
  250. }
  251. ID_INLINE idVec2 idDict::GetVec2( const char *key, const char *defaultString ) const {
  252. idVec2 out;
  253. GetVec2( key, defaultString, out );
  254. return out;
  255. }
  256. ID_INLINE idVec4 idDict::GetVec4( const char *key, const char *defaultString ) const {
  257. idVec4 out;
  258. GetVec4( key, defaultString, out );
  259. return out;
  260. }
  261. ID_INLINE idAngles idDict::GetAngles( const char *key, const char *defaultString ) const {
  262. idAngles out;
  263. GetAngles( key, defaultString, out );
  264. return out;
  265. }
  266. ID_INLINE idMat3 idDict::GetMatrix( const char *key, const char *defaultString ) const {
  267. idMat3 out;
  268. GetMatrix( key, defaultString, out );
  269. return out;
  270. }
  271. ID_INLINE int idDict::GetNumKeyVals() const {
  272. return args.Num();
  273. }
  274. ID_INLINE const idKeyValue *idDict::GetKeyVal( int index ) const {
  275. if ( index >= 0 && index < args.Num() ) {
  276. return &args[ index ];
  277. }
  278. return NULL;
  279. }
  280. #endif /* !__DICT_H__ */