libamxc  1.10.3
C Generic Data Containers
amxc_variant.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 #include <string.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 
60 #include <amxc/amxc_string.h>
61 #include <amxc/amxc_string_split.h>
62 #include <amxc/amxc_variant_type.h>
63 #include <amxc_variant_priv.h>
64 #include <amxc/amxc_utils.h>
65 #include <amxc/amxc_macros.h>
66 
68  const char* const path,
69  const int flags) {
70  amxc_var_t* retval = NULL;
71  amxc_string_t str_path;
72  amxc_llist_t parts;
73 
74  amxc_string_init(&str_path, 0);
75  amxc_llist_init(&parts);
76 
77  amxc_string_set(&str_path, path);
78  amxc_string_split_to_llist(&str_path, &parts, '.');
79  amxc_string_clean(&str_path);
80 
81  retval = var;
82 
83  amxc_llist_for_each(it, &parts) {
85  size_t len = amxc_string_text_length(key);
86  uint32_t offset = 0;
87  char* token = amxc_string_take_buffer(key);
88  amxc_var_t* temp = NULL;
89 
90  // skip empty parts
91  if((token == NULL) || (*token == 0)) {
92  free(token);
93  continue;
94  }
95 
96  if((token[0] == '\'') || (token[0] == '"')) {
97  token[len - 1] = 0;
98  offset = 1;
99  }
100 
101  temp = amxc_var_get_key(retval, token + offset, AMXC_VAR_FLAG_DEFAULT);
102  if((temp == NULL) && ((flags & AMXC_VAR_FLAG_NO_INDEX) == 0)) {
103  char* endptr = NULL;
104  int index = strtol(token + offset, &endptr, 0);
105  if(*endptr == 0) {
106  temp = amxc_var_get_index(retval, index, AMXC_VAR_FLAG_DEFAULT);
107  }
108  }
109 
110  if((temp == NULL) && ((flags & AMXC_VAR_FLAG_AUTO_ADD) != 0)) {
111  if(amxc_var_type_of(retval) == AMXC_VAR_ID_LIST) {
112  temp = amxc_var_add_new(retval);
113  } else if(amxc_var_type_of(retval) == AMXC_VAR_ID_HTABLE) {
114  temp = amxc_var_add_new_key(retval, token + offset);
115  } else if(amxc_var_type_of(retval) == AMXC_VAR_ID_NULL) {
117  temp = amxc_var_add_new_key(retval, token + offset);
118  }
119  }
120 
121  retval = temp;
122  free(token);
123  amxc_string_delete(&key);
124 
125  if(temp == NULL) {
126  break;
127  }
128  }
129 
130  amxc_string_clean(&str_path);
132 
133  return (amxc_var_t*) retval;
134 }
135 
137  const amxc_var_t* const src) {
138  dest->data = src->data;
139  return 0;
140 }
141 
143  amxc_var_t* const src) {
144  dest->data = src->data;
145  src->data.data = NULL;
146  return 0;
147 }
148 
150  UNUSED const amxc_var_t* const src) {
151  dest->data.data = NULL;
152  return 0;
153 }
154 
156  const amxc_var_t* const src) {
157  int retval = -1;
158  amxc_var_t* var = NULL;
159 
160  if(amxc_var_new(&var) != 0) {
161  amxc_llist_clean(&dest->data.vl, NULL);
162  goto exit;
163  }
164 
165  amxc_var_copy(var, src);
166  amxc_llist_append(&dest->data.vl, &var->lit);
167 
168  retval = 0;
169 
170 exit:
171  return retval;
172 }
173 
175  const amxc_var_t* const src) {
176  int retval = -1;
177  amxc_var_t* var = NULL;
178 
179  if(amxc_var_new(&var) != 0) {
180  amxc_htable_clean(&dest->data.vm, NULL);
181  goto exit;
182  }
183 
184  amxc_var_copy(var, src);
185  amxc_htable_insert(&dest->data.vm, "1", &var->hit);
186 
187  retval = 0;
188 
189 exit:
190  return retval;
191 }
192 
193 
195  int retval = -1;
196  when_null(var, exit);
197 
198  *var = (amxc_var_t*) calloc(1, sizeof(amxc_var_t));
199  when_null(*var, exit);
200 
201  retval = amxc_var_init(*var);
202 
203 exit:
204  return retval;
205 }
206 
208  when_null(var, exit);
209 
210  if(*var != NULL) {
212  amxc_llist_it_clean(&((*var)->lit), NULL);
213  amxc_htable_it_clean(&((*var)->hit), NULL);
214  }
215 
216  free(*var);
217  *var = NULL;
218 
219 exit:
220  return;
221 }
222 
224  int retval = -1;
225  when_null(var, exit);
226 
227  var->type_id = 0;
228  var->data.data = NULL;
231  retval = 0;
232 
233 exit:
234  return retval;
235 }
236 
238  amxc_var_type_t* var_type = NULL;
239  when_null(var, exit);
240 
241  var_type = amxc_var_get_type(var->type_id);
242  // ISSUE: No type available anymore, probably unregister,
243  // dangling variant !!
244  if(var_type == NULL) {
245  goto clean;
246  }
247 
248  if((var_type != NULL) &&
249  (var_type->del != NULL)) {
250  var_type->del(var);
251  }
252 
253 clean:
254  var->type_id = 0;
255  var->data.data = NULL;
256 
257 exit:
258  return;
259 }
260 
261 int amxc_var_set_type(amxc_var_t* const var, const uint32_t type) {
262  int retval = -1;
263  amxc_var_type_t* var_type = NULL;
264  when_null(var, exit);
265 
266  var_type = amxc_var_get_type(type);
267  when_null(var_type, exit);
268 
269  // first clean the variant
270  // this to make sure all allocated memory for the content of the variant
271  // is freed
273 
274  var->type_id = type;
275  if(var_type->init != NULL) {
276  retval = var_type->init(var);
277  } else {
278  retval = 0;
279  }
280 
281 exit:
282  return retval;
283 }
284 
285 int amxc_var_copy(amxc_var_t* const dest, const amxc_var_t* const src) {
286  int retval = -1;
287  amxc_var_type_t* type = NULL;
288  when_null(dest, exit);
289  when_null(src, exit);
290 
291  // reset destinationn variant
292  amxc_var_clean(dest);
293  // get the type function pointers of the src
294  type = amxc_var_get_type(src->type_id);
295  when_null(type, exit);
296  when_null(type->copy, exit);
297 
298  when_failed(amxc_var_set_type(dest, src->type_id), exit);
299  retval = type->copy(dest, src);
300  if(retval != 0) {
301  amxc_var_clean(dest);
302  }
303 
304 exit:
305  return retval;
306 }
307 
308 int amxc_var_move(amxc_var_t* const dest, amxc_var_t* const src) {
309  int retval = -1;
310  amxc_var_type_t* type = NULL;
311  when_null(dest, exit);
312  when_null(src, exit);
313 
314  // reset destinationn variant
315  amxc_var_clean(dest);
316  // get the type function pointers of the src
317  type = amxc_var_get_type(src->type_id);
318  when_null(type, exit);
319  when_null(type->move, exit);
320 
321  when_failed(amxc_var_set_type(dest, src->type_id), exit);
322  retval = type->move(dest, src);
323  if(retval != 0) {
324  amxc_var_clean(dest);
325  } else {
326  amxc_var_clean(src);
327  }
328 
329 exit:
330  return retval;
331 }
332 
333 int amxc_var_convert(amxc_var_t* const dest,
334  const amxc_var_t* const src,
335  uint32_t type_id) {
336  int retval = -1;
337  amxc_var_type_t* src_type = NULL;
338  amxc_var_type_t* dst_type = NULL;
339  when_null(dest, exit);
340  when_null(src, exit);
341 
342  src_type = amxc_var_get_type(src->type_id);
343  when_null(src_type, exit);
344 
345  if(type_id != AMXC_VAR_ID_ANY) {
346  dst_type = amxc_var_get_type(type_id);
347  when_null(dst_type, exit);
348  when_failed(amxc_var_set_type(dest, type_id), exit);
349  } else {
351  dest->type_id = AMXC_VAR_ID_ANY;
352  }
353 
354  // try to convert src to dst using covert_to function from src type
355  retval = src_type->convert_to != NULL ?
356  src_type->convert_to(dest, src) : -1;
357  when_true(retval == 0, exit);
358 
359  if(type_id != AMXC_VAR_ID_ANY) {
360  // try to convert src to dst using covert_from function from dest type
361  retval = dst_type->convert_from != NULL ?
362  dst_type->convert_from(dest, src) : -1;
363  }
364 
365 exit:
366  if(retval != 0) {
367  amxc_var_clean(dest);
368  }
369  return retval;
370 }
371 
373  const uint32_t type_id) {
374  int retval = -1;
375  amxc_var_t intermediate;
376 
377  amxc_var_init(&intermediate);
378  when_null(var, exit);
379 
380  if(var->type_id == type_id) {
381  retval = 0;
382  goto exit;
383  }
384 
385  retval = amxc_var_convert(&intermediate, var, type_id);
386  when_failed(retval, exit);
387 
388  if(var->type_id != intermediate.type_id) {
389  amxc_var_move(var, &intermediate);
390  }
391 
392 exit:
393  amxc_var_clean(&intermediate);
394  return retval;
395 }
396 
397 int amxc_var_compare(const amxc_var_t* const var1,
398  const amxc_var_t* const var2,
399  int* result) {
400  int retval = -1;
401  amxc_var_type_t* var1_type = NULL;
402  amxc_var_type_t* var2_type = NULL;
403  amxc_var_t converted_var;
404  amxc_var_init(&converted_var);
405  when_null(result, exit);
406 
407  *result = 0;
408 
409  if((var1 == NULL) && (var2 == NULL)) {
410  retval = 0;
411  goto exit;
412  }
413 
414  if((var1 != NULL) && (var2 == NULL)) {
415  retval = 0;
416  *result = 1;
417  goto exit;
418  }
419 
420  if(var1 == NULL) {
421  retval = 0;
422  *result = -1;
423  goto exit;
424  }
425 
426  var1_type = amxc_var_get_type(var1->type_id);
427  var2_type = amxc_var_get_type(var2->type_id);
428 
429  when_null(var1_type, exit);
430  when_null(var2_type, exit);
431 
432  if((var1_type->compare != NULL) &&
433  ( amxc_var_convert(&converted_var,
434  var2,
435  amxc_var_type_of(var1)) == 0)) {
436  retval = var1_type->compare(var1, &converted_var, result);
437  } else if((var2_type->compare != NULL) &&
438  ( amxc_var_convert(&converted_var,
439  var1,
440  amxc_var_type_of(var2)) == 0)) {
441  retval = var2_type->compare(&converted_var, var2, result);
442  }
443 
444 exit:
445  amxc_var_clean(&converted_var);
446  return retval;
447 }
448 
450  const char* const key,
451  const int flags) {
452  amxc_var_t* retval = NULL;
453  amxc_var_type_t* var_type = NULL;
454  when_null(var, exit);
455  when_null(key, exit);
456  when_true(*key == 0, exit);
457 
458  var_type = amxc_var_get_type(var->type_id);
459  when_null(var_type, exit);
460 
461  retval = var_type->get_key != NULL ?
462  var_type->get_key(var, key, flags) : NULL;
463 
464 exit:
465  return retval;
466 }
467 
469  const char* const key,
470  amxc_var_t* data,
471  const int flags) {
472  int retval = -1;
473  amxc_var_type_t* var_type = NULL;
474  when_null(var, exit);
475  when_null(key, exit);
476  when_true(*key == 0, exit);
477  when_null(data, exit);
478 
479  var_type = amxc_var_get_type(var->type_id);
480  when_null(var_type, exit);
481 
482  retval = var_type->set_key != NULL ?
483  var_type->set_key(var, data, key, flags) : -1;
484 
485 exit:
486  return retval;
487 }
488 
490  const int64_t index,
491  const int flags) {
492 
493  amxc_var_t* retval = NULL;
494  amxc_var_type_t* var_type = NULL;
495  when_null(var, exit);
496 
497  var_type = amxc_var_get_type(var->type_id);
498  when_null(var_type, exit);
499 
500  retval = var_type->get_index != NULL ?
501  var_type->get_index(var, index, flags) : NULL;
502 
503 exit:
504  return retval;
505 }
506 
508  const int64_t index,
509  amxc_var_t* data,
510  const int flags) {
511  int retval = -1;
512  amxc_var_type_t* var_type = NULL;
513  when_null(var, exit);
514  when_null(data, exit);
515 
516  var_type = amxc_var_get_type(var->type_id);
517  when_null(var_type, exit);
518 
519  retval = var_type->set_index != NULL ?
520  var_type->set_index(var, data, index, flags) : -1;
521 
522 exit:
523  return retval;
524 }
525 
527  const char* key) {
528  amxc_var_t* data = NULL;
529  amxc_var_type_t* var_type = NULL;
530  int retval = -1;
531  when_null(var, exit);
532  when_null(key, exit);
533  when_true(*key == 0, exit);
534 
535  var_type = amxc_var_get_type(var->type_id);
536  when_null(var_type, exit);
537 
538  when_failed(amxc_var_new(&data), exit);
539  retval = var_type->set_key != NULL ?
540  var_type->set_key(var, data, key, AMXC_VAR_FLAG_DEFAULT) :
541  -1;
542 
543  if(retval != 0) {
545  }
546 
547 exit:
548  return data;
549 }
550 
552  amxc_var_t* data = NULL;
553  amxc_var_type_t* var_type = NULL;
554  int retval = -1;
555  when_null(var, exit);
556 
557  var_type = amxc_var_get_type(var->type_id);
558  when_null(var_type, exit);
559 
560  when_failed(amxc_var_new(&data), exit);
561  retval = var_type->set_index != NULL ?
562  var_type->set_index(var, data, -1, AMXC_VAR_FLAG_DEFAULT) :
563  -1;
564 
565  if(retval != 0) {
567  }
568 
569 exit:
570  return data;
571 }
572 
574  const char* const path,
575  const int flags) {
576  amxc_var_t* retval = NULL;
577 
578  retval = amxc_var_find((amxc_var_t*) var, path, flags & ~AMXC_VAR_FLAG_AUTO_ADD);
579  when_null(retval, exit);
580 
581  if((flags & AMXC_VAR_FLAG_COPY) == AMXC_VAR_FLAG_COPY) {
582  amxc_var_t* copy = NULL;
583  if(amxc_var_new(&copy) != 0) {
584  retval = NULL;
585  goto exit;
586  }
587  if(amxc_var_copy(copy, retval) != 0) {
588  retval = NULL;
589  free(copy);
590  goto exit;
591  }
592  retval = copy;
593  }
594 
595 exit:
596  return retval;
597 }
598 
600  const int flags,
601  const char* const fmt,
602  ...
603  ) {
604  amxc_var_t* retval = NULL;
605  amxc_string_t path;
606  va_list args;
607 
608  amxc_string_init(&path, 0);
609  when_null(var, exit);
610  when_null(fmt, exit);
611 
612  va_start(args, fmt);
613  amxc_string_vappendf(&path, fmt, args);
614  va_end(args);
615 
616  retval = amxc_var_get_path(var, amxc_string_get(&path, 0), flags);
617 
618 exit:
619  amxc_string_clean(&path);
620  return retval;
621 }
622 
624  const char* const path,
625  amxc_var_t* data,
626  const int flags) {
627  amxc_var_t* variant = NULL;
628  int retval = -1;
629 
630  variant = amxc_var_find(var, path, flags);
631  when_null(variant, exit);
632 
633  if((flags & AMXC_VAR_FLAG_COPY) == AMXC_VAR_FLAG_COPY) {
634  retval = amxc_var_copy(variant, data);
635  } else {
636  retval = amxc_var_move(variant, data);
637  }
638 
639 exit:
640  return retval;
641 }
642 
644  amxc_var_t* data,
645  const int flags,
646  const char* const fmt,
647  ...
648  ) {
649  int retval = -1;
650  amxc_string_t path;
651  va_list args;
652 
653  amxc_string_init(&path, 0);
654  when_null(var, exit);
655  when_null(fmt, exit);
656 
657  va_start(args, fmt);
658  amxc_string_vappendf(&path, fmt, args);
659  va_end(args);
660 
661  retval = amxc_var_set_path(var, amxc_string_get(&path, 0), data, flags);
662 
663 exit:
664  amxc_string_clean(&path);
665  return retval;
666 }
667 
668 uint32_t amxc_var_type_of(const amxc_var_t* const var) {
669  return var != NULL ? var->type_id : AMXC_VAR_ID_INVALID;
670 }
671 
672 const char* amxc_var_type_name_of(const amxc_var_t* const var) {
673  return var != NULL ? amxc_var_get_type_name_from_id(var->type_id) : NULL;
674 }
675 
677  amxc_string_t* retval = NULL;
678 
679  when_null(var, exit);
683  when_null(var->data.s, exit);
684 
685  when_failed(amxc_string_new(&retval, 0), exit);
686  when_null(retval, exit);
687 
688  amxc_string_push_buffer(retval, var->data.s, strlen(var->data.s) + 1);
689  var->data.s = NULL;
691 
692 exit:
693  return retval;
694 }
695 
697  int retval = -1;
698  char* buffer = NULL;
699 
700  when_null(var, exit);
701  when_null(val, exit);
702 
704 
705  buffer = amxc_string_take_buffer(val);
706  retval = amxc_var_push_cstring_t(var, buffer);
707 
708 exit:
709  return retval;
710 }
711 
713  amxc_var_t* first = NULL;
714 
715  when_null(var, exit);
716 
717  switch(amxc_var_type_of(var)) {
718  case AMXC_VAR_ID_HTABLE: {
720  when_null(it, exit);
722  }
723  break;
724  case AMXC_VAR_ID_LIST: {
726  when_null(it, exit);
728  }
729  break;
730  default:
731  break;
732  }
733 
734 exit:
735  return first;
736 }
737 
739  amxc_var_t* last = NULL;
740 
741  when_null(var, exit);
742 
743  switch(amxc_var_type_of(var)) {
744  case AMXC_VAR_ID_HTABLE: {
746  when_null(it, exit);
747  last = amxc_var_from_htable_it(it);
748  }
749  break;
750  case AMXC_VAR_ID_LIST: {
752  when_null(it, exit);
753  last = amxc_var_from_llist_it(it);
754  }
755  break;
756  default:
757  break;
758  }
759 
760 exit:
761  return last;
762 }
763 
765  amxc_var_t* next = NULL;
766 
767  when_null(var, exit);
768 
769  if(var->hit.ait != NULL) {
771  when_null(it, exit);
772  next = amxc_var_from_htable_it(it);
773  } else if(var->lit.llist != NULL) {
775  when_null(it, exit);
776  next = amxc_var_from_llist_it(it);
777  }
778 
779 exit:
780  return next;
781 }
782 
784  amxc_var_t* prev = NULL;
785 
786  when_null(var, exit);
787 
788  if(var->hit.ait != NULL) {
790  when_null(it, exit);
791  prev = amxc_var_from_htable_it(it);
792  } else if(var->lit.llist != NULL) {
794  when_null(it, exit);
795  prev = amxc_var_from_llist_it(it);
796  }
797 
798 exit:
799  return prev;
800 }
801 
803  amxc_var_t* parent = NULL;
804 
805  when_null(var, exit);
806 
807  if(var->hit.ait != NULL) {
808  if(var->hit.ait->array != NULL) {
810  parent = amxc_container_of(ptable, amxc_var_t, data);
811  }
812  } else if(var->lit.llist != NULL) {
813  amxc_llist_t* plist = var->lit.llist;
814  parent = amxc_container_of(plist, amxc_var_t, data);
815  }
816 
817 exit:
818  return parent;
819 }
820 
821 const char* amxc_var_key(const amxc_var_t* const var) {
822  const char* key = NULL;
823 
824  when_null(var, exit);
825 
826  if(var->hit.ait != NULL) {
827  key = amxc_htable_it_get_key(&var->hit);
828  }
829 
830 exit:
831  return key;
832 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_true(x, l)
Definition: amxc_macros.h:134
#define PRIVATE
Definition: amxc_macros.h:66
#define when_null(x, l)
Definition: amxc_macros.h:126
#define UNUSED
Definition: amxc_macros.h:70
Ambiorix string API header file.
int PRIVATE amxc_var_default_move(amxc_var_t *const dest, amxc_var_t *const src)
Definition: amxc_variant.c:142
int PRIVATE amxc_var_default_convert_to_null(amxc_var_t *const dest, UNUSED const amxc_var_t *const src)
Definition: amxc_variant.c:149
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 amxc_var_set_pathf(amxc_var_t *const var, amxc_var_t *data, const int flags, const char *const fmt,...)
Definition: amxc_variant.c:643
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_default_convert_to_list(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:155
static amxc_var_t * amxc_var_find(amxc_var_t *const var, const char *const path, const int flags)
Definition: amxc_variant.c:67
amxc_var_t * amxc_var_get_pathf(const amxc_var_t *const var, const int flags, const char *const fmt,...)
Definition: amxc_variant.c:599
#define amxc_var_from_htable_it(ht_it)
Get the variant pointer from an amxc htable iterator.
Definition: amxc_variant.h:790
#define amxc_var_from_llist_it(ll_it)
Get the variant pointer from an amxc linked list iterator.
Definition: amxc_variant.h:799
Ambiorix ring buffer API header file.
#define amxc_container_of(addr, type, member)
Calculates the address of the containing structure.
Definition: amxc_common.h:83
amxc_htable_it_t * amxc_htable_it_get_next(const amxc_htable_it_t *const reference)
Gets the next iterator in the hash table.
int amxc_htable_it_init(amxc_htable_it_t *const it)
Initializes a hash table.iterator.
void amxc_htable_it_clean(amxc_htable_it_t *const it, amxc_htable_it_delete_t func)
Removes the iterator from the htable and frees allocated memory.
amxc_htable_it_t * amxc_htable_it_get_previous(const amxc_htable_it_t *const reference)
Gets the previous iterator in the hash table.
AMXC_INLINE const char * amxc_htable_it_get_key(const amxc_htable_it_t *const it)
Gets the key from the iterator.
Definition: amxc_htable.h:672
amxc_htable_it_t * amxc_htable_get_first(const amxc_htable_t *const htable)
Gets the first item stored in the table.
Definition: amxc_htable.c:313
amxc_htable_it_t * amxc_htable_get_last(const amxc_htable_t *const htable)
Gets the last item stored in the table.
Definition: amxc_htable.c:326
int amxc_htable_insert(amxc_htable_t *const htable, const char *const key, amxc_htable_it_t *const it)
Inserts an item in the hash table.
Definition: amxc_htable.c:237
void amxc_htable_clean(amxc_htable_t *const htable, amxc_htable_it_delete_t func)
Removes all items from the hash table.
Definition: amxc_htable.c:200
int amxc_llist_it_init(amxc_llist_it_t *const it)
Initializes a linked list iterator.
Definition: amxc_llist_it.c:89
AMXC_INLINE amxc_llist_it_t * amxc_llist_it_get_previous(const amxc_llist_it_t *const reference)
Gets the previous iterator in the list.
Definition: amxc_llist.h:842
AMXC_INLINE amxc_llist_it_t * amxc_llist_it_get_next(const amxc_llist_it_t *const reference)
Gets the next iterator in the list.
Definition: amxc_llist.h:824
void amxc_llist_it_clean(amxc_llist_it_t *const it, amxc_llist_it_delete_t func)
Removes the iterator from the list and frees allocated memory.
int amxc_llist_append(amxc_llist_t *const llist, amxc_llist_it_t *const it)
Adds an item to the end of the linked list.
Definition: amxc_llist.c:169
int amxc_llist_init(amxc_llist_t *const llist)
Initializes a linked list.
Definition: amxc_llist.c:111
void amxc_llist_clean(amxc_llist_t *const llist, amxc_llist_it_delete_t func)
Removes all items from the linked list.
Definition: amxc_llist.c:124
#define amxc_llist_for_each(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:253
AMXC_INLINE amxc_llist_it_t * amxc_llist_get_last(const amxc_llist_t *const llist)
Gets the last item of the linked list.
Definition: amxc_llist.h:732
AMXC_INLINE amxc_llist_it_t * amxc_llist_get_first(const amxc_llist_t *const llist)
Gets the first item of the linked list.
Definition: amxc_llist.h:713
amxc_string_split_status_t amxc_string_split_to_llist(const amxc_string_t *const string, amxc_llist_t *list, const char separator)
Simple split function using a single character separator.
void amxc_string_list_it_free(amxc_llist_it_t *it)
Helper function to delete an item in a linked list.
Definition: amxc_utils.c:327
void amxc_string_delete(amxc_string_t **string)
Frees the previously allocated string.
Definition: amxc_string.c:150
const char * amxc_string_get(const amxc_string_t *const string, const size_t offset)
Gets the content of the string buffer.
Definition: amxc_string.c:339
int amxc_string_push_buffer(amxc_string_t *const string, char *buffer, size_t length)
Sets the string buffer.
Definition: amxc_string.c:372
size_t amxc_string_set(amxc_string_t *const string, const char *text)
Sets a 0 terminated string in the string buffer.
Definition: amxc_string.c:826
int amxc_string_new(amxc_string_t **string, const size_t length)
Allocates a string.
Definition: amxc_string.c:118
AMXC_INLINE size_t amxc_string_text_length(const amxc_string_t *const string)
Gets the current size of the used string buffer.
Definition: amxc_string.h:997
int int amxc_string_vappendf(amxc_string_t *const string, const char *fmt, va_list ap)
Appends a formatted string to a string.
Definition: amxc_string.c:537
int amxc_string_init(amxc_string_t *const string, const size_t length)
Initializes a string.
Definition: amxc_string.c:163
void amxc_string_clean(amxc_string_t *const string)
Frees the string buffer and reset length attributes.
Definition: amxc_string.c:189
char * amxc_string_take_buffer(amxc_string_t *const string)
Takes the string buffer.
Definition: amxc_string.c:356
#define AMXC_VAR_FLAG_NO_INDEX
Only search by key and not by index. This flag can be used with amxc_var_get_path function.
Definition: amxc_variant.h:411
#define AMXC_VAR_FLAG_DEFAULT
The default flag, do not copy, use variant as is.
Definition: amxc_variant.h:392
#define AMXC_VAR_FLAG_COPY
Copy the variant, creates a new variant, leaves the source variant untouched.
Definition: amxc_variant.h:398
#define AMXC_VAR_FLAG_AUTO_ADD
Add none existing variants to composite variants. This flag can be used with amxc_var_set_path and am...
Definition: amxc_variant.h:418
amxc_string_t * amxc_var_take_amxc_string_t(amxc_var_t *const var)
Takes a value from a variant.
Definition: amxc_variant.c:676
int amxc_var_push_amxc_string_t(amxc_var_t *const var, amxc_string_t *val)
Pushes a value in a variant.
Definition: amxc_variant.c:696
int amxc_var_push_cstring_t(amxc_var_t *const var, char *val)
Pushes a value in a variant.
Definition: variant_char.c:629
#define AMXC_VAR_ID_INVALID
Invalid variant type id.
Definition: amxc_variant.h:122
#define AMXC_VAR_ID_NULL
Null variant type id (aka void)
Definition: amxc_variant.h:128
#define AMXC_VAR_ID_CSTRING
C-string variant id (aka char *), null terminated string.
Definition: amxc_variant.h:134
#define AMXC_VAR_ID_SSV_STRING
Space Separated Values string variant id.
Definition: amxc_variant.h:236
#define AMXC_VAR_ID_ANY
Special variant id, typically used in cast or conversion functions.
Definition: amxc_variant.h:247
#define AMXC_VAR_ID_CSV_STRING
Comma Separated Values string variant id.
Definition: amxc_variant.h:230
#define AMXC_VAR_ID_LIST
Ambiorix Linked List variant id.
Definition: amxc_variant.h:206
#define AMXC_VAR_ID_HTABLE
Ambiorix Hash Table variant id.
Definition: amxc_variant.h:212
amxc_var_type_t * amxc_var_get_type(unsigned int type_id)
Get the type definition structure.
const char * amxc_var_get_type_name_from_id(const uint32_t type_id)
Get the type name.
int amxc_var_move(amxc_var_t *const dest, amxc_var_t *const src)
Moves the type and data from one variant (source) in another variant (destination).
Definition: amxc_variant.c:308
uint32_t amxc_var_type_of(const amxc_var_t *const var)
Gets the variant type id of a variant.
Definition: amxc_variant.c:668
amxc_var_t * amxc_var_get_last(const amxc_var_t *const var)
Gets the last variant in a htable or list variant.
Definition: amxc_variant.c:738
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
amxc_var_t * amxc_var_get_path(const amxc_var_t *const var, const char *const path, const int flags)
Retrieves the variant at the given path of a composite variant.
Definition: amxc_variant.c:573
int amxc_var_set_path(amxc_var_t *const var, const char *const path, amxc_var_t *data, const int flags)
Sets the variant at the given path of a composite variant.
Definition: amxc_variant.c:623
amxc_var_t * amxc_var_get_key(const amxc_var_t *const var, const char *const key, const int flags)
Get a reference to a part of composed variant using a key.
Definition: amxc_variant.c:449
const char * amxc_var_key(const amxc_var_t *const var)
Gets the key, with which the variant is stored in a htable variant.
Definition: amxc_variant.c:821
int amxc_var_cast(amxc_var_t *const var, const uint32_t type_id)
Casts the variant to another variant type id.
Definition: amxc_variant.c:372
int amxc_var_set_index(amxc_var_t *const var, const int64_t index, amxc_var_t *data, const int flags)
Set a part of composed variant using an index.
Definition: amxc_variant.c:507
amxc_var_t * amxc_var_get_previous(const amxc_var_t *const var)
Gets the previous variant.
Definition: amxc_variant.c:783
int amxc_var_new(amxc_var_t **var)
Allocates a variant and initializes it to the null variant type.
Definition: amxc_variant.c:194
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
int amxc_var_copy(amxc_var_t *const dest, const amxc_var_t *const src)
Copy the type and data from one variant (source) in another variant (destination).
Definition: amxc_variant.c:285
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
int amxc_var_set_key(amxc_var_t *const var, const char *const key, amxc_var_t *data, const int flags)
Sets a part of composed variant using a key.
Definition: amxc_variant.c:468
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
amxc_var_t * amxc_var_get_parent(const amxc_var_t *const var)
Gets the containing variant.
Definition: amxc_variant.c:802
amxc_var_t * amxc_var_get_index(const amxc_var_t *const var, const int64_t index, const int flags)
Get a reference to a part of composed variant using an index.
Definition: amxc_variant.c:489
amxc_var_t * amxc_var_get_next(const amxc_var_t *const var)
Gets the next variant.
Definition: amxc_variant.c:764
amxc_var_t * amxc_var_get_first(const amxc_var_t *const var)
Gets the first variant in a htable or list variant.
Definition: amxc_variant.c:712
int amxc_var_convert(amxc_var_t *const dest, const amxc_var_t *const src, uint32_t type_id)
Converts one variant (source) to another variant(destination) using the specified variant type id.
Definition: amxc_variant.c:333
int amxc_var_compare(const amxc_var_t *const var1, const amxc_var_t *const var2, int *result)
Compares two variants.
Definition: amxc_variant.c:397
const char * amxc_var_type_name_of(const amxc_var_t *const var)
Gets the variant type name of a variant.
Definition: amxc_variant.c:672
amxc_array_t * array
Definition: amxc_array.h:175
The hash table iterator structure.
Definition: amxc_htable.h:138
amxc_array_it_t * ait
Definition: amxc_htable.h:139
The hash table structure.
Definition: amxc_htable.h:175
The linked list iterator structure.
Definition: amxc_llist.h:215
struct _amxc_llist * llist
Definition: amxc_llist.h:220
The linked list structure.
Definition: amxc_llist.h:228
The string structure.
Definition: amxc_string.h:103
A variant type structure.
amxc_var_get_key_fn_t get_key
amxc_var_get_index_fn_t get_index
amxc_var_compare_fn_t compare
amxc_var_move_fn_t move
amxc_var_free_fn_t del
amxc_var_set_key_fn_t set_key
amxc_var_convert_fn_t convert_from
amxc_var_convert_fn_t convert_to
amxc_var_set_index_fn_t set_index
amxc_var_copy_fn_t copy
amxc_var_new_fn_t init
The variant struct definition.
Definition: amxc_variant.h:861
void * data
Definition: amxc_variant.h:883
amxc_llist_it_t lit
Definition: amxc_variant.h:862
amxc_htable_it_t hit
Definition: amxc_variant.h:863
uint32_t type_id
Definition: amxc_variant.h:864
char data[]
static amxc_htable_it_t it[2000]
static amxc_var_t * var
Definition: test_issue_58.c:77
static amxc_var_t * first
Definition: test_issue_59.c:81