Counter Strike : Global Offensive Source Code
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.

282 lines
6.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ====
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "fgdlib/wckeyvalues.h"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include <tier0/memdbgon.h>
  9. //-----------------------------------------------------------------------------
  10. // Purpose: Destructor.
  11. //-----------------------------------------------------------------------------
  12. MDkeyvalue::~MDkeyvalue(void)
  13. {
  14. }
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Assignment operator.
  17. //-----------------------------------------------------------------------------
  18. MDkeyvalue &MDkeyvalue::operator =(const MDkeyvalue &other)
  19. {
  20. strcpy(szKey, other.szKey);
  21. strcpy(szValue, other.szValue);
  22. return(*this);
  23. }
  24. //-----------------------------------------------------------------------------
  25. //-----------------------------------------------------------------------------
  26. void WCKVBase_Vector::RemoveKeyAt(int nIndex)
  27. {
  28. Assert(nIndex >= 0);
  29. Assert(nIndex < (int)this->m_KeyValues.Count());
  30. if ((nIndex >= 0) && (nIndex < (int)this->m_KeyValues.Count()))
  31. {
  32. this->m_KeyValues.Remove(nIndex);
  33. }
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Adds the key to the keyvalue array. Allows duplicate keys.
  37. //
  38. // NOTE: This should only be used for keyvalue lists that do not require
  39. // unique key names! If you use this function then you should use GetCount
  40. // and GetKey/Value by index rather than GetValue by key name.
  41. //-----------------------------------------------------------------------------
  42. void WCKVBase_Vector::AddKeyValue(const char *pszKey, const char *pszValue)
  43. {
  44. if (!pszKey || !pszValue)
  45. {
  46. return;
  47. }
  48. char szTmpKey[KEYVALUE_MAX_KEY_LENGTH];
  49. char szTmpValue[KEYVALUE_MAX_VALUE_LENGTH];
  50. strcpy(szTmpKey, pszKey);
  51. strcpy(szTmpValue, pszValue);
  52. StripEdgeWhiteSpace(szTmpKey);
  53. StripEdgeWhiteSpace(szTmpValue);
  54. //
  55. // Add the keyvalue to our list.
  56. //
  57. MDkeyvalue newkv;
  58. strcpy(newkv.szKey, szTmpKey);
  59. strcpy(newkv.szValue, szTmpValue);
  60. this->m_KeyValues.AddToTail(newkv);
  61. }
  62. int WCKVBase_Vector::FindByKeyName( const char *pKeyName ) const
  63. {
  64. for ( int i=0; i < this->m_KeyValues.Count(); i++ )
  65. {
  66. if ( V_stricmp( this->m_KeyValues[i].szKey, pKeyName ) == 0 )
  67. return i;
  68. }
  69. return GetInvalidIndex();
  70. }
  71. void WCKVBase_Vector::InsertKeyValue( const MDkeyvalue &kv )
  72. {
  73. this->m_KeyValues.AddToTail( kv );
  74. }
  75. //-----------------------------------------------------------------------------
  76. //-----------------------------------------------------------------------------
  77. void WCKVBase_Dict::RemoveKeyAt(int nIndex)
  78. {
  79. this->m_KeyValues.RemoveAt(nIndex);
  80. }
  81. int WCKVBase_Dict::FindByKeyName( const char *pKeyName ) const
  82. {
  83. return this->m_KeyValues.Find( pKeyName );
  84. }
  85. void WCKVBase_Dict::InsertKeyValue( const MDkeyvalue &kv )
  86. {
  87. this->m_KeyValues.Insert( kv.szKey, kv );
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: Constructor. Sets the initial size of the keyvalue array.
  91. //-----------------------------------------------------------------------------
  92. template<class Base>
  93. WCKeyValuesT<Base>::WCKeyValuesT(void)
  94. {
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose: Destructor. Deletes the contents of this keyvalue array.
  98. //-----------------------------------------------------------------------------
  99. template<class Base>
  100. WCKeyValuesT<Base>::~WCKeyValuesT(void)
  101. {
  102. //int i = 0;
  103. //while (i < this->m_KeyValues.GetSize())
  104. //{
  105. // delete this->m_KeyValues.GetAt(i++);
  106. //}
  107. RemoveAll();
  108. }
  109. //-----------------------------------------------------------------------------
  110. //-----------------------------------------------------------------------------
  111. template<class Base>
  112. const char *WCKeyValuesT<Base>::GetValue(const char *pszKey, int *piIndex) const
  113. {
  114. int i = this->FindByKeyName( pszKey );
  115. if ( i == this->GetInvalidIndex() )
  116. {
  117. return NULL;
  118. }
  119. else
  120. {
  121. if(piIndex)
  122. piIndex[0] = i;
  123. return this->m_KeyValues[i].szValue;
  124. }
  125. }
  126. //-----------------------------------------------------------------------------
  127. //-----------------------------------------------------------------------------
  128. template<class Base>
  129. void WCKeyValuesT<Base>::RemoveKey(const char *pszKey)
  130. {
  131. SetValue(pszKey, (const char *)NULL);
  132. }
  133. //-----------------------------------------------------------------------------
  134. //-----------------------------------------------------------------------------
  135. template<class Base>
  136. void WCKeyValuesT<Base>::SetValue(const char *pszKey, int iValue)
  137. {
  138. char szValue[100];
  139. Q_snprintf(szValue, sizeof(szValue), "%d", iValue);
  140. SetValue(pszKey, szValue);
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose: Strips leading and trailing whitespace from the string.
  144. // Input : psz -
  145. //-----------------------------------------------------------------------------
  146. void StripEdgeWhiteSpace(char *psz)
  147. {
  148. if (!psz || !*psz)
  149. return;
  150. char *pszBase = psz;
  151. while (V_isspace(*psz))
  152. {
  153. psz++;
  154. }
  155. int iLen = strlen(psz) - 1;
  156. if ( iLen >= 0 )
  157. {
  158. while (V_isspace(psz[iLen]))
  159. {
  160. psz[iLen--] = 0;
  161. }
  162. }
  163. if (psz != pszBase)
  164. {
  165. memmove(pszBase, psz, iLen + 2);
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Purpose:
  170. // Input : pszKey -
  171. // pszValue -
  172. //-----------------------------------------------------------------------------
  173. template<class Base>
  174. void WCKeyValuesT<Base>::SetValue(const char *pszKey, const char *pszValue)
  175. {
  176. char szTmpKey[KEYVALUE_MAX_KEY_LENGTH];
  177. char szTmpValue[KEYVALUE_MAX_VALUE_LENGTH];
  178. strcpy(szTmpKey, pszKey);
  179. if (pszValue != NULL)
  180. {
  181. strcpy(szTmpValue, pszValue);
  182. }
  183. else
  184. {
  185. szTmpValue[0] = 0;
  186. }
  187. StripEdgeWhiteSpace(szTmpKey);
  188. StripEdgeWhiteSpace(szTmpValue);
  189. int i = this->FindByKeyName( szTmpKey );
  190. if ( i == this->GetInvalidIndex() )
  191. {
  192. if ( pszValue )
  193. {
  194. //
  195. // Add the keyvalue to our list.
  196. //
  197. MDkeyvalue newkv;
  198. Q_strncpy( newkv.szKey, szTmpKey, sizeof( newkv.szKey ) );
  199. Q_strncpy( newkv.szValue, szTmpValue, sizeof( newkv.szValue ) );
  200. this->InsertKeyValue( newkv );
  201. }
  202. }
  203. else
  204. {
  205. if (pszValue != NULL)
  206. {
  207. V_strncpy(this->m_KeyValues[i].szValue, szTmpValue, sizeof(this->m_KeyValues[i].szValue));
  208. }
  209. //
  210. // If we are setting to a NULL value, delete the key.
  211. //
  212. else
  213. {
  214. this->RemoveKeyAt( i );
  215. }
  216. }
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Purpose:
  220. //-----------------------------------------------------------------------------
  221. template<class Base>
  222. void WCKeyValuesT<Base>::RemoveAll(void)
  223. {
  224. this->m_KeyValues.RemoveAll();
  225. }
  226. // Explicit instantiations.
  227. template class WCKeyValuesT<WCKVBase_Dict>;
  228. template class WCKeyValuesT<WCKVBase_Vector>;