libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
boolean.c
Go to the documentation of this file.
1 
15 #include "plugins_types.h"
16 
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "libyang.h"
23 
24 /* additional internal headers for some useful simple macros */
25 #include "common.h"
26 #include "compat.h"
27 #include "plugins_internal.h" /* LY_TYPE_*_STR */
28 
38 API LY_ERR
39 lyplg_type_store_boolean(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
40  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
41  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
42  struct ly_err_item **err)
43 {
44  LY_ERR ret = LY_SUCCESS;
45  int8_t i;
46 
47  /* init storage */
48  memset(storage, 0, sizeof *storage);
49  storage->realtype = type;
50 
51  if (format == LY_VALUE_LYB) {
52  /* validation */
53  if (value_len != 1) {
54  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB boolean value size %zu (expected 1).",
55  value_len);
56  goto cleanup;
57  }
58 
59  /* store value */
60  i = *(int8_t *)value;
61  storage->boolean = i ? 1 : 0;
62 
63  /* store canonical value */
64  ret = lydict_insert(ctx, i ? "true" : "false", 0, &storage->_canonical);
65  LY_CHECK_GOTO(ret, cleanup);
66 
67  /* success */
68  goto cleanup;
69  }
70 
71  /* check hints */
72  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
73  LY_CHECK_GOTO(ret, cleanup);
74 
75  /* validate and store the value */
76  if ((value_len == ly_strlen_const("true")) && !strncmp(value, "true", ly_strlen_const("true"))) {
77  i = 1;
78  } else if ((value_len == ly_strlen_const("false")) && !strncmp(value, "false", ly_strlen_const("false"))) {
79  i = 0;
80  } else {
81  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid boolean value \"%.*s\".", (int)value_len,
82  (char *)value);
83  goto cleanup;
84  }
85  storage->boolean = i;
86 
87  /* store canonical value, it always is */
88  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
89  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
90  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
91  LY_CHECK_GOTO(ret, cleanup);
92  } else {
93  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
94  LY_CHECK_GOTO(ret, cleanup);
95  }
96 
97 cleanup:
98  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
99  free((char *)value);
100  }
101 
102  if (ret) {
103  lyplg_type_free_simple(ctx, storage);
104  }
105  return ret;
106 }
107 
108 API LY_ERR
109 lyplg_type_compare_boolean(const struct lyd_value *val1, const struct lyd_value *val2)
110 {
111  if (val1->realtype != val2->realtype) {
112  return LY_ENOT;
113  }
114 
115  if (val1->boolean != val2->boolean) {
116  return LY_ENOT;
117  }
118  return LY_SUCCESS;
119 }
120 
121 API const void *
122 lyplg_type_print_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
123  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
124 {
125  if (format == LY_VALUE_LYB) {
126  *dynamic = 0;
127  if (value_len) {
128  *value_len = sizeof value->boolean;
129  }
130  return &value->boolean;
131  }
132 
133  /* use the cached canonical value */
134  if (dynamic) {
135  *dynamic = 0;
136  }
137  if (value_len) {
138  *value_len = strlen(value->_canonical);
139  }
140  return value->_canonical;
141 }
142 
150 const struct lyplg_type_record plugins_boolean[] = {
151  {
152  .module = "",
153  .revision = NULL,
154  .name = LY_TYPE_BOOL_STR,
155 
156  .plugin.id = "libyang 2 - boolean, version 1",
157  .plugin.store = lyplg_type_store_boolean,
158  .plugin.validate = NULL,
159  .plugin.compare = lyplg_type_compare_boolean,
160  .plugin.sort = NULL,
161  .plugin.print = lyplg_type_print_boolean,
162  .plugin.duplicate = lyplg_type_dup_simple,
163  .plugin.free = lyplg_type_free_simple
164  },
165  {0}
166 };
API const void * lyplg_type_print_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
Definition: boolean.c:122
API 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 *UNUSED(prefix_data), uint32_t hints, const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
Definition: boolean.c:39
const struct lyplg_type_record plugins_boolean[]
Plugin information for boolean type implementation.
Definition: boolean.c:150
libyang context handler.
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,...
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...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:242
@ LYVE_DATA
Definition: log.h:279
@ LY_ENOT
Definition: log.h:256
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
API 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:109
const char * module
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's hints (if any) in the specified format.
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
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.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
Compiled YANG data node.
Definition: tree_schema.h:1644
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
@ LY_VALUE_LYB
Definition: tree.h:241
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:532
YANG data representation.
Definition: tree_data.h:531