libyang  3.1.0
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
boolean.c
Go to the documentation of this file.
1 
15 #include "plugins_types.h"
16 
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include "libyang.h"
21 
22 /* additional internal headers for some useful simple macros */
23 #include "compat.h"
24 #include "ly_common.h"
25 #include "plugins_internal.h" /* LY_TYPE_*_STR */
26 
36 LIBYANG_API_DEF LY_ERR
37 lyplg_type_store_boolean(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
38  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
39  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
40  struct ly_err_item **err)
41 {
42  LY_ERR ret = LY_SUCCESS;
43  int8_t i;
44 
45  /* init storage */
46  memset(storage, 0, sizeof *storage);
47  storage->realtype = type;
48 
49  if (format == LY_VALUE_LYB) {
50  /* validation */
51  if (value_len != 1) {
52  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB boolean value size %zu (expected 1).",
53  value_len);
54  goto cleanup;
55  }
56 
57  /* store value */
58  i = *(int8_t *)value;
59  storage->boolean = i ? 1 : 0;
60 
61  /* store canonical value, it always is */
62  ret = lydict_insert(ctx, i ? "true" : "false", 0, &storage->_canonical);
63  LY_CHECK_GOTO(ret, cleanup);
64 
65  /* success */
66  goto cleanup;
67  }
68 
69  /* check hints */
70  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
71  LY_CHECK_GOTO(ret, cleanup);
72 
73  /* validate and store the value */
74  if ((value_len == ly_strlen_const("true")) && !strncmp(value, "true", ly_strlen_const("true"))) {
75  i = 1;
76  } else if ((value_len == ly_strlen_const("false")) && !strncmp(value, "false", ly_strlen_const("false"))) {
77  i = 0;
78  } else {
79  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid boolean value \"%.*s\".", (int)value_len,
80  (char *)value);
81  goto cleanup;
82  }
83  storage->boolean = i;
84 
85  /* store canonical value, it always is */
86  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
87  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
88  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
89  LY_CHECK_GOTO(ret, cleanup);
90  } else {
91  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
92  LY_CHECK_GOTO(ret, cleanup);
93  }
94 
95 cleanup:
96  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
97  free((char *)value);
98  }
99 
100  if (ret) {
101  lyplg_type_free_simple(ctx, storage);
102  }
103  return ret;
104 }
105 
106 LIBYANG_API_DEF LY_ERR
107 lyplg_type_compare_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
108 {
109  if (val1->boolean != val2->boolean) {
110  return LY_ENOT;
111  }
112  return LY_SUCCESS;
113 }
114 
115 LIBYANG_API_DEF int
116 lyplg_type_sort_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
117 {
118  if (val1->boolean > val2->boolean) {
119  return 1;
120  } else if (val1->boolean < val2->boolean) {
121  return -1;
122  } else {
123  return 0;
124  }
125 }
126 
127 LIBYANG_API_DEF const void *
128 lyplg_type_print_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
129  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
130 {
131  if (format == LY_VALUE_LYB) {
132  *dynamic = 0;
133  if (value_len) {
134  *value_len = sizeof value->boolean;
135  }
136  return &value->boolean;
137  }
138 
139  /* use the cached canonical value */
140  if (dynamic) {
141  *dynamic = 0;
142  }
143  if (value_len) {
144  *value_len = strlen(value->_canonical);
145  }
146  return value->_canonical;
147 }
148 
156 const struct lyplg_type_record plugins_boolean[] = {
157  {
158  .module = "",
159  .revision = NULL,
160  .name = LY_TYPE_BOOL_STR,
161 
162  .plugin.id = "libyang 2 - boolean, version 1",
163  .plugin.store = lyplg_type_store_boolean,
164  .plugin.validate = NULL,
165  .plugin.compare = lyplg_type_compare_boolean,
166  .plugin.sort = lyplg_type_sort_boolean,
167  .plugin.print = lyplg_type_print_boolean,
168  .plugin.duplicate = lyplg_type_dup_simple,
169  .plugin.free = lyplg_type_free_simple,
170  .plugin.lyb_data_len = 1,
171  },
172  {0}
173 };
struct lysc_type * realtype
Definition: tree_data.h:575
Compiled YANG data node.
Definition: tree_schema.h:1439
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
#define LYPLG_TYPE_STORE_DYNAMIC
The main libyang public header.
YANG data representation.
Definition: tree_data.h:571
const char * _canonical
Definition: tree_data.h:572
Libyang full error structure.
Definition: log.h:285
Definition: log.h:277
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, size_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser&#39;s hints (if any) in the specified format.
const char * module
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
LIBYANG_API_DECL LY_ERR lyplg_type_compare_boolean(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in boolean type.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
Definition: log.h:248
LY_DATA_TYPE basetype
Definition: tree_schema.h:1305
LIBYANG_API_DEF int lyplg_type_sort_boolean(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for the built-in boolean type.
LIBYANG_API_DECL LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
LIBYANG_API_DECL LY_ERR lyplg_type_store_boolean(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in boolean type.
LIBYANG_API_DECL void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for a generic simple type.
LIBYANG_API_DECL const void * lyplg_type_print_boolean(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Implementation of lyplg_type_print_clb for the built-in boolean type.
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
Definition: log.h:254