libyang  2.1.30
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 "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 (val1->realtype != val2->realtype) {
165  return LY_ENOT;
166  }
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 const void *
176 lyplg_type_print_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
177  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
178 {
179  int64_t num = 0;
180  void *buf;
181 
182  if (format == LY_VALUE_LYB) {
183  num = htole64(value->dec64);
184  if (num == value->dec64) {
185  /* values are equal, little-endian */
186  *dynamic = 0;
187  if (value_len) {
188  *value_len = sizeof value->dec64;
189  }
190  return &value->dec64;
191  } else {
192  /* values differ, big-endian */
193  buf = calloc(1, sizeof value->dec64);
194  LY_CHECK_RET(!buf, NULL);
195 
196  *dynamic = 1;
197  if (value_len) {
198  *value_len = sizeof value->dec64;
199  }
200  memcpy(buf, &num, sizeof value->dec64);
201  return buf;
202  }
203  }
204 
205  /* use the cached canonical value */
206  if (dynamic) {
207  *dynamic = 0;
208  }
209  if (value_len) {
210  *value_len = strlen(value->_canonical);
211  }
212  return value->_canonical;
213 }
214 
223  {
224  .module = "",
225  .revision = NULL,
226  .name = LY_TYPE_DEC64_STR,
227 
228  .plugin.id = "libyang 2 - decimal64, version 1",
229  .plugin.store = lyplg_type_store_decimal64,
230  .plugin.validate = NULL,
231  .plugin.compare = lyplg_type_compare_decimal64,
232  .plugin.sort = NULL,
233  .plugin.print = lyplg_type_print_decimal64,
234  .plugin.duplicate = lyplg_type_dup_simple,
235  .plugin.free = lyplg_type_free_simple,
236  .plugin.lyb_data_len = 8,
237  },
238  {0}
239 };
struct lysc_type * realtype
Definition: tree_data.h:564
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:256
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
#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:299
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:291
Definition: log.h:262
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 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:268
struct lyplg_type_record plugins_decimal64[]
Plugin information for decimal64 type implementation.
Definition: decimal64.c:222
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
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: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.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:254
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.