/[adonthell]/adonthell/src/base/flat.h
ViewVC logotype

Diff of /adonthell/src/base/flat.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by ksterker, Wed Nov 3 07:30:16 2004 UTC revision 1.10 by ksterker, Mon Nov 15 08:54:33 2004 UTC
# Line 149  namespace base Line 149  namespace base
149               * @name Methods to flatten data               * @name Methods to flatten data
150               */               */
151              //@{              //@{
152                /**
153                 * Store a boolean value.
154                 * @param name id used to retrieve the value later on.
155                 * @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.
164                 * @param name id used to retrieve the value later on.
165                 * @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.
173                 * @param name id used to retrieve the value later on.
174                 * @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.
182                 * @param name id used to retrieve the value later on.
183                 * @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.
191                 * @param name id used to retrieve the value later on.
192                 * @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.
201                 * @param name id used to retrieve the value later on.
202                 * @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.
211                 * @param name id used to retrieve the value later on.
212                 * @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.
221                 * @param name id used to retrieve the value later on.
222                 * @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.
231                 * @param name id used to retrieve the value later on.
232                 * @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.
240                 * @param name id used to retrieve the value later on.
241                 * @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];
# Line 197  namespace base Line 247  namespace base
247                  put (name, T_FLOAT, strlen (buffer) + 1, buffer);                  put (name, T_FLOAT, strlen (buffer) + 1, buffer);
248              }              }
249                            
250                /**
251                 * Store double value.
252                 * @param name id used to retrieve the value later on.
253                 * @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];
# Line 204  namespace base Line 259  namespace base
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.
264                 * @param name id used to retrieve the value later on.
265                 * @param b binary data to store.
266                 * @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
274                 * and safer value retrieval.
275                 * @param name id used to retrieve the value later on.
276                 * @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              }              }
# Line 217  namespace base Line 284  namespace base
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
289                 * success() to check whether value retrieval was successful.
290                 * @param name id used to retrieve the value later on.
291                 * @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
301                 * success() to check whether value retrieval was successful.
302                 * @param name id used to retrieve the value later on.
303                 * @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
313                 * success() to check whether value retrieval was successful.
314                 * @param name id used to retrieve the value later on.
315                 * @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
325                 * success() to check whether value retrieval was successful.
326                 * @param name id used to retrieve the value later on.
327                 * @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
337                 * success() to check whether value retrieval was successful.
338                 * @param name id used to retrieve the value later on.
339                 * @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
349                 * success() to check whether value retrieval was successful.
350                 * @param name id used to retrieve the value later on.
351                 * @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
361                 * success() to check whether value retrieval was successful.
362                 * @param name id used to retrieve the value later on.
363                 * @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));
# Line 260  namespace base Line 369  namespace base
369                                    
370              }              }
371    
372                /**
373                 * Retrieve 32 bit signed integer previously stored with given id. Call
374                 * success() to check whether value retrieval was successful.
375                 * @param name id used to retrieve the value later on.
376                 * @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
386                 * success() to check whether value retrieval was successful.
387                 * @param name id used to retrieve the value later on.
388                 * @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
398                 * success() to check whether value retrieval was successful.
399                 * @param name id used to retrieve the value later on.
400                 * @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
410                 * success() to check whether value retrieval was successful.
411                 * @param name id used to retrieve the value later on.
412                 * @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
422                 * success() to check whether value retrieval was successful.
423                 * @param name id used to retrieve the value later on.
424                 * @param size will contain number of bytes returned.
425                 * @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) {
# Line 296  namespace base Line 436  namespace base
436                  return NULL;                  return NULL;
437              }              }
438                            
439                /**
440                 * Retrieve a nested flat with given id. Call
441                 * success() to check whether value retrieval was successful.
442                 * @param name id used to retrieve the value later on.
443                 * @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);
# Line 316  namespace base Line 462  namespace base
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.
467                 * @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.
473                 * @param buffer byte array containing flattened data.
474                 * @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;

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26