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 /* TODO: escape special characters */
103 sdb_strbuf_append(buf, "\"%s\"", datum->data.string);
104 break;
105 case SDB_TYPE_DATETIME:
106 {
107 char tmp[64];
108 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
109 datum->data.datetime))
110 return -1;
111 tmp[sizeof(tmp) - 1] = '\0';
112 sdb_strbuf_append(buf, "%s", tmp);
113 }
114 break;
115 case SDB_TYPE_BINARY:
116 {
117 size_t i;
118 /* TODO: improve this! */
119 sdb_strbuf_append(buf, "%s", "\"");
120 for (i = 0; i < datum->data.binary.length; ++i)
121 sdb_strbuf_append(buf, "\\%x",
122 (int)datum->data.binary.datum[i]);
123 sdb_strbuf_append(buf, "%s", "\"");
124 }
125 break;
126 default:
127 return -1;
128 }
129 return 0;
130 } /* sdb_data_format */
132 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */