ae8e662d98d22835c4dacaf23f251a5b9c1d0d29
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 sdb_data_free_datum(dst);
69 *dst = tmp;
70 return 0;
71 } /* sdb_data_copy */
73 void
74 sdb_data_free_datum(sdb_data_t *datum)
75 {
76 if (! datum)
77 return;
79 switch (datum->type) {
80 case SDB_TYPE_STRING:
81 if (datum->data.string)
82 free(datum->data.string);
83 datum->data.string = NULL;
84 break;
85 case SDB_TYPE_BINARY:
86 if (datum->data.binary.datum)
87 free(datum->data.binary.datum);
88 datum->data.binary.datum = NULL;
89 datum->data.binary.length = 0;
90 break;
91 }
92 } /* sdb_data_free_datum */
94 int
95 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
96 {
97 #define CMP_NULL(a, b) \
98 do { \
99 if (!(a) && !(b)) return 0; \
100 if (!(a)) return -1; \
101 if (!(b)) return 1; \
102 } while (0)
104 CMP_NULL(d1, d2);
106 if (d1->type != d2->type)
107 return -1;
109 #define CMP(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
110 switch (d1->type) {
111 case SDB_TYPE_INTEGER:
112 return CMP(d1->data.integer, d2->data.integer);
113 case SDB_TYPE_DECIMAL:
114 return CMP(d1->data.decimal, d2->data.decimal);
115 case SDB_TYPE_STRING:
116 CMP_NULL(d1->data.string, d2->data.string);
117 return strcasecmp(d1->data.string, d2->data.string);
118 case SDB_TYPE_DATETIME:
119 return CMP(d1->data.datetime, d2->data.datetime);
120 case SDB_TYPE_BINARY:
121 {
122 int diff;
124 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
126 /* on a common prefix, the shorter datum sorts less */
127 if (d1->data.binary.length < d2->data.binary.length) {
128 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
129 d1->data.binary.length);
130 diff = diff ? diff : -1;
131 }
132 else if (d1->data.binary.length > d2->data.binary.length) {
133 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
134 d2->data.binary.length);
135 diff = diff ? diff : 1;
136 }
137 else
138 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
139 d1->data.binary.length);
141 return diff;
142 }
143 default:
144 return -1;
145 }
146 #undef CMP
147 #undef CMP_NULL
148 } /* sdb_data_cmp */
150 size_t
151 sdb_data_strlen(const sdb_data_t *datum)
152 {
153 if (! datum)
154 return 0;
156 switch (datum->type) {
157 case SDB_TYPE_INTEGER:
158 /* log(64) */
159 return 20;
160 case SDB_TYPE_DECIMAL:
161 /* XXX: -0xN.NNNNNNp+NNN */
162 return 42;
163 case SDB_TYPE_STRING:
164 if (! datum->data.string)
165 return 6; /* "NULL" */
166 /* in the worst case, each character needs to be escaped */
167 return 2 * strlen(datum->data.string) + 2;
168 case SDB_TYPE_DATETIME:
169 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
170 return 27;
171 case SDB_TYPE_BINARY:
172 /* "\xNN" */
173 return 4 * datum->data.binary.length + 2;
174 }
175 return 0;
176 } /* sdb_data_strlen */
178 int
179 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
180 {
181 char tmp[sdb_data_strlen(datum) + 1];
182 char *data = NULL;
183 int ret = -1;
185 size_t i, pos;
187 if ((! datum) || (! buf))
188 return -1;
190 switch (datum->type) {
191 case SDB_TYPE_INTEGER:
192 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
193 break;
194 case SDB_TYPE_DECIMAL:
195 ret = snprintf(buf, buflen, "%a", datum->data.decimal);
196 break;
197 case SDB_TYPE_STRING:
198 if (! datum->data.string)
199 data = "NULL";
200 else {
201 pos = 0;
202 for (i = 0; i < strlen(datum->data.string); ++i) {
203 char byte = datum->data.string[i];
205 if ((byte == '\\') || (byte == '"')) {
206 tmp[pos] = '\\';
207 ++pos;
208 }
209 tmp[pos] = byte;
210 ++pos;
211 }
212 tmp[pos] = '\0';
213 data = tmp;
214 }
215 break;
216 case SDB_TYPE_DATETIME:
217 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
218 datum->data.datetime))
219 return -1;
220 tmp[sizeof(tmp) - 1] = '\0';
221 data = tmp;
222 break;
223 case SDB_TYPE_BINARY:
224 pos = 0;
225 for (i = 0; i < datum->data.binary.length; ++i) {
226 int byte = (int)datum->data.binary.datum[i];
227 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
228 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
230 tmp[pos] = '\\';
231 tmp[pos + 1] = 'x';
232 pos += 2;
234 if (byte > 0xf) {
235 tmp[pos] = hex[byte >> 4];
236 ++pos;
237 }
238 tmp[pos] = hex[byte & 0xf];
239 ++pos;
240 }
241 tmp[pos] = '\0';
242 data = tmp;
243 break;
244 }
246 if (data) {
247 if (quoted == SDB_UNQUOTED)
248 ret = snprintf(buf, buflen, "%s", data);
249 else if (quoted == SDB_SINGLE_QUOTED)
250 ret = snprintf(buf, buflen, "'%s'", data);
251 else
252 ret = snprintf(buf, buflen, "\"%s\"", data);
253 }
254 buf[buflen - 1] = '\0';
255 return ret;
256 } /* sdb_data_format */
258 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */