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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "zbxalgo.h"
void zbx_variant_clear(zbx_variant_t *value)
{
switch (value->type)
{
case ZBX_VARIANT_STR:
zbx_free(value->data.str);
break;
}
value->type = ZBX_VARIANT_NONE;
}
void zbx_variant_set_str(zbx_variant_t *value, char *text)
{
value->data.str = text;
value->type = ZBX_VARIANT_STR;
}
void zbx_variant_set_dbl(zbx_variant_t *value, double value_dbl)
{
value->data.dbl = value_dbl;
value->type = ZBX_VARIANT_DBL;
}
void zbx_variant_set_ui64(zbx_variant_t *value, zbx_uint64_t value_ui64)
{
value->data.ui64 = value_ui64;
value->type = ZBX_VARIANT_UI64;
}
void zbx_variant_set_none(zbx_variant_t *value)
{
value->type = ZBX_VARIANT_NONE;
}
void zbx_variant_set_variant(zbx_variant_t *value, const zbx_variant_t *source)
{
switch (source->type)
{
case ZBX_VARIANT_STR:
zbx_variant_set_str(value, zbx_strdup(NULL, source->data.str));
break;
case ZBX_VARIANT_UI64:
zbx_variant_set_ui64(value, source->data.ui64);
break;
case ZBX_VARIANT_DBL:
zbx_variant_set_dbl(value, source->data.dbl);
break;
case ZBX_VARIANT_NONE:
value->type = ZBX_VARIANT_NONE;
break;
}
}
static int variant_to_dbl(zbx_variant_t *value)
{
char buffer[MAX_STRING_LEN];
double value_dbl;
switch (value->type)
{
case ZBX_VARIANT_DBL:
return SUCCEED;
case ZBX_VARIANT_UI64:
zbx_variant_set_dbl(value, (double)value->data.ui64);
return SUCCEED;
case ZBX_VARIANT_STR:
zbx_strlcpy(buffer, value->data.str, sizeof(buffer));
break;
default:
return FAIL;
}
zbx_ltrim(buffer, " \"+");
zbx_rtrim(buffer, " \"\n\r");
if (SUCCEED != is_double(buffer))
return FAIL;
value_dbl = atof(buffer);
zbx_variant_clear(value);
zbx_variant_set_dbl(value, value_dbl);
return SUCCEED;
}
static int variant_to_ui64(zbx_variant_t *value)
{
zbx_uint64_t value_ui64;
char buffer[MAX_STRING_LEN];
switch (value->type)
{
case ZBX_VARIANT_UI64:
return SUCCEED;
case ZBX_VARIANT_DBL:
if (0 > value->data.dbl)
return FAIL;
zbx_variant_set_ui64(value, value->data.dbl);
return SUCCEED;
case ZBX_VARIANT_STR:
zbx_strlcpy(buffer, value->data.str, sizeof(buffer));
break;
default:
return FAIL;
}
zbx_ltrim(buffer, " \"+");
zbx_rtrim(buffer, " \"\n\r");
del_zeros(buffer);
if (SUCCEED != is_uint64(buffer, &value_ui64))
return FAIL;
zbx_variant_clear(value);
zbx_variant_set_ui64(value, value_ui64);
return SUCCEED;
}
static int variant_to_str(zbx_variant_t *value)
{
char *value_str;
switch (value->type)
{
case ZBX_VARIANT_STR:
return SUCCEED;
case ZBX_VARIANT_DBL:
value_str = zbx_dsprintf(NULL, ZBX_FS_DBL, value->data.dbl);
del_zeros(value_str);
break;
case ZBX_VARIANT_UI64:
value_str = zbx_dsprintf(NULL, ZBX_FS_UI64, value->data.ui64);
break;
default:
return FAIL;
}
zbx_variant_clear(value);
zbx_variant_set_str(value, value_str);
return SUCCEED;
}
int zbx_variant_convert(zbx_variant_t *value, int type)
{
switch(type)
{
case ZBX_VARIANT_UI64:
return variant_to_ui64(value);
case ZBX_VARIANT_DBL:
return variant_to_dbl(value);
case ZBX_VARIANT_STR:
return variant_to_str(value);
case ZBX_VARIANT_NONE:
zbx_variant_clear(value);
return SUCCEED;
default:
return FAIL;
}
}
int zbx_variant_set_numeric(zbx_variant_t *value, const char *text)
{
zbx_uint64_t value_ui64;
char buffer[MAX_STRING_LEN];
zbx_strlcpy(buffer, text, sizeof(buffer));
zbx_ltrim(buffer, " \"+");
zbx_rtrim(buffer, " \"\n\r");
del_zeros(buffer);
if (SUCCEED == is_uint64(buffer, &value_ui64))
{
zbx_variant_set_ui64(value, value_ui64);
return SUCCEED;
}
if (SUCCEED == is_double(buffer))
{
zbx_variant_set_dbl(value, atof(buffer));
return SUCCEED;
}
return FAIL;
}
const char *zbx_variant_value_desc(const zbx_variant_t *value)
{
static char buffer[ZBX_MAX_UINT64_LEN + 1];
switch (value->type)
{
case ZBX_VARIANT_DBL:
zbx_snprintf(buffer, sizeof(buffer), ZBX_FS_DBL, value->data.dbl);
del_zeros(buffer);
return buffer;
case ZBX_VARIANT_UI64:
zbx_snprintf(buffer, sizeof(buffer), ZBX_FS_UI64, value->data.ui64);
return buffer;
case ZBX_VARIANT_STR:
return value->data.str;
case ZBX_VARIANT_NONE:
return "";
default:
THIS_SHOULD_NEVER_HAPPEN;
return ZBX_UNKNOWN_STR;
}
}
const char *zbx_variant_type_desc(const zbx_variant_t *value)
{
switch (value->type)
{
case ZBX_VARIANT_DBL:
return "double";
case ZBX_VARIANT_UI64:
return "uint64";
case ZBX_VARIANT_STR:
return "string";
case ZBX_VARIANT_NONE:
return "none";
default:
THIS_SHOULD_NEVER_HAPPEN;
return ZBX_UNKNOWN_STR;
}
}
int zbx_validate_value_dbl(double value)
{
/* field with precision 16, scale 4 [NUMERIC(16,4)] */
const double pg_min_numeric = -1e12;
const double pg_max_numeric = 1e12;
if (value <= pg_min_numeric || value >= pg_max_numeric)
return FAIL;
return SUCCEED;
}
/******************************************************************************
* *
* Function: variant_compare_empty *
* *
* Purpose: compares two variant values when at least one is empty *
* *
******************************************************************************/
static int variant_compare_empty(const zbx_variant_t *value1, const zbx_variant_t *value2)
{
if (ZBX_VARIANT_NONE == value1->type)
{
if (ZBX_VARIANT_NONE == value2->type)
return 0;
return -1;
}
return 1;
}
/******************************************************************************
* *
* Function: variant_compare_str *
* *
* Purpose: compares two variant values when at least one is string *
* *
******************************************************************************/
static int variant_compare_str(const zbx_variant_t *value1, const zbx_variant_t *value2)
{
if (ZBX_VARIANT_STR == value1->type)
return strcmp(value1->data.str, zbx_variant_value_desc(value2));
return strcmp(zbx_variant_value_desc(value1), value2->data.str);
}
/******************************************************************************
* *
* Function: variant_compare_dbl *
* *
* Purpose: compares two variant values when at least one is double and the *
* other is double or uint64 *
* *
******************************************************************************/
static int variant_compare_dbl(const zbx_variant_t *value1, const zbx_variant_t *value2)
{
double value1_dbl, value2_dbl;
switch (value1->type)
{
case ZBX_VARIANT_DBL:
value1_dbl = value1->data.dbl;
break;
case ZBX_VARIANT_UI64:
value1_dbl = value1->data.ui64;
break;
default:
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
switch (value2->type)
{
case ZBX_VARIANT_DBL:
value2_dbl = value2->data.dbl;
break;
case ZBX_VARIANT_UI64:
value2_dbl = value2->data.ui64;
break;
default:
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
if (SUCCEED == zbx_double_compare(value1_dbl, value2_dbl))
return 0;
ZBX_RETURN_IF_NOT_EQUAL(value1_dbl, value2_dbl);
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
/******************************************************************************
* *
* Function: variant_compare_ui64 *
* *
* Purpose: compares two variant values when both are uint64 *
* *
******************************************************************************/
static int variant_compare_ui64(const zbx_variant_t *value1, const zbx_variant_t *value2)
{
ZBX_RETURN_IF_NOT_EQUAL(value1->data.ui64, value2->data.ui64);
return 0;
}
/******************************************************************************
* *
* Function: zbx_variant_compare *
* *
* Purpose: compares two variant values *
* *
* Parameters: value1 - [IN] the first value *
* value2 - [IN] the second value *
* *
* Return value: <0 - the first value is less than second *
* >0 - the first value is greater than second *
* 0 - the values are equal *
* *
* Comments: The following priority is applied: *
* 1) value of none type is always less than other types, two *
* none types are equal *
* 2) if any of value is of string type, the other is converted to *
* string and both are compared *
* 3) if any of value is of floating type, the other is converted *
* to floating value and both are compared *
* 4) only uin64 types are left, compare as uin64 *
* *
******************************************************************************/
int zbx_variant_compare(const zbx_variant_t *value1, const zbx_variant_t *value2)
{
if (ZBX_VARIANT_NONE == value1->type || ZBX_VARIANT_NONE == value2->type)
return variant_compare_empty(value1, value2);
if (ZBX_VARIANT_STR == value1->type || ZBX_VARIANT_STR == value2->type)
return variant_compare_str(value1, value2);
if (ZBX_VARIANT_DBL == value1->type || ZBX_VARIANT_DBL == value2->type)
return variant_compare_dbl(value1, value2);
if (ZBX_VARIANT_UI64 == value1->type && ZBX_VARIANT_UI64 == value2->type)
return variant_compare_ui64(value1, value2);
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
|