aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/fgdlib/wckeyvalues.h
blob: 730d92d195acb33f89e5f40d5f6beaaf741dcc73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#ifndef WCKEYVALUES_H
#define WCKEYVALUES_H
#pragma once


#include <tier0/dbg.h>
#include <utlvector.h>
#include <utldict.h>
#pragma warning(push, 1)
#pragma warning(disable:4701 4702 4530)
#include <fstream>
#pragma warning(pop)


#define KEYVALUE_MAX_KEY_LENGTH			80
#define KEYVALUE_MAX_VALUE_LENGTH		512


class MDkeyvalue 
{
	public:

		//
		// Constructors/Destructor.
		//
		inline MDkeyvalue(void);
		inline MDkeyvalue(const char *pszKey, const char *pszValue);
		~MDkeyvalue(void);

		MDkeyvalue &operator =(const MDkeyvalue &other);
		
		inline void Set(const char *pszKey, const char *pszValue);
		inline const char *Key(void) const;
		inline const char *Value(void) const;

		//
		// Serialization functions.
		//
		int SerializeRMF(std::fstream &f, BOOL bRMF);
		int SerializeMAP(std::fstream &f, BOOL bRMF);

		char szKey[KEYVALUE_MAX_KEY_LENGTH];			// The name of this key.
		char szValue[KEYVALUE_MAX_VALUE_LENGTH];		// The value of this key, stored as a string.
};


//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
MDkeyvalue::MDkeyvalue(void)
{
	szKey[0] = '\0';
	szValue[0] = '\0';
}


//-----------------------------------------------------------------------------
// Purpose: Constructor with assignment.
//-----------------------------------------------------------------------------
MDkeyvalue::MDkeyvalue(const char *pszKey, const char *pszValue)
{
	szKey[0] = '\0';
	szValue[0] = '\0';

	Set(pszKey, pszValue);
}


//-----------------------------------------------------------------------------
// Purpose: Assigns a key and value.
//-----------------------------------------------------------------------------
void MDkeyvalue::Set(const char *pszKey, const char *pszValue)
{
	Assert(pszKey);
	Assert(pszValue);

	strcpy(szKey, pszKey);
	strcpy(szValue, pszValue);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the string keyname.
//-----------------------------------------------------------------------------
const char *MDkeyvalue::Key(void) const
{
	return szKey;
}


//-----------------------------------------------------------------------------
// Purpose: Returns the string value of this keyvalue.
//-----------------------------------------------------------------------------
const char *MDkeyvalue::Value(void) const
{
	return szValue;
}


typedef CUtlVector<MDkeyvalue> KeyValueArray;


// Used in cases where there can be duplicate key names.
class WCKVBase_Vector
{
public:
	
	// Iteration helpers.
	inline int GetCount() const			{ return m_KeyValues.Count(); }
	inline int GetFirst() const			{ return m_KeyValues.Count() - 1; }
	inline int GetNext( int i ) const	{ return i - 1; }
	static inline int GetInvalidIndex()	{ return -1; }

	void RemoveKeyAt(int nIndex);
	int FindByKeyName( const char *pKeyName ) const; // Returns the same value as GetInvalidIndex if not found.

	// Special function used for non-unique keyvalue lists.
	void AddKeyValue(const char *pszKey, const char *pszValue);

protected:

	void InsertKeyValue( const MDkeyvalue &kv );

protected:
	CUtlVector<MDkeyvalue> m_KeyValues;
};

// Used for most key/value sets because it's fast. Does not allow duplicate key names.
class WCKVBase_Dict
{
public:

	// Iteration helpers. Note that there is no GetCount() because you can't iterate
	// these by incrementing a counter.
	inline int GetFirst() const			{ return m_KeyValues.First(); }
	inline int GetNext( int i ) const	{ return m_KeyValues.Next( i ); }
	static inline int GetInvalidIndex()	{ return CUtlDict<MDkeyvalue,unsigned short>::InvalidIndex(); }

	int FindByKeyName( const char *pKeyName ) const; // Returns the same value as GetInvalidIndex if not found.
	void RemoveKeyAt(int nIndex);

protected:
	void InsertKeyValue( const MDkeyvalue &kv );

protected:
	CUtlDict<MDkeyvalue,unsigned short> m_KeyValues;
};


// See below for typedefs of this class you can use.
template<class Base>
class WCKeyValuesT : public Base
{
public:

	WCKeyValuesT(void);
	~WCKeyValuesT(void);

	void RemoveAll(void);
	void RemoveKey(const char *pszKey);

	void SetValue(const char *pszKey, const char *pszValue);
	void SetValue(const char *pszKey, int iValue);

	const char *GetKey(int nIndex) const;
	MDkeyvalue &GetKeyValue(int nIndex);
	const MDkeyvalue& GetKeyValue(int nIndex) const;
	const char *GetValue(int nIndex) const;
	const char *GetValue(const char *pszKey, int *piIndex = NULL) const;
};


// These have explicit template instantiations so you can use them.
typedef WCKeyValuesT<WCKVBase_Dict> WCKeyValues;
typedef WCKeyValuesT<WCKVBase_Vector> WCKeyValuesVector;


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nIndex - 
//-----------------------------------------------------------------------------
template<class Base>
inline const char *WCKeyValuesT<Base>::GetKey(int nIndex) const
{
	return(m_KeyValues.Element(nIndex).szKey);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nIndex - 
// Output : MDKeyValue
//-----------------------------------------------------------------------------
template<class Base>
inline MDkeyvalue &WCKeyValuesT<Base>::GetKeyValue(int nIndex)
{
	return(m_KeyValues.Element(nIndex));
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nIndex - 
// Output : MDkeyvalue
//-----------------------------------------------------------------------------
template<class Base>
inline const MDkeyvalue& WCKeyValuesT<Base>::GetKeyValue(int nIndex) const
{
	return(m_KeyValues.Element(nIndex));
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nIndex - 
//-----------------------------------------------------------------------------
template<class Base>
inline const char *WCKeyValuesT<Base>::GetValue(int nIndex) const
{
	return(m_KeyValues.Element(nIndex).szValue);
}


void StripEdgeWhiteSpace(char *psz);


#endif // WCKEYVALUES_H