libyang  2.1.148
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
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 "common.h"
24 #include "compat.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;
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 (type_dec->range) {
144  /* check range of the number */
145  ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
146  strlen(storage->_canonical), err);
147  LY_CHECK_GOTO(ret, cleanup);
148  }
149 
150 cleanup:
151  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
152  free((void *)value);
153  }
154 
155  if (ret) {
156  lyplg_type_free_simple(ctx, storage);
157  }
158  return ret;
159 }
160 
161 LIBYANG_API_DEF LY_ERR
162 lyplg_type_compare_decimal64(const struct lyd_value *val1, const struct lyd_value *val2)
163 {
164  /* if type is the same, the fraction digits are, too */
165  if (val1->dec64 != val2->dec64) {
166  return LY_ENOT;
167  }
168  return LY_SUCCESS;
169 }
170 
171 LIBYANG_API_DEF const void *
172 lyplg_type_print_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
173  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
174 {
175  int64_t num = 0;
176  void *buf;
177 
178  if (format == LY_VALUE_LYB) {
179  num = htole64(value->dec64);
180  if (num == value->dec64) {
181  /* values are equal, little-endian */
182  *dynamic = 0;
183  if (value_len) {
184  *value_len = sizeof value->dec64;
185  }
186  return &value->dec64;
187  } else {
188  /* values differ, big-endian */
189  buf = calloc(1, sizeof value->dec64);
190  LY_CHECK_RET(!buf, NULL);
191 
192  *dynamic = 1;
193  if (value_len) {
194  *value_len = sizeof value->dec64;
195  }
196  memcpy(buf, &num, sizeof value->dec64);
197  return buf;
198  }
199  }
200 
201  /* use the cached canonical value */
202  if (dynamic) {
203  *dynamic = 0;
204  }
205  if (value_len) {
206  *value_len = strlen(value->_canonical);
207  }
208  return value->_canonical;
209 }
210 
219  {
220  .module = "",
221  .revision = NULL,
222  .name = LY_TYPE_DEC64_STR,
223 
224  .plugin.id = "libyang 2 - decimal64, version 1",
225  .plugin.store = lyplg_type_store_decimal64,
226  .plugin.validate = NULL,
227  .plugin.compare = lyplg_type_compare_decimal64,
228  .plugin.sort = NULL,
229  .plugin.print = lyplg_type_print_decimal64,
230  .plugin.duplicate = lyplg_type_dup_simple,
231  .plugin.free = lyplg_type_free_simple,
232  .plugin.lyb_data_len = 8,
233  },
234  {0}
235 };
Compiled YANG data node.
Definition: tree_schema.h:1414
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
Definition: log.h:253
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
LIBYANG_API_DEF 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 *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: decimal64.c:84
#define LYPLG_TYPE_STORE_DYNAMIC
struct lysc_range * range
Definition: tree_schema.h:1318
The main libyang public header.
uint8_t fraction_digits
Definition: tree_schema.h:1317
YANG data representation.
Definition: tree_data.h:560
const char * _canonical
Definition: tree_data.h:561
Libyang full error structure.
Definition: log.h:296
const struct lysc_type * realtype
Definition: tree_data.h:564
Definition: log.h:288
Definition: log.h:259
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_DEF LY_ERR lyplg_type_compare_decimal64(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in decimal64 type.
Definition: decimal64.c:162
Definition: log.h:265
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.
const struct lyplg_type_record plugins_decimal64[]
Plugin information for decimal64 type implementation.
Definition: decimal64.c:218
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
LY_DATA_TYPE basetype
Definition: tree_schema.h:1299
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 const void * lyplg_type_print_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
Definition: decimal64.c:172
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:251
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.
libyang context handler.
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.