libamxc  1.10.3
C Generic Data Containers
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
amxc_variant.c File Reference
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <amxc/amxc_string.h>
#include <amxc/amxc_string_split.h>
#include <amxc/amxc_variant_type.h>
#include <amxc_variant_priv.h>
#include <amxc/amxc_utils.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Functions

static amxc_var_tamxc_var_find (amxc_var_t *const var, const char *const path, const int flags)
 
int PRIVATE amxc_var_default_copy (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)
 
int PRIVATE amxc_var_default_convert_to_null (amxc_var_t *const dest, UNUSED const amxc_var_t *const src)
 
int PRIVATE amxc_var_default_convert_to_list (amxc_var_t *const dest, const amxc_var_t *const src)
 
int PRIVATE amxc_var_default_convert_to_htable (amxc_var_t *const dest, const amxc_var_t *const src)
 
int amxc_var_new (amxc_var_t **var)
 Allocates a variant and initializes it to the null variant type. More...
 
void amxc_var_delete (amxc_var_t **var)
 Frees the previously allocated variant. More...
 
int amxc_var_init (amxc_var_t *const var)
 Initializes a variant. More...
 
void amxc_var_clean (amxc_var_t *const var)
 Clean-up and reset variant. More...
 
int amxc_var_set_type (amxc_var_t *const var, const uint32_t type)
 Change the variant data type. More...
 
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). More...
 
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). More...
 
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. More...
 
int amxc_var_cast (amxc_var_t *const var, const uint32_t type_id)
 Casts the variant to another variant type id. More...
 
int amxc_var_compare (const amxc_var_t *const var1, const amxc_var_t *const var2, int *result)
 Compares two variants. More...
 
amxc_var_tamxc_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. More...
 
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. More...
 
amxc_var_tamxc_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. More...
 
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. More...
 
amxc_var_tamxc_var_add_new_key (amxc_var_t *const var, const char *key)
 Adds a new variant with a key to a composite variant. More...
 
amxc_var_tamxc_var_add_new (amxc_var_t *const var)
 Adds a new variant to a composite variant. More...
 
amxc_var_tamxc_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. More...
 
amxc_var_tamxc_var_get_pathf (const amxc_var_t *const var, const int flags, const char *const fmt,...)
 
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. More...
 
int amxc_var_set_pathf (amxc_var_t *const var, amxc_var_t *data, const int flags, const char *const fmt,...)
 
uint32_t amxc_var_type_of (const amxc_var_t *const var)
 Gets the variant type id of a variant. More...
 
const char * amxc_var_type_name_of (const amxc_var_t *const var)
 Gets the variant type name of a variant. More...
 
amxc_string_tamxc_var_take_amxc_string_t (amxc_var_t *const var)
 Takes a value from a variant. More...
 
int amxc_var_push_amxc_string_t (amxc_var_t *const var, amxc_string_t *val)
 Pushes a value in a variant. More...
 
amxc_var_tamxc_var_get_first (const amxc_var_t *const var)
 Gets the first variant in a htable or list variant. More...
 
amxc_var_tamxc_var_get_last (const amxc_var_t *const var)
 Gets the last variant in a htable or list variant. More...
 
amxc_var_tamxc_var_get_next (const amxc_var_t *const var)
 Gets the next variant. More...
 
amxc_var_tamxc_var_get_previous (const amxc_var_t *const var)
 Gets the previous variant. More...
 
amxc_var_tamxc_var_get_parent (const amxc_var_t *const var)
 Gets the containing variant. More...
 
const char * amxc_var_key (const amxc_var_t *const var)
 Gets the key, with which the variant is stored in a htable variant. More...
 

Function Documentation

◆ amxc_var_default_convert_to_htable()

int PRIVATE amxc_var_default_convert_to_htable ( amxc_var_t *const  dest,
const amxc_var_t *const  src 
)

Definition at line 174 of file amxc_variant.c.

175  {
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 }
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_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_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
The variant struct definition.
Definition: amxc_variant.h:861
void * data
Definition: amxc_variant.h:883
amxc_htable_it_t hit
Definition: amxc_variant.h:863
static amxc_var_t * var
Definition: test_issue_58.c:77

◆ amxc_var_default_convert_to_list()

int PRIVATE amxc_var_default_convert_to_list ( amxc_var_t *const  dest,
const amxc_var_t *const  src 
)

Definition at line 155 of file amxc_variant.c.

156  {
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 }
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
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
amxc_llist_it_t lit
Definition: amxc_variant.h:862

◆ amxc_var_default_convert_to_null()

int PRIVATE amxc_var_default_convert_to_null ( amxc_var_t *const  dest,
UNUSED const amxc_var_t *const  src 
)

Definition at line 149 of file amxc_variant.c.

150  {
151  dest->data.data = NULL;
152  return 0;
153 }

◆ amxc_var_default_copy()

int PRIVATE amxc_var_default_copy ( amxc_var_t *const  dest,
const amxc_var_t *const  src 
)

Definition at line 136 of file amxc_variant.c.

137  {
138  dest->data = src->data;
139  return 0;
140 }

◆ amxc_var_default_move()

int PRIVATE amxc_var_default_move ( amxc_var_t *const  dest,
amxc_var_t *const  src 
)

Definition at line 142 of file amxc_variant.c.

143  {
144  dest->data = src->data;
145  src->data.data = NULL;
146  return 0;
147 }

◆ amxc_var_find()

static amxc_var_t* amxc_var_find ( amxc_var_t *const  var,
const char *const  path,
const int  flags 
)
static

Definition at line 67 of file amxc_variant.c.

69  {
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 }
#define amxc_container_of(addr, type, member)
Calculates the address of the containing structure.
Definition: amxc_common.h:83
int amxc_llist_init(amxc_llist_t *const llist)
Initializes a linked list.
Definition: amxc_llist.c:111
#define amxc_llist_for_each(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:253
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
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
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 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_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
#define AMXC_VAR_ID_NULL
Null variant type id (aka void)
Definition: amxc_variant.h:128
#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
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
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_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
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
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_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
The linked list structure.
Definition: amxc_llist.h:228
The string structure.
Definition: amxc_string.h:103
static amxc_htable_it_t it[2000]

◆ amxc_var_get_pathf()

amxc_var_t* amxc_var_get_pathf ( const amxc_var_t *const  var,
const int  flags,
const char *const  fmt,
  ... 
)

Definition at line 599 of file amxc_variant.c.

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 }
#define when_null(x, l)
Definition: amxc_macros.h:126
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 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
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

◆ amxc_var_set_pathf()

int amxc_var_set_pathf ( amxc_var_t *const  var,
amxc_var_t data,
const int  flags,
const char *const  fmt,
  ... 
)

Definition at line 643 of file amxc_variant.c.

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 }
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
char data[]