libyang  4.0.0
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
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 "libyang.h"
22 
23 /* additional internal headers for some useful simple macros */
24 #include "compat.h"
25 #include "ly_common.h"
26 #include "plugins_internal.h" /* LY_TYPE_*_STR */
27 
37 static void
38 lyplg_type_lyb_size_empty(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
39  uint32_t *fixed_size_bits)
40 {
41  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
42  *fixed_size_bits = 0;
43 }
44 
45 static LY_ERR
46 lyplg_type_store_empty(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
47  uint32_t options, LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), uint32_t hints,
48  const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext),
49  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
50 {
51  LY_ERR ret = LY_SUCCESS;
52  uint32_t value_size;
53 
54  /* init storage */
55  memset(storage, 0, sizeof *storage);
56  storage->realtype = type;
57 
58  /* validation */
59  if (value_size_bits) {
60  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid empty value size %" PRIu32 " b.", value_size_bits);
61  goto cleanup;
62  }
63  value_size = 0;
64 
65  /* check hints */
66  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
67  LY_CHECK_GOTO(ret, cleanup);
68 
69  /* store canonical value, it always is */
70  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
71  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
72  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
73  LY_CHECK_GOTO(ret, cleanup);
74  } else {
75  ret = lydict_insert(ctx, "", value_size, &storage->_canonical);
76  LY_CHECK_GOTO(ret, cleanup);
77  }
78 
79 cleanup:
80  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
81  free((void *)value);
82  }
83 
84  if (ret) {
85  lyplg_type_free_simple(ctx, storage);
86  }
87  return ret;
88 }
89 
97 const struct lyplg_type_record plugins_empty[] = {
98  {
99  .module = "",
100  .revision = NULL,
101  .name = LY_TYPE_EMPTY_STR,
102 
103  .plugin.id = "ly2 empty",
104  .plugin.lyb_size = lyplg_type_lyb_size_empty,
105  .plugin.store = lyplg_type_store_empty,
106  .plugin.validate_value = NULL,
107  .plugin.validate_tree = NULL,
108  .plugin.compare = lyplg_type_compare_simple,
109  .plugin.sort = lyplg_type_sort_simple,
110  .plugin.print = lyplg_type_print_simple,
111  .plugin.duplicate = lyplg_type_dup_simple,
112  .plugin.free = lyplg_type_free_simple,
113  },
114  {0}
115 };
struct lysc_type * realtype
Definition: tree_data.h:567
Compiled YANG data node.
Definition: tree_schema.h:1434
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
YANG extension compiled instance.
Definition: plugins_exts.h:436
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, uint32_t *value_size_bits)
Implementation of lyplg_type_print_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.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
#define LYPLG_TYPE_STORE_DYNAMIC
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.
The main libyang public header.
YANG data representation.
Definition: tree_data.h:563
const char * _canonical
Definition: tree_data.h:564
Libyang full error structure.
Definition: log.h:285
Definition: log.h:277
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 error structure.
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...
Definition: log.h:248
const char * module
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, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
struct lyplg_type_record plugins_empty[]
Plugin information for empty type implementation.
Definition: empty.c:97
LY_DATA_TYPE basetype
Definition: tree_schema.h:1300
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_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&#39;s hints (if any) in the specified format.
API for (user) types plugins.
libyang context handler.