3
* Copyright (C) 2008 PreludeIDS Technologies. All Rights Reserved.
4
* Author: Yoann Vandoorselaere <yoann@prelude-ids.com>
6
* This file is part of the Prelude library.
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2, or (at your option)
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; see the file COPYING. If not, write to
20
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
#include "prelude-error.hxx"
28
#include "idmef-value.hxx"
30
using namespace Prelude;
33
IDMEFValue::~IDMEFValue()
36
idmef_value_destroy(_value);
40
idmef_value_type_id_t IDMEFValue::GetType() const
42
return idmef_value_get_type(_value);
46
bool IDMEFValue::IsNull() const
48
return (! _value) ? TRUE : FALSE;
52
IDMEFValue::IDMEFValue()
58
IDMEFValue::IDMEFValue(const IDMEFValue &value)
60
idmef_value_t *v = NULL;
63
v = idmef_value_ref(value._value);
69
void IDMEFValue::_InitFromString(const char *value)
72
prelude_string_t *str;
74
ret = prelude_string_new_dup(&str, value);
76
throw PreludeError(ret);
78
ret = idmef_value_new_string(&_value, str);
80
prelude_string_destroy(str);
81
throw PreludeError(ret);
86
IDMEFValue::IDMEFValue(const char *value)
88
_InitFromString(value);
92
IDMEFValue::IDMEFValue(std::string value)
94
_InitFromString(value.c_str());
98
IDMEFValue::IDMEFValue(int8_t value)
100
int ret = idmef_value_new_int8(&_value, value);
102
throw PreludeError(ret);
106
IDMEFValue::IDMEFValue(uint8_t value)
108
int ret = idmef_value_new_uint8(&_value, value);
110
throw PreludeError(ret);
114
IDMEFValue::IDMEFValue(int16_t value)
116
int ret = idmef_value_new_int16(&_value, value);
118
throw PreludeError(ret);
122
IDMEFValue::IDMEFValue(uint16_t value)
124
int ret = idmef_value_new_uint16(&_value, value);
126
throw PreludeError(ret);
130
IDMEFValue::IDMEFValue(int32_t value)
132
int ret = idmef_value_new_int32(&_value, value);
134
throw PreludeError(ret);
138
IDMEFValue::IDMEFValue(uint32_t value)
140
int ret = idmef_value_new_uint32(&_value, value);
142
throw PreludeError(ret);
146
IDMEFValue::IDMEFValue(int64_t value)
148
int ret = idmef_value_new_int64(&_value, value);
150
throw PreludeError(ret);
154
IDMEFValue::IDMEFValue(uint64_t value)
156
int ret = idmef_value_new_uint64(&_value, value);
158
throw PreludeError(ret);
162
IDMEFValue::IDMEFValue(float value)
164
int ret = idmef_value_new_float(&_value, value);
166
throw PreludeError(ret);
170
IDMEFValue::IDMEFValue(double value)
172
int ret = idmef_value_new_double(&_value, value);
174
throw PreludeError(ret);
178
IDMEFValue::IDMEFValue(IDMEFTime &time)
180
int ret = idmef_value_new_time(&_value, time);
182
throw PreludeError(ret);
186
IDMEFValue::IDMEFValue(idmef_value_t *value)
192
int IDMEFValue::Match(const IDMEFValue &value, int op)
194
return idmef_value_match(this->_value, value._value, (idmef_criterion_operator_t) op);
198
IDMEFValue IDMEFValue::Clone() const
201
idmef_value_t *clone;
203
ret = idmef_value_clone(_value, &clone);
205
throw PreludeError(ret);
207
return IDMEFValue(clone);
211
static int iterate_cb(idmef_value_t *value, void *extra)
213
std::list<IDMEFValue> *vlist = (std::list<IDMEFValue> *) extra;
215
vlist->push_back(IDMEFValue(idmef_value_ref(value)));
221
IDMEFValue::operator std::list<IDMEFValue> () const
223
std::list<IDMEFValue> vlist;
228
if ( GetType() != IDMEF_VALUE_TYPE_LIST ) {
230
s << "Left value doesn't fit '" << idmef_value_type_to_string(GetType()) << "' requirement";
231
throw PreludeError(s.str());
234
idmef_value_iterate(_value, iterate_cb, &vlist);
240
IDMEFValue::operator IDMEFTime () const
242
prelude_except_if_fail(_value);
244
if ( GetType() != IDMEF_VALUE_TYPE_TIME ) {
246
s << "Left value doesn't fit '" << idmef_value_type_to_string(GetType()) << "' requirement";
247
throw PreludeError(s.str());
250
return IDMEFTime(idmef_time_ref(idmef_value_get_time(_value)));
255
IDMEFValue::operator int32_t () const
257
idmef_value_type_id_t vtype;
259
prelude_except_if_fail(_value);
262
if ( vtype == IDMEF_VALUE_TYPE_INT8 )
263
return idmef_value_get_int8(_value);
265
else if ( vtype == IDMEF_VALUE_TYPE_UINT8 )
266
return idmef_value_get_uint8(_value);
268
else if ( vtype == IDMEF_VALUE_TYPE_INT16 )
269
return idmef_value_get_int16(_value);
271
else if ( vtype == IDMEF_VALUE_TYPE_UINT16 )
272
return idmef_value_get_uint16(_value);
274
else if ( vtype == IDMEF_VALUE_TYPE_INT32 )
275
return idmef_value_get_int32(_value);
277
else if ( vtype == IDMEF_VALUE_TYPE_ENUM )
278
return idmef_value_get_enum(_value);
281
s << "Left value doesn't fit '" << idmef_value_type_to_string(vtype) << "' requirement";
282
throw PreludeError(s.str());
286
IDMEFValue::operator uint32_t () const
288
idmef_value_type_id_t vtype;
290
prelude_except_if_fail(_value);
293
if ( vtype == IDMEF_VALUE_TYPE_UINT32 )
294
return idmef_value_get_uint32(_value);
296
return (int32_t) *this;
300
IDMEFValue::operator int64_t () const
302
idmef_value_type_id_t vtype;
304
prelude_except_if_fail(_value);
307
if ( vtype == IDMEF_VALUE_TYPE_INT64 )
308
return idmef_value_get_int64(_value);
310
return (uint32_t) *this;
314
IDMEFValue::operator uint64_t () const
316
idmef_value_type_id_t vtype;
318
prelude_except_if_fail(_value);
321
if ( vtype == IDMEF_VALUE_TYPE_UINT64 )
322
return idmef_value_get_uint64(_value);
324
return (int64_t) *this;
328
IDMEFValue::operator float () const
330
idmef_value_type_id_t vtype;
332
prelude_except_if_fail(_value);
335
if ( vtype == IDMEF_VALUE_TYPE_FLOAT )
336
return idmef_value_get_float(_value);
338
else if ( vtype == IDMEF_VALUE_TYPE_DATA ) {
339
idmef_data_t *d = idmef_value_get_data(_value);
341
if ( idmef_data_get_type(d) == IDMEF_DATA_TYPE_FLOAT )
342
return idmef_data_get_float(d);
346
s << "Left value doesn't fit '" << idmef_value_type_to_string(vtype) << "' requirement";
347
throw PreludeError(s.str());
351
IDMEFValue::operator double () const
353
idmef_value_type_id_t vtype;
355
prelude_except_if_fail(_value);
358
if ( vtype == IDMEF_VALUE_TYPE_DOUBLE )
359
return idmef_value_get_double(_value);
362
return (float) *this;
366
std::string IDMEFValue::convert_string() const
369
prelude_except_if_fail(_value);
371
if ( GetType() == IDMEF_VALUE_TYPE_STRING )
372
return prelude_string_get_string(idmef_value_get_string(_value));
374
else if ( GetType() == IDMEF_VALUE_TYPE_TIME )
375
return IDMEFTime(idmef_time_ref(idmef_value_get_time(_value)));
377
else if ( GetType() == IDMEF_VALUE_TYPE_ENUM )
378
return idmef_class_enum_to_string(idmef_value_get_class(_value), idmef_value_get_enum(_value));
380
else if ( GetType() == IDMEF_VALUE_TYPE_DATA ) {
381
idmef_data_t *d = idmef_value_get_data(_value);
382
idmef_data_type_t t = idmef_data_get_type(d);
384
if ( t == IDMEF_DATA_TYPE_CHAR_STRING )
385
return (const char *) idmef_data_get_char_string(d);
387
else if ( t == IDMEF_DATA_TYPE_CHAR ) {
388
s << idmef_data_get_char(d);
392
else if ( t == IDMEF_DATA_TYPE_FLOAT ) {
393
s << idmef_data_get_float(d);
397
else if ( t == IDMEF_DATA_TYPE_UINT32 ) {
398
s << idmef_data_get_uint32(d);
402
else if ( t == IDMEF_DATA_TYPE_UINT64 ) {
403
s << idmef_data_get_uint64(d);
408
s << "Left value doesn't fit 'data' type '" << t << "' requirement";
409
throw PreludeError(s.str());
413
s << "Left value doesn't fit '" << idmef_value_type_to_string(GetType()) << "' requirement";
414
throw PreludeError(s.str());
418
IDMEFValue &IDMEFValue::operator=(const IDMEFValue &p)
420
if ( this != &p && _value != p._value ) {
422
idmef_value_destroy(_value);
424
_value = (p._value) ? idmef_value_ref(p._value) : NULL;
431
IDMEFValue::operator const char*() const
433
static std::string t;
434
t = this->convert_string();
439
IDMEFValue::operator idmef_value_t *() const