libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
string.c
Go to the documentation of this file.
1 
15 #include "plugins_types.h"
16 
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include "libyang.h"
22 
23 /* additional internal headers for some useful simple macros */
24 #include "common.h"
25 #include "compat.h"
26 #include "plugins_internal.h" /* LY_TYPE_*_STR */
27 
37 API LY_ERR
38 lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
39  uint32_t options, LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), uint32_t hints,
40  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
41  struct ly_err_item **err)
42 {
43  LY_ERR ret = LY_SUCCESS;
44  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
45 
46  /* init storage */
47  memset(storage, 0, sizeof *storage);
48  storage->realtype = type;
49 
50  /* check hints */
51  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
52  LY_CHECK_GOTO(ret, cleanup);
53 
54  /* length restriction of the string */
55  if (type_str->length) {
56  /* value_len is in bytes, but we need number of characters here */
57  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
58  LY_CHECK_GOTO(ret, cleanup);
59  }
60 
61  /* pattern restrictions */
62  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
63  LY_CHECK_GOTO(ret, cleanup);
64 
65  /* store canonical value */
66  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
67  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
68  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
69  LY_CHECK_GOTO(ret, cleanup);
70  } else {
71  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
72  LY_CHECK_GOTO(ret, cleanup);
73  }
74 
75 cleanup:
76  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
77  free((void *)value);
78  }
79 
80  if (ret) {
81  lyplg_type_free_simple(ctx, storage);
82  }
83  return ret;
84 }
85 
93 const struct lyplg_type_record plugins_string[] = {
94  {
95  .module = "",
96  .revision = NULL,
97  .name = LY_TYPE_STRING_STR,
98 
99  .plugin.id = "libyang 2 - string, version 1",
100  .plugin.store = lyplg_type_store_string,
101  .plugin.validate = NULL,
102  .plugin.compare = lyplg_type_compare_simple,
103  .plugin.sort = NULL,
104  .plugin.print = lyplg_type_print_simple,
105  .plugin.duplicate = lyplg_type_dup_simple,
106  .plugin.free = lyplg_type_free_simple
107  },
108  {0}
109 };
libyang context handler.
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 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...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:242
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
const char * module
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.
LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, size_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
LY_ERR lyplg_type_validate_patterns(struct lysc_pattern **patterns, const char *str, size_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
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, size_t *value_len)
Implementation of lyplg_type_print_clb for a generic simple type.
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.
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.
LY_ERR lyplg_type_compare_simple(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:1531
struct lysc_pattern ** patterns
Definition: tree_schema.h:1558
struct lysc_range * length
Definition: tree_schema.h:1557
Compiled YANG data node.
Definition: tree_schema.h:1644
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
@ LY_TYPE_STRING
Definition: tree.h:210
The main libyang public header.
API for (user) types plugins.
API LY_ERR lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT UNUSED(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: string.c:38
const struct lyplg_type_record plugins_string[]
Plugin information for string type implementation.
Definition: string.c:93
const struct lysc_type * realtype
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:532
YANG data representation.
Definition: tree_data.h:531