libyang  3.4.2
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
decimal64.c
Go to the documentation of this file.
1 
15 #include "plugins_types.h"
16 
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include "libyang.h"
21 
22 /* additional internal headers for some useful simple macros */
23 #include "compat.h"
24 #include "ly_common.h"
25 #include "plugins_internal.h" /* LY_TYPE_*_STR */
26 
44 static LY_ERR
45 decimal64_num2str(int64_t num, struct lysc_type_dec *type, char **str)
46 {
47  char *ret;
48 
49  /* allocate the value */
50  ret = calloc(1, LY_NUMBER_MAXLEN);
51  LY_CHECK_RET(!ret, LY_EMEM);
52 
53  if (num) {
54  int count = sprintf(ret, "%" PRId64 " ", num);
55 
56  if (((num > 0) && ((count - 1) <= type->fraction_digits)) || ((count - 2) <= type->fraction_digits)) {
57  /* we have 0. value, print the value with the leading zeros
58  * (one for 0. and also keep the correct with of num according
59  * to fraction-digits value)
60  * for (num < 0) - extra character for '-' sign */
61  count = sprintf(ret, "%0*" PRId64 " ", (num > 0) ? (type->fraction_digits + 1) : (type->fraction_digits + 2), num);
62  }
63  for (uint8_t i = type->fraction_digits, j = 1; i > 0; i--) {
64  if (j && (i > 1) && (ret[count - 2] == '0')) {
65  /* we have trailing zero to skip */
66  ret[count - 1] = '\0';
67  } else {
68  j = 0;
69  ret[count - 1] = ret[count - 2];
70  }
71  count--;
72  }
73  ret[count - 1] = '.';
74  } else {
75  /* zero */
76  sprintf(ret, "0.0");
77  }
78 
79  *str = ret;
80  return LY_SUCCESS;
81 }
82 
83 LIBYANG_API_DEF LY_ERR
84 lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
85  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
86  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
87  struct ly_err_item **err)
88 {
89  struct lysc_type_dec *type_dec = (struct lysc_type_dec *)type;
90  LY_ERR ret = LY_SUCCESS;
91  int64_t num = 0;
92  char *canon;
93 
94  /* init storage */
95  memset(storage, 0, sizeof *storage);
96  storage->realtype = type;
97 
98  if (format == LY_VALUE_LYB) {
99  /* validation */
100  if (value_len != 8) {
101  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB decimal64 value size %zu (expected 8).",
102  value_len);
103  goto cleanup;
104  }
105 
106  /* we have the decimal64 number, in host byte order */
107  memcpy(&num, value, value_len);
108  num = le64toh(num);
109  } else {
110  /* check hints */
111  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
112  LY_CHECK_GOTO(ret, cleanup);
113 
114  /* parse decimal64 value */
115  ret = lyplg_type_parse_dec64(type_dec->fraction_digits, value, value_len, &num, err);
116  LY_CHECK_GOTO(ret, cleanup);
117  }
118 
119  /* store value */
120  storage->dec64 = num;
121 
122  /* we need canonical value for the range check */
123  if (format == LY_VALUE_CANON) {
124  /* store canonical value */
125  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
126  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
127  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
128  LY_CHECK_GOTO(ret, cleanup);
129  } else {
130  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
131  LY_CHECK_GOTO(ret, cleanup);
132  }
133  } else {
134  /* generate canonical value */
135  ret = decimal64_num2str(num, type_dec, &canon);
136  LY_CHECK_GOTO(ret, cleanup);
137 
138  /* store it */
139  ret = lydict_insert_zc(ctx, canon, (const char **)&storage->_canonical);
140  LY_CHECK_GOTO(ret, cleanup);
141  }
142 
143  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
144  /* validate value */
145  if (type_dec->range) {
146  /* check range of the number */
147  ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
148  strlen(storage->_canonical), err);
149  LY_CHECK_GOTO(ret, cleanup);
150  }
151  }
152 
153 cleanup:
154  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
155  free((void *)value);
156  }
157 
158  if (ret) {
159  lyplg_type_free_simple(ctx, storage);
160  }
161  return ret;
162 }
163 
164 LIBYANG_API_DEF LY_ERR
165 lyplg_type_compare_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
166  const struct lyd_value *val2)
167 {
168  /* if type is the same, the fraction digits are, too */
169  if (val1->dec64 != val2->dec64) {
170  return LY_ENOT;
171  }
172  return LY_SUCCESS;
173 }
174 
175 LIBYANG_API_DEF int
176 lyplg_type_sort_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
177 {
178  if (val1->dec64 > val2->dec64) {
179  return 1;
180  } else if (val1->dec64 < val2->dec64) {
181  return -1;
182  } else {
183  return 0;
184  }
185 }
186 
187 LIBYANG_API_DEF const void *
188 lyplg_type_print_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
189  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
190 {
191  int64_t num = 0;
192  void *buf;
193 
194  if (format == LY_VALUE_LYB) {
195  num = htole64(value->dec64);
196  if (num == value->dec64) {
197  /* values are equal, little-endian */
198  *dynamic = 0;
199  if (value_len) {
200  *value_len = sizeof value->dec64;
201  }
202  return &value->dec64;
203  } else {
204  /* values differ, big-endian */
205  buf = calloc(1, sizeof value->dec64);
206  LY_CHECK_RET(!buf, NULL);
207 
208  *dynamic = 1;
209  if (value_len) {
210  *value_len = sizeof value->dec64;
211  }
212  memcpy(buf, &num, sizeof value->dec64);
213  return buf;
214  }
215  }
216 
217  /* use the cached canonical value */
218  if (dynamic) {
219  *dynamic = 0;
220  }
221  if (value_len) {
222  *value_len = strlen(value->_canonical);
223  }
224  return value->_canonical;
225 }
226 
234 const struct lyplg_type_record plugins_decimal64[] = {
235  {
236  .module = "",
237  .revision = NULL,
238  .name = LY_TYPE_DEC64_STR,
239 
240  .plugin.id = "libyang 2 - decimal64, version 1",
241  .plugin.store = lyplg_type_store_decimal64,
242  .plugin.validate = NULL,
243  .plugin.compare = lyplg_type_compare_decimal64,
244  .plugin.sort = lyplg_type_sort_decimal64,
245  .plugin.print = lyplg_type_print_decimal64,
246  .plugin.duplicate = lyplg_type_dup_simple,
247  .plugin.free = lyplg_type_free_simple,
248  .plugin.lyb_data_len = 8,
249  },
250  {0}
251 };
struct lysc_type * realtype
Definition: tree_data.h:575
Compiled YANG data node.
Definition: tree_schema.h:1439
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:242
#define LYPLG_TYPE_STORE_DYNAMIC
struct lysc_range * range
Definition: tree_schema.h:1328
LIBYANG_API_DECL LY_ERR lyplg_type_compare_decimal64(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in decimal64 type.
The main libyang public header.
uint8_t fraction_digits
Definition: tree_schema.h:1327
YANG data representation.
Definition: tree_data.h:571
const char * _canonical
Definition: tree_data.h:572
Libyang full error structure.
Definition: log.h:285
LIBYANG_API_DECL LY_ERR lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in decimal64 type.
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 lyplg_type_parse_dec64(uint8_t fraction_digits, const char *value, size_t value_len, int64_t *ret, struct ly_err_item **err)
Convert a string with a decimal64 value into libyang representation: ret = value * 10^fraction-digits...
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 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&#39;s hints (if any) in the specified format.
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
Definition: log.h:248
LIBYANG_API_DECL const void * lyplg_type_print_decimal64(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 the built-in decimal64 type.
LY_DATA_TYPE basetype
Definition: tree_schema.h:1305
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_DEF int lyplg_type_sort_decimal64(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for the built-in decimal64 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.
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
#define LYPLG_TYPE_STORE_ONLY
Definition: log.h:254
LIBYANG_API_DECL 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.