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.

233 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef WCKEYVALUES_H
  7. #define WCKEYVALUES_H
  8. #pragma once
  9. #include <tier0/dbg.h>
  10. #include <utlvector.h>
  11. #include <utldict.h>
  12. #pragma warning(push, 1)
  13. #pragma warning(disable:4701 4702 4530)
  14. #include <fstream>
  15. #pragma warning(pop)
  16. #define KEYVALUE_MAX_KEY_LENGTH 80
  17. #define KEYVALUE_MAX_VALUE_LENGTH 512
  18. class MDkeyvalue
  19. {
  20. public:
  21. //
  22. // Constructors/Destructor.
  23. //
  24. inline MDkeyvalue(void);
  25. inline MDkeyvalue(const char *pszKey, const char *pszValue);
  26. ~MDkeyvalue(void);
  27. MDkeyvalue &operator =(const MDkeyvalue &other);
  28. inline void Set(const char *pszKey, const char *pszValue);
  29. inline const char *Key(void) const;
  30. inline const char *Value(void) const;
  31. //
  32. // Serialization functions.
  33. //
  34. int SerializeRMF(std::fstream &f, BOOL bRMF);
  35. int SerializeMAP(std::fstream &f, BOOL bRMF);
  36. char szKey[KEYVALUE_MAX_KEY_LENGTH]; // The name of this key.
  37. char szValue[KEYVALUE_MAX_VALUE_LENGTH]; // The value of this key, stored as a string.
  38. };
  39. //-----------------------------------------------------------------------------
  40. // Purpose: Constructor.
  41. //-----------------------------------------------------------------------------
  42. MDkeyvalue::MDkeyvalue(void)
  43. {
  44. szKey[0] = '\0';
  45. szValue[0] = '\0';
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: Constructor with assignment.
  49. //-----------------------------------------------------------------------------
  50. MDkeyvalue::MDkeyvalue(const char *pszKey, const char *pszValue)
  51. {
  52. szKey[0] = '\0';
  53. szValue[0] = '\0';
  54. Set(pszKey, pszValue);
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Assigns a key and value.
  58. //-----------------------------------------------------------------------------
  59. void MDkeyvalue::Set(const char *pszKey, const char *pszValue)
  60. {
  61. Assert(pszKey);
  62. Assert(pszValue);
  63. strcpy(szKey, pszKey);
  64. strcpy(szValue, pszValue);
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Returns the string keyname.
  68. //-----------------------------------------------------------------------------
  69. const char *MDkeyvalue::Key(void) const
  70. {
  71. return szKey;
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Returns the string value of this keyvalue.
  75. //-----------------------------------------------------------------------------
  76. const char *MDkeyvalue::Value(void) const
  77. {
  78. return szValue;
  79. }
  80. typedef CUtlVector<MDkeyvalue> KeyValueArray;
  81. // Used in cases where there can be duplicate key names.
  82. class WCKVBase_Vector
  83. {
  84. public:
  85. // Iteration helpers.
  86. inline int GetCount() const { return m_KeyValues.Count(); }
  87. inline int GetFirst() const { return m_KeyValues.Count() - 1; }
  88. inline int GetNext( int i ) const { return i - 1; }
  89. static inline int GetInvalidIndex() { return -1; }
  90. void RemoveKeyAt(int nIndex);
  91. int FindByKeyName( const char *pKeyName ) const; // Returns the same value as GetInvalidIndex if not found.
  92. // Special function used for non-unique keyvalue lists.
  93. void AddKeyValue(const char *pszKey, const char *pszValue);
  94. protected:
  95. void InsertKeyValue( const MDkeyvalue &kv );
  96. protected:
  97. CUtlVector<MDkeyvalue> m_KeyValues;
  98. };
  99. // Used for most key/value sets because it's fast. Does not allow duplicate key names.
  100. class WCKVBase_Dict
  101. {
  102. public:
  103. // Iteration helpers. Note that there is no GetCount() because you can't iterate
  104. // these by incrementing a counter.
  105. inline int GetFirst() const { return m_KeyValues.First(); }
  106. inline int GetNext( int i ) const { return m_KeyValues.Next( i ); }
  107. static inline int GetInvalidIndex() { return CUtlDict<MDkeyvalue,unsigned short>::InvalidIndex(); }
  108. int FindByKeyName( const char *pKeyName ) const; // Returns the same value as GetInvalidIndex if not found.
  109. void RemoveKeyAt(int nIndex);
  110. protected:
  111. void InsertKeyValue( const MDkeyvalue &kv );
  112. protected:
  113. CUtlDict<MDkeyvalue,unsigned short> m_KeyValues;
  114. };
  115. // See below for typedefs of this class you can use.
  116. template<class Base>
  117. class WCKeyValuesT : public Base
  118. {
  119. public:
  120. WCKeyValuesT(void);
  121. ~WCKeyValuesT(void);
  122. void RemoveAll(void);
  123. void RemoveKey(const char *pszKey);
  124. void SetValue(const char *pszKey, const char *pszValue);
  125. void SetValue(const char *pszKey, int iValue);
  126. const char *GetKey(int nIndex) const;
  127. MDkeyvalue &GetKeyValue(int nIndex);
  128. const MDkeyvalue& GetKeyValue(int nIndex) const;
  129. const char *GetValue(int nIndex) const;
  130. const char *GetValue(const char *pszKey, int *piIndex = NULL) const;
  131. };
  132. // These have explicit template instantiations so you can use them.
  133. typedef WCKeyValuesT<WCKVBase_Dict> WCKeyValues;
  134. typedef WCKeyValuesT<WCKVBase_Vector> WCKeyValuesVector;
  135. //-----------------------------------------------------------------------------
  136. // Purpose:
  137. // Input : nIndex -
  138. //-----------------------------------------------------------------------------
  139. template<class Base>
  140. inline const char *WCKeyValuesT<Base>::GetKey(int nIndex) const
  141. {
  142. return(m_KeyValues.Element(nIndex).szKey);
  143. }
  144. //-----------------------------------------------------------------------------
  145. // Purpose:
  146. // Input : nIndex -
  147. // Output : MDKeyValue
  148. //-----------------------------------------------------------------------------
  149. template<class Base>
  150. inline MDkeyvalue &WCKeyValuesT<Base>::GetKeyValue(int nIndex)
  151. {
  152. return(m_KeyValues.Element(nIndex));
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose:
  156. // Input : nIndex -
  157. // Output : MDkeyvalue
  158. //-----------------------------------------------------------------------------
  159. template<class Base>
  160. inline const MDkeyvalue& WCKeyValuesT<Base>::GetKeyValue(int nIndex) const
  161. {
  162. return(m_KeyValues.Element(nIndex));
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose:
  166. // Input : nIndex -
  167. //-----------------------------------------------------------------------------
  168. template<class Base>
  169. inline const char *WCKeyValuesT<Base>::GetValue(int nIndex) const
  170. {
  171. return(m_KeyValues.Element(nIndex).szValue);
  172. }
  173. void StripEdgeWhiteSpace(char *psz);
  174. #endif // WCKEYVALUES_H