libyang  6.4.26
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
empty.c
Go to the documentation of this file.
1 
16 #include "plugins_types.h"
17 
18 #include <stdint.h>
19 #include <stdlib.h>
20 
21 #include "compat.h"
22 #include "dict.h"
23 #include "ly_common.h"
24 #include "plugins_internal.h"
25 
35 static void
36 lyplg_type_lyb_size_empty(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
37  uint64_t *fixed_size_bits)
38 {
39  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
40  *fixed_size_bits = 0;
41 }
42 
43 static LY_ERR
44 lyplg_type_store_empty(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
45  uint32_t options, LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), uint32_t hints,
46  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
47  struct ly_err_item **err)
48 {
49  LY_ERR ret = LY_SUCCESS;
50  uint32_t value_size;
51 
52  /* init storage */
53  memset(storage, 0, sizeof *storage);
54  storage->realtype = type;
55 
56  /* validation */
57  if (value_size_bits) {
58  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid empty value size %" PRIu64 " b.", value_size_bits);
59  goto cleanup;
60  }
61  value_size = 0;
62 
63  /* check hints */
64  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
65  LY_CHECK_GOTO(ret, cleanup);
66 
67  /* store canonical value, it always is */
68  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
69  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
70  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
71  LY_CHECK_GOTO(ret, cleanup);
72  } else {
73  ret = lydict_insert(ctx, "", value_size, &storage->_canonical);
74  LY_CHECK_GOTO(ret, cleanup);
75  }
76 
77 cleanup:
78  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
79  free((void *)value);
80  }
81 
82  if (ret) {
83  lyplg_type_free_simple(ctx, storage);
84  }
85  return ret;
86 }
87 
95 const struct lyplg_type_record plugins_empty[] = {
96  {
97  .module = "",
98  .revision = NULL,
99  .name = LY_TYPE_EMPTY_STR,
100 
101  .plugin.id = "ly2 empty",
102  .plugin.lyb_size = lyplg_type_lyb_size_empty,
103  .plugin.store = lyplg_type_store_empty,
104  .plugin.validate_value = NULL,
105  .plugin.validate_tree = NULL,
106  .plugin.compare = lyplg_type_compare_simple,
107  .plugin.sort = lyplg_type_sort_simple,
108  .plugin.print = lyplg_type_print_simple,
109  .plugin.duplicate = lyplg_type_dup_simple,
110  .plugin.free = lyplg_type_free_simple,
111  },
112  {0}
113 };
libyang dictionary
const struct lyplg_type_record plugins_empty[]
Plugin information for empty type implementation.
Definition: empty.c:95
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:252
@ LYVE_DATA
Definition: log.h:290
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
Libyang full error structure.
Definition: log.h:298
const char * module
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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 ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill an error structure.
@ LYPLG_LYB_SIZE_FIXED_BITS
LIBYANG_API_DECL const void * lyplg_type_print_simple(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
Implementation of lyplg_type_print_clb for a generic simple type.
LIBYANG_API_DEF int lyplg_type_sort_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for a generic simple type.
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.
LIBYANG_API_DECL LY_ERR lyplg_type_compare_simple(const struct ly_ctx *ctx, 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:1278
Compiled YANG data node.
Definition: tree_schema.h:1412
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93