libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
enumeration.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 
21 #include "libyang.h"
22 
23 /* additional internal headers for some useful simple macros */
24 #include "common.h"
25 #include "compat.h"
26 #include "plugins_internal.h" /* LY_TYPE_*_STR */
27 
37 API LY_ERR
38 lyplg_type_store_enum(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
39  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
40  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
41  struct ly_err_item **err)
42 {
43  struct lysc_type_enum *type_enum = (struct lysc_type_enum *)type;
44  LY_ERR ret = LY_SUCCESS;
46  ly_bool found = 0;
47 
48  /* init storage */
49  memset(storage, 0, sizeof *storage);
50  storage->realtype = type;
51 
52  if (format == LY_VALUE_LYB) {
53  /* validation */
54  if (value_len != 4) {
55  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB enumeration value size %zu (expected 4).",
56  value_len);
57  goto cleanup;
58  }
59 
60  /* find the matching enumeration value item */
61  LY_ARRAY_FOR(type_enum->enums, u) {
62  if (type_enum->enums[u].value == *(int32_t *)value) {
63  found = 1;
64  break;
65  }
66  }
67 
68  if (!found) {
69  /* value not found */
70  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value % " PRIi32 ".",
71  *(int32_t *)value);
72  goto cleanup;
73  }
74  } else {
75  /* check hints */
76  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
77  LY_CHECK_GOTO(ret, cleanup);
78 
79  /* find the matching enumeration value item */
80  LY_ARRAY_FOR(type_enum->enums, u) {
81  if (!ly_strncmp(type_enum->enums[u].name, value, value_len)) {
82  found = 1;
83  break;
84  }
85  }
86 
87  if (!found) {
88  /* enum not found */
89  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value \"%.*s\".", (int)value_len,
90  (char *)value);
91  goto cleanup;
92  }
93  }
94 
95  /* store value */
96  storage->enum_item = &type_enum->enums[u];
97 
98  /* store canonical value */
99  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
100  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
101  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
102  LY_CHECK_GOTO(ret, cleanup);
103  } else {
104  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
105  LY_CHECK_GOTO(ret, cleanup);
106  }
107 
108 cleanup:
109  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
110  free((void *)value);
111  }
112 
113  if (ret) {
114  lyplg_type_free_simple(ctx, storage);
115  }
116  return ret;
117 }
118 
119 API const void *
120 lyplg_type_print_enum(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 = 4;
127  }
128  return &value->enum_item->value;
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 
148 const struct lyplg_type_record plugins_enumeration[] = {
149  {
150  .module = "",
151  .revision = NULL,
152  .name = LY_TYPE_ENUM_STR,
153 
154  .plugin.id = "libyang 2 - enumeration, version 1",
155  .plugin.store = lyplg_type_store_enum,
156  .plugin.validate = NULL,
157  .plugin.compare = lyplg_type_compare_simple,
158  .plugin.sort = NULL,
159  .plugin.print = lyplg_type_print_enum,
160  .plugin.duplicate = lyplg_type_dup_simple,
161  .plugin.free = lyplg_type_free_simple
162  },
163  {0}
164 };
API const void * lyplg_type_print_enum(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: enumeration.c:120
const struct lyplg_type_record plugins_enumeration[]
Plugin information for enumeration type implementation.
Definition: enumeration.c:148
API LY_ERR lyplg_type_store_enum(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: enumeration.c:38
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_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
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.
LY_ERR lyplg_type_compare_simple(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
struct lysc_type_bitenum_item * enums
Definition: tree_schema.h:1579
Compiled YANG data node.
Definition: tree_schema.h:1644
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
Definition: tree.h:167
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
Definition: tree.h:104
@ 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