1 /*
2 * SysDB - src/include/core/data.h
3 * Copyright (C) 2012-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 #ifndef SDB_CORE_DATA_H
29 #define SDB_CORE_DATA_H 1
31 #include "core/time.h"
33 #include <inttypes.h>
34 #include <stddef.h>
36 #include <sys/types.h>
37 #include <regex.h>
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 enum {
44 SDB_TYPE_INTEGER = 1,
45 SDB_TYPE_DECIMAL,
46 SDB_TYPE_STRING,
47 SDB_TYPE_DATETIME,
48 SDB_TYPE_BINARY,
49 SDB_TYPE_REGEX,
50 };
52 #define SDB_TYPE_TO_STRING(t) \
53 (((t) == SDB_TYPE_INTEGER) ? "INTEGER" \
54 : ((t) == SDB_TYPE_DECIMAL) ? "DECIMAL" \
55 : ((t) == SDB_TYPE_STRING) ? "STRING" \
56 : ((t) == SDB_TYPE_DATETIME) ? "DATETIME" \
57 : ((t) == SDB_TYPE_BINARY) ? "BINARY" \
58 : ((t) == SDB_TYPE_REGEX) ? "REGEX" : "UNKNOWN")
60 /*
61 * sdb_data_t:
62 * A datum retrieved from an arbitrary data source.
63 */
64 typedef struct {
65 int type;
66 union {
67 int64_t integer; /* SDB_TYPE_INTEGER */
68 double decimal; /* SDB_TYPE_DECIMAL */
69 char *string; /* SDB_TYPE_STRING */
70 sdb_time_t datetime; /* SDB_TYPE_DATETIME */
71 struct {
72 size_t length;
73 unsigned char *datum;
74 } binary; /* SDB_TYPE_BINARY */
75 struct {
76 char *raw;
77 regex_t regex;
78 } re; /* SDB_TYPE_REGEX */
79 } data;
80 } sdb_data_t;
81 #define SDB_DATA_INIT { 0, { .integer = 0 } }
83 /*
84 * sdb_data_copy:
85 * Copy the datum stored in 'src' to the memory location pointed to by 'dst'.
86 * Any dynamic data (strings, binary data) is copied to newly allocated
87 * memory. Use, for example, sdb_data_free_datum() to free any dynamic memory
88 * stored in a datum. On error, 'dst' is unchanged. Else, any dynamic memory
89 * in 'dst' will be freed.
90 *
91 * Returns:
92 * - 0 on success
93 * - a negative value else
94 */
95 int
96 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src);
98 /*
99 * sdb_data_free_datum:
100 * Free any dynamic memory referenced by the specified datum. Does not free
101 * the memory allocated by the sdb_data_t object itself. This function must
102 * not be used if any static or stack memory is referenced from the data
103 * object.
104 */
105 void
106 sdb_data_free_datum(sdb_data_t *datum);
108 /*
109 * sdb_data_cmp:
110 * Compare two data points. A NULL datum is considered less than any non-NULL
111 * datum. On data-type mismatch, the function always returns a non-zero value.
112 *
113 * Returns:
114 * - a value less than zero if d1 compares less than d2
115 * - zero if d1 compares equal to d2
116 * - a value greater than zero if d1 compares greater than d2
117 */
118 int
119 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2);
121 /*
122 * sdb_data_strcmp:
123 * Compare the string values of two data points. A NULL datum is considered
124 * less than any non-NULL. This function works for arbitrary combination of
125 * data-types.
126 *
127 * Returns:
128 * - a value less than zero if d1 compares less than d2
129 * - zero if d1 compares equal to d2
130 * - a value greater than zero if d1 compares greater than d2
131 */
132 int
133 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2);
135 /*
136 * sdb_data_isnull:
137 * Determine whether a datum is NULL. A datum is considered to be NULL if
138 * either datum is NULL or if the string or binary datum is NULL.
139 */
140 _Bool
141 sdb_data_isnull(const sdb_data_t *datum);
143 /*
144 * Operators supported by sdb_data_eval_expr.
145 */
146 enum {
147 SDB_DATA_ADD = 1, /* addition */
148 SDB_DATA_SUB, /* substraction */
149 SDB_DATA_MUL, /* multiplication */
150 SDB_DATA_DIV, /* division */
151 SDB_DATA_MOD, /* modulo */
152 SDB_DATA_CONCAT, /* string / binary data concatenation */
153 };
155 #define SDB_DATA_OP_TO_STRING(op) \
156 (((op) == SDB_DATA_ADD) \
157 ? "+" \
158 : ((op) == SDB_DATA_SUB) \
159 ? "-" \
160 : ((op) == SDB_DATA_MUL) \
161 ? "*" \
162 : ((op) == SDB_DATA_DIV) \
163 ? "/" \
164 : ((op) == SDB_DATA_MOD) \
165 ? "%" \
166 : ((op) == SDB_DATA_CONCAT) \
167 ? "||" : "UNKNOWN")
169 /*
170 * sdb_data_expr_eval:
171 * Evaluate a simple arithmetic expression on two data points. String and
172 * binary data only support concatenation and all other data types only
173 * support the other operators. The result may be allocated dynamically and
174 * has to be freed by the caller (using sdb_data_free_datum).
175 *
176 * The data-types of d1 and d2 have to be the same, except for the following
177 * cases:
178 * - <integer> or <decimal> <mul> <datetime>
179 * - <datetime> <mul> or <div> or <mod> <integer> or <decimal>
180 *
181 * Returns:
182 * - 0 on success
183 * - a negative value else
184 */
185 int
186 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
187 sdb_data_t *res);
189 /*
190 * sdb_data_strlen:
191 * Returns a (worst-case) estimate for the number of bytes required to format
192 * the datum as a string. Does not take the terminating null byte into
193 * account.
194 */
195 size_t
196 sdb_data_strlen(const sdb_data_t *datum);
198 enum {
199 SDB_UNQUOTED = 0,
200 SDB_SINGLE_QUOTED,
201 SDB_DOUBLE_QUOTED,
202 };
204 /*
205 * sdb_data_format:
206 * Output the specified datum to the specified string using a default format.
207 * The value of 'quoted' determines whether and how non-integer and
208 * non-decimal values are quoted. If the buffer size is less than the return
209 * value of sdb_data_strlen, the datum may be truncated. The buffer will
210 * always be nul-terminated after calling this function.
211 *
212 * Returns:
213 * - the number of characters written to the buffer (excluding the terminated
214 * null byte) or the number of characters which would have been written in
215 * case the output was truncated
216 * - a negative value else
217 */
218 int
219 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted);
221 /*
222 * sdb_data_parse:
223 * Parse the specified string into a datum using the specified type. The
224 * string value is expected to be a raw value of the specified type. Integer
225 * and decimal numbers may be signed or unsigned octal (base 8, if the first
226 * character of the string is "0"), sedecimal (base 16, if the string includes
227 * the "0x" prefix), or decimal. Decimal numbers may also be "infinity" or
228 * "NaN" or may use a decimal exponent. Date-time values are expected to be
229 * specified as (floating point) number of seconds since the epoch. For string
230 * and binary data, the input string is passed to the datum. The function does
231 * not allocate new memory for that purpose. Use sdb_data_copy() if you want
232 * to do that. For regex data, the input string is copied to newly allocated
233 * memory and also compiled to a regex. Use sdb_data_free_datum() to free the
234 * dynamically allocated memory.
235 *
236 * Returns:
237 * - 0 on success
238 * - a negative value else
239 */
240 int
241 sdb_data_parse(char *str, int type, sdb_data_t *data);
243 #ifdef __cplusplus
244 } /* extern "C" */
245 #endif
247 #endif /* ! SDB_CORE_DATA_H */
249 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */