libyang 3.4.2
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
lyds_tree.c
Go to the documentation of this file.
1
15#include "plugins_types.h"
16
17#include <assert.h> /* assert */
18#include <stddef.h> /* NULL */
19#include <string.h> /* memset */
20
21#include "compat.h"
22#include "libyang.h"
23#include "ly_common.h"
24#include "tree_data_sorted.h"
25
26static void lyplg_type_free_lyds(const struct ly_ctx *ctx, struct lyd_value *value);
27
28static LY_ERR
29lyplg_type_store_lyds(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
30 size_t UNUSED(value_len), uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data),
31 uint32_t UNUSED(hints), const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage,
32 struct lys_glob_unres *UNUSED(unres), struct ly_err_item **UNUSED(err))
33{
34 int ret;
35 struct rb_node *rbt = NULL;
36 struct lyd_value_lyds_tree *val = NULL;
37
38 /* Prepare value memory. */
40 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
41
42 if (format == LY_VALUE_CANON) {
43 /* The canonical value for lyds_tree type is the empty string, so @p value is like NULL. */
44 memset(storage->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE);
45 storage->realtype = type;
46 return LY_SUCCESS;
47 } else if ((format != LY_VALUE_LYB) || (options & LYPLG_TYPE_STORE_DYNAMIC)) {
48 return LY_EVALID;
49 }
50
51 /* Create a new Red-black tree. The insertion of additional data nodes should be done via lyds_insert(). */
52 ret = lyds_create_node((struct lyd_node *)value, &rbt);
53 LY_CHECK_GOTO(ret, cleanup);
54
55 /* Set the root of the Red-black tree. */
56 storage->realtype = type;
57 val->rbt = rbt;
58
59cleanup:
60 if (ret) {
61 lyplg_type_free_lyds(ctx, storage);
62 }
63
64 return ret;
65}
66
67static void
68lyplg_type_free_lyds(const struct ly_ctx *UNUSED(ctx), struct lyd_value *value)
69{
70 struct lyd_value_lyds_tree *val = NULL;
71
72 /* The canonical value is not used at all. */
73 assert(!value->_canonical);
74 LYD_VALUE_GET(value, val);
75
76 /* Release Red-black tree. */
77 lyds_free_tree(val->rbt);
79 memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE);
80}
81
82static LY_ERR
83lyplg_type_dupl_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *original, struct lyd_value *dup)
84{
85 /* The duplicate is not created here, but at the caller, which creates a duplicate lyds tree
86 * implicitly by inserting duplicate nodes into the data tree.
87 */
88 memset(dup, 0, sizeof *dup);
89 dup->realtype = original->realtype;
90
91 return LY_SUCCESS;
92}
93
94static LY_ERR
95lyplg_type_compare_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(val1),
96 const struct lyd_value *UNUSED(val2))
97{
98 return LY_ENOT;
99}
100
101static int
102lyplg_type_sort_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(val1),
103 const struct lyd_value *UNUSED(val2))
104{
105 return 0;
106}
107
108static const void *
109lyplg_type_print_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(value),
110 LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
111{
112 if (dynamic) {
113 *dynamic = 0;
114 }
115 if (value_len) {
116 *value_len = 0;
117 }
118
119 return "";
120}
121
130 {
131 .module = "yang",
132 .revision = NULL,
133 .name = "lyds_tree",
134
135 .plugin.id = "libyang 2 - lyds_tree, version 1",
136 .plugin.store = lyplg_type_store_lyds,
137 .plugin.validate = NULL,
138 .plugin.compare = lyplg_type_compare_lyds,
139 .plugin.sort = lyplg_type_sort_lyds,
140 .plugin.print = lyplg_type_print_lyds,
141 .plugin.duplicate = lyplg_type_dupl_lyds,
142 .plugin.free = lyplg_type_free_lyds,
143 .plugin.lyb_data_len = 0
144 },
145 {0}
146};
libyang context handler.
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
@ LY_EMEM
Definition log.h:239
@ LY_ENOT
Definition log.h:251
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
Libyang full error structure.
Definition log.h:282
const char *const char * revision
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
#define LYPLG_TYPE_STORE_DYNAMIC
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_VALUE_CANON
Definition tree.h:235
@ 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
const struct lyplg_type_record plugins_lyds_tree[]
Plugin information for lyds_tree type implementation.
Definition lyds_tree.c:129
API for (user) types plugins.
struct rb_node * rbt
Definition tree_data.h:727
const struct lysc_type * realtype
Definition tree_data.h:575
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:614
const char * _canonical
Definition tree_data.h:572
Generic structure for a data node.
Definition tree_data.h:799
YANG data representation.
Definition tree_data.h:571
Special lyd_value structure for lyds tree value.
Definition tree_data.h:726