libamxc  1.10.3
C Generic Data Containers
variant_timestamp.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdlib.h>
56 
57 #include <amxc_variant_priv.h>
58 
59 static int variant_ts_init(amxc_var_t* const var) {
60  return amxc_ts_parse(&var->data.ts, "0001-01-01T00:00:00Z", 20);
61 }
62 
63 
64 static int variant_ts_to_string(amxc_var_t* const dest,
65  const amxc_var_t* const src) {
66  int retval = -1;
67  dest->data.s = (char*) calloc(40, sizeof(char));
68 
69  when_null(dest->data.s, exit);
70 
71  retval = amxc_ts_format(&src->data.ts, dest->data.s, 40);
72  retval = retval != 0 ? 0 : -1;
73 
74 exit:
75  return retval;
76 }
77 
78 static int variant_ts_to_int(amxc_var_t* const dest,
79  const amxc_var_t* const src) {
80  dest->data.i64 = src->data.ts.sec;
81  return 0;
82 }
83 
84 static int variant_ts_to_double(amxc_var_t* const dest,
85  const amxc_var_t* const src) {
86  dest->data.d = src->data.ts.sec + src->data.ts.nsec / 1000000000;
87  return 0;
88 }
89 
90 static int variant_ts_convert_to(amxc_var_t* const dest,
91  const amxc_var_t* const src) {
92  int retval = -1;
93 
96  variant_ts_to_string, // cstring_t
97  NULL, // int8_t
98  NULL, // int16_t
99  NULL, // int32_t
100  variant_ts_to_int, // int64_t
101  NULL, // uint8_t
102  NULL, // uint16_t
103  NULL, // uint32_t
104  NULL, // uint64_t
105  NULL, // float
106  variant_ts_to_double, // double
107  NULL, // bool
108  amxc_var_default_convert_to_list, // amxc_llist_t
109  amxc_var_default_convert_to_htable, // amxc_htable_t
110  NULL, // fd_t
111  amxc_var_default_copy, // amxc_ts_t
112  variant_ts_to_string, // csvstring_t
113  variant_ts_to_string, // ssvstring_t
114  amxc_var_default_copy, // any
115  };
116 
117  if(dest->type_id >= AMXC_VAR_ID_CUSTOM_BASE) {
118  goto exit;
119  }
120 
121  if(convfn[dest->type_id] != NULL) {
122  if(dest->type_id == AMXC_VAR_ID_ANY) {
124  }
125  retval = convfn[dest->type_id](dest, src);
126  }
127 
128 exit:
129  return retval;
130 }
131 
132 static int variant_ts_compare(const amxc_var_t* const lval,
133  const amxc_var_t* const rval,
134  int* const result) {
135  *result = amxc_ts_compare(&lval->data.ts, &rval->data.ts);
136 
137  return 0;
138 }
139 
142  .del = NULL,
143  .copy = amxc_var_default_copy,
144  .move = amxc_var_default_move,
145  .convert_from = NULL,
146  .convert_to = variant_ts_convert_to,
147  .compare = variant_ts_compare,
148  .get_key = NULL,
149  .set_key = NULL,
150  .get_index = NULL,
151  .set_index = NULL,
152  .type_id = 0,
153  .hit = { .ait = NULL, .key = NULL, .next = NULL },
155 };
156 
157 CONSTRUCTOR static void amxc_var_ts_init(void) {
159 }
160 
161 DESTRUCTOR static void amxc_var_ts_cleanup(void) {
163 }
164 
166  int retval = -1;
167  when_null(var, exit);
168  when_null(ts, exit);
169 
171 
172  var->data.ts.sec = ts->sec;
173  var->data.ts.nsec = ts->nsec;
174  var->data.ts.offset = ts->offset;
175 
176  retval = 0;
177 
178 exit:
179  return retval;
180 }
181 
183  amxc_ts_t* ts = NULL;
184  amxc_var_t variant;
185  when_null(var, exit);
186 
187  amxc_var_init(&variant);
189  ts = (amxc_ts_t*) calloc(1, sizeof(amxc_ts_t));
190  when_null(ts, exit);
191 
192  ts->sec = variant.data.ts.sec;
193  ts->nsec = variant.data.ts.nsec;
194  ts->offset = variant.data.ts.offset;
195 
196  amxc_var_clean(&variant);
197 
198 exit:
199  return ts;
200 }
201 
203  const amxc_ts_t* retval = NULL;
204  when_null(var, exit);
206 
207  retval = &var->data.ts;
208 
209 exit:
210  return retval;
211 }
212 
214  amxc_var_t* subvar = NULL;
215 
216  when_null(var, exit);
217  subvar = amxc_var_add_new(var);
218  when_null(subvar, exit);
219 
220  if(ts == NULL) {
222  } else {
223  if(amxc_var_set_amxc_ts_t(subvar, ts) != 0) {
224  amxc_var_delete(&subvar);
225  }
226  }
227 
228 exit:
229  return subvar;
230 }
231 
233  const char* key,
234  amxc_ts_t* ts) {
235  amxc_var_t* subvar = NULL;
236 
237  when_null(var, exit);
238  subvar = amxc_var_add_new_key(var, key);
239  when_null(subvar, exit);
240 
241  if(ts == NULL) {
243  } else {
244  if(amxc_var_set_amxc_ts_t(subvar, ts) != 0) {
245  amxc_var_delete(&subvar);
246  }
247  }
248 
249 exit:
250  return subvar;
251 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_true(x, l)
Definition: amxc_macros.h:134
#define CONSTRUCTOR
Definition: amxc_macros.h:86
#define when_null(x, l)
Definition: amxc_macros.h:126
#define DESTRUCTOR
Definition: amxc_macros.h:90
int PRIVATE amxc_var_default_convert_to_null(amxc_var_t *const dest, const amxc_var_t *const src)
int PRIVATE amxc_var_default_move(amxc_var_t *const dest, amxc_var_t *const src)
Definition: amxc_variant.c:142
uint32_t PRIVATE amxc_var_add_type(amxc_var_type_t *const type, const uint32_t index)
int PRIVATE amxc_var_default_convert_to_htable(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:174
int PRIVATE amxc_var_default_copy(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:136
int PRIVATE amxc_var_remove_type(amxc_var_type_t *const type)
int PRIVATE amxc_var_default_convert_to_list(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:155
size_t amxc_ts_format(const amxc_ts_t *tsp, char *dst, size_t len)
Transforms unix epoch time to a string.
int amxc_ts_compare(const amxc_ts_t *tsp1, const amxc_ts_t *tsp2)
Checks if tsp1 comes after tsp2.
int amxc_ts_parse(amxc_ts_t *tsp, const char *str, size_t len)
Transforms the given string in to unix epoch time.
const amxc_ts_t * amxc_var_get_const_amxc_ts_t(const amxc_var_t *const var)
Conversion helper function.
int amxc_var_set_amxc_ts_t(amxc_var_t *var, amxc_ts_t *ts)
Setter helper function.
amxc_var_t * amxc_var_add_new_key_amxc_ts_t(amxc_var_t *const var, const char *key, amxc_ts_t *ts)
Conversion helper function.
amxc_var_t * amxc_var_add_new_amxc_ts_t(amxc_var_t *const var, amxc_ts_t *ts)
Conversion helper function.
amxc_ts_t * amxc_var_get_amxc_ts_t(const amxc_var_t *var)
Conversion helper function.
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
#define AMXC_VAR_NAME_TIMESTAMP
Provides a name for variant id AMXC_VAR_ID_TIMESTAMP.
Definition: amxc_variant.h:366
#define AMXC_VAR_ID_ANY
Special variant id, typically used in cast or conversion functions.
Definition: amxc_variant.h:247
#define AMXC_VAR_ID_TIMESTAMP
Ambiorix timestamp variant id.
Definition: amxc_variant.h:224
int(* amxc_var_convert_fn_t)(amxc_var_t *const dest, const amxc_var_t *const src)
Variant type callback function prototype for dynamically converting one type to another.
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
int amxc_var_init(amxc_var_t *const var)
Initializes a variant.
Definition: amxc_variant.c:223
amxc_var_t * amxc_var_add_new(amxc_var_t *const var)
Adds a new variant to a composite variant.
Definition: amxc_variant.c:551
void amxc_var_clean(amxc_var_t *const var)
Clean-up and reset variant.
Definition: amxc_variant.c:237
void amxc_var_delete(amxc_var_t **var)
Frees the previously allocated variant.
Definition: amxc_variant.c:207
amxc_var_t * amxc_var_add_new_key(amxc_var_t *const var, const char *key)
Adds a new variant with a key to a composite variant.
Definition: amxc_variant.c:526
int amxc_var_convert(amxc_var_t *const dest, const amxc_var_t *src, const uint32_t type_id)
Converts one variant (source) to another variant(destination) using the specified variant type id.
Definition: amxc_variant.c:333
A variant type structure.
amxc_var_new_fn_t init
The variant struct definition.
Definition: amxc_variant.h:861
void * data
Definition: amxc_variant.h:883
uint32_t type_id
Definition: amxc_variant.h:864
The timestamp structure (unix epoch time).
int32_t nsec
int16_t offset
static amxc_var_t * var
Definition: test_issue_58.c:77
static int variant_ts_compare(const amxc_var_t *const lval, const amxc_var_t *const rval, int *const result)
static amxc_var_type_t amxc_variant_ts
static int variant_ts_to_int(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_ts_to_double(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_ts_init(amxc_var_t *const var)
static int variant_ts_convert_to(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_ts_to_string(amxc_var_t *const dest, const amxc_var_t *const src)
static DESTRUCTOR void amxc_var_ts_cleanup(void)
static CONSTRUCTOR void amxc_var_ts_init(void)