Code

data: Correctly format NULL values.
[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 #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                         /* TODO: escape special characters */
107                         sdb_strbuf_append(buf, "\"%s\"", datum->data.string);
108                         break;
109                 case SDB_TYPE_DATETIME:
110                         {
111                                 char tmp[64];
112                                 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
113                                                         datum->data.datetime))
114                                         return -1;
115                                 tmp[sizeof(tmp) - 1] = '\0';
116                                 sdb_strbuf_append(buf, "\"%s\"", tmp);
117                         }
118                         break;
119                 case SDB_TYPE_BINARY:
120                         {
121                                 char tmp[4 * datum->data.binary.length + 1];
122                                 size_t i, pos;
124                                 pos = 0;
125                                 for (i = 0; i < datum->data.binary.length; ++i) {
126                                         int byte = (int)datum->data.binary.datum[i];
127                                         char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
128                                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
130                                         tmp[pos] = '\\';
131                                         tmp[pos + 1] = 'x';
132                                         pos += 2;
134                                         if (byte > 0xf) {
135                                                 tmp[pos] = hex[byte >> 4];
136                                                 ++pos;
137                                         }
138                                         tmp[pos] = hex[byte & 0xf];
139                                         ++pos;
140                                 }
141                                 tmp[pos] = '\0';
142                                 sdb_strbuf_append(buf, "\"%s\"", tmp);
143                         }
144                         break;
145                 default:
146                         return -1;
147         }
148         return 0;
149 } /* sdb_data_format */
151 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */