Code

data: Added sdb_data_parse_op().
[sysdb.git] / src / include / core / data.h
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, /* extended, case-insensitive POSIX 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                 : ((op) == SDB_DATA_SUB) ? "-" \
158                 : ((op) == SDB_DATA_MUL) ? "*" \
159                 : ((op) == SDB_DATA_DIV) ? "/" \
160                 : ((op) == SDB_DATA_MOD) ? "%" \
161                 : ((op) == SDB_DATA_CONCAT) ? "||" : "UNKNOWN")
163 /*
164  * sdb_data_parse_op:
165  * Parse the string representation of an operator supported by
166  * sdb_data_expr_eval.
167  *
168  * Returns:
169  *  - the ID of the operator
170  *  - a negative value in case the operator does not exist
171  */
172 int
173 sdb_data_parse_op(const char *op);
175 /*
176  * sdb_data_expr_eval:
177  * Evaluate a simple arithmetic expression on two data points. String and
178  * binary data only support concatenation and all other data types only
179  * support the other operators. The result may be allocated dynamically and
180  * has to be freed by the caller (using sdb_data_free_datum).
181  *
182  * The data-types of d1 and d2 have to be the same, except for the following
183  * cases:
184  *  - <integer> or <decimal> <mul> <datetime>
185  *  - <datetime> <mul> or <div> or <mod> <integer> or <decimal>
186  *
187  * Returns:
188  *  - 0 on success
189  *  - a negative value else
190  */
191 int
192 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
193                 sdb_data_t *res);
195 /*
196  * sdb_data_strlen:
197  * Returns a (worst-case) estimate for the number of bytes required to format
198  * the datum as a string. Does not take the terminating null byte into
199  * account.
200  */
201 size_t
202 sdb_data_strlen(const sdb_data_t *datum);
204 enum {
205         SDB_UNQUOTED = 0,
206         SDB_SINGLE_QUOTED,
207         SDB_DOUBLE_QUOTED,
208 };
210 /*
211  * sdb_data_format:
212  * Output the specified datum to the specified string using a default format.
213  * The value of 'quoted' determines whether and how non-integer and
214  * non-decimal values are quoted. If the buffer size is less than the return
215  * value of sdb_data_strlen, the datum may be truncated. The buffer will
216  * always be nul-terminated after calling this function.
217  *
218  * Returns:
219  *  - the number of characters written to the buffer (excluding the terminated
220  *    null byte) or the number of characters which would have been written in
221  *    case the output was truncated
222  *  - a negative value else
223  */
224 int
225 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted);
227 /*
228  * sdb_data_parse:
229  * Parse the specified string into a datum using the specified type. The
230  * string value is expected to be a raw value of the specified type. Integer
231  * and decimal numbers may be signed or unsigned octal (base 8, if the first
232  * character of the string is "0"), sedecimal (base 16, if the string includes
233  * the "0x" prefix), or decimal. Decimal numbers may also be "infinity" or
234  * "NaN" or may use a decimal exponent. Date-time values are expected to be
235  * specified as (floating point) number of seconds since the epoch. For string
236  * and binary data, the input string is passed to the datum. The function does
237  * not allocate new memory for that purpose. Use sdb_data_copy() if you want
238  * to do that. For regex data, the input string is copied to newly allocated
239  * memory and also compiled to a regex. Use sdb_data_free_datum() to free the
240  * dynamically allocated memory.
241  *
242  * Returns:
243  *  - 0 on success
244  *  - a negative value else
245  */
246 int
247 sdb_data_parse(char *str, int type, sdb_data_t *data);
249 #ifdef __cplusplus
250 } /* extern "C" */
251 #endif
253 #endif /* ! SDB_CORE_DATA_H */
255 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */