Code

data: Added sdb_data_array_get().
[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  * Operator support maxtrix.
51  * <type1> <op> <type2> -> op_matrix[<op>][<type1>][<type2>]
52  */
54 /* add, sub, mul, div, mod, concat */
56 /* integer, decimal, string, datetime, binary, regex */
58 static int op_matrix[6][6][6] = {
59         /* SDB_DATA_ADD */
60         {
61                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
62                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
63                 { -1, -1, -1, -1, -1, -1 },
64                 { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
65                 { -1, -1, -1, -1, -1, -1 },
66                 { -1, -1, -1, -1, -1, -1 },
67         },
69         /* SDB_DATA_SUB */
70         {
71                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
72                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
73                 { -1, -1, -1, -1, -1, -1 },
74                 { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
75                 { -1, -1, -1, -1, -1, -1 },
76                 { -1, -1, -1, -1, -1, -1 },
77         },
79         /* SDB_DATA_MUL */
80         {
81                 { SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
82                 { -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 },
83                 { -1, -1, -1, -1, -1, -1 },
84                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
85                 { -1, -1, -1, -1, -1, -1 },
86                 { -1, -1, -1, -1, -1, -1 },
87         },
89         /* SDB_DATA_DIV */
90         {
91                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
92                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
93                 { -1, -1, -1, -1, -1, -1 },
94                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
95                 { -1, -1, -1, -1, -1, -1 },
96                 { -1, -1, -1, -1, -1, -1 },
97         },
99         /* SDB_DATA_MOD */
100         {
101                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
102                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
103                 { -1, -1, -1, -1, -1, -1 },
104                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
105                 { -1, -1, -1, -1, -1, -1 },
106                 { -1, -1, -1, -1, -1, -1 },
107         },
109         /* SDB_DATA_CONCAT */
110         {
111                 { -1, -1, -1, -1, -1, -1 },
112                 { -1, -1, -1, -1, -1, -1 },
113                 { -1, -1, SDB_TYPE_STRING, -1, -1, -1 },
114                 { -1, -1, -1, -1, -1, -1 },
115                 { -1, -1, -1, -1, SDB_TYPE_BINARY, -1 },
116                 { -1, -1, -1, -1, -1, -1 },
117         },
118 };
120 /*
121  * private helper functions
122  */
124 /* this function supports in-place copies */
125 static int
126 copy_array_values(sdb_data_t *dst, const sdb_data_t *src, size_t elem_size)
128         int type = src->type & 0xff;
130         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)) {
131                 if (dst != src)
132                         memcpy(dst->data.array.values, src->data.array.values,
133                                         src->data.array.length * elem_size);
134         }
135         else if (type == SDB_TYPE_STRING) {
136                 char **s = src->data.array.values;
137                 char **d = dst->data.array.values;
138                 size_t i;
140                 for (i = 0; i < src->data.array.length; ++i) {
141                         d[i] = strdup(s[i]);
142                         if (! d[i])
143                                 return -1;
144                 }
145         }
146         else {
147                 /* TODO */
148                 errno = ENOTSUP;
149                 return -1;
150         }
151         return 0;
152 } /* copy_array_values */
154 static void
155 free_array_values(sdb_data_t *datum)
157         int type = datum->type & 0xff;
159         if (type == SDB_TYPE_STRING) {
160                 char **v = datum->data.array.values;
161                 size_t i;
163                 for (i = 0; i < datum->data.array.length; ++i) {
164                         if (v[i])
165                                 free(v[i]);
166                         v[i] = NULL;
167                 }
168         }
169 } /* free_array_values */
171 /* compare two arrays element-by-element returning how the first non-equal
172  * elements compare to each other */
173 static int
174 array_cmp(const sdb_data_t *a1, const sdb_data_t *a2)
176         int type = a1->type & 0xff;
177         size_t len, i;
179         assert((a1->type == a2->type) && (a1->type & SDB_TYPE_ARRAY));
181         len = SDB_MIN(a1->data.array.length, a2->data.array.length);
183         if (type == SDB_TYPE_INTEGER) {
184                 int64_t *v1 = a1->data.array.values;
185                 int64_t *v2 = a2->data.array.values;
187                 for (i = 0; i < len; ++i)
188                         if (v1[i] != v2[i])
189                                 return SDB_CMP(v1[i], v2[i]);
190         }
191         else if (type == SDB_TYPE_DECIMAL) {
192                 double *v1 = a1->data.array.values;
193                 double *v2 = a2->data.array.values;
195                 for (i = 0; i < len; ++i)
196                         if (v1[i] != v2[i])
197                                 return SDB_CMP(v1[i], v2[i]);
198         }
199         else if (type == SDB_TYPE_STRING) {
200                 char **v1 = a1->data.array.values;
201                 char **v2 = a2->data.array.values;
203                 for (i = 0; i < len; ++i) {
204                         int diff = strcasecmp(v1[i], v2[i]);
205                         if (diff)
206                                 return diff;
207                 }
208         }
209         else {
210                 /* TODO */
211                 errno = ENOTSUP;
212                 /* but fall through to ensure stable sorting: */
213         }
214         return SDB_CMP(a1->data.array.length, a2->data.array.length);
215 } /* array_cmp */
217 /* Calculate the linear function 'd1 + n * d2'. */
218 static int
219 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
221         if (d1->type != d2->type)
222                 return -1;
224         if (d1->type == SDB_TYPE_INTEGER)
225                 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
226         else if (d1->type == SDB_TYPE_DECIMAL)
227                 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
228         else if (d1->type ==  SDB_TYPE_DATETIME)
229                 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
230         else
231                 return -1;
232         res->type = d1->type;
233         return 0;
234 } /* data_lin */
236 /* Multiply d1 with d2. */
237 static int
238 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
240         if (d1->type == SDB_TYPE_INTEGER) {
241                 if (d2->type == SDB_TYPE_INTEGER)
242                         res->data.integer = d1->data.integer * d2->data.integer;
243                 else if (d2->type == SDB_TYPE_DATETIME) {
244                         res->data.datetime = (sdb_time_t)d1->data.integer
245                                 * d2->data.datetime;
246                         res->type = SDB_TYPE_DATETIME;
247                         return 0;
248                 }
249                 else
250                         return -1;
251         }
252         else if (d1->type == SDB_TYPE_DECIMAL) {
253                 if (d2->type == SDB_TYPE_DECIMAL)
254                         res->data.decimal = d1->data.decimal * d2->data.decimal;
255                 else if (d2->type == SDB_TYPE_DATETIME) {
256                         res->data.datetime = (sdb_time_t)(d1->data.decimal
257                                         * (double)d2->data.datetime);
258                         res->type = SDB_TYPE_DATETIME;
259                         return 0;
260                 }
261                 else
262                         return -1;
263         }
264         else if (d1->type == SDB_TYPE_DATETIME) {
265                 if (d2->type == SDB_TYPE_DATETIME)
266                         res->data.datetime = d1->data.datetime
267                                 * d2->data.datetime;
268                 else if (d2->type == SDB_TYPE_INTEGER)
269                         res->data.datetime = d1->data.datetime
270                                 * (sdb_time_t)d2->data.integer;
271                 else if (d2->type == SDB_TYPE_DECIMAL)
272                         res->data.datetime = (sdb_time_t)((double)d1->data.datetime
273                                         * d2->data.decimal);
274                 else
275                         return -1;
276         }
277         else
278                 return -1;
280         res->type = d1->type;
281         return 0;
282 } /* data_mul */
284 /* Device d1 by d2 and return the result and the remainder. */
285 static int
286 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
287                 sdb_data_t *res, sdb_data_t *rem)
289         if (d1->type == SDB_TYPE_INTEGER) {
290                 if (d2->type != SDB_TYPE_INTEGER)
291                         return -1;
292                 if (res)
293                         res->data.integer = d1->data.integer / d2->data.integer;
294                 if (rem)
295                         rem->data.integer = d1->data.integer % d2->data.integer;
296         }
297         else if (d1->type == SDB_TYPE_DECIMAL) {
298                 if (d2->type != SDB_TYPE_DECIMAL)
299                         return -1;
300                 if (res)
301                         res->data.decimal = d1->data.decimal / d2->data.decimal;
302                 if (rem)
303                         rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
304         }
305         else if (d1->type == SDB_TYPE_DATETIME) {
306                 if (d2->type == SDB_TYPE_DECIMAL) {
307                         if (res)
308                                 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
309                                                 / d2->data.decimal);
310                         if (rem) {
311                                 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
312                                 rem->data.datetime = (sdb_time_t)tmp;
313                         }
314                 }
315                 else {
316                         sdb_time_t a, b;
317                         if (d2->type == SDB_TYPE_DATETIME) {
318                                 a = d1->data.datetime;
319                                 b = d2->data.datetime;
320                         }
321                         else if (d2->type == SDB_TYPE_INTEGER) {
322                                 a = d1->data.datetime;
323                                 b = (sdb_time_t)d2->data.integer;
324                         }
325                         else
326                                 return -1;
327                         if (res)
328                                 res->data.datetime = a / b;
329                         if (rem)
330                                 rem->data.datetime = a % b;
331                 }
332         }
333         else
334                 return -1;
336         if (res)
337                 res->type = d1->type;
338         if (rem)
339                 rem->type = d1->type;
340         return 0;
341 } /* data_div */
343 /* Concatenate d1 and d2. */
344 static int
345 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
347         unsigned char *new;
348         unsigned char *s1, *s2;
349         size_t len1, len2;
351         /* TODO: support array plus element */
352         if (d1->type != d2->type)
353                 return -1;
355         if (d1->type == SDB_TYPE_STRING) {
356                 s1 = (unsigned char *)d1->data.string;
357                 s2 = (unsigned char *)d2->data.string;
358                 len1 = s1 ? strlen((char *)s1) : 0;
359                 len2 = s2 ? strlen((char *)s2) : 0;
360         }
361         else if (d1->type == SDB_TYPE_BINARY) {
362                 s1 = d1->data.binary.datum;
363                 s2 = d2->data.binary.datum;
364                 len1 = d1->data.binary.length;
365                 len2 = d2->data.binary.length;
366         }
367         else if (d1->type & SDB_TYPE_ARRAY) {
368                 size_t elem_size = sdb_data_sizeof(d1->type & 0xff);
369                 s1 = (unsigned char *)d1->data.array.values;
370                 s2 = (unsigned char *)d2->data.array.values;
371                 len1 = d1->data.array.length * elem_size;
372                 len2 = d2->data.array.length * elem_size;
373         }
374         else
375                 return -1;
377         assert(s1 && s2);
379         new = malloc(len1 + len2 + 1);
380         if (! new)
381                 return -1;
383         if (len1)
384                 memcpy(new, s1, len1);
385         if (len2)
386                 memcpy(new + len1, s2, len2);
387         new[len1 + len2] = '\0';
389         res->type = d1->type;
390         if (res->type == SDB_TYPE_STRING) {
391                 res->data.string = (char *)new;
392         }
393         else if (res->type == SDB_TYPE_BINARY) {
394                 res->data.binary.datum = new;
395                 res->data.binary.length = len1 + len2;
396         }
397         else if (d1->type & SDB_TYPE_ARRAY) {
398                 res->data.array.values = new;
399                 res->data.array.length = d1->data.array.length + d2->data.array.length;
400                 if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) {
401                         /* this leaks already copied values but there's not much we can
402                          * do and this should only happen if we're in trouble anyway */
403                         free(new);
404                         res->data.array.values = NULL;
405                         res->data.array.length = 0;
406                         return -1;
407                 }
408         }
409         return 0;
410 } /* data_concat */
412 /*
413  * public API
414  */
416 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
418 int
419 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
421         sdb_data_t tmp;
423         if ((! dst) || (! src))
424                 return -1;
426         tmp = *src;
427         if (src->type == SDB_TYPE_STRING) {
428                 if (src->data.string) {
429                         tmp.data.string = strdup(src->data.string);
430                         if (! tmp.data.string)
431                                 return -1;
432                 }
433         }
434         else if (src->type == SDB_TYPE_BINARY) {
435                 if (src->data.binary.datum) {
436                         tmp.data.binary.datum = malloc(src->data.binary.length);
437                         if (! tmp.data.binary.datum)
438                                 return -1;
439                         memcpy(tmp.data.binary.datum, src->data.binary.datum,
440                                         src->data.binary.length);
441                 }
442         }
443         else if (src->type == SDB_TYPE_REGEX) {
444                 if (src->data.re.raw) {
445                         tmp.data.re.raw = strdup(src->data.re.raw);
446                         if (! tmp.data.re.raw)
447                                 return -1;
448                         /* we need to recompile because the regex might point to
449                          * dynamically allocated memory */
450                         if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
451                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
452                                 free(tmp.data.re.raw);
453                                 return -1;
454                         }
455                 }
456                 else
457                         memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
458         }
459         else if (src->type & SDB_TYPE_ARRAY) {
460                 if (src->data.array.values) {
461                         size_t elem_size = sdb_data_sizeof(src->type & 0xff);
462                         tmp.data.array.values = calloc(src->data.array.length, elem_size);
463                         if (! tmp.data.array.values)
464                                 return -1;
465                         if (copy_array_values(&tmp, src, elem_size)) {
466                                 sdb_data_free_datum(&tmp);
467                                 return -1;
468                         }
469                 }
470         }
472         sdb_data_free_datum(dst);
473         *dst = tmp;
474         return 0;
475 } /* sdb_data_copy */
477 void
478 sdb_data_free_datum(sdb_data_t *datum)
480         if (! datum)
481                 return;
483         if (datum->type == SDB_TYPE_STRING) {
484                 if (datum->data.string)
485                         free(datum->data.string);
486                 datum->data.string = NULL;
487         }
488         else if (datum->type == SDB_TYPE_BINARY) {
489                 if (datum->data.binary.datum)
490                         free(datum->data.binary.datum);
491                 datum->data.binary.datum = NULL;
492                 datum->data.binary.length = 0;
493         }
494         else if (datum->type == SDB_TYPE_REGEX) {
495                 if (datum->data.re.raw) {
496                         free(datum->data.re.raw);
497                         regfree(&datum->data.re.regex);
498                 }
499                 datum->data.re.raw = NULL;
500                 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
501         }
502         else if (datum->type & SDB_TYPE_ARRAY) {
503                 free_array_values(datum);
504                 if (datum->data.array.values)
505                         free(datum->data.array.values);
506                 datum->data.array.values = NULL;
507                 datum->data.array.length = 0;
508         }
509 } /* sdb_data_free_datum */
511 int
512 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
514 #define CMP_NULL(a, b) \
515         do { \
516                 if (!(a) && !(b)) return 0; \
517                 if (!(a)) return -1; \
518                 if (!(b)) return 1; \
519         } while (0)
521         CMP_NULL(d1, d2);
523         if (d1->type != d2->type)
524                 return SDB_CMP(d1->type, d2->type);
526         if (d1->type == SDB_TYPE_INTEGER)
527                 return SDB_CMP(d1->data.integer, d2->data.integer);
528         else if (d1->type == SDB_TYPE_DECIMAL)
529                 return SDB_CMP(d1->data.decimal, d2->data.decimal);
530         else if (d1->type == SDB_TYPE_STRING) {
531                 CMP_NULL(d1->data.string, d2->data.string);
532                 return strcasecmp(d1->data.string, d2->data.string);
533         }
534         else if (d1->type == SDB_TYPE_DATETIME)
535                 return SDB_CMP(d1->data.datetime, d2->data.datetime);
536         else if (d1->type == SDB_TYPE_BINARY) {
537                 int diff;
539                 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
541                 /* on a common prefix, the shorter datum sorts less */
542                 if (d1->data.binary.length < d2->data.binary.length) {
543                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
544                                         d1->data.binary.length);
545                         diff = diff ? diff : -1;
546                 }
547                 else if (d1->data.binary.length > d2->data.binary.length) {
548                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
549                                         d2->data.binary.length);
550                         diff = diff ? diff : 1;
551                 }
552                 else
553                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
554                                         d1->data.binary.length);
556                 return diff;
557         }
558         else if (d1->type == SDB_TYPE_REGEX) {
559                 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
560                 return strcmp(d1->data.re.raw, d2->data.re.raw);
561         }
562         else if (d1->type & SDB_TYPE_ARRAY) {
563                 CMP_NULL(d1->data.array.values, d2->data.array.values);
564                 return array_cmp(d1, d2);
565         }
566         return -1;
567 } /* sdb_data_cmp */
569 int
570 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
572         char d1_str[sdb_data_strlen(d1) + 1];
573         char d2_str[sdb_data_strlen(d2) + 1];
575         if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
576                 /* TODO */
577                 errno = ENOTSUP;
578                 return -1;
579         }
581         if (sdb_data_isnull(d1))
582                 d1 = NULL;
583         if (sdb_data_isnull(d2))
584                 d2 = NULL;
586         CMP_NULL(d1, d2);
588         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
589                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
590         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
591                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
593         return strcasecmp(d1_str, d2_str);
594 #undef CMP_NULL
595 } /* sdb_data_strcmp */
597 _Bool
598 sdb_data_isnull(const sdb_data_t *datum)
600         if (! datum)
601                 return 1;
602         if (datum->type == SDB_TYPE_NULL)
603                 return 1;
604         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
605                 return 1;
606         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
607                 return 1;
608         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
609                 return 1;
610         if ((datum->type & SDB_TYPE_ARRAY) && (! datum->data.array.values))
611                 return 1;
612         return 0;
613 } /* sdb_data_isnull */
615 _Bool
616 sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array)
618         size_t i;
620         if (sdb_data_isnull(value) || sdb_data_isnull(array))
621                 return 0;
622         if ((value->type & SDB_TYPE_ARRAY) || (! (array->type & SDB_TYPE_ARRAY)))
623                 return 0;
624         if (value->type != (array->type & 0xff))
625                 return 0;
627         if (value->type == SDB_TYPE_INTEGER) {
628                 int64_t *v = array->data.array.values;
629                 for (i = 0; i < array->data.array.length; ++i)
630                         if (value->data.integer == v[i])
631                                 return 1;
632         }
633         else if (value->type == SDB_TYPE_DECIMAL) {
634                 double *v = array->data.array.values;
635                 for (i = 0; i < array->data.array.length; ++i)
636                         if (value->data.decimal == v[i])
637                                 return 1;
638         }
639         else if (value->type == SDB_TYPE_STRING) {
640                 char **v = array->data.array.values;
641                 for (i = 0; i < array->data.array.length; ++i)
642                         if (!strcasecmp(value->data.string, v[i]))
643                                 return 1;
644         }
645         else {
646                 /* TODO */
647                 errno = ENOTSUP;
648                 return 0;
649         }
650         return 0;
651 } /* sdb_data_inarray */
653 int
654 sdb_data_array_get(const sdb_data_t *array, size_t i, sdb_data_t *value)
656         sdb_data_t tmp = SDB_DATA_INIT;
657         int type;
659         if ((! array) || (! (array->type & SDB_TYPE_ARRAY)))
660                 return -1;
661         if (i >= array->data.array.length)
662                 return -1;
664         type = array->type & 0xff;
665         if (type == SDB_TYPE_INTEGER) {
666                 int64_t *v = array->data.array.values;
667                 tmp.data.integer = v[i];
668         }
669         else if (type == SDB_TYPE_DECIMAL) {
670                 double *v = array->data.array.values;
671                 tmp.data.decimal = v[i];
672         }
673         else if (type == SDB_TYPE_STRING) {
674                 char **v = array->data.array.values;
675                 tmp.data.string = v[i];
676         }
677         else {
678                 /* TODO */
679                 errno = ENOTSUP;
680                 return -1;
681         }
683         if (value) {
684                 *value = tmp;
685                 value->type = type;
686         }
687         return 0;
688 } /* sdb_data_array_get */
690 int
691 sdb_data_parse_op(const char *op)
693         if (! strcmp(op, "+"))
694                 return SDB_DATA_ADD;
695         else if (! strcmp(op, "-"))
696                 return SDB_DATA_SUB;
697         else if (! strcmp(op, "*"))
698                 return SDB_DATA_MUL;
699         else if (! strcmp(op, "/"))
700                 return SDB_DATA_DIV;
701         else if (! strcmp(op, "%"))
702                 return SDB_DATA_MOD;
703         else if (! strcmp(op, "||"))
704                 return SDB_DATA_CONCAT;
705         return -1;
706 } /* sdb_data_parse_op */
708 int
709 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
710                 sdb_data_t *res)
712         if ((! d1) || (! d2) || (! res))
713                 return -1;
714         if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
715                 *res = SDB_DATA_NULL;
716                 return 0;
717         }
718         switch (op) {
719                 case SDB_DATA_CONCAT:
720                         return data_concat(d1, d2, res);
721                 case SDB_DATA_ADD:
722                         return data_lin(d1, 1, d2, res);
723                 case SDB_DATA_SUB:
724                         return data_lin(d1, -1, d2, res);
725                 case SDB_DATA_MUL:
726                         return data_mul(d1, d2, res);
727                 case SDB_DATA_DIV:
728                         return data_div(d1, d2, res, NULL);
729                 case SDB_DATA_MOD:
730                         return data_div(d1, d2, NULL, res);
731         }
732         return -1;
733 } /* sdb_data_expr_eval */
735 int
736 sdb_data_expr_type(int op, int type1, int type2)
738         int types_num = (int)SDB_STATIC_ARRAY_LEN(op_matrix[0]);
740         assert(SDB_STATIC_ARRAY_LEN(op_matrix[0])
741                         == SDB_STATIC_ARRAY_LEN(op_matrix[0][0]));
743         if ((op <= 0) || (SDB_STATIC_ARRAY_LEN(op_matrix) < (size_t)op))
744                 return -1;
746         /* arrays only support concat; type has to match */
747         if ((type1 & SDB_TYPE_ARRAY) || (type2 & SDB_TYPE_ARRAY)) {
748                 if ((type1 != type2) || (op != SDB_DATA_CONCAT))
749                         return -1;
750                 return type1;
751         }
752         if ((type1 < 0) || (types_num < type1)
753                         || (type2 < 0) || (types_num < type2))
754                 return -1;
756         if ((type1 == SDB_TYPE_NULL) || (type2 == SDB_TYPE_NULL))
757                 return SDB_TYPE_NULL;
758         return op_matrix[op - 1][type1 - 1][type2 - 1];
759 } /* sdb_data_expr_type */
761 size_t
762 sdb_data_strlen(const sdb_data_t *datum)
764         if (! datum)
765                 return 0;
767         if (datum->type == SDB_TYPE_INTEGER) {
768                 /* log(64) */
769                 return 20;
770         }
771         else if (datum->type == SDB_TYPE_DECIMAL) {
772                 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
773                 return 42;
774         }
775         else if (datum->type == SDB_TYPE_STRING) {
776                 if (! datum->data.string)
777                         return 6; /* NULL */
778                 /* in the worst case, each character needs to be escaped */
779                 return 2 * strlen(datum->data.string) + 2;
780         }
781         else if (datum->type == SDB_TYPE_DATETIME) {
782                 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
783                 return 27;
784         }
785         else if (datum->type == SDB_TYPE_BINARY) {
786                 if (! datum->data.binary.datum)
787                         return 6; /* NULL */
788                 /* "\xNN" */
789                 return 4 * datum->data.binary.length + 2;
790         }
791         else if (datum->type == SDB_TYPE_REGEX) {
792                 if (! datum->data.re.raw)
793                         return 6; /* NULL */
794                 /* "/.../" */
795                 return strlen(datum->data.re.raw) + 4;
796         }
797         else if (datum->type & SDB_TYPE_ARRAY) {
798                 /* TODO */
799                 errno = ENOTSUP;
800                 return 0;
801         }
802         return 0;
803 } /* sdb_data_strlen */
805 int
806 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
808         char tmp[sdb_data_strlen(datum) + 1];
809         char *data = NULL;
810         _Bool is_null = 0;
811         int ret = -1;
813         size_t i, pos;
815         if ((! datum) || (! buf))
816                 return -1;
818         if (datum->type == SDB_TYPE_INTEGER) {
819                 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
820         }
821         else if (datum->type == SDB_TYPE_DECIMAL) {
822                 ret = snprintf(buf, buflen, "%g", datum->data.decimal);
823         }
824         else if (datum->type == SDB_TYPE_STRING) {
825                 if (! datum->data.string)
826                         is_null = 1;
827                 else {
828                         pos = 0;
829                         for (i = 0; i < strlen(datum->data.string); ++i) {
830                                 char byte = datum->data.string[i];
832                                 if ((byte == '\\') || (byte == '"')) {
833                                         tmp[pos] = '\\';
834                                         ++pos;
835                                 }
836                                 tmp[pos] = byte;
837                                 ++pos;
838                         }
839                         tmp[pos] = '\0';
840                         data = tmp;
841                 }
842         }
843         else if (datum->type == SDB_TYPE_DATETIME) {
844                 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
845                                         datum->data.datetime))
846                         return -1;
847                 tmp[sizeof(tmp) - 1] = '\0';
848                 data = tmp;
849         }
850         else if (datum->type == SDB_TYPE_BINARY) {
851                 pos = 0;
852                 for (i = 0; i < datum->data.binary.length; ++i) {
853                         int byte = (int)datum->data.binary.datum[i];
854                         char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
855                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
857                         tmp[pos] = '\\';
858                         tmp[pos + 1] = 'x';
859                         pos += 2;
861                         if (byte > 0xf) {
862                                 tmp[pos] = hex[byte >> 4];
863                                 ++pos;
864                         }
865                         tmp[pos] = hex[byte & 0xf];
866                         ++pos;
867                 }
868                 if (datum->data.binary.datum) {
869                         tmp[pos] = '\0';
870                         data = tmp;
871                 }
872                 else
873                         is_null = 1;
874         }
875         else if (datum->type == SDB_TYPE_REGEX) {
876                 if (! datum->data.re.raw)
877                         is_null = 1;
878                 else {
879                         snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
880                         data = tmp;
881                 }
882         }
883         else if (datum->type & SDB_TYPE_ARRAY) {
884                 /* TODO */
885                 errno = ENOTSUP;
886                 return -1;
887         }
889         if (is_null) {
890                 /* never quote NULL */
891                 strncpy(buf, "NULL", buflen);
892                 ret = (int)SDB_MIN(buflen, 4);
893         }
894         else if (data) {
895                 if (quoted == SDB_UNQUOTED)
896                         ret = snprintf(buf, buflen, "%s", data);
897                 else if (quoted == SDB_SINGLE_QUOTED)
898                         ret = snprintf(buf, buflen, "'%s'", data);
899                 else
900                         ret = snprintf(buf, buflen, "\"%s\"", data);
901         }
902         buf[buflen - 1] = '\0';
903         return ret;
904 } /* sdb_data_format */
906 int
907 sdb_data_parse(char *str, int type, sdb_data_t *data)
909         sdb_data_t tmp;
911         char *endptr = NULL;
913         errno = 0;
914         if (type == SDB_TYPE_INTEGER) {
915                 tmp.data.integer = strtoll(str, &endptr, 0);
916         }
917         else if (type == SDB_TYPE_DECIMAL) {
918                 tmp.data.decimal = strtod(str, &endptr);
919         }
920         else if (type == SDB_TYPE_STRING) {
921                 tmp.data.string = str;
922         }
923         else if (type == SDB_TYPE_DATETIME) {
924                 double datetime = strtod(str, &endptr);
925                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
926         }
927         else if (type == SDB_TYPE_BINARY) {
928                 /* we don't support any binary information containing 0-bytes */
929                 tmp.data.binary.length = strlen(str);
930                 tmp.data.binary.datum = (unsigned char *)str;
931         }
932         else if (type == SDB_TYPE_REGEX) {
933                 tmp.data.re.raw = strdup(str);
934                 if (! tmp.data.re.raw)
935                         return -1;
936                 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
937                                         REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
938                         sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
939                                         "expression '%s'", tmp.data.re.raw);
940                         free(tmp.data.re.raw);
941                         return -1;
942                 }
943                 if (! data) {
944                         tmp.type = SDB_TYPE_REGEX;
945                         sdb_data_free_datum(&tmp);
946                 }
947         }
948         else if (type & SDB_TYPE_ARRAY) {
949                 /* TODO */
950                 errno = ENOTSUP;
951                 return -1;
952         }
953         else {
954                 errno = EINVAL;
955                 return -1;
956         }
958         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
959                         || (type == SDB_TYPE_DATETIME)) {
960                 if (errno || (str == endptr)) {
961                         char errbuf[1024];
962                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
963                                         "'%s' as numeric value (type %i): %s", str, type,
964                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
965                         return -1;
966                 }
967                 else if (endptr && (*endptr != '\0'))
968                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
969                                         "number while parsing numeric value (type %i): %s.",
970                                         type, endptr);
971         }
973         if (data) {
974                 *data = tmp;
975                 data->type = type;
976         }
977         return 0;
978 } /* sdb_data_parse */
980 size_t
981 sdb_data_sizeof(int type)
983         sdb_data_t v;
984         if (type == SDB_TYPE_INTEGER)
985                 return sizeof(v.data.integer);
986         else if (type == SDB_TYPE_DECIMAL)
987                 return sizeof(v.data.decimal);
988         else if (type == SDB_TYPE_STRING)
989                 return sizeof(v.data.string);
990         else if (type == SDB_TYPE_DATETIME)
991                 return sizeof(v.data.datetime);
992         else if (type == SDB_TYPE_BINARY)
993                 return sizeof(v.data.binary);
994         else if (type == SDB_TYPE_REGEX)
995                 return sizeof(v.data.re);
996         return 0;
997 } /* sdb_data_sizeof */
999 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */