libamxc  1.10.3
C Generic Data Containers
Join composite types into a single string
Collaboration diagram for Join composite types into a single string:

Functions

int amxc_string_join_llist (amxc_string_t *string, const amxc_llist_t *list, char separator)
 Joins a list of amxc_string_t values into a single string with a separator. More...
 
int amxc_string_join_var (amxc_string_t *string, const amxc_var_t *const var, const char *sep)
 Joins a variant containing a list of variants into a single string. More...
 
int amxc_string_join_var_until (amxc_string_t *string, const amxc_var_t *const var, const char *sep, const char *end, bool remove)
 Joins a variant containing a list of variants into a single string until the end string is encountered . More...
 
int amxc_string_csv_join_var (amxc_string_t *string, const amxc_var_t *const var)
 Joins a variant containing a list of variants into a single string using ',' as separator. More...
 
int amxc_string_ssv_join_var (amxc_string_t *string, const amxc_var_t *const var)
 Joins a variant containing a list of variants into a single string using ' ' as separator. More...
 

Detailed Description

When a list of strings or a list of variants is available they can be joined into a single string using one of these functions

Function Documentation

◆ amxc_string_csv_join_var()

int amxc_string_csv_join_var ( amxc_string_t string,
const amxc_var_t *const  var 
)

Joins a variant containing a list of variants into a single string using ',' as separator.

This function does the same as amxc_string_join_var and uses ',' as the value separator.

Parameters
stringa pointer to a amxc_string_t structure, the joined string will be put in this string.
varThe variant containing a linked list of variants
Returns
0 on success. -1 if an error has occurred

Definition at line 164 of file amxc_string_join.c.

165  {
166  return amxc_string_join_var(string, var, ",");
167 }
int amxc_string_join_var(amxc_string_t *string, const amxc_var_t *const var, const char *separator)
Joins a variant containing a list of variants into a single string.
static amxc_var_t * var
Definition: test_issue_58.c:77

◆ amxc_string_join_llist()

int amxc_string_join_llist ( amxc_string_t string,
const amxc_llist_t list,
char  separator 
)

Joins a list of amxc_string_t values into a single string with a separator.

All amxc_string_t items in the linked list are joined into one amxc_string_t. A single character separator can be provided and will be put between all individual items.

Note
The characters [ and ] can not be used as a separator. When a space character is used (tab, new line, ...) as a separator it is replaced with a single space ' '.
Warning
This function only works on a linked list where all items are of the type amxc_string_t.
Parameters
stringa pointer to a amxc_string_t structure, the joined string will be put in this string.
listthe linked list of strings
separatorsingle character separator.
Returns
0 on success. -1 if an error has occurred

Definition at line 69 of file amxc_string_join.c.

71  {
72  int retval = -1;
73 
74  when_null(string, exit);
75  when_null(list, exit);
76  when_true(isalnum(separator) != 0, exit);
77  when_true(separator == '[' || separator == ']', exit);
78 
79  if(isspace(separator) != 0) {
80  separator = ' ';
81  }
82 
83  amxc_llist_for_each(it, list) {
85  if(!amxc_string_is_empty(string)) {
86  amxc_string_appendf(string, "%c", separator);
87  }
88  amxc_string_append(string,
89  amxc_string_get(part, 0),
91  }
92 
93  retval = 0;
94 
95 exit:
96  return retval;
97 }
#define when_true(x, l)
Definition: amxc_macros.h:134
#define when_null(x, l)
Definition: amxc_macros.h:126
#define amxc_llist_for_each(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:253
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
AMXC_INLINE int amxc_string_append(amxc_string_t *const string, const char *const text, const size_t length)
Appends text to the end of the current content of the string buffer.
Definition: amxc_string.h:920
#define amxc_string_from_llist_it(ll_it)
Get the pointer to a string structure from an amxc linked list iterator.
Definition: amxc_string.h:95
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_appendf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Appends a formatted string to a string.
AMXC_INLINE bool amxc_string_is_empty(const amxc_string_t *const string)
Checks if the string is empty.
Definition: amxc_string.h:1015
The string structure.
Definition: amxc_string.h:103
static amxc_htable_it_t it[2000]

◆ amxc_string_join_var()

int amxc_string_join_var ( amxc_string_t string,
const amxc_var_t *const  var,
const char *  sep 
)

Joins a variant containing a list of variants into a single string.

All variants in the linked list of variants are converted to a string and added to the result string. A separator string can be provided and will be put between the items.

If an item in the list is a variant containing a list, this list will be added as a string starting with '[' and ending with ']'. All items in that list are added as a string and separated with ','.

Warning
This function only works when the provided variant contains a list of variants.
Parameters
stringa pointer to a amxc_string_t structure, the joined string will be put in this string.
varThe variant containing a linked list of variants
sepseparator string.
Returns
0 on success. -1 if an error has occurred

Definition at line 158 of file amxc_string_join.c.

160  {
161  return amxc_string_join_var_until(string, var, separator, NULL, false);
162 }
int amxc_string_join_var_until(amxc_string_t *string, const amxc_var_t *const var, const char *separator, const char *end, bool remove)
Joins a variant containing a list of variants into a single string until the end string is encountere...

◆ amxc_string_join_var_until()

int amxc_string_join_var_until ( amxc_string_t string,
const amxc_var_t *const  var,
const char *  sep,
const char *  end,
bool  remove 
)

Joins a variant containing a list of variants into a single string until the end string is encountered .

All variants in the linked list of variants are converted to a string and added to the result string. A separator string can be provided and will be put between the items.

If an item in the list is a variant containing a list, this list will be added as a string starting with '[' and ending with ']'. All items in that list are added as a string and separated with ','.

When a variant in the list is encountered that after converting to a string matches the end string, joining is stopped. The end string itself is not added to the result string.

When remove is set to true, all variants used will be removed from the list of variants.

Warning
This function only works when the provided variant contains a list of variants.
Parameters
stringa pointer to a amxc_string_t structure, the joined string will be put in this string.
varThe variant containing a linked list of variants
sepseparator string or NULL
endend string or NULL
removewhen set to true, all used variants are removed from the list.
Returns
0 on success. -1 if an error has occurred

Definition at line 99 of file amxc_string_join.c.

103  {
104  int retval = -1;
105  const amxc_llist_t* list = NULL;
106  const char* sep = "";
107  when_null(string, exit);
108  when_null(var, exit);
110 
112 
113  amxc_llist_for_each(it, list) {
115 
116  amxc_string_appendf(string, "%s", sep);
117 
118  if(amxc_var_type_of(part) == AMXC_VAR_ID_LIST) {
119  amxc_string_append(string, "[", 1);
120  amxc_string_csv_join_var(string, part);
121  amxc_string_append(string, "]", 1);
122  } else if((amxc_var_type_of(part) == AMXC_VAR_ID_CSTRING) ||
125  const char* txt = amxc_var_constcast(cstring_t, part);
126  if((txt != NULL) && (end != NULL) && (strcmp(txt, end) == 0)) {
127  if(remove) {
128  amxc_var_delete(&part);
129  }
130  break;
131  }
132  amxc_string_appendf(string, "%s", txt);
133  } else {
134  char* txt = amxc_var_dyncast(cstring_t, part);
135  if((txt != NULL) && (end != NULL) && (strcmp(txt, end) == 0)) {
136  if(remove) {
137  amxc_var_delete(&part);
138  }
139  free(txt);
140  break;
141  }
142  amxc_string_appendf(string, "%s", txt);
143  free(txt);
144  }
145  if(remove) {
146  amxc_var_delete(&part);
147  }
148  sep = separator;
149  }
150 
151  retval = 0;
152 
153 exit:
154  return retval;
155 
156 }
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
#define amxc_var_from_llist_it(ll_it)
Get the variant pointer from an amxc linked list iterator.
Definition: amxc_variant.h:799
int amxc_string_csv_join_var(amxc_string_t *string, const amxc_var_t *const var)
Joins a variant containing a list of variants into a single string using ',' as separator.
#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_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
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
#define amxc_var_dyncast(type, var)
Dynamic cast a variant to a certain type.
Definition: amxc_variant.h:678
void amxc_var_delete(amxc_var_t **var)
Frees the previously allocated variant.
Definition: amxc_variant.c:207
#define amxc_var_constcast(type, var)
Takes the content from a variant.
Definition: amxc_variant.h:722
The linked list structure.
Definition: amxc_llist.h:228
The variant struct definition.
Definition: amxc_variant.h:861

◆ amxc_string_ssv_join_var()

int amxc_string_ssv_join_var ( amxc_string_t string,
const amxc_var_t *const  var 
)

Joins a variant containing a list of variants into a single string using ' ' as separator.

This function does the same as amxc_string_join_var and uses '' as the value separator.

Parameters
stringa pointer to a amxc_string_t structure, the joined string will be put in this string.
varThe variant containing a linked list of variants
Returns
0 on success. -1 if an error has occurred

Definition at line 169 of file amxc_string_join.c.

170  {
171  return amxc_string_join_var(string, var, " ");
172 }