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 #include "core/data.h"
30 #include <inttypes.h>
32 #include <stdlib.h>
33 #include <string.h>
35 /*
36 * public API
37 */
39 int
40 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
41 {
42 sdb_data_t tmp;
44 if ((! dst) || (! src))
45 return -1;
47 tmp = *src;
48 switch (src->type) {
49 case SDB_TYPE_STRING:
50 tmp.data.string = strdup(src->data.string);
51 if (! tmp.data.string)
52 return -1;
53 break;
54 case SDB_TYPE_BINARY:
55 tmp.data.binary.datum = malloc(src->data.binary.length);
56 if (! tmp.data.binary.datum)
57 return -1;
58 memcpy(tmp.data.binary.datum, src->data.binary.datum,
59 src->data.binary.length);
60 break;
61 }
63 *dst = tmp;
64 return 0;
65 } /* sdb_data_copy */
67 void
68 sdb_data_free_datum(sdb_data_t *datum)
69 {
70 if (! datum)
71 return;
73 switch (datum->type) {
74 case SDB_TYPE_STRING:
75 if (datum->data.string)
76 free(datum->data.string);
77 datum->data.string = NULL;
78 break;
79 case SDB_TYPE_BINARY:
80 if (datum->data.binary.datum)
81 free(datum->data.binary.datum);
82 datum->data.binary.datum = NULL;
83 datum->data.binary.length = 0;
84 break;
85 }
86 } /* sdb_data_free_datum */
88 int
89 sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
90 {
91 if ((! datum) || (! buf))
92 return -1;
94 switch (datum->type) {
95 case SDB_TYPE_INTEGER:
96 sdb_strbuf_append(buf, "%"PRIi64, datum->data.integer);
97 break;
98 case SDB_TYPE_DECIMAL:
99 sdb_strbuf_append(buf, "%a", datum->data.decimal);
100 break;
101 case SDB_TYPE_STRING:
102 if (! datum->data.string) {
103 sdb_strbuf_append(buf, "\"NULL\"");
104 return 0;
105 }
106 {
107 char tmp[2 * strlen(datum->data.string) + 1];
108 size_t i, pos;
110 pos = 0;
111 for (i = 0; i < strlen(datum->data.string); ++i) {
112 char byte = datum->data.string[i];
114 if ((byte == '\\') || (byte == '"')) {
115 tmp[pos] = '\\';
116 ++pos;
117 }
118 tmp[pos] = byte;
119 ++pos;
120 }
121 tmp[pos] = '\0';
122 sdb_strbuf_append(buf, "\"%s\"", tmp);
123 }
124 break;
125 case SDB_TYPE_DATETIME:
126 {
127 char tmp[64];
128 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
129 datum->data.datetime))
130 return -1;
131 tmp[sizeof(tmp) - 1] = '\0';
132 sdb_strbuf_append(buf, "\"%s\"", tmp);
133 }
134 break;
135 case SDB_TYPE_BINARY:
136 {
137 char tmp[4 * datum->data.binary.length + 1];
138 size_t i, pos;
140 pos = 0;
141 for (i = 0; i < datum->data.binary.length; ++i) {
142 int byte = (int)datum->data.binary.datum[i];
143 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
144 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
146 tmp[pos] = '\\';
147 tmp[pos + 1] = 'x';
148 pos += 2;
150 if (byte > 0xf) {
151 tmp[pos] = hex[byte >> 4];
152 ++pos;
153 }
154 tmp[pos] = hex[byte & 0xf];
155 ++pos;
156 }
157 tmp[pos] = '\0';
158 sdb_strbuf_append(buf, "\"%s\"", tmp);
159 }
160 break;
161 default:
162 return -1;
163 }
164 return 0;
165 } /* sdb_data_format */
167 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */