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 "core/data.h"
34 #include <inttypes.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 /*
41 * public API
42 */
44 int
45 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
46 {
47 sdb_data_t tmp;
49 if ((! dst) || (! src))
50 return -1;
52 tmp = *src;
53 switch (src->type) {
54 case SDB_TYPE_STRING:
55 tmp.data.string = strdup(src->data.string);
56 if (! tmp.data.string)
57 return -1;
58 break;
59 case SDB_TYPE_BINARY:
60 tmp.data.binary.datum = malloc(src->data.binary.length);
61 if (! tmp.data.binary.datum)
62 return -1;
63 memcpy(tmp.data.binary.datum, src->data.binary.datum,
64 src->data.binary.length);
65 break;
66 }
68 *dst = tmp;
69 return 0;
70 } /* sdb_data_copy */
72 void
73 sdb_data_free_datum(sdb_data_t *datum)
74 {
75 if (! datum)
76 return;
78 switch (datum->type) {
79 case SDB_TYPE_STRING:
80 if (datum->data.string)
81 free(datum->data.string);
82 datum->data.string = NULL;
83 break;
84 case SDB_TYPE_BINARY:
85 if (datum->data.binary.datum)
86 free(datum->data.binary.datum);
87 datum->data.binary.datum = NULL;
88 datum->data.binary.length = 0;
89 break;
90 }
91 } /* sdb_data_free_datum */
93 int
94 sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2)
95 {
96 #define CMP_NULL(a, b) \
97 do { \
98 if (!(a) && !(b)) return 0; \
99 if (!(a)) return -1; \
100 if (!(b)) return 1; \
101 } while (0)
103 CMP_NULL(d1, d2);
105 if (d1->type != d2->type)
106 return -1;
108 #define CMP(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
109 switch (d1->type) {
110 case SDB_TYPE_INTEGER:
111 return CMP(d1->data.integer, d2->data.integer);
112 case SDB_TYPE_DECIMAL:
113 return CMP(d1->data.decimal, d2->data.decimal);
114 case SDB_TYPE_STRING:
115 CMP_NULL(d1->data.string, d2->data.string);
116 return strcasecmp(d1->data.string, d2->data.string);
117 case SDB_TYPE_DATETIME:
118 return CMP(d1->data.datetime, d2->data.datetime);
119 case SDB_TYPE_BINARY:
120 {
121 int diff;
123 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
125 /* on a common prefix, the shorter datum sorts less */
126 if (d1->data.binary.length < d2->data.binary.length) {
127 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
128 d1->data.binary.length);
129 diff = diff ? diff : -1;
130 }
131 else if (d1->data.binary.length > d2->data.binary.length) {
132 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
133 d2->data.binary.length);
134 diff = diff ? diff : 1;
135 }
136 else
137 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
138 d1->data.binary.length);
140 return diff;
141 }
142 default:
143 return -1;
144 }
145 #undef CMP
146 #undef CMP_NULL
147 } /* sdb_data_cmp */
149 size_t
150 sdb_data_strlen(const sdb_data_t *datum)
151 {
152 if (! datum)
153 return 0;
155 switch (datum->type) {
156 case SDB_TYPE_INTEGER:
157 /* log(64) */
158 return 20;
159 case SDB_TYPE_DECIMAL:
160 /* XXX: -0xN.NNNNNNp+NNN */
161 return 42;
162 case SDB_TYPE_STRING:
163 if (! datum->data.string)
164 return 6; /* "NULL" */
165 /* in the worst case, each character needs to be escaped */
166 return 2 * strlen(datum->data.string) + 2;
167 case SDB_TYPE_DATETIME:
168 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
169 return 27;
170 case SDB_TYPE_BINARY:
171 /* "\xNN" */
172 return 4 * datum->data.binary.length + 2;
173 }
174 return 0;
175 } /* sdb_data_strlen */
177 int
178 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
179 {
180 char tmp[sdb_data_strlen(datum) + 1];
181 char *data = NULL;
182 int ret = -1;
184 size_t i, pos;
186 if ((! datum) || (! buf))
187 return -1;
189 switch (datum->type) {
190 case SDB_TYPE_INTEGER:
191 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
192 break;
193 case SDB_TYPE_DECIMAL:
194 ret = snprintf(buf, buflen, "%a", datum->data.decimal);
195 break;
196 case SDB_TYPE_STRING:
197 if (! datum->data.string)
198 data = "NULL";
199 else {
200 pos = 0;
201 for (i = 0; i < strlen(datum->data.string); ++i) {
202 char byte = datum->data.string[i];
204 if ((byte == '\\') || (byte == '"')) {
205 tmp[pos] = '\\';
206 ++pos;
207 }
208 tmp[pos] = byte;
209 ++pos;
210 }
211 tmp[pos] = '\0';
212 data = tmp;
213 }
214 break;
215 case SDB_TYPE_DATETIME:
216 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
217 datum->data.datetime))
218 return -1;
219 tmp[sizeof(tmp) - 1] = '\0';
220 data = tmp;
221 break;
222 case SDB_TYPE_BINARY:
223 pos = 0;
224 for (i = 0; i < datum->data.binary.length; ++i) {
225 int byte = (int)datum->data.binary.datum[i];
226 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
227 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
229 tmp[pos] = '\\';
230 tmp[pos + 1] = 'x';
231 pos += 2;
233 if (byte > 0xf) {
234 tmp[pos] = hex[byte >> 4];
235 ++pos;
236 }
237 tmp[pos] = hex[byte & 0xf];
238 ++pos;
239 }
240 tmp[pos] = '\0';
241 data = tmp;
242 break;
243 }
245 if (data) {
246 if (quoted == SDB_UNQUOTED)
247 ret = snprintf(buf, buflen, "%s", data);
248 else if (quoted == SDB_SINGLE_QUOTED)
249 ret = snprintf(buf, buflen, "'%s'", data);
250 else
251 ret = snprintf(buf, buflen, "\"%s\"", data);
252 }
253 buf[buflen - 1] = '\0';
254 return ret;
255 } /* sdb_data_format */
257 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */