libyang 2.1.148
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
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->boolean != val2->boolean) {
110 return LY_ENOT;
111 }
112 return LY_SUCCESS;
113}
114
115LIBYANG_API_DEF const void *
116lyplg_type_print_boolean(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
117 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
118{
119 if (format == LY_VALUE_LYB) {
120 *dynamic = 0;
121 if (value_len) {
122 *value_len = sizeof value->boolean;
123 }
124 return &value->boolean;
125 }
126
127 /* use the cached canonical value */
128 if (dynamic) {
129 *dynamic = 0;
130 }
131 if (value_len) {
132 *value_len = strlen(value->_canonical);
133 }
134 return value->_canonical;
135}
136
145 {
146 .module = "",
147 .revision = NULL,
148 .name = LY_TYPE_BOOL_STR,
149
150 .plugin.id = "libyang 2 - boolean, version 1",
151 .plugin.store = lyplg_type_store_boolean,
152 .plugin.validate = NULL,
153 .plugin.compare = lyplg_type_compare_boolean,
154 .plugin.sort = NULL,
155 .plugin.print = lyplg_type_print_boolean,
156 .plugin.duplicate = lyplg_type_dup_simple,
157 .plugin.free = lyplg_type_free_simple,
158 .plugin.lyb_data_len = 1,
159 },
160 {0}
161};
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:116
const struct lyplg_type_record plugins_boolean[]
Plugin information for boolean type implementation.
Definition boolean.c:144
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:248
@ LYVE_DATA
Definition log.h:285
@ LY_ENOT
Definition log.h:262
@ LY_EVALID
Definition log.h:256
@ LY_SUCCESS
Definition log.h:249
Libyang full error structure.
Definition log.h:293
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
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
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_VALUE_LYB
Definition tree.h:240
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
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