25 #include "ly_common.h"
26 #include "plugins_internal.h"
37 static LY_ERR lyplg_type_validate_value_decimal64(
const struct ly_ctx *ctx,
const struct lysc_type *type,
49 decimal64_num2str(int64_t num,
struct lysc_type_dec *type,
char **str)
54 ret = calloc(1, LY_NUMBER_MAXLEN);
58 int count = sprintf(ret,
"%" PRId64
" ", num);
68 if (j && (i > 1) && (ret[count - 2] ==
'0')) {
70 ret[count - 1] =
'\0';
73 ret[count - 1] = ret[count - 2];
89 uint32_t *fixed_size_bits)
92 *fixed_size_bits = 64;
96 lyplg_type_store_decimal64(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
97 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
108 memset(storage, 0,
sizeof *storage);
113 LY_CHECK_GOTO(ret, cleanup);
117 memcpy(&num, value, value_size);
122 LY_CHECK_GOTO(ret, cleanup);
126 LY_CHECK_GOTO(ret, cleanup);
130 storage->dec64 = num;
137 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
138 LY_CHECK_GOTO(ret, cleanup);
141 LY_CHECK_GOTO(ret, cleanup);
145 ret = decimal64_num2str(num, type_dec, &canon);
146 LY_CHECK_GOTO(ret, cleanup);
150 LY_CHECK_GOTO(ret, cleanup);
155 ret = lyplg_type_validate_value_decimal64(ctx, type, storage, err);
156 LY_CHECK_GOTO(ret, cleanup);
160 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
174 lyplg_type_validate_value_decimal64(
const struct ly_ctx *UNUSED(ctx),
const struct lysc_type *type,
181 LY_CHECK_ARG_RET(NULL, type, storage, err,
LY_EINVAL);
183 num = storage->dec64;
185 if (type_dec->
range) {
196 lyplg_type_compare_decimal64(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
200 if (val1->dec64 != val2->dec64) {
207 lyplg_type_sort_decimal64(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
209 if (val1->dec64 > val2->dec64) {
211 }
else if (val1->dec64 < val2->dec64) {
220 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_size_bits)
226 num = htole64(value->dec64);
227 if (num == value->dec64) {
230 if (value_size_bits) {
231 *value_size_bits =
sizeof value->dec64 * 8;
233 return &value->dec64;
236 buf = calloc(1,
sizeof value->dec64);
237 LY_CHECK_RET(!buf, NULL);
240 if (value_size_bits) {
241 *value_size_bits =
sizeof value->dec64 * 8;
243 memcpy(buf, &num,
sizeof value->dec64);
252 if (value_size_bits) {
253 *value_size_bits = strlen(value->
_canonical) * 8;
269 .name = LY_TYPE_DEC64_STR,
271 .plugin.id =
"ly2 decimal64",
272 .plugin.lyb_size = lyplg_type_lyb_size_decimal64,
273 .plugin.store = lyplg_type_store_decimal64,
274 .plugin.validate_value = lyplg_type_validate_value_decimal64,
275 .plugin.validate_tree = NULL,
276 .plugin.compare = lyplg_type_compare_decimal64,
277 .plugin.sort = lyplg_type_sort_decimal64,
278 .plugin.print = lyplg_type_print_decimal64,
struct lysc_type * realtype
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
YANG extension compiled instance.
LY_ERR
libyang's error codes returned by the libyang functions.
uint8_t ly_bool
Type to indicate boolean value.
#define LYPLG_TYPE_STORE_DYNAMIC
LIBYANG_API_DECL LY_ERR lyplg_type_parse_dec64(uint8_t fraction_digits, const char *value, uint32_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...
struct lysc_range * range
The main libyang public header.
YANG data representation.
Libyang full error structure.
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 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.
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_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.
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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.
API for (user) types plugins.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, uint32_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
#define LYPLG_TYPE_STORE_ONLY