~ubuntu-branches/ubuntu/trusty/libprelude/trusty

« back to all changes in this revision

Viewing changes to bindings/c++/idmef-value.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-04-29 11:31:50 UTC
  • mfrom: (1.1.12 upstream) (2.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090429113150-qw9oxc1e50ldljck
Tags: 0.9.22-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Build-Depend on libltdl7-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****
 
2
*
 
3
* Copyright (C) 2008 PreludeIDS Technologies. All Rights Reserved.
 
4
* Author: Yoann Vandoorselaere <yoann@prelude-ids.com>
 
5
*
 
6
* This file is part of the Prelude library.
 
7
*
 
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)
 
11
* any later version.
 
12
*
 
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.
 
17
*
 
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.
 
21
*
 
22
*****/
 
23
 
 
24
#include <sstream>
 
25
 
 
26
#include "prelude-error.hxx"
 
27
#include "idmef.hxx"
 
28
#include "idmef-value.hxx"
 
29
 
 
30
using namespace Prelude;
 
31
 
 
32
 
 
33
IDMEFValue::~IDMEFValue()
 
34
{
 
35
        if ( _value )
 
36
                idmef_value_destroy(_value);
 
37
}
 
38
 
 
39
 
 
40
idmef_value_type_id_t IDMEFValue::GetType() const
 
41
{
 
42
        return idmef_value_get_type(_value);
 
43
}
 
44
 
 
45
 
 
46
bool IDMEFValue::IsNull() const
 
47
{
 
48
        return (! _value) ? TRUE : FALSE;
 
49
}
 
50
 
 
51
 
 
52
IDMEFValue::IDMEFValue()
 
53
{
 
54
        _value = NULL;
 
55
}
 
56
 
 
57
 
 
58
IDMEFValue::IDMEFValue(const IDMEFValue &value)
 
59
{
 
60
        idmef_value_t *v = NULL;
 
61
 
 
62
        if ( value._value )
 
63
                v = idmef_value_ref(value._value);
 
64
 
 
65
        _value = v;
 
66
}
 
67
 
 
68
 
 
69
void IDMEFValue::_InitFromString(const char *value)
 
70
{
 
71
        int ret;
 
72
        prelude_string_t *str;
 
73
 
 
74
        ret = prelude_string_new_dup(&str, value);
 
75
        if ( ret < 0 )
 
76
                throw PreludeError(ret);
 
77
 
 
78
        ret = idmef_value_new_string(&_value, str);
 
79
        if ( ret < 0 ) {
 
80
                prelude_string_destroy(str);
 
81
                throw PreludeError(ret);
 
82
        }
 
83
}
 
84
 
 
85
 
 
86
IDMEFValue::IDMEFValue(const char *value)
 
87
{
 
88
        _InitFromString(value);
 
89
}
 
90
 
 
91
 
 
92
IDMEFValue::IDMEFValue(std::string value)
 
93
{
 
94
        _InitFromString(value.c_str());
 
95
}
 
96
 
 
97
 
 
98
IDMEFValue::IDMEFValue(int8_t value)
 
99
{
 
100
        int ret = idmef_value_new_int8(&_value, value);
 
101
        if ( ret < 0 )
 
102
                throw PreludeError(ret);
 
103
}
 
104
 
 
105
 
 
106
IDMEFValue::IDMEFValue(uint8_t value)
 
107
{
 
108
        int ret = idmef_value_new_uint8(&_value, value);
 
109
        if ( ret < 0 )
 
110
                throw PreludeError(ret);
 
111
}
 
112
 
 
113
 
 
114
IDMEFValue::IDMEFValue(int16_t value)
 
115
{
 
116
        int ret = idmef_value_new_int16(&_value, value);
 
117
        if ( ret < 0 )
 
118
                throw PreludeError(ret);
 
119
}
 
120
 
 
121
 
 
122
IDMEFValue::IDMEFValue(uint16_t value)
 
123
{
 
124
        int ret = idmef_value_new_uint16(&_value, value);
 
125
        if ( ret < 0 )
 
126
                throw PreludeError(ret);
 
127
}
 
128
 
 
129
 
 
130
IDMEFValue::IDMEFValue(int32_t value)
 
131
{
 
132
        int ret = idmef_value_new_int32(&_value, value);
 
133
        if ( ret < 0 )
 
134
                throw PreludeError(ret);
 
135
}
 
136
 
 
137
 
 
138
IDMEFValue::IDMEFValue(uint32_t value)
 
139
{
 
140
        int ret = idmef_value_new_uint32(&_value, value);
 
141
        if ( ret < 0 )
 
142
                throw PreludeError(ret);
 
143
}
 
144
 
 
145
 
 
146
IDMEFValue::IDMEFValue(int64_t value)
 
147
{
 
148
        int ret = idmef_value_new_int64(&_value, value);
 
149
        if ( ret < 0 )
 
150
                throw PreludeError(ret);
 
151
}
 
152
 
 
153
 
 
154
IDMEFValue::IDMEFValue(uint64_t value)
 
155
{
 
156
        int ret = idmef_value_new_uint64(&_value, value);
 
157
        if ( ret < 0 )
 
158
                throw PreludeError(ret);
 
159
}
 
160
 
 
161
 
 
162
IDMEFValue::IDMEFValue(float value)
 
163
{
 
164
        int ret = idmef_value_new_float(&_value, value);
 
165
        if ( ret < 0 )
 
166
                throw PreludeError(ret);
 
167
}
 
168
 
 
169
 
 
170
IDMEFValue::IDMEFValue(double value)
 
171
{
 
172
        int ret = idmef_value_new_double(&_value, value);
 
173
        if ( ret < 0 )
 
174
                throw PreludeError(ret);
 
175
}
 
176
 
 
177
 
 
178
IDMEFValue::IDMEFValue(IDMEFTime &time)
 
179
{
 
180
        int ret = idmef_value_new_time(&_value, time);
 
181
        if ( ret < 0 )
 
182
                throw PreludeError(ret);
 
183
}
 
184
 
 
185
 
 
186
IDMEFValue::IDMEFValue(idmef_value_t *value)
 
187
{
 
188
        _value = value;
 
189
}
 
190
 
 
191
 
 
192
int IDMEFValue::Match(const IDMEFValue &value, int op)
 
193
{
 
194
        return idmef_value_match(this->_value, value._value, (idmef_criterion_operator_t) op);
 
195
}
 
196
 
 
197
 
 
198
IDMEFValue IDMEFValue::Clone() const
 
199
{
 
200
        int ret;
 
201
        idmef_value_t *clone;
 
202
 
 
203
        ret = idmef_value_clone(_value, &clone);
 
204
        if ( ret < 0 )
 
205
                throw PreludeError(ret);
 
206
 
 
207
        return IDMEFValue(clone);
 
208
}
 
209
 
 
210
 
 
211
static int iterate_cb(idmef_value_t *value, void *extra)
 
212
{
 
213
        std::list<IDMEFValue> *vlist = (std::list<IDMEFValue> *) extra;
 
214
 
 
215
        vlist->push_back(IDMEFValue(idmef_value_ref(value)));
 
216
 
 
217
        return 0;
 
218
}
 
219
 
 
220
 
 
221
IDMEFValue::operator std::list<IDMEFValue> () const
 
222
{
 
223
        std::list<IDMEFValue> vlist;
 
224
 
 
225
        if ( ! _value )
 
226
                return vlist;
 
227
 
 
228
        if ( GetType() != IDMEF_VALUE_TYPE_LIST ) {
 
229
                std::stringstream s;
 
230
                s << "Left value doesn't fit '" << idmef_value_type_to_string(GetType()) << "' requirement";
 
231
                throw PreludeError(s.str());
 
232
        }
 
233
 
 
234
        idmef_value_iterate(_value, iterate_cb, &vlist);
 
235
 
 
236
        return vlist;
 
237
}
 
238
 
 
239
 
 
240
IDMEFValue::operator IDMEFTime () const
 
241
{
 
242
        prelude_except_if_fail(_value);
 
243
 
 
244
        if ( GetType() != IDMEF_VALUE_TYPE_TIME ) {
 
245
                std::stringstream s;
 
246
                s << "Left value doesn't fit '" << idmef_value_type_to_string(GetType()) << "' requirement";
 
247
                throw PreludeError(s.str());
 
248
        }
 
249
 
 
250
        return IDMEFTime(idmef_time_ref(idmef_value_get_time(_value)));
 
251
}
 
252
 
 
253
 
 
254
 
 
255
IDMEFValue::operator int32_t () const
 
256
{
 
257
        idmef_value_type_id_t vtype;
 
258
 
 
259
        prelude_except_if_fail(_value);
 
260
        vtype = GetType();
 
261
 
 
262
        if ( vtype == IDMEF_VALUE_TYPE_INT8 )
 
263
                return idmef_value_get_int8(_value);
 
264
 
 
265
        else if ( vtype == IDMEF_VALUE_TYPE_UINT8 )
 
266
                return idmef_value_get_uint8(_value);
 
267
 
 
268
        else if ( vtype == IDMEF_VALUE_TYPE_INT16 )
 
269
                return idmef_value_get_int16(_value);
 
270
 
 
271
        else if ( vtype == IDMEF_VALUE_TYPE_UINT16 )
 
272
                return idmef_value_get_uint16(_value);
 
273
 
 
274
        else if ( vtype == IDMEF_VALUE_TYPE_INT32 )
 
275
                return idmef_value_get_int32(_value);
 
276
 
 
277
        else if ( vtype == IDMEF_VALUE_TYPE_ENUM )
 
278
                return idmef_value_get_enum(_value);
 
279
 
 
280
        std::stringstream s;
 
281
        s << "Left value doesn't fit '" << idmef_value_type_to_string(vtype) << "' requirement";
 
282
        throw PreludeError(s.str());
 
283
}
 
284
 
 
285
 
 
286
IDMEFValue::operator uint32_t () const
 
287
{
 
288
        idmef_value_type_id_t vtype;
 
289
 
 
290
        prelude_except_if_fail(_value);
 
291
        vtype = GetType();
 
292
 
 
293
        if ( vtype == IDMEF_VALUE_TYPE_UINT32 )
 
294
                return idmef_value_get_uint32(_value);
 
295
        else
 
296
                return (int32_t) *this;
 
297
}
 
298
 
 
299
 
 
300
IDMEFValue::operator int64_t () const
 
301
{
 
302
        idmef_value_type_id_t vtype;
 
303
 
 
304
        prelude_except_if_fail(_value);
 
305
        vtype = GetType();
 
306
 
 
307
        if ( vtype == IDMEF_VALUE_TYPE_INT64 )
 
308
                return idmef_value_get_int64(_value);
 
309
        else
 
310
                return (uint32_t) *this;
 
311
}
 
312
 
 
313
 
 
314
IDMEFValue::operator uint64_t () const
 
315
{
 
316
        idmef_value_type_id_t vtype;
 
317
 
 
318
        prelude_except_if_fail(_value);
 
319
        vtype = GetType();
 
320
 
 
321
        if ( vtype == IDMEF_VALUE_TYPE_UINT64 )
 
322
                return idmef_value_get_uint64(_value);
 
323
        else
 
324
                return (int64_t) *this;
 
325
}
 
326
 
 
327
 
 
328
IDMEFValue::operator float () const
 
329
{
 
330
        idmef_value_type_id_t vtype;
 
331
 
 
332
        prelude_except_if_fail(_value);
 
333
        vtype = GetType();
 
334
 
 
335
        if ( vtype == IDMEF_VALUE_TYPE_FLOAT )
 
336
                return idmef_value_get_float(_value);
 
337
 
 
338
        else if ( vtype == IDMEF_VALUE_TYPE_DATA ) {
 
339
                idmef_data_t *d = idmef_value_get_data(_value);
 
340
 
 
341
                if ( idmef_data_get_type(d) == IDMEF_DATA_TYPE_FLOAT )
 
342
                        return idmef_data_get_float(d);
 
343
        }
 
344
 
 
345
        std::stringstream s;
 
346
        s << "Left value doesn't fit '" << idmef_value_type_to_string(vtype) << "' requirement";
 
347
        throw PreludeError(s.str());
 
348
}
 
349
 
 
350
 
 
351
IDMEFValue::operator double () const
 
352
{
 
353
        idmef_value_type_id_t vtype;
 
354
 
 
355
        prelude_except_if_fail(_value);
 
356
        vtype = GetType();
 
357
 
 
358
        if ( vtype == IDMEF_VALUE_TYPE_DOUBLE )
 
359
                return idmef_value_get_double(_value);
 
360
 
 
361
        else
 
362
                return (float) *this;
 
363
}
 
364
 
 
365
 
 
366
std::string IDMEFValue::convert_string() const
 
367
{
 
368
        std::stringstream s;
 
369
        prelude_except_if_fail(_value);
 
370
 
 
371
        if ( GetType() == IDMEF_VALUE_TYPE_STRING )
 
372
                return prelude_string_get_string(idmef_value_get_string(_value));
 
373
 
 
374
        else if ( GetType() == IDMEF_VALUE_TYPE_TIME )
 
375
                return IDMEFTime(idmef_time_ref(idmef_value_get_time(_value)));
 
376
 
 
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));
 
379
 
 
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);
 
383
 
 
384
                if ( t == IDMEF_DATA_TYPE_CHAR_STRING )
 
385
                        return (const char *) idmef_data_get_char_string(d);
 
386
 
 
387
                else if ( t == IDMEF_DATA_TYPE_CHAR ) {
 
388
                        s << idmef_data_get_char(d);
 
389
                        return s.str();
 
390
                }
 
391
 
 
392
                else if ( t == IDMEF_DATA_TYPE_FLOAT ) {
 
393
                        s << idmef_data_get_float(d);
 
394
                        return s.str();
 
395
                }
 
396
 
 
397
                else if ( t == IDMEF_DATA_TYPE_UINT32 ) {
 
398
                        s << idmef_data_get_uint32(d);
 
399
                        return s.str();
 
400
                }
 
401
 
 
402
                else if ( t == IDMEF_DATA_TYPE_UINT64 ) {
 
403
                        s << idmef_data_get_uint64(d);
 
404
                        return s.str();
 
405
                }
 
406
 
 
407
                else {
 
408
                        s << "Left value doesn't fit 'data' type '" << t << "' requirement";
 
409
                        throw PreludeError(s.str());
 
410
                }
 
411
        }
 
412
 
 
413
        s << "Left value doesn't fit '" << idmef_value_type_to_string(GetType()) << "' requirement";
 
414
        throw PreludeError(s.str());
 
415
}
 
416
 
 
417
 
 
418
IDMEFValue &IDMEFValue::operator=(const IDMEFValue &p)
 
419
{
 
420
        if ( this != &p && _value != p._value ) {
 
421
                if ( _value )
 
422
                        idmef_value_destroy(_value);
 
423
 
 
424
                _value = (p._value) ? idmef_value_ref(p._value) : NULL;
 
425
        }
 
426
 
 
427
        return *this;
 
428
}
 
429
 
 
430
 
 
431
IDMEFValue::operator const char*() const
 
432
{
 
433
        static std::string t;
 
434
        t = this->convert_string();
 
435
        return t.c_str();
 
436
}
 
437
 
 
438
 
 
439
IDMEFValue::operator idmef_value_t *() const
 
440
{
 
441
        return _value;
 
442
}