1 |
/* |
/* |
2 |
$Id$ |
$Id$ |
3 |
|
|
4 |
Copyright (C) 2004 Kai Sterker <kaisterker@linuxgames.com> |
Copyright (C) 2004 Kai Sterker <kaisterker@linuxgames.com> |
5 |
Part of the Adonthell Project http://adonthell.linuxgames.com |
Part of the Adonthell Project http://adonthell.linuxgames.com |
6 |
|
|
7 |
Adonthell is free software; you can redistribute it and/or modify |
Adonthell is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
(at your option) any later version. |
11 |
|
|
12 |
Adonthell is distributed in the hope that it will be useful, |
Adonthell is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
GNU General Public License for more details. |
16 |
|
|
17 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
18 |
along with Adonthell; if not, write to the Free Software |
along with Adonthell; if not, write to the Free Software |
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
/** |
/** |
23 |
* @file base/flat.h |
* @file base/flat.h |
24 |
* @author Kai Sterker <kaisterker@linuxgames.com> |
* @author Kai Sterker <kaisterker@linuxgames.com> |
25 |
* |
* |
26 |
* @brief An internal class used to flatten various data. |
* @brief An internal class used to flatten various data. |
27 |
*/ |
*/ |
28 |
|
|
29 |
#ifndef BASE_FLAT |
#ifndef BASE_FLAT |
30 |
#define BASE_FLAT |
#define BASE_FLAT |
31 |
|
|
32 |
#include <stdio.h> |
#include <stdio.h> |
33 |
#include <string> |
#include <string> |
34 |
#include "base/endians.h" |
#include "base/endians.h" |
35 |
#include "python/callback_support.h" |
#include "python/callback_support.h" |
36 |
|
|
37 |
using std::string; |
using std::string; |
38 |
|
|
39 |
namespace base |
namespace base |
40 |
{ |
{ |
41 |
/** |
/** |
42 |
* This class can store basic data types in a way that helps to easily make |
* This class can store basic data types in a way that helps to easily make |
43 |
* that data persistent or transfer it over a network for later retrieval. |
* that data persistent or transfer it over a network for later retrieval. |
44 |
* As it can only cope with basic data types, objects must take care of saving |
* As it can only cope with basic data types, objects must take care of saving |
45 |
* and restoring the data correctly. |
* and restoring the data correctly. |
46 |
*/ |
*/ |
47 |
class flat |
class flat |
48 |
{ |
{ |
49 |
/** |
/** |
50 |
* Internal structure to store unflattened data |
* Internal structure to store unflattened data |
51 |
*/ |
*/ |
52 |
class data { |
class data { |
53 |
public: |
public: |
54 |
char* Name; |
char* Name; |
55 |
u_int8 Type; |
u_int8 Type; |
56 |
u_int32 Size; |
u_int32 Size; |
57 |
char* Content; |
char* Content; |
58 |
|
|
59 |
data *Next; |
data *Next; |
60 |
|
|
61 |
~data () { delete Next; } |
~data () { delete Next; } |
62 |
}; |
}; |
63 |
|
|
64 |
public: |
public: |
65 |
/** |
/** |
66 |
* Data types that can be flattened. |
* Data types that can be flattened. |
67 |
*/ |
*/ |
68 |
enum { |
enum { |
69 |
T_BOOL, T_CHAR, T_UINT8, T_SINT8, T_UINT16, |
T_BOOL, T_CHAR, T_UINT8, T_SINT8, T_UINT16, |
70 |
T_SINT16, T_UINT32, T_SINT32, T_STRING, |
T_SINT16, T_UINT32, T_SINT32, T_STRING, |
71 |
T_FLOAT, T_DOUBLE, T_BLOB, T_FLAT |
T_FLOAT, T_DOUBLE, T_BLOB, T_FLAT |
72 |
}; |
}; |
73 |
|
|
74 |
/** |
/** |
75 |
* Create a new flattener. |
* Create a new flattener. |
76 |
* @param size initial size of the internal buffer |
* @param size initial size of the internal buffer |
77 |
*/ |
*/ |
78 |
flat (const u_int16 & size = 32); |
flat (const u_int16 & size = 32); |
79 |
|
|
80 |
#ifndef SWIG |
#ifndef SWIG |
81 |
/** |
/** |
82 |
* Create a new flattener. A copy of the given buffer |
* Create a new flattener. A copy of the given buffer |
83 |
* will be made. |
* will be made. |
84 |
* @param buffer a buffer with flattened data. |
* @param buffer a buffer with flattened data. |
85 |
* @param size size of buffer in bytes |
* @param size size of buffer in bytes |
86 |
*/ |
*/ |
87 |
flat (const char *buffer, const u_int32 & size); |
flat (const char *buffer, const u_int32 & size); |
88 |
|
|
89 |
/** |
/** |
90 |
* Copy constructor. |
* Copy constructor. |
91 |
* @param f another %flat to copy into this object. |
* @param f another %flat to copy into this object. |
92 |
*/ |
*/ |
93 |
flat (const flat & f); |
flat (const flat & f); |
94 |
|
|
95 |
/** |
/** |
96 |
* assignment operator |
* assignment operator |
97 |
* @param f another &flat to be assigned to this object. |
* @param f another &flat to be assigned to this object. |
98 |
*/ |
*/ |
99 |
void operator= (const flat & f) |
void operator= (const flat & f) |
100 |
{ |
{ |
101 |
char *tmp = new char[f.size ()]; |
char *tmp = new char[f.size ()]; |
102 |
memcpy (tmp, f.getBuffer (), f.size ()); |
memcpy (tmp, f.getBuffer (), f.size ()); |
103 |
setBuffer (tmp, f.size ()); |
setBuffer (tmp, f.size ()); |
104 |
} |
} |
105 |
#endif // SWIG |
#endif // SWIG |
106 |
|
|
107 |
/** |
/** |
108 |
* Destructor |
* Destructor |
109 |
*/ |
*/ |
110 |
virtual ~flat () { |
virtual ~flat () { |
111 |
delete[] Buffer; |
delete[] Buffer; |
112 |
delete Data; |
delete Data; |
113 |
} |
} |
114 |
|
|
115 |
/** |
/** |
116 |
* Reset flattener. |
* Reset flattener. |
117 |
*/ |
*/ |
118 |
void clear () { |
void clear () { |
119 |
Size = 0; |
Size = 0; |
120 |
delete Data; |
delete Data; |
121 |
Data = NULL; |
Data = NULL; |
122 |
Ptr = Buffer; |
Ptr = Buffer; |
123 |
Success = true; |
Success = true; |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
127 |
* Return size of data stored in this object. |
* Return size of data stored in this object. |
128 |
* @param length of flattened data in bytes. |
* @return length of flattened data in bytes. |
129 |
*/ |
*/ |
130 |
u_int32 size () const { |
u_int32 size () const { |
131 |
return Size; |
return Size; |
132 |
} |
} |
133 |
|
|
134 |
/** |
/** |
135 |
* Check whether the last operation was successful. |
* Check whether the last operation was successful. |
136 |
* @return \b false if an error occured, \b true otherwise. |
* @return \b false if an error occured, \b true otherwise. |
137 |
*/ |
*/ |
138 |
bool success () const { |
bool success () const { |
139 |
return Success; |
return Success; |
140 |
} |
} |
141 |
|
|
142 |
/** |
/** |
143 |
* Calculate a checksum for the internal data. |
* Calculate a checksum for the internal data. |
144 |
* @return Adler32 checksum for this object. |
* @return Adler32 checksum for this object. |
145 |
*/ |
*/ |
146 |
u_int32 checksum () const; |
u_int32 checksum () const; |
147 |
|
|
148 |
/** |
/** |
149 |
* @name Methods to flatten data |
* @name Methods to flatten data |
150 |
*/ |
*/ |
151 |
//@{ |
//@{ |
152 |
/** |
/** |
153 |
* Store a boolean value. |
* Store a boolean value. |
154 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
155 |
* @param b value to store. |
* @param b value to store. |
156 |
*/ |
*/ |
157 |
void put_bool (const string & name, const bool & b) { |
void put_bool (const string & name, const bool & b) { |
158 |
char sb = (u_int8) b; |
char sb = (u_int8) b; |
159 |
put (name, T_BOOL, 1, (void *) &sb); |
put (name, T_BOOL, 1, (void *) &sb); |
160 |
} |
} |
161 |
|
|
162 |
/** |
/** |
163 |
* Store a character value. |
* Store a character value. |
164 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
165 |
* @param c value to store. |
* @param c value to store. |
166 |
*/ |
*/ |
167 |
void put_char (const string & name, const char & c) { |
void put_char (const string & name, const char & c) { |
168 |
put (name, T_CHAR, 1, (void *) &c); |
put (name, T_CHAR, 1, (void *) &c); |
169 |
} |
} |
170 |
|
|
171 |
/** |
/** |
172 |
* Store 8 bit unsigned integer value. |
* Store 8 bit unsigned integer value. |
173 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
174 |
* @param i value to store. |
* @param i value to store. |
175 |
*/ |
*/ |
176 |
void put_uint8 (const string & name, const u_int8 & i) { |
void put_uint8 (const string & name, const u_int8 & i) { |
177 |
put (name, T_UINT8, 1, (void*) &i); |
put (name, T_UINT8, 1, (void*) &i); |
178 |
} |
} |
179 |
|
|
180 |
/** |
/** |
181 |
* Store 8 bit signed integer value. |
* Store 8 bit signed integer value. |
182 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
183 |
* @param i value to store. |
* @param i value to store. |
184 |
*/ |
*/ |
185 |
void put_sint8 (const string & name, const s_int8 & i) { |
void put_sint8 (const string & name, const s_int8 & i) { |
186 |
put (name, T_SINT8, 1, (void*) &i); |
put (name, T_SINT8, 1, (void*) &i); |
187 |
} |
} |
188 |
|
|
189 |
/** |
/** |
190 |
* Store 16 bit unsigned integer value. |
* Store 16 bit unsigned integer value. |
191 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
192 |
* @param i value to store. |
* @param i value to store. |
193 |
*/ |
*/ |
194 |
void put_uint16 (const string & name, const u_int16 & i) { |
void put_uint16 (const string & name, const u_int16 & i) { |
195 |
SwapLE16 (i); |
SwapLE16 (i); |
196 |
put (name, T_UINT16, 2, (void*) &i); |
put (name, T_UINT16, 2, (void*) &i); |
197 |
} |
} |
198 |
|
|
199 |
/** |
/** |
200 |
* Store 16 bit signed integer value. |
* Store 16 bit signed integer value. |
201 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
202 |
* @param i value to store. |
* @param i value to store. |
203 |
*/ |
*/ |
204 |
void put_sint16 (const string & name, const s_int16 & i) { |
void put_sint16 (const string & name, const s_int16 & i) { |
205 |
SwapLE16 (i); |
SwapLE16 (i); |
206 |
put (name, T_SINT16, 2, (void*) &i); |
put (name, T_SINT16, 2, (void*) &i); |
207 |
} |
} |
208 |
|
|
209 |
/** |
/** |
210 |
* Store 32 bit unsigned integer value. |
* Store 32 bit unsigned integer value. |
211 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
212 |
* @param i value to store. |
* @param i value to store. |
213 |
*/ |
*/ |
214 |
void put_uint32 (const string & name, const u_int32 & i) { |
void put_uint32 (const string & name, const u_int32 & i) { |
215 |
SwapLE32 (i); |
SwapLE32 (i); |
216 |
put (name, T_UINT32, 4, (void*) &i); |
put (name, T_UINT32, 4, (void*) &i); |
217 |
} |
} |
218 |
|
|
219 |
/** |
/** |
220 |
* Store 32 bit signed integer value. |
* Store 32 bit signed integer value. |
221 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
222 |
* @param i value to store. |
* @param i value to store. |
223 |
*/ |
*/ |
224 |
void put_sint32 (const string & name, const s_int32 & i) { |
void put_sint32 (const string & name, const s_int32 & i) { |
225 |
SwapLE32 (i); |
SwapLE32 (i); |
226 |
put (name, T_SINT32, 4, (void*) &i); |
put (name, T_SINT32, 4, (void*) &i); |
227 |
} |
} |
228 |
|
|
229 |
/** |
/** |
230 |
* Store character string. |
* Store character string. |
231 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
232 |
* @param s string to store. |
* @param s string to store. |
233 |
*/ |
*/ |
234 |
void put_string (const string & name, const string & s) { |
void put_string (const string & name, const string & s) { |
235 |
put (name, T_STRING, s.length () + 1, s.c_str ()); |
put (name, T_STRING, s.length () + 1, s.c_str ()); |
236 |
} |
} |
237 |
|
|
238 |
/** |
/** |
239 |
* Store floating point value. |
* Store floating point value. |
240 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
241 |
* @param f value to store. |
* @param f value to store. |
242 |
*/ |
*/ |
243 |
void put_float (const string & name, const float & f) { |
void put_float (const string & name, const float & f) { |
244 |
// store floats in a format that is compatible across platforms |
// store floats in a format that is compatible across platforms |
245 |
char buffer[32]; |
char buffer[32]; |
246 |
snprintf (buffer, 31, "%.24g", f); |
snprintf (buffer, 31, "%.24g", f); |
247 |
put (name, T_FLOAT, strlen (buffer) + 1, buffer); |
put (name, T_FLOAT, strlen (buffer) + 1, buffer); |
248 |
} |
} |
249 |
|
|
250 |
/** |
/** |
251 |
* Store double value. |
* Store double value. |
252 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
253 |
* @param d value to store. |
* @param d value to store. |
254 |
*/ |
*/ |
255 |
void put_double (const string & name, const double & d) { |
void put_double (const string & name, const double & d) { |
256 |
// store doubles in a format that is compatible across platforms |
// store doubles in a format that is compatible across platforms |
257 |
char buffer[64]; |
char buffer[64]; |
258 |
snprintf (buffer, 63, "%.48g", d); |
snprintf (buffer, 63, "%.48g", d); |
259 |
put (name, T_DOUBLE, strlen (buffer) + 1, buffer); |
put (name, T_DOUBLE, strlen (buffer) + 1, buffer); |
260 |
} |
} |
261 |
|
|
262 |
/** |
/** |
263 |
* Store binary data of arbitrary length. |
* Store binary data of arbitrary length. |
264 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
265 |
* @param b binary data to store. |
* @param b binary data to store. |
266 |
* @param size length of data stored. |
* @param size length of data stored. |
267 |
*/ |
*/ |
268 |
void put_block (const string & name, void *b, const u_int32 & size) { |
void put_block (const string & name, void *b, const u_int32 & size) { |
269 |
put (name, T_BLOB, size, b); |
put (name, T_BLOB, size, b); |
270 |
} |
} |
271 |
|
|
272 |
/** |
/** |
273 |
* Store another flat. Used to create nested structures for easier |
* Store another flat. Used to create nested structures for easier |
274 |
* and safer value retrieval. |
* and safer value retrieval. |
275 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
276 |
* @param out flat to store. |
* @param out flat to store. |
277 |
*/ |
*/ |
278 |
void put_flat (const string & name, const flat & out) { |
void put_flat (const string & name, const flat & out) { |
279 |
put (name, T_FLAT, out.size (), out.getBuffer ()); |
put (name, T_FLAT, out.size (), out.getBuffer ()); |
280 |
} |
} |
281 |
//@} |
//@} |
282 |
|
|
283 |
/** |
/** |
284 |
* @name Methods to retrieve flattened data |
* @name Methods to retrieve flattened data |
285 |
*/ |
*/ |
286 |
//@{ |
//@{ |
287 |
/** |
/** |
288 |
* Retrieve a boolean value previously stored with given id. Call |
* Retrieve a boolean value previously stored with given id. Call |
289 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
290 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
291 |
* @return value stored or \b false on error. |
* @return value stored or \b false on error. |
292 |
*/ |
*/ |
293 |
bool get_bool (const string & name) { |
bool get_bool (const string & name) { |
294 |
data *d = get (name, T_BOOL); |
data *d = get (name, T_BOOL); |
295 |
if (d) return (bool) *((u_int8*) d->Content); |
if (d) return (bool) *((u_int8*) d->Content); |
296 |
else return false; |
else return false; |
297 |
} |
} |
298 |
|
|
299 |
/** |
/** |
300 |
* Retrieve a character value previously stored with given id. Call |
* Retrieve a character value previously stored with given id. Call |
301 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
302 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
303 |
* @return value stored or \b '\0' on error. |
* @return value stored or \b '\\0' on error. |
304 |
*/ |
*/ |
305 |
char get_char (const string & name) { |
char get_char (const string & name) { |
306 |
data *d = get (name, T_CHAR); |
data *d = get (name, T_CHAR); |
307 |
if (d) return (char) *d->Content; |
if (d) return (char) *d->Content; |
308 |
else return '\0'; |
else return '\0'; |
309 |
} |
} |
310 |
|
|
311 |
/** |
/** |
312 |
* Retrieve 8 bit unsigned integer previously stored with given id. Call |
* Retrieve 8 bit unsigned integer previously stored with given id. Call |
313 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
314 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
315 |
* @return value stored or \b 0 on error. |
* @return value stored or \b 0 on error. |
316 |
*/ |
*/ |
317 |
u_int8 get_uint8 (const string & name) { |
u_int8 get_uint8 (const string & name) { |
318 |
data *d = get (name, T_UINT8); |
data *d = get (name, T_UINT8); |
319 |
if (d) return *((u_int8*) d->Content); |
if (d) return *((u_int8*) d->Content); |
320 |
else return 0; |
else return 0; |
321 |
} |
} |
322 |
|
|
323 |
/** |
/** |
324 |
* Retrieve 8 bit signed integer previously stored with given id. Call |
* Retrieve 8 bit signed integer previously stored with given id. Call |
325 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
326 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
327 |
* @return value stored or \b -1 on error. |
* @return value stored or \b -1 on error. |
328 |
*/ |
*/ |
329 |
s_int8 get_sint8 (const string & name) { |
s_int8 get_sint8 (const string & name) { |
330 |
data *d = get (name, T_SINT8); |
data *d = get (name, T_SINT8); |
331 |
if (d) return *((s_int8*) d->Content); |
if (d) return *((s_int8*) d->Content); |
332 |
else return -1; |
else return -1; |
333 |
} |
} |
334 |
|
|
335 |
/** |
/** |
336 |
* Retrieve 16 bit unsigned integer previously stored with given id. Call |
* Retrieve 16 bit unsigned integer previously stored with given id. Call |
337 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
338 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
339 |
* @return value stored or \b 0 on error. |
* @return value stored or \b 0 on error. |
340 |
*/ |
*/ |
341 |
u_int16 get_uint16 (const string & name) { |
u_int16 get_uint16 (const string & name) { |
342 |
data *d = get (name, T_UINT16); |
data *d = get (name, T_UINT16); |
343 |
if (d) return SwapLE16 (*((u_int16*) d->Content)); |
if (d) return SwapLE16 (*((u_int16*) d->Content)); |
344 |
else return 0; |
else return 0; |
345 |
} |
} |
346 |
|
|
347 |
/** |
/** |
348 |
* Retrieve 16 bit signed integer previously stored with given id. Call |
* Retrieve 16 bit signed integer previously stored with given id. Call |
349 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
350 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
351 |
* @return value stored or \b -1 on error. |
* @return value stored or \b -1 on error. |
352 |
*/ |
*/ |
353 |
s_int16 get_sint16 (const string & name) { |
s_int16 get_sint16 (const string & name) { |
354 |
data *d = get (name, T_SINT16); |
data *d = get (name, T_SINT16); |
355 |
if (d) return SwapLE16 (*((s_int16*) d->Content)); |
if (d) return SwapLE16 (*((s_int16*) d->Content)); |
356 |
else return -1; |
else return -1; |
357 |
} |
} |
358 |
|
|
359 |
/** |
/** |
360 |
* Retrieve 32 bit unsigned integer previously stored with given id. Call |
* Retrieve 32 bit unsigned integer previously stored with given id. Call |
361 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
362 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
363 |
* @return value stored or \b 0 on error. |
* @return value stored or \b 0 on error. |
364 |
*/ |
*/ |
365 |
u_int32 get_uint32 (const string & name) { |
u_int32 get_uint32 (const string & name) { |
366 |
data *d = get (name, T_UINT32); |
data *d = get (name, T_UINT32); |
367 |
if (d) return SwapLE32 (*((u_int32*) d->Content)); |
if (d) return SwapLE32 (*((u_int32*) d->Content)); |
368 |
else return 0; |
else return 0; |
369 |
|
|
370 |
} |
} |
371 |
|
|
372 |
/** |
/** |
373 |
* Retrieve 32 bit signed integer previously stored with given id. Call |
* Retrieve 32 bit signed integer previously stored with given id. Call |
374 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
375 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
376 |
* @return value stored or \b -1 on error. |
* @return value stored or \b -1 on error. |
377 |
*/ |
*/ |
378 |
s_int32 get_sint32 (const string & name) { |
s_int32 get_sint32 (const string & name) { |
379 |
data *d = get (name, T_SINT32); |
data *d = get (name, T_SINT32); |
380 |
if (d) return SwapLE32 (*((s_int32*) d->Content)); |
if (d) return SwapLE32 (*((s_int32*) d->Content)); |
381 |
else return -1; |
else return -1; |
382 |
} |
} |
383 |
|
|
384 |
/** |
/** |
385 |
* Retrieve string previously stored with given id. Call |
* Retrieve string previously stored with given id. Call |
386 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
387 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
388 |
* @return value stored or \b "" (empty string) on error. |
* @return value stored or \b "" (empty string) on error. |
389 |
*/ |
*/ |
390 |
string get_string (const string & name) { |
string get_string (const string & name) { |
391 |
data *d = get (name, T_STRING); |
data *d = get (name, T_STRING); |
392 |
if (d) return string (d->Content); |
if (d) return string (d->Content); |
393 |
else return string (""); |
else return string (""); |
394 |
} |
} |
395 |
|
|
396 |
/** |
/** |
397 |
* Retrieve floating point number previously stored with given id. Call |
* Retrieve floating point number previously stored with given id. Call |
398 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
399 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
400 |
* @return value stored or \b 0.0 on error. |
* @return value stored or \b 0.0 on error. |
401 |
*/ |
*/ |
402 |
float get_float (const string & name) { |
float get_float (const string & name) { |
403 |
data *d = get (name, T_FLOAT); |
data *d = get (name, T_FLOAT); |
404 |
if (d) return (float) strtod (d->Content, NULL); |
if (d) return (float) strtod (d->Content, NULL); |
405 |
else return 0.0; |
else return 0.0; |
406 |
} |
} |
407 |
|
|
408 |
/** |
/** |
409 |
* Retrieve double value previously stored with given id. Call |
* Retrieve double value previously stored with given id. Call |
410 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
411 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
412 |
* @return value stored or \b 0.0 on error. |
* @return value stored or \b 0.0 on error. |
413 |
*/ |
*/ |
414 |
double get_double (const string & name) { |
double get_double (const string & name) { |
415 |
data *d = get (name, T_DOUBLE); |
data *d = get (name, T_DOUBLE); |
416 |
if (d) return strtod (d->Content, NULL); |
if (d) return strtod (d->Content, NULL); |
417 |
else return 0.0; |
else return 0.0; |
418 |
} |
} |
419 |
|
|
420 |
/** |
/** |
421 |
* Retrieve binary data previously stored with given id. Call |
* Retrieve binary data previously stored with given id. Call |
422 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
423 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
424 |
* @param size will contain number of bytes returned. |
* @param size will contain number of bytes returned. |
425 |
* @return value stored or \b NULL on error. |
* @return value stored or \b NULL on error. |
426 |
*/ |
*/ |
427 |
void* get_block (const string & name, int *size = NULL) { |
void* get_block (const string & name, int *size = NULL) { |
428 |
data *d = get (name, T_BLOB); |
data *d = get (name, T_BLOB); |
429 |
if (d) { |
if (d) { |
430 |
if (size != NULL) *size = d->Size; |
if (size != NULL) *size = d->Size; |
431 |
char *ret = new char[d->Size]; |
char *ret = new char[d->Size]; |
432 |
return memcpy (ret, d->Content, d->Size); |
return memcpy (ret, d->Content, d->Size); |
433 |
} |
} |
434 |
|
|
435 |
if (size != NULL) *size = 0; |
if (size != NULL) *size = 0; |
436 |
return NULL; |
return NULL; |
437 |
} |
} |
438 |
|
|
439 |
/** |
/** |
440 |
* Retrieve a nested flat with given id. Call |
* Retrieve a nested flat with given id. Call |
441 |
* success() to check whether value retrieval was successful. |
* success() to check whether value retrieval was successful. |
442 |
* @param name id used to retrieve the value later on. |
* @param name id used to retrieve the value later on. |
443 |
* @return value stored or \b empty flat on error. |
* @return value stored or \b empty flat on error. |
444 |
*/ |
*/ |
445 |
flat get_flat (const string & name) { |
flat get_flat (const string & name) { |
446 |
data *d = get (name, T_FLAT); |
data *d = get (name, T_FLAT); |
447 |
if (d) return flat (d->Content, d->Size); |
if (d) return flat (d->Content, d->Size); |
448 |
else return flat (); |
else return flat (); |
449 |
} |
} |
450 |
|
|
451 |
/** |
/** |
452 |
* Retrieve the next value in stream. |
* Retrieve the next value in stream. |
453 |
* @param value will contain a pointer to the value |
* @param value will contain a pointer to the value |
454 |
* @param size will contain the size of value |
* @param size will contain the size of value |
455 |
* @param name will contain the name of the field |
* @param name will contain the name of the field |
456 |
* @return type of value fetched. |
* @return type of value fetched. |
457 |
*/ |
*/ |
458 |
int next (void **value, int *size = NULL, char **name = NULL); |
int next (void **value, int *size = NULL, char **name = NULL); |
459 |
//@} |
//@} |
460 |
|
|
461 |
#ifndef SWIG |
#ifndef SWIG |
462 |
GET_TYPE_NAME_VIRTUAL(base::flat) |
GET_TYPE_NAME_VIRTUAL(base::flat) |
463 |
#endif // SWIG |
#endif // SWIG |
464 |
protected: |
protected: |
465 |
/** |
/** |
466 |
* Return internal buffer of this flattener. |
* Return internal buffer of this flattener. |
467 |
* @return byte array containing the flattened data. |
* @return byte array containing the flattened data. |
468 |
*/ |
*/ |
469 |
const char *getBuffer () const { return Buffer; } |
const char *getBuffer () const { return Buffer; } |
470 |
|
|
471 |
/** |
/** |
472 |
* Assign a new buffer with given size to this flattener. |
* Assign a new buffer with given size to this flattener. |
473 |
* @param buffer byte array containing flattened data. |
* @param buffer byte array containing flattened data. |
474 |
* @param size length of the byte array. |
* @param size length of the byte array. |
475 |
*/ |
*/ |
476 |
void setBuffer (char* buffer, const u_int32 & size) { |
void setBuffer (char* buffer, const u_int32 & size) { |
477 |
delete[] Buffer; |
delete[] Buffer; |
478 |
delete Data; |
delete Data; |
479 |
|
|
480 |
Buffer = buffer; |
Buffer = buffer; |
481 |
Capacity = size; |
Capacity = size; |
482 |
Success = true; |
Success = true; |
483 |
Ptr = Buffer; |
Ptr = Buffer; |
484 |
Size = size; |
Size = size; |
485 |
Data = NULL; |
Data = NULL; |
486 |
} |
} |
487 |
|
|
488 |
private: |
private: |
489 |
/// Pointer to unflattened data. Valid after first call to parse(). |
/// Pointer to unflattened data. Valid after first call to parse(). |
490 |
data *Data; |
data *Data; |
491 |
|
|
492 |
/// Points to record last fetched with get_*() or next() |
/// Points to record last fetched with get_*() or next() |
493 |
data *Decoded; |
data *Decoded; |
494 |
|
|
495 |
/** |
/** |
496 |
* Writes data into the buffer, growing it if neccessary. |
* Writes data into the buffer, growing it if neccessary. |
497 |
* @param name Identifier for the data |
* @param name Identifier for the data |
498 |
* @param type Type of the data |
* @param type Type of the data |
499 |
* @param size Size of the data |
* @param size Size of the data |
500 |
* @param data the data to add to the buffer. |
* @param data the data to add to the buffer. |
501 |
*/ |
*/ |
502 |
void put (const string & name, const u_int8 & type, const u_int32 & size, const void *data); |
void put (const string & name, const u_int8 & type, const u_int32 & size, const void *data); |
503 |
|
|
504 |
/** |
/** |
505 |
* Reads data from the buffer. |
* Reads data from the buffer. |
506 |
* @param name Identifier of data to retrieve |
* @param name Identifier of data to retrieve |
507 |
* @param type Type of data to retrieve |
* @param type Type of data to retrieve |
508 |
* @return data structure filled with desired data, or NULL if data does not exist. |
* @return data structure filled with desired data, or NULL if data does not exist. |
509 |
*/ |
*/ |
510 |
data* get (const string & name, const u_int8 & type); |
data* get (const string & name, const u_int8 & type); |
511 |
|
|
512 |
/** |
/** |
513 |
* Unflatten the internal buffer for easier data retrieval. |
* Unflatten the internal buffer for easier data retrieval. |
514 |
*/ |
*/ |
515 |
void parse (); |
void parse (); |
516 |
|
|
517 |
/** |
/** |
518 |
* Grow the internal buffer. This will double its current capacity. |
* Grow the internal buffer. This will double its current capacity. |
519 |
*/ |
*/ |
520 |
void grow (); |
void grow (); |
521 |
|
|
522 |
/// Buffer storing the flattened objects |
/// Buffer storing the flattened objects |
523 |
char *Buffer; |
char *Buffer; |
524 |
|
|
525 |
/// Pointer to the current position in the Buffer |
/// Pointer to the current position in the Buffer |
526 |
char *Ptr; |
char *Ptr; |
527 |
|
|
528 |
/// Bufferspace currently used |
/// Bufferspace currently used |
529 |
u_int32 Size; |
u_int32 Size; |
530 |
|
|
531 |
/// Maximum capacity of the Buffer |
/// Maximum capacity of the Buffer |
532 |
u_int32 Capacity; |
u_int32 Capacity; |
533 |
|
|
534 |
/// Indicates an error during get |
/// Indicates an error during get |
535 |
bool Success; |
bool Success; |
536 |
}; |
}; |
537 |
} |
} |
538 |
#endif // BASE_FLAT |
#endif // BASE_FLAT |