libyang  2.0.7
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 <inttypes.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "libyang.h"
24 
25 /* additional internal headers for some useful simple macros */
26 #include "common.h"
27 #include "compat.h"
28 #include "plugins_internal.h" /* LY_TYPE_*_STR */
29 
47 static LY_ERR
48 decimal64_num2str(int64_t num, struct lysc_type_dec *type, char **str)
49 {
50  char *ret;
51 
52  /* allocate the value */
53  ret = calloc(1, LY_NUMBER_MAXLEN);
54  LY_CHECK_RET(!ret, LY_EMEM);
55 
56  if (num) {
57  int count = sprintf(ret, "%" PRId64 " ", num);
58  if (((num > 0) && ((count - 1) <= type->fraction_digits)) || ((count - 2) <= type->fraction_digits)) {
59  /* we have 0. value, print the value with the leading zeros
60  * (one for 0. and also keep the correct with of num according
61  * to fraction-digits value)
62  * for (num < 0) - extra character for '-' sign */
63  count = sprintf(ret, "%0*" PRId64 " ", (num > 0) ? (type->fraction_digits + 1) : (type->fraction_digits + 2), num);
64  }
65  for (uint8_t i = type->fraction_digits, j = 1; i > 0; i--) {
66  if (j && (i > 1) && (ret[count - 2] == '0')) {
67  /* we have trailing zero to skip */
68  ret[count - 1] = '\0';
69  } else {
70  j = 0;
71  ret[count - 1] = ret[count - 2];
72  }
73  count--;
74  }
75  ret[count - 1] = '.';
76  } else {
77  /* zero */
78  sprintf(ret, "0.0");
79  }
80 
81  *str = ret;
82  return LY_SUCCESS;
83 }
84 
85 API LY_ERR
86 lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
87  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
88  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
89  struct ly_err_item **err)
90 {
91  struct lysc_type_dec *type_dec = (struct lysc_type_dec *)type;
92  LY_ERR ret = LY_SUCCESS;
93  int64_t num;
94  char *canon;
95 
96  /* init storage */
97  memset(storage, 0, sizeof *storage);
98  storage->realtype = type;
99 
100  if (format == LY_VALUE_LYB) {
101  /* validation */
102  if (value_len != 8) {
103  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB decimal64 value size %zu (expected 8).",
104  value_len);
105  goto cleanup;
106  }
107 
108  /* we have the decimal64 number */
109  num = *(int64_t *)value;
110  } else {
111  /* check hints */
112  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
113  LY_CHECK_GOTO(ret, cleanup);
114 
115  /* parse decimal64 value */
116  ret = lyplg_type_parse_dec64(type_dec->fraction_digits, value, value_len, &num, err);
117  LY_CHECK_GOTO(ret, cleanup);
118  }
119 
120  /* store value */
121  storage->dec64 = num;
122 
123  /* we need canonical value for the range check */
124  if (format == LY_VALUE_CANON) {
125  /* store canonical value */
126  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
127  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
128  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
129  LY_CHECK_GOTO(ret, cleanup);
130  } else {
131  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
132  LY_CHECK_GOTO(ret, cleanup);
133  }
134  } else {
135  /* generate canonical value */
136  ret = decimal64_num2str(num, type_dec, &canon);
137  LY_CHECK_GOTO(ret, cleanup);
138 
139  /* store it */
140  ret = lydict_insert_zc(ctx, canon, (const char **)&storage->_canonical);
141  LY_CHECK_GOTO(ret, cleanup);
142  }
143 
144  if (type_dec->range) {
145  /* check range of the number */
146  ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
147  strlen(storage->_canonical), err);
148  LY_CHECK_GOTO(ret, cleanup);
149  }
150 
151 cleanup:
152  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
153  free((void *)value);
154  }
155 
156  if (ret) {
157  lyplg_type_free_simple(ctx, storage);
158  }
159  return ret;
160 }
161 
162 API LY_ERR
163 lyplg_type_compare_decimal64(const struct lyd_value *val1, const struct lyd_value *val2)
164 {
165  if (val1->realtype != val2->realtype) {
166  return LY_ENOT;
167  }
168 
169  /* if type is the same, the fraction digits are, too */
170  if (val1->dec64 != val2->dec64) {
171  return LY_ENOT;
172  }
173  return LY_SUCCESS;
174 }
175 
176 API const void *
177 lyplg_type_print_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
178  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
179 {
180  if (format == LY_VALUE_LYB) {
181  *dynamic = 0;
182  if (value_len) {
183  *value_len = sizeof value->dec64;
184  }
185  return &value->dec64;
186  }
187 
188  /* use the cached canonical value */
189  if (dynamic) {
190  *dynamic = 0;
191  }
192  if (value_len) {
193  *value_len = strlen(value->_canonical);
194  }
195  return value->_canonical;
196 }
197 
205 const struct lyplg_type_record plugins_decimal64[] = {
206  {
207  .module = "",
208  .revision = NULL,
209  .name = LY_TYPE_DEC64_STR,
210 
211  .plugin.id = "libyang 2 - decimal64, version 1",
212  .plugin.store = lyplg_type_store_decimal64,
213  .plugin.validate = NULL,
214  .plugin.compare = lyplg_type_compare_decimal64,
215  .plugin.sort = NULL,
216  .plugin.print = lyplg_type_print_decimal64,
217  .plugin.duplicate = lyplg_type_dup_simple,
218  .plugin.free = lyplg_type_free_simple
219  },
220  {0}
221 };
const struct lyplg_type_record plugins_decimal64[]
Plugin information for decimal64 type implementation.
Definition: decimal64.c:205
API 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:86
API 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:177
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
@ LYVE_DATA
Definition: log.h:279
@ LY_EMEM
Definition: log.h:244
@ LY_ENOT
Definition: log.h:256
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
API 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:163
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_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...
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 ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
struct lysc_range * range
Definition: tree_schema.h:1549
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
uint8_t fraction_digits
Definition: tree_schema.h:1548
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_VALUE_CANON
Definition: tree.h:236
@ LY_VALUE_LYB
Definition: tree.h:241
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
API for (user) types plugins.
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