libyang  2.1.30
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 "common.h"
24 #include "compat.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 lyd_value *val1, const struct lyd_value *val2)
108 {
109  if (val1->realtype != val2->realtype) {
110  return LY_ENOT;
111  }
112 
113  if (val1->boolean != val2->boolean) {
114  return LY_ENOT;
115  }
116  return LY_SUCCESS;
117 }
118 
119 LIBYANG_API_DEF const void *
120 lyplg_type_print_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
121  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
122 {
123  if (format == LY_VALUE_LYB) {
124  *dynamic = 0;
125  if (value_len) {
126  *value_len = sizeof value->boolean;
127  }
128  return &value->boolean;
129  }
130 
131  /* use the cached canonical value */
132  if (dynamic) {
133  *dynamic = 0;
134  }
135  if (value_len) {
136  *value_len = strlen(value->_canonical);
137  }
138  return value->_canonical;
139 }
140 
149  {
150  .module = "",
151  .revision = NULL,
152  .name = LY_TYPE_BOOL_STR,
153 
154  .plugin.id = "libyang 2 - boolean, version 1",
155  .plugin.store = lyplg_type_store_boolean,
156  .plugin.validate = NULL,
157  .plugin.compare = lyplg_type_compare_boolean,
158  .plugin.sort = NULL,
159  .plugin.print = lyplg_type_print_boolean,
160  .plugin.duplicate = lyplg_type_dup_simple,
161  .plugin.free = lyplg_type_free_simple,
162  .plugin.lyb_data_len = 1,
163  },
164  {0}
165 };
struct lysc_type * realtype
Definition: tree_data.h:564
Compiled YANG data node.
Definition: tree_schema.h:1414
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
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:560
const char * _canonical
Definition: tree_data.h:561
Libyang full error structure.
Definition: log.h:299
Definition: log.h:291
Definition: log.h:262
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
Definition: log.h:268
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 lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in boolean type.
Definition: boolean.c:107
struct lyplg_type_record plugins_boolean[]
Plugin information for boolean type implementation.
Definition: boolean.c:148
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
LY_DATA_TYPE basetype
Definition: tree_schema.h:1299
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.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:254
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.
libyang context handler.