Code

data: Added support for comparing integer, decimal, and string arrays.
[sysdb.git] / src / core / data.c
1 /*
2  * SysDB - src/core/data.c
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
34 #include "core/data.h"
35 #include "utils/error.h"
37 #include <assert.h>
39 #include <errno.h>
41 #include <inttypes.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include <math.h>
49 /*
50  * private helper functions
51  */
53 /* this function supports in-place copies */
54 static int
55 copy_array_values(sdb_data_t *dst, const sdb_data_t *src, size_t elem_size)
56 {
57         int type = src->type & 0xff;
59         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)) {
60                 if (dst != src)
61                         memcpy(dst->data.array.values, src->data.array.values,
62                                         src->data.array.length * elem_size);
63         }
64         else if (type == SDB_TYPE_STRING) {
65                 char **s = src->data.array.values;
66                 char **d = dst->data.array.values;
67                 size_t i;
69                 for (i = 0; i < src->data.array.length; ++i) {
70                         d[i] = strdup(s[i]);
71                         if (! d[i])
72                                 return -1;
73                 }
74         }
75         else {
76                 /* TODO */
77                 errno = ENOTSUP;
78                 return -1;
79         }
80         return 0;
81 } /* copy_array_values */
83 static void
84 free_array_values(sdb_data_t *datum)
85 {
86         int type = datum->type & 0xff;
88         if (type == SDB_TYPE_STRING) {
89                 char **v = datum->data.array.values;
90                 size_t i;
92                 for (i = 0; i < datum->data.array.length; ++i) {
93                         if (v[i])
94                                 free(v[i]);
95                         v[i] = NULL;
96                 }
97         }
98 } /* free_array_values */
100 /* compare two arrays element-by-element returning how the first non-equal
101  * elements compare to each other */
102 static int
103 array_cmp(const sdb_data_t *a1, const sdb_data_t *a2)
105         int type = a1->type & 0xff;
106         size_t len, i;
108         assert((a1->type == a2->type) && (a1->type & SDB_TYPE_ARRAY));
110         len = SDB_MIN(a1->data.array.length, a2->data.array.length);
112         if (type == SDB_TYPE_INTEGER) {
113                 int64_t *v1 = a1->data.array.values;
114                 int64_t *v2 = a2->data.array.values;
116                 for (i = 0; i < len; ++i)
117                         if (v1[i] != v2[i])
118                                 return SDB_CMP(v1[i], v2[i]);
119         }
120         else if (type == SDB_TYPE_DECIMAL) {
121                 double *v1 = a1->data.array.values;
122                 double *v2 = a2->data.array.values;
124                 for (i = 0; i < len; ++i)
125                         if (v1[i] != v2[i])
126                                 return SDB_CMP(v1[i], v2[i]);
127         }
128         else if (type == SDB_TYPE_STRING) {
129                 char **v1 = a1->data.array.values;
130                 char **v2 = a2->data.array.values;
132                 for (i = 0; i < len; ++i) {
133                         int diff = strcasecmp(v1[i], v2[i]);
134                         if (diff)
135                                 return diff;
136                 }
137         }
138         else {
139                 /* TODO */
140                 errno = ENOTSUP;
141                 /* but fall through to ensure stable sorting: */
142         }
143         return SDB_CMP(a1->data.array.length, a2->data.array.length);
144 } /* array_cmp */
146 /* Calculate the linear function 'd1 + n * d2'. */
147 static int
148 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
150         if (d1->type != d2->type)
151                 return -1;
153         if (d1->type == SDB_TYPE_INTEGER)
154                 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
155         else if (d1->type == SDB_TYPE_DECIMAL)
156                 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
157         else if (d1->type ==  SDB_TYPE_DATETIME)
158                 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
159         else
160                 return -1;
161         res->type = d1->type;
162         return 0;
163 } /* data_lin */
165 /* Multiply d1 with d2. */
166 static int
167 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
169         if (d1->type == SDB_TYPE_INTEGER) {
170                 if (d2->type == SDB_TYPE_INTEGER)
171                         res->data.integer = d1->data.integer * d2->data.integer;
172                 else if (d2->type == SDB_TYPE_DATETIME) {
173                         res->data.datetime = (sdb_time_t)d1->data.integer
174                                 * d2->data.datetime;
175                         res->type = SDB_TYPE_DATETIME;
176                         return 0;
177                 }
178                 else
179                         return -1;
180         }
181         else if (d1->type == SDB_TYPE_DECIMAL) {
182                 if (d2->type == SDB_TYPE_DECIMAL)
183                         res->data.decimal = d1->data.decimal * d2->data.decimal;
184                 else if (d2->type == SDB_TYPE_DATETIME) {
185                         res->data.datetime = (sdb_time_t)(d1->data.decimal
186                                         * (double)d2->data.datetime);
187                         res->type = SDB_TYPE_DATETIME;
188                         return 0;
189                 }
190                 else
191                         return -1;
192         }
193         else if (d1->type == SDB_TYPE_DATETIME) {
194                 if (d2->type == SDB_TYPE_DATETIME)
195                         res->data.datetime = d1->data.datetime
196                                 * d2->data.datetime;
197                 else if (d2->type == SDB_TYPE_INTEGER)
198                         res->data.datetime = d1->data.datetime
199                                 * (sdb_time_t)d2->data.integer;
200                 else if (d2->type == SDB_TYPE_DECIMAL)
201                         res->data.datetime = (sdb_time_t)((double)d1->data.datetime
202                                         * d2->data.decimal);
203                 else
204                         return -1;
205         }
206         else
207                 return -1;
209         res->type = d1->type;
210         return 0;
211 } /* data_mul */
213 /* Device d1 by d2 and return the result and the remainder. */
214 static int
215 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
216                 sdb_data_t *res, sdb_data_t *rem)
218         if (d1->type == SDB_TYPE_INTEGER) {
219                 if (d2->type != SDB_TYPE_INTEGER)
220                         return -1;
221                 if (res)
222                         res->data.integer = d1->data.integer / d2->data.integer;
223                 if (rem)
224                         rem->data.integer = d1->data.integer % d2->data.integer;
225         }
226         else if (d1->type == SDB_TYPE_DECIMAL) {
227                 if (d2->type != SDB_TYPE_DECIMAL)
228                         return -1;
229                 if (res)
230                         res->data.decimal = d1->data.decimal / d2->data.decimal;
231                 if (rem)
232                         rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
233         }
234         else if (d1->type == SDB_TYPE_DATETIME) {
235                 if (d2->type == SDB_TYPE_DECIMAL) {
236                         if (res)
237                                 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
238                                                 / d2->data.decimal);
239                         if (rem) {
240                                 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
241                                 rem->data.datetime = (sdb_time_t)tmp;
242                         }
243                 }
244                 else {
245                         sdb_time_t a, b;
246                         if (d2->type == SDB_TYPE_DATETIME) {
247                                 a = d1->data.datetime;
248                                 b = d2->data.datetime;
249                         }
250                         else if (d2->type == SDB_TYPE_INTEGER) {
251                                 a = d1->data.datetime;
252                                 b = (sdb_time_t)d2->data.integer;
253                         }
254                         else
255                                 return -1;
256                         if (res)
257                                 res->data.datetime = a / b;
258                         if (rem)
259                                 rem->data.datetime = a % b;
260                 }
261         }
262         else
263                 return -1;
265         if (res)
266                 res->type = d1->type;
267         if (rem)
268                 rem->type = d1->type;
269         return 0;
270 } /* data_div */
272 /* Concatenate d1 and d2. */
273 static int
274 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
276         unsigned char *new;
277         unsigned char *s1, *s2;
278         size_t len1, len2;
280         /* TODO: support array plus element */
281         if (d1->type != d2->type)
282                 return -1;
284         if (d1->type == SDB_TYPE_STRING) {
285                 s1 = (unsigned char *)d1->data.string;
286                 s2 = (unsigned char *)d2->data.string;
287                 len1 = s1 ? strlen((char *)s1) : 0;
288                 len2 = s2 ? strlen((char *)s2) : 0;
289         }
290         else if (d1->type == SDB_TYPE_BINARY) {
291                 s1 = d1->data.binary.datum;
292                 s2 = d2->data.binary.datum;
293                 len1 = d1->data.binary.length;
294                 len2 = d2->data.binary.length;
295         }
296         else if (d1->type & SDB_TYPE_ARRAY) {
297                 size_t elem_size = sdb_data_sizeof(d1->type & 0xff);
298                 s1 = (unsigned char *)d1->data.array.values;
299                 s2 = (unsigned char *)d2->data.array.values;
300                 len1 = d1->data.array.length * elem_size;
301                 len2 = d2->data.array.length * elem_size;
302         }
303         else
304                 return -1;
306         assert(s1 && s2);
308         new = malloc(len1 + len2 + 1);
309         if (! new)
310                 return -1;
312         if (len1)
313                 memcpy(new, s1, len1);
314         if (len2)
315                 memcpy(new + len1, s2, len2);
316         new[len1 + len2] = '\0';
318         res->type = d1->type;
319         if (res->type == SDB_TYPE_STRING) {
320                 res->data.string = (char *)new;
321         }
322         else if (res->type == SDB_TYPE_BINARY) {
323                 res->data.binary.datum = new;
324                 res->data.binary.length = len1 + len2;
325         }
326         else if (d1->type & SDB_TYPE_ARRAY) {
327                 res->data.array.values = new;
328                 res->data.array.length = d1->data.array.length + d2->data.array.length;
329                 if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) {
330                         /* this leaks already copied values but there's not much we can
331                          * do and this should only happen if we're in trouble anyway */
332                         free(new);
333                         res->data.array.values = NULL;
334                         res->data.array.length = 0;
335                         return -1;
336                 }
337         }
338         return 0;
339 } /* data_concat */
341 /*
342  * public API
343  */
345 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
347 int
348 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
350         sdb_data_t tmp;
352         if ((! dst) || (! src))
353                 return -1;
355         tmp = *src;
356         if (src->type == SDB_TYPE_STRING) {
357                 if (src->data.string) {
358                         tmp.data.string = strdup(src->data.string);
359                         if (! tmp.data.string)
360                                 return -1;
361                 }
362         }
363         else if (src->type == SDB_TYPE_BINARY) {
364                 if (src->data.binary.datum) {
365                         tmp.data.binary.datum = malloc(src->data.binary.length);
366                         if (! tmp.data.binary.datum)
367                                 return -1;
368                         memcpy(tmp.data.binary.datum, src->data.binary.datum,
369                                         src->data.binary.length);
370                 }
371         }
372         else if (src->type == SDB_TYPE_REGEX) {
373                 if (src->data.re.raw) {
374                         tmp.data.re.raw = strdup(src->data.re.raw);
375                         if (! tmp.data.re.raw)
376                                 return -1;
377                         /* we need to recompile because the regex might point to
378                          * dynamically allocated memory */
379                         if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
380                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
381                                 free(tmp.data.re.raw);
382                                 return -1;
383                         }
384                 }
385                 else
386                         memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
387         }
388         else if (src->type & SDB_TYPE_ARRAY) {
389                 if (src->data.array.values) {
390                         size_t elem_size = sdb_data_sizeof(src->type & 0xff);
391                         tmp.data.array.values = calloc(src->data.array.length, elem_size);
392                         if (! tmp.data.array.values)
393                                 return -1;
394                         if (copy_array_values(&tmp, src, elem_size)) {
395                                 sdb_data_free_datum(&tmp);
396                                 return -1;
397                         }
398                 }
399         }
401         sdb_data_free_datum(dst);
402         *dst = tmp;
403         return 0;
404 } /* sdb_data_copy */
406 void
407 sdb_data_free_datum(sdb_data_t *datum)
409         if (! datum)
410                 return;
412         if (datum->type == SDB_TYPE_STRING) {
413                 if (datum->data.string)
414                         free(datum->data.string);
415                 datum->data.string = NULL;
416         }
417         else if (datum->type == SDB_TYPE_BINARY) {
418                 if (datum->data.binary.datum)
419                         free(datum->data.binary.datum);
420                 datum->data.binary.datum = NULL;
421                 datum->data.binary.length = 0;
422         }
423         else if (datum->type == SDB_TYPE_REGEX) {
424                 if (datum->data.re.raw) {
425                         free(datum->data.re.raw);
426                         regfree(&datum->data.re.regex);
427                 }
428                 datum->data.re.raw = NULL;
429                 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
430         }
431         else if (datum->type & SDB_TYPE_ARRAY) {
432                 free_array_values(datum);
433                 if (datum->data.array.values)
434                         free(datum->data.array.values);
435                 datum->data.array.values = NULL;
436                 datum->data.array.length = 0;
437         }
438 } /* sdb_data_free_datum */
440 int
441 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
443 #define CMP_NULL(a, b) \
444         do { \
445                 if (!(a) && !(b)) return 0; \
446                 if (!(a)) return -1; \
447                 if (!(b)) return 1; \
448         } while (0)
450         CMP_NULL(d1, d2);
452         if (d1->type != d2->type)
453                 return SDB_CMP(d1->type, d2->type);
455         if (d1->type == SDB_TYPE_INTEGER)
456                 return SDB_CMP(d1->data.integer, d2->data.integer);
457         else if (d1->type == SDB_TYPE_DECIMAL)
458                 return SDB_CMP(d1->data.decimal, d2->data.decimal);
459         else if (d1->type == SDB_TYPE_STRING) {
460                 CMP_NULL(d1->data.string, d2->data.string);
461                 return strcasecmp(d1->data.string, d2->data.string);
462         }
463         else if (d1->type == SDB_TYPE_DATETIME)
464                 return SDB_CMP(d1->data.datetime, d2->data.datetime);
465         else if (d1->type == SDB_TYPE_BINARY) {
466                 int diff;
468                 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
470                 /* on a common prefix, the shorter datum sorts less */
471                 if (d1->data.binary.length < d2->data.binary.length) {
472                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
473                                         d1->data.binary.length);
474                         diff = diff ? diff : -1;
475                 }
476                 else if (d1->data.binary.length > d2->data.binary.length) {
477                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
478                                         d2->data.binary.length);
479                         diff = diff ? diff : 1;
480                 }
481                 else
482                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
483                                         d1->data.binary.length);
485                 return diff;
486         }
487         else if (d1->type == SDB_TYPE_REGEX) {
488                 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
489                 return strcmp(d1->data.re.raw, d2->data.re.raw);
490         }
491         else if (d1->type & SDB_TYPE_ARRAY) {
492                 CMP_NULL(d1->data.array.values, d2->data.array.values);
493                 return array_cmp(d1, d2);
494         }
495         return -1;
496 } /* sdb_data_cmp */
498 int
499 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
501         char d1_str[sdb_data_strlen(d1) + 1];
502         char d2_str[sdb_data_strlen(d2) + 1];
504         if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
505                 /* TODO */
506                 errno = ENOTSUP;
507                 return -1;
508         }
510         if (sdb_data_isnull(d1))
511                 d1 = NULL;
512         if (sdb_data_isnull(d2))
513                 d2 = NULL;
515         CMP_NULL(d1, d2);
517         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
518                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
519         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
520                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
522         return strcasecmp(d1_str, d2_str);
523 #undef CMP_NULL
524 } /* sdb_data_strcmp */
526 _Bool
527 sdb_data_isnull(const sdb_data_t *datum)
529         if (! datum)
530                 return 1;
531         if (datum->type == SDB_TYPE_NULL)
532                 return 1;
533         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
534                 return 1;
535         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
536                 return 1;
537         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
538                 return 1;
539         if ((datum->type & SDB_TYPE_ARRAY) && (! datum->data.array.values))
540                 return 1;
541         return 0;
542 } /* sdb_data_isnull */
544 int
545 sdb_data_parse_op(const char *op)
547         if (! strcmp(op, "+"))
548                 return SDB_DATA_ADD;
549         else if (! strcmp(op, "-"))
550                 return SDB_DATA_SUB;
551         else if (! strcmp(op, "*"))
552                 return SDB_DATA_MUL;
553         else if (! strcmp(op, "/"))
554                 return SDB_DATA_DIV;
555         else if (! strcmp(op, "%"))
556                 return SDB_DATA_MOD;
557         else if (! strcmp(op, "||"))
558                 return SDB_DATA_CONCAT;
559         return -1;
560 } /* sdb_data_parse_op */
562 int
563 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
564                 sdb_data_t *res)
566         if ((! d1) || (! d2) || (! res))
567                 return -1;
568         if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
569                 *res = SDB_DATA_NULL;
570                 return 0;
571         }
572         switch (op) {
573                 case SDB_DATA_CONCAT:
574                         return data_concat(d1, d2, res);
575                 case SDB_DATA_ADD:
576                         return data_lin(d1, 1, d2, res);
577                 case SDB_DATA_SUB:
578                         return data_lin(d1, -1, d2, res);
579                 case SDB_DATA_MUL:
580                         return data_mul(d1, d2, res);
581                 case SDB_DATA_DIV:
582                         return data_div(d1, d2, res, NULL);
583                 case SDB_DATA_MOD:
584                         return data_div(d1, d2, NULL, res);
585         }
586         return -1;
587 } /* sdb_data_expr_eval */
589 size_t
590 sdb_data_strlen(const sdb_data_t *datum)
592         if (! datum)
593                 return 0;
595         if (datum->type == SDB_TYPE_INTEGER) {
596                 /* log(64) */
597                 return 20;
598         }
599         else if (datum->type == SDB_TYPE_DECIMAL) {
600                 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
601                 return 42;
602         }
603         else if (datum->type == SDB_TYPE_STRING) {
604                 if (! datum->data.string)
605                         return 8; /* "<NULL>" */
606                 /* in the worst case, each character needs to be escaped */
607                 return 2 * strlen(datum->data.string) + 2;
608         }
609         else if (datum->type == SDB_TYPE_DATETIME) {
610                 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
611                 return 27;
612         }
613         else if (datum->type == SDB_TYPE_BINARY) {
614                 if (! datum->data.binary.datum)
615                         return 8; /* "<NULL>" */
616                 /* "\xNN" */
617                 return 4 * datum->data.binary.length + 2;
618         }
619         else if (datum->type == SDB_TYPE_REGEX) {
620                 if (! datum->data.re.raw)
621                         return 8; /* "<NULL>" */
622                 /* "/.../" */
623                 return strlen(datum->data.re.raw) + 4;
624         }
625         else if (datum->type & SDB_TYPE_ARRAY) {
626                 /* TODO */
627                 errno = ENOTSUP;
628                 return 0;
629         }
630         return 0;
631 } /* sdb_data_strlen */
633 int
634 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
636         char tmp[sdb_data_strlen(datum) + 1];
637         char *data = NULL;
638         int ret = -1;
640         size_t i, pos;
642         if ((! datum) || (! buf))
643                 return -1;
645         if (datum->type == SDB_TYPE_INTEGER) {
646                 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
647         }
648         else if (datum->type == SDB_TYPE_DECIMAL) {
649                 ret = snprintf(buf, buflen, "%g", datum->data.decimal);
650         }
651         else if (datum->type == SDB_TYPE_STRING) {
652                 if (! datum->data.string)
653                         data = "<NULL>";
654                 else {
655                         pos = 0;
656                         for (i = 0; i < strlen(datum->data.string); ++i) {
657                                 char byte = datum->data.string[i];
659                                 if ((byte == '\\') || (byte == '"')) {
660                                         tmp[pos] = '\\';
661                                         ++pos;
662                                 }
663                                 tmp[pos] = byte;
664                                 ++pos;
665                         }
666                         tmp[pos] = '\0';
667                         data = tmp;
668                 }
669         }
670         else if (datum->type == SDB_TYPE_DATETIME) {
671                 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
672                                         datum->data.datetime))
673                         return -1;
674                 tmp[sizeof(tmp) - 1] = '\0';
675                 data = tmp;
676         }
677         else if (datum->type == SDB_TYPE_BINARY) {
678                 pos = 0;
679                 for (i = 0; i < datum->data.binary.length; ++i) {
680                         int byte = (int)datum->data.binary.datum[i];
681                         char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
682                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
684                         tmp[pos] = '\\';
685                         tmp[pos + 1] = 'x';
686                         pos += 2;
688                         if (byte > 0xf) {
689                                 tmp[pos] = hex[byte >> 4];
690                                 ++pos;
691                         }
692                         tmp[pos] = hex[byte & 0xf];
693                         ++pos;
694                 }
695                 if (datum->data.binary.datum) {
696                         tmp[pos] = '\0';
697                         data = tmp;
698                 }
699                 else
700                         data = "<NULL>";
701         }
702         else if (datum->type == SDB_TYPE_REGEX) {
703                 if (! datum->data.re.raw)
704                         data = "<NULL>";
705                 else {
706                         snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
707                         data = tmp;
708                 }
709         }
710         else if (datum->type & SDB_TYPE_ARRAY) {
711                 /* TODO */
712                 errno = ENOTSUP;
713                 return -1;
714         }
716         if (data) {
717                 if (quoted == SDB_UNQUOTED)
718                         ret = snprintf(buf, buflen, "%s", data);
719                 else if (quoted == SDB_SINGLE_QUOTED)
720                         ret = snprintf(buf, buflen, "'%s'", data);
721                 else
722                         ret = snprintf(buf, buflen, "\"%s\"", data);
723         }
724         buf[buflen - 1] = '\0';
725         return ret;
726 } /* sdb_data_format */
728 int
729 sdb_data_parse(char *str, int type, sdb_data_t *data)
731         sdb_data_t tmp;
733         char *endptr = NULL;
735         errno = 0;
736         if (type == SDB_TYPE_INTEGER) {
737                 tmp.data.integer = strtoll(str, &endptr, 0);
738         }
739         else if (type == SDB_TYPE_DECIMAL) {
740                 tmp.data.decimal = strtod(str, &endptr);
741         }
742         else if (type == SDB_TYPE_STRING) {
743                 tmp.data.string = str;
744         }
745         else if (type == SDB_TYPE_DATETIME) {
746                 double datetime = strtod(str, &endptr);
747                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
748         }
749         else if (type == SDB_TYPE_BINARY) {
750                 /* we don't support any binary information containing 0-bytes */
751                 tmp.data.binary.length = strlen(str);
752                 tmp.data.binary.datum = (unsigned char *)str;
753         }
754         else if (type == SDB_TYPE_REGEX) {
755                 tmp.data.re.raw = strdup(str);
756                 if (! tmp.data.re.raw)
757                         return -1;
758                 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
759                                         REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
760                         sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
761                                         "expression '%s'", tmp.data.re.raw);
762                         free(tmp.data.re.raw);
763                         return -1;
764                 }
765                 if (! data) {
766                         tmp.type = SDB_TYPE_REGEX;
767                         sdb_data_free_datum(&tmp);
768                 }
769         }
770         else if (type & SDB_TYPE_ARRAY) {
771                 /* TODO */
772                 errno = ENOTSUP;
773                 return -1;
774         }
775         else {
776                 errno = EINVAL;
777                 return -1;
778         }
780         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
781                         || (type == SDB_TYPE_DATETIME)) {
782                 if (errno || (str == endptr)) {
783                         char errbuf[1024];
784                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
785                                         "'%s' as numeric value (type %i): %s", str, type,
786                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
787                         return -1;
788                 }
789                 else if (endptr && (*endptr != '\0'))
790                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
791                                         "number while parsing numeric value (type %i): %s.",
792                                         type, endptr);
793         }
795         if (data) {
796                 *data = tmp;
797                 data->type = type;
798         }
799         return 0;
800 } /* sdb_data_parse */
802 size_t
803 sdb_data_sizeof(int type)
805         sdb_data_t v;
806         if (type == SDB_TYPE_INTEGER)
807                 return sizeof(v.data.integer);
808         else if (type == SDB_TYPE_DECIMAL)
809                 return sizeof(v.data.decimal);
810         else if (type == SDB_TYPE_STRING)
811                 return sizeof(v.data.string);
812         else if (type == SDB_TYPE_DATETIME)
813                 return sizeof(v.data.datetime);
814         else if (type == SDB_TYPE_BINARY)
815                 return sizeof(v.data.binary);
816         else if (type == SDB_TYPE_REGEX)
817                 return sizeof(v.data.re);
818         return 0;
819 } /* sdb_data_sizeof */
821 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */