libyang  3.7.5
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
26 static void lyplg_type_free_lyds(const struct ly_ctx *ctx, struct lyd_value *value);
27 
28 static LY_ERR
29 lyplg_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. */
39  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
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 
59 cleanup:
60  if (ret) {
61  lyplg_type_free_lyds(ctx, storage);
62  }
63 
64  return ret;
65 }
66 
67 static void
68 lyplg_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 
82 static LY_ERR
83 lyplg_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 
94 static LY_ERR
95 lyplg_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 
101 static int
102 lyplg_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 
108 static const void *
109 lyplg_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 };
struct lysc_type * realtype
Definition: tree_data.h:579
Compiled YANG data node.
Definition: tree_schema.h:1439
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
struct lyplg_type_record plugins_lyds_tree[]
Plugin information for lyds_tree type implementation.
Definition: lyds_tree.c:129
Generic structure for a data node.
Definition: tree_data.h:803
lyds_free_tree(val->rbt)
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:242
#define LYPLG_TYPE_STORE_DYNAMIC
struct rb_node * rbt
Definition: tree_data.h:731
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
YANG data representation.
Definition: tree_data.h:575
const char * _canonical
Definition: tree_data.h:576
Libyang full error structure.
Definition: log.h:285
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:618
const char * module
Special lyd_value structure for lyds tree value.
Definition: tree_data.h:730
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
Definition: log.h:248
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
Definition: log.h:254
assert(!value->_canonical)