libyang  4.0.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
time_period.c
Go to the documentation of this file.
1 
16 #include "plugins_types.h"
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "libyang.h"
22 
23 #include "compat.h"
24 #include "ly_common.h"
25 #include "plugins_internal.h" /* LY_TYPE_*_STR */
26 
41 static int
42 lyplg_type_sort_time_period(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
43 {
44  const char *value1, *value2;
45  char unit1, unit2;
46  long v1, v2;
47 
48  value1 = lyd_value_get_canonical(ctx, val1);
49  value2 = lyd_value_get_canonical(ctx, val2);
50 
51  /* get the units (last character) and the values (all characters except the last one) */
52  unit1 = value1[strlen(value1) - 1];
53  unit2 = value2[strlen(value2) - 1];
54  v1 = strtol(value1, NULL, 10);
55  v2 = strtol(value2, NULL, 10);
56 
57  /* descending order */
58  if (unit1 == unit2) {
59  if (v1 > v2) {
60  return -1;
61  } else if (v1 == v2) {
62  return 0;
63  } else {
64  return 1;
65  }
66  } else if (unit1 == 'm') {
67  return -1;
68  } else if ((unit1 == 'w') && (unit2 != 'm')) {
69  return -1;
70  } else if ((unit1 == 'd') && (unit2 == 'h')) {
71  return -1;
72  } else {
73  return 1;
74  }
75 }
76 
85  {
86  .module = "libnetconf2-netconf-server",
87  .revision = "2024-07-09",
88  .name = "time-period",
89 
90  .plugin.id = "ly2 time-period",
91  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
92  .plugin.store = lyplg_type_store_string,
93  .plugin.validate_value = NULL,
94  .plugin.validate_tree = NULL,
95  .plugin.compare = lyplg_type_compare_simple,
96  .plugin.sort = lyplg_type_sort_time_period,
97  .plugin.print = lyplg_type_print_simple,
98  .plugin.duplicate = lyplg_type_dup_simple,
99  .plugin.free = lyplg_type_free_simple,
100  },
101  {0}
102 };
LIBYANG_API_DECL const void * lyplg_type_print_simple(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, uint32_t *value_size_bits)
Implementation of lyplg_type_print_clb for a generic simple type.
LIBYANG_API_DECL LY_ERR lyplg_type_compare_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
struct lyplg_type_record plugins_time_period[]
Plugin information for time-period type implementation.
Definition: time_period.c:84
The main libyang public header.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes...
YANG data representation.
Definition: tree_data.h:563
LIBYANG_API_DECL const char * lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Get the (canonical) value of a lyd_value.
const char * module
LIBYANG_API_DECL LY_ERR lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, const struct lysc_ext_instance *top_ext, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in string type.
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.
API for (user) types plugins.
libyang context handler.