libamxp  1.4.0
Patterns C Implementation
amxp_expression.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #if !defined(__AMXP_EXPR_H__)
56 #define __AMXP_EXPR_H__
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
137 typedef struct _amxp_expr amxp_expr_t;
138 
144 typedef enum _expr_status {
153 
154 
166 typedef ssize_t (* amxp_expr_reader_t) (amxp_expr_t* expr,
167  void* buf,
168  size_t max_size);
169 
194  amxc_var_t* value,
195  const char* path,
196  void* priv);
197 
219  amxc_var_t* args,
220  amxc_var_t* ret);
221 
240 typedef bool (* amxp_expr_bool_func_t) (amxp_expr_t* expr,
241  amxc_var_t* args);
242 
243 typedef struct _amxp_expr_node amxp_expr_node_t;
244 
245 struct _amxp_expr {
246  void* scanner;
247  char* expression;
248  amxc_rbuffer_t rbuffer;
250  bool verify;
252  amxc_string_t msg;
255  amxc_lstack_t nodes;
258  void* priv;
259 };
260 
285 amxp_expr_status_t amxp_expr_new(amxp_expr_t** expr, const char* expression);
286 
350  const char* expr_fmt,
351  ...) \
352  __attribute__ ((format(printf, 2, 3)));
353 
370  const char* expr_fmt,
371  va_list args);
372 
382 void amxp_expr_delete(amxp_expr_t** expr);
383 
406  const char* expr_fmt,
407  ...) \
408  __attribute__ ((format(printf, 2, 3)));
409 
426  const char* expr_fmt,
427  va_list args);
428 
452 amxp_expr_status_t amxp_expr_init(amxp_expr_t* expr, const char* expression);
453 
465 void amxp_expr_clean(amxp_expr_t* expr);
466 
477 void amxp_expr_dump_tree(amxp_expr_t* expr);
478 
510  void* priv,
511  amxp_expr_status_t* status);
512 
529 bool amxp_expr_eval(amxp_expr_t* expr, amxp_expr_status_t* status);
530 
549  const amxc_var_t* const data,
550  amxp_expr_status_t* status);
551 
598 int amxp_expr_find_var_paths(const amxc_var_t* const var,
599  amxc_llist_t* paths,
600  const char* path);
601 
648 int amxp_expr_find_var_values(const amxc_var_t* const var,
649  amxc_htable_t* values,
650  const char* path);
651 
673 amxc_var_t* amxp_expr_find_var(const amxc_var_t* const var,
674  const char* path);
692  amxc_var_t* value,
693  const char* path,
694  void* priv);
695 
715  const amxc_set_t* const data,
716  amxp_expr_status_t* status);
717 
735  amxc_var_t* value,
736  const char* path,
737  void* priv);
738 
753 int amxp_expr_add_value_fn(const char* fn_name,
755 
770 int amxp_expr_add_bool_fn(const char* fn_name,
772 
783 const char* amxp_expr_get_string(amxp_expr_t* expr);
784 
785 #ifdef __cplusplus
786 }
787 #endif
788 
789 #endif // __AMXP_EXPR_H__
790 
const char * amxp_expr_get_string(amxp_expr_t *expr)
Returns the string representation of the given expression.
ssize_t(* amxp_expr_reader_t)(amxp_expr_t *expr, void *buf, size_t max_size)
Expression reader function.
amxp_expr_status_t amxp_expr_new(amxp_expr_t **expr, const char *expression)
Allocates and initializes an expression.
_expr_status
Expression status/error codes.
bool amxp_expr_eval(amxp_expr_t *expr, amxp_expr_status_t *status)
Evaluates an expression.
amxp_expr_status_t amxp_expr_buildf_new(amxp_expr_t **expr, const char *expr_fmt,...) __attribute__((format(printf
Allocates and initializes an expression with format-string and safety checks.
int amxp_expr_find_var_values(const amxc_var_t *const var, amxc_htable_t *values, const char *path)
Search matching variant paths in a composite variant.
amxp_expr_status_t amxp_expr_buildf(amxp_expr_t *expr, const char *expr_fmt,...) __attribute__((format(printf
Initializes an expression structure.
amxp_expr_status_t amxp_expr_status_t amxp_expr_vbuildf(amxp_expr_t *expr, const char *expr_fmt, va_list args)
va_list version of amxp_expr_buildf
bool amxp_expr_eval_set(amxp_expr_t *expr, const amxc_set_t *const data, amxp_expr_status_t *status)
Evaluates an expression against a set.
bool amxp_expr_eval_var(amxp_expr_t *expr, const amxc_var_t *const data, amxp_expr_status_t *status)
Evaluates an expression against a composite variant.
void amxp_expr_clean(amxp_expr_t *expr)
Clean-up the expression structure.
int amxp_expr_find_var_paths(const amxc_var_t *const var, amxc_llist_t *paths, const char *path)
Search matching variant paths in a composite variant.
amxp_expr_status_t amxp_expr_status_t amxp_expr_vbuildf_new(amxp_expr_t **expr, const char *expr_fmt, va_list args)
va_list version of amxp_expr_buildf_new
amxp_expr_status_t(* amxp_expr_get_field_t)(amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
Field fetcher function signature.
bool(* amxp_expr_bool_func_t)(amxp_expr_t *expr, amxc_var_t *args)
Callback function that evaluates to true or false.
int amxp_expr_add_value_fn(const char *fn_name, amxp_expr_value_func_t fn)
Adds a value calculation function.
amxp_expr_status_t(* amxp_expr_value_func_t)(amxp_expr_t *expr, amxc_var_t *args, amxc_var_t *ret)
Callback function that can "calculate" a value.
amxp_expr_status_t amxp_expr_get_field_var(amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
Variant field fetcher implementation.
amxp_expr_status_t amxp_expr_init(amxp_expr_t *expr, const char *expression)
Initializes an expression structure.
bool amxp_expr_evaluate(amxp_expr_t *expr, amxp_expr_get_field_t fn, void *priv, amxp_expr_status_t *status)
Evaluates an expression.
int amxp_expr_add_bool_fn(const char *fn_name, amxp_expr_bool_func_t fn)
Adds a boolean evaluation function.
void amxp_expr_dump_tree(amxp_expr_t *expr)
Dumps the binary tree in dot formatted text file to stdout.
amxp_expr_status_t amxp_expr_get_field_set(amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
Flag field fetcher implementation for sets.
amxc_var_t * amxp_expr_find_var(const amxc_var_t *const var, const char *path)
Search a matching variant and returns a pointer to that variant.
enum _expr_status amxp_expr_status_t
Expression status/error codes.
void amxp_expr_delete(amxp_expr_t **expr)
Deletes a previously allocated expression structure.
@ amxp_expr_status_syntax_error
@ amxp_expr_status_invalid_value
@ amxp_expr_status_field_not_found
@ amxp_expr_status_unknown_error
@ amxp_expr_status_ok
@ amxp_expr_status_function_not_found
@ amxp_expr_status_invalid_regexp
char * expression
amxc_rbuffer_t rbuffer
amxp_expr_get_field_t get_field
amxp_expr_reader_t reader
amxc_string_t msg
amxc_lstack_t nodes
amxp_expr_status_t status
static amxc_string_t path