Code

store_expr: Added framework for arithmetic expressions.
[sysdb.git] / src / core / store_expr.c
1 /*
2  * SysDB - src/core/store_expr.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 /*
29  * This module implements expressions which may be executed in the store.
30  */
32 #if HAVE_CONFIG_H
33 #       include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "sysdb.h"
37 #include "core/store-private.h"
38 #include "core/object.h"
40 #include <assert.h>
42 /*
43  * private data types
44  */
46 struct sdb_store_expr {
47         sdb_object_t super;
49         int type;
50         sdb_store_expr_t *left;
51         sdb_store_expr_t *right;
53         sdb_data_t data;
54 };
56 /*
57  * private data types
58  */
60 static int
61 expr_init(sdb_object_t *obj, va_list ap)
62 {
63         int type = va_arg(ap, int);
64         sdb_store_expr_t *left  = va_arg(ap, sdb_store_expr_t *);
65         sdb_store_expr_t *right = va_arg(ap, sdb_store_expr_t *);
66         const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
68         sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
70         if (! type) {
71                 if (! value)
72                         return -1;
73         } else {
74                 if (value)
75                         return -1;
76                 if ((! left) || (! right))
77                         return -1;
78         }
80         if (value)
81                 if (sdb_data_copy(&expr->data, value))
82                         return -1;
84         sdb_object_ref(SDB_OBJ(left));
85         sdb_object_ref(SDB_OBJ(right));
87         expr->type  = type;
88         expr->left  = left;
89         expr->right = right;
90         return 0;
91 } /* expr_init */
93 static void
94 expr_destroy(sdb_object_t *obj)
95 {
96         sdb_store_expr_t *expr = SDB_STORE_EXPR(obj);
97         sdb_object_deref(SDB_OBJ(expr->left));
98         sdb_object_deref(SDB_OBJ(expr->right));
100         if (expr->data.type)
101                 sdb_data_free_datum(&expr->data);
102 } /* expr_destroy */
104 static sdb_type_t expr_type = {
105         /* size = */ sizeof(sdb_store_expr_t),
106         /* init = */ expr_init,
107         /* destroy = */ expr_destroy,
108 };
110 /*
111  * public API
112  */
114 sdb_store_expr_t *
115 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right)
117         return SDB_STORE_EXPR(sdb_object_create("store-expr", expr_type,
118                                 op, left, right, NULL));
119 } /* sdb_store_expr_create */
121 sdb_store_expr_t *
122 sdb_store_expr_constvalue(const sdb_data_t *value)
124         return SDB_STORE_EXPR(sdb_object_create("store-constvalue", expr_type,
125                                 0, NULL, NULL, value));
126 } /* sdb_store_expr_constvalue */
128 int
129 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_data_t *res)
131         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
133         if ((! expr) || (! res))
134                 return -1;
136         if (! expr->type)
137                 return sdb_data_copy(res, &expr->data);
139         if (sdb_store_expr_eval(expr->left, &v1))
140                 return -1;
141         if (sdb_store_expr_eval(expr->right, &v2)) {
142                 sdb_data_free_datum(&v1);
143                 return -1;
144         }
146         if (sdb_data_expr_eval(expr->type, &v1, &v2, res)) {
147                 sdb_data_free_datum(&v1);
148                 sdb_data_free_datum(&v2);
149                 return -1;
150         }
151         return 0;
152 } /* sdb_store_expr_eval */
154 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */