libamxc  1.10.3
C Generic Data Containers
string utility functions
Collaboration diagram for string utility functions:

Functions

int amxc_string_resolve_esc (amxc_string_t *const string)
 Resolves escaped characters in a string. More...
 
int amxc_string_esc (amxc_string_t *const string)
 Add escape characters to a string. More...
 
int amxc_string_resolve_env (amxc_string_t *const string)
 Resolves environment variables. More...
 
int amxc_string_resolve_var (amxc_string_t *const string, const amxc_var_t *const data)
 Resolves variant path variables. More...
 
int amxc_string_resolve (amxc_string_t *const string, const amxc_var_t *const data)
 Resolves variant paths and environment variables. More...
 
int amxc_string_set_resolved (amxc_string_t *string, const char *text, const amxc_var_t *const data)
 Sets the resolved string. More...
 
int amxc_string_new_resolved (amxc_string_t **string, const char *text, const amxc_var_t *const data)
 Sets the resolved string. More...
 
amxc_llist_it_tamxc_llist_add_string (amxc_llist_t *const llist, const char *text)
 Adds a string (char*) to a linked list of amxc_string_t structures. More...
 
void amxc_string_list_it_free (amxc_llist_it_t *it)
 Helper function to delete an item in a linked list. More...
 

Detailed Description

A list of utility functions and macro's that make common string manipulation easier.

Function Documentation

◆ amxc_llist_add_string()

amxc_llist_it_t* amxc_llist_add_string ( amxc_llist_t *const  llist,
const char *  text 
)

Adds a string (char*) to a linked list of amxc_string_t structures.

This function will allocate a new amxc_string_t structure and copies the provide string (char*) into the amxc_string_t structure.

The new allocated structure is appended to the provided linked list using amxc_llist_append

Parameters
llista pointer to a linked list iterator
textthe string
Returns
The linked list iterator of the new allocated amxc_string_t when successful, NULL otherwise

Definition at line 306 of file amxc_utils.c.

307  {
308  amxc_llist_it_t* it = NULL;
309  amxc_string_t* string = NULL;
310 
311  when_null(llist, exit);
312  when_null(text, exit);
313 
314  when_failed(amxc_string_new(&string, 0), exit);
315  when_failed(amxc_string_append(string, text, strlen(text)), exit);
316  amxc_llist_append(llist, &string->it);
317 
318  it = &string->it;
319 
320 exit:
321  if(it == NULL) {
322  amxc_string_delete(&string);
323  }
324  return it;
325 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_null(x, l)
Definition: amxc_macros.h:126
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_string_delete(amxc_string_t **string)
Frees the previously allocated string.
Definition: amxc_string.c:150
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
int amxc_string_new(amxc_string_t **string, const size_t length)
Allocates a string.
Definition: amxc_string.c:118
The linked list iterator structure.
Definition: amxc_llist.h:215
The string structure.
Definition: amxc_string.h:103
amxc_llist_it_t it
Definition: amxc_string.h:108
static amxc_htable_it_t it[2000]
static amxc_llist_t * llist

◆ amxc_string_esc()

int amxc_string_esc ( amxc_string_t *const  string)

Add escape characters to a string.

Some characters or combinations of characters in a string can have a special purpose i.e. "$(my_env_var)". To avoid that these are replaced by functions like amxc_string_resolve_env or amxc_string_resolve_var, the characters can be escaped with a \ i.e. "\$\‍(my_env_var\‍)". This function can be used to add the escape character.

Only these characters are taken into consideration when removing the escape character: $, (, ), {, }, ", ', .

Parameters
stringan amxc_string_t pointer.
Returns
the number of replacements that have been done.

Definition at line 226 of file amxc_utils.c.

226  {
227  int retval = 0;
228 
229  retval += amxc_string_replace(string, "\\", "\\\\", UINT32_MAX);
230  retval += amxc_string_replace(string, "(", "\\(", UINT32_MAX);
231  retval += amxc_string_replace(string, ")", "\\)", UINT32_MAX);
232  retval += amxc_string_replace(string, "{", "\\{", UINT32_MAX);
233  retval += amxc_string_replace(string, "}", "\\}", UINT32_MAX);
234  retval += amxc_string_replace(string, "\"", "\\\"", UINT32_MAX);
235  retval += amxc_string_replace(string, "'", "\\'", UINT32_MAX);
236  retval += amxc_string_replace(string, "\n", "\\n", UINT32_MAX);
237  retval += amxc_string_replace(string, "\t", "\\t", UINT32_MAX);
238 
239  return retval;
240 }
int amxc_string_replace(amxc_string_t *const string, const char *needle, const char *newstr, uint32_t max)
Replaces a number of sub-string occurrences in a string.
Definition: amxc_string.c:789

◆ amxc_string_list_it_free()

void amxc_string_list_it_free ( amxc_llist_it_t it)

Helper function to delete an item in a linked list.

This function can be passed to amxc_llist_delete or amxc_llist_clean if the linked list only contains amxc_string_t structures.

Note
Only use this function when cleaning up a linked list containing only amxc_string_t structures.
Parameters
ita pointer to a linked list iterator

Definition at line 327 of file amxc_utils.c.

327  {
329  amxc_string_delete(&part);
330 }
#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_string_new_resolved()

int amxc_string_new_resolved ( amxc_string_t **  string,
const char *  text,
const amxc_var_t *const  data 
)

Sets the resolved string.

This function will allocate a new amxc_string_t structure on the heap.

If the provided text contains environment variable references or variant path references, the resolved text is put in the string. it will also remove the escape character '\' when in front of $, (, ), {, }, ", ', .

Parameters
stringan amxc_string_t pointer.
textthe text
dataa variant containing data, preferable a htable or list.
Returns
the number of replacements that have been done.

Definition at line 289 of file amxc_utils.c.

291  {
292  int retval = -1;
293 
294  when_null(string, exit);
295  when_failed(amxc_string_new(string, 0), exit);
296 
297  retval = amxc_string_set_resolved(*string, text, data);
298  if(retval == 0) {
299  amxc_string_delete(string);
300  }
301 
302 exit:
303  return retval;
304 }
int amxc_string_set_resolved(amxc_string_t *string, const char *text, const amxc_var_t *const data)
Sets the resolved string.
Definition: amxc_utils.c:258
char data[]

◆ amxc_string_resolve()

int amxc_string_resolve ( amxc_string_t *const  string,
const amxc_var_t *const  data 
)

Resolves variant paths and environment variables.

This functions calls amxc_string_resolve_var and amxc_string_resolve_env to resolve all environment variables and variant paths mentioned in the string.

The escape character '\' will be removed using amxc_string_resolve_esc when in front of $, (, ), {, }, ", ', .

Parameters
stringan amxc_string_t pointer.
dataa variant containing data, preferable a htable or list.
Returns
the number of replacements that have been done.

Definition at line 242 of file amxc_utils.c.

243  {
244  int changes = 0;
245  int total = 0;
246  do {
247  total += changes;
248  changes = 0;
249  changes += amxc_string_resolve_env(string);
250  changes += amxc_string_resolve_var(string, data);
251  } while(changes > 0);
252 
253  total += amxc_string_resolve_esc(string);
254 
255  return total;
256 }
int amxc_string_resolve_var(amxc_string_t *const string, const amxc_var_t *const data)
Resolves variant path variables.
Definition: amxc_utils.c:164
int amxc_string_resolve_esc(amxc_string_t *const string)
Resolves escaped characters in a string.
Definition: amxc_utils.c:183
int amxc_string_resolve_env(amxc_string_t *const string)
Resolves environment variables.
Definition: amxc_utils.c:148

◆ amxc_string_resolve_env()

int amxc_string_resolve_env ( amxc_string_t *const  string)

Resolves environment variables.

Replaces the environment variable with its value. The name of the environment variable must be put between $( and ). This function will get the value of the environment variable and replaces the $(<NAME>) with the value.

If the environment variable does not exist, it will be replaced with an empty string. If no environment variables are available in the string, the string is not changed.

Parameters
stringan amxc_string_t pointer.
Returns
the number of replacements that have been done.

Definition at line 148 of file amxc_utils.c.

148  {
149  int retval = 0;
150 
151  when_null(string, exit);
152  when_true(amxc_string_is_empty(string), exit);
153 
154  retval = amxc_string_generic_resolve(string,
155  "$(",
156  ")",
158  NULL);
159 
160 exit:
161  return retval;
162 }
#define when_true(x, l)
Definition: amxc_macros.h:134
static void amxc_string_resolve_replace_env(amxc_string_t *const string, size_t pos, size_t length, const char *txt, UNUSED const void *priv)
Definition: amxc_utils.c:72
static int amxc_string_generic_resolve(amxc_string_t *const string, const char *start_string, const char *end_string, amxc_string_replace_cb_t fn, const void *priv)
Definition: amxc_utils.c:102
AMXC_INLINE bool amxc_string_is_empty(const amxc_string_t *const string)
Checks if the string is empty.
Definition: amxc_string.h:1015

◆ amxc_string_resolve_esc()

int amxc_string_resolve_esc ( amxc_string_t *const  string)

Resolves escaped characters in a string.

Some characters or combinations of characters in a string can have a special purpose i.e. "$(my_env_var)". To avoid that these are replaced by functions like amxc_string_resolve_env or amxc_string_resolve_var, the characters can be escaped with a \ i.e. "\$\‍(my_env_var\‍)". This function can be used to remove the escape character from the string.

Only these characters are taken into consideration when removing the escape character: $, (, ), {, }, ", ', .

Parameters
stringan amxc_string_t pointer.
Returns
the number of replacements that have been done.

Definition at line 183 of file amxc_utils.c.

183  {
184  int retval = 0;
185  amxc_string_t resolved;
186  size_t length = 0;
187  char* buffer = NULL;
188 
189  amxc_string_init(&resolved, amxc_string_buffer_length(string));
190  for(uint32_t i = 0; i < amxc_string_text_length(string); i++) {
191  if(string->buffer[i] != '\\') {
192  amxc_string_append(&resolved, string->buffer + i, 1);
193  continue;
194  }
195  if(i + 1 >= amxc_string_text_length(string)) {
196  amxc_string_append(&resolved, string->buffer, 1);
197  break;
198  }
199  switch(string->buffer[i + 1]) {
200  case 'n':
201  amxc_string_append(&resolved, "\n", 1);
202  i++;
203  retval++;
204  break;
205  case 't':
206  amxc_string_append(&resolved, "\t", 1);
207  i++;
208  retval++;
209  break;
210  default:
211  amxc_string_append(&resolved, string->buffer + i + 1, 1);
212  i++;
213  retval++;
214  break;
215  }
216  }
217 
218  length = amxc_string_buffer_length(&resolved);
219  buffer = amxc_string_take_buffer(&resolved);
220  amxc_string_push_buffer(string, buffer, length);
221 
222  amxc_string_clean(&resolved);
223  return retval;
224 }
AMXC_INLINE size_t amxc_string_buffer_length(const amxc_string_t *const string)
Gets the current size of the allocate string buffer.
Definition: amxc_string.h:976
int amxc_string_push_buffer(amxc_string_t *const string, char *buffer, size_t length)
Sets the string buffer.
Definition: amxc_string.c:372
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
char * buffer
Definition: amxc_string.h:104

◆ amxc_string_resolve_var()

int amxc_string_resolve_var ( amxc_string_t *const  string,
const amxc_var_t *const  data 
)

Resolves variant path variables.

Replaces the variant path with its value. The variant path must be put between ${ and }. This function will get the value of that path and replaces the ${<PATH>} with the value.

If the path does not exist, it will be replaced with an empty string. If no variant paths are available in the string, the string is not changed.

Parameters
stringan amxc_string_t pointer.
dataa variant containing data, preferable a htable or list.
Returns
the number of replacements that have been done.

Definition at line 164 of file amxc_utils.c.

165  {
166  int retval = 0;
167 
168  when_null(string, exit);
169  when_true(amxc_string_is_empty(string), exit);
170  when_null(data, exit);
172 
173  retval = amxc_string_generic_resolve(string,
174  "${",
175  "}",
177  data);
178 
179 exit:
180  return retval;
181 }
static void amxc_string_resolve_replace_var(amxc_string_t *const string, size_t pos, size_t length, const char *txt, const void *priv)
Definition: amxc_utils.c:84
#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

◆ amxc_string_set_resolved()

int amxc_string_set_resolved ( amxc_string_t string,
const char *  text,
const amxc_var_t *const  data 
)

Sets the resolved string.

This function will always clear the content of the provided amxc_string_t structure (resets it to an empty string).

If the provided text contains environment variable references or variant path references, the resolved text is put in the string, it will also remove the escape character '\' when in front of $, (, ), {, }, ", ', .

Parameters
stringan amxc_string_t pointer.
textthe text
dataa variant containing data, preferable a htable or list.
Returns
the number of replacements that have been done.

Definition at line 258 of file amxc_utils.c.

260  {
261  int retval = 0;
262  size_t length = 0;
263  size_t i = 0;
264 
265  when_null(text, exit);
266  when_true(*text == 0, exit);
267  when_null(string, exit);
268 
269  amxc_string_reset(string);
270  length = strlen(text);
271  for(i = 0; i < length; i++) {
272  if(text[i] == '$') {
273  break;
274  }
275  }
276 
277  if(i < length) {
278  amxc_string_appendf(string, "%s", text);
279  retval = amxc_string_resolve(string, data);
280  if(retval == 0) {
281  amxc_string_reset(string);
282  }
283  }
284 
285 exit:
286  return retval;
287 }
int amxc_string_resolve(amxc_string_t *const string, const amxc_var_t *const data)
Resolves variant paths and environment variables.
Definition: amxc_utils.c:242
void amxc_string_reset(amxc_string_t *const string)
Resets the buffer, reset the content to all 0.
Definition: amxc_string.c:203
int amxc_string_appendf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Appends a formatted string to a string.