libyang 2.0.231
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 <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
36LIBYANG_API_DEF LY_ERR
37lyplg_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
95cleanup:
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
106LIBYANG_API_DEF LY_ERR
107lyplg_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
119LIBYANG_API_DEF const void *
120lyplg_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};
LIBYANG_API_DEF 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:37
LIBYANG_API_DEF 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:120
const struct lyplg_type_record plugins_boolean[]
Plugin information for boolean type implementation.
Definition: boolean.c:148
libyang context handler.
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 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
libyang's error codes returned by the libyang functions.
Definition: log.h:244
@ LYVE_DATA
Definition: log.h:281
@ LY_ENOT
Definition: log.h:258
@ LY_EVALID
Definition: log.h:252
@ LY_SUCCESS
Definition: log.h:245
Libyang full error structure.
Definition: log.h:289
LIBYANG_API_DEF 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
const char * module
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.
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's hints (if any) in the specified format.
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 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:1536
Compiled YANG data node.
Definition: tree_schema.h:1650
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:34
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:564
const char * _canonical
Definition: tree_data.h:561
YANG data representation.
Definition: tree_data.h:560