libamxc  1.10.3
C Generic Data Containers
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Split a string into a composite type
Collaboration diagram for Split a string into a composite type:

Typedefs

typedef enum _amxc_string_split_status amxc_string_split_status_t
 The possible string split errors. More...
 
typedef amxc_string_split_status_t(* amxc_string_split_builder_t) (amxc_llist_t *all, amxc_var_t *var)
 Callback function definition. More...
 

Enumerations

enum  _amxc_string_split_status {
  AMXC_STRING_SPLIT_OK , AMXC_ERROR_STRING_SPLIT_INVALID_INPUT , AMXC_ERROR_STRING_MISSING_DQUOTE , AMXC_ERROR_STRING_MISSING_SQUOTE ,
  AMXC_ERROR_STRING_MISSING_RBRACKET , AMXC_ERROR_STRING_MISSING_SBRACKET , AMXC_ERROR_STRING_MISSING_CBRACKET
}
 The possible string split errors. More...
 

Functions

amxc_string_tamxc_string_get_from_llist (const amxc_llist_t *const llist, const unsigned int index)
 Helper function to be used with amxc_string_split_llist. More...
 
const char * amxc_string_get_text_from_llist (const amxc_llist_t *const llist, const unsigned int index)
 Helper function to be used with amxc_string_split_llist. More...
 
amxc_string_split_status_t amxc_string_split_word (const amxc_string_t *const string, amxc_llist_t *list, const char **reason)
 Split a string in individual words or punctuation signs. More...
 
amxc_string_split_status_t amxc_string_split (const amxc_string_t *const string, amxc_var_t *var, amxc_string_split_builder_t fn, const char **reason)
 Split a string in individual words or punctuation signs. More...
 
amxc_string_split_status_t amxc_string_csv_to_var (const amxc_string_t *const string, amxc_var_t *var, const char **reason)
 Split a string in individual parts assuming that the string contains comma separated values. More...
 
amxc_string_split_status_t amxc_string_ssv_to_var (const amxc_string_t *const string, amxc_var_t *var, const char **reason)
 Split a string in individual parts assuming that the string contains space separated values. More...
 
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. More...
 

Detailed Description

Often string parsing is needed. Either parsing a string is simple, like splitting it on each occurrence of separator, or more complex, like taking into account quotes and brackets.

Using these functions may help in parsing a string.

Typedef Documentation

◆ amxc_string_split_builder_t

typedef amxc_string_split_status_t(* amxc_string_split_builder_t) (amxc_llist_t *all, amxc_var_t *var)

Callback function definition.

When using Split a string into a composite type to split a string, a filter/builder callback function can be provided.

The string is splitted into parts as describe in amxc_string_split_word, where each part is added to a linked list.

The filter/builder callback function can then combine parts or filter them out before adding them the variant which contains a linked list of variants.

Parameters
alllinked list containing all string parts
varvar the result variant.
Returns
One of the amxc_string_split_status_t status ids.

Definition at line 112 of file amxc_string_split.h.

◆ amxc_string_split_status_t

The possible string split errors.

Enumeration Type Documentation

◆ _amxc_string_split_status

The possible string split errors.

Enumerator
AMXC_STRING_SPLIT_OK 

No error, all ok

AMXC_ERROR_STRING_SPLIT_INVALID_INPUT 

Invalid input

AMXC_ERROR_STRING_MISSING_DQUOTE 

Missing double quote

AMXC_ERROR_STRING_MISSING_SQUOTE 

Missing single quote

AMXC_ERROR_STRING_MISSING_RBRACKET 

Missing round bracket

AMXC_ERROR_STRING_MISSING_SBRACKET 

Missing square bracket

AMXC_ERROR_STRING_MISSING_CBRACKET 

Missing curly bracket

Definition at line 82 of file amxc_string_split.h.

82  {
enum _amxc_string_split_status amxc_string_split_status_t
The possible string split errors.
@ AMXC_STRING_SPLIT_OK
@ AMXC_ERROR_STRING_SPLIT_INVALID_INPUT
@ AMXC_ERROR_STRING_MISSING_CBRACKET
@ AMXC_ERROR_STRING_MISSING_RBRACKET
@ AMXC_ERROR_STRING_MISSING_SQUOTE
@ AMXC_ERROR_STRING_MISSING_DQUOTE
@ AMXC_ERROR_STRING_MISSING_SBRACKET

Function Documentation

◆ amxc_string_csv_to_var()

amxc_string_split_status_t amxc_string_csv_to_var ( const amxc_string_t *const  string,
amxc_var_t var,
const char **  reason 
)

Split a string in individual parts assuming that the string contains comma separated values.

Calls Split a string into a composite type.

This function takes into account square brackets (for lists in lists), double and single quotes. When a ',' is between double or single quotes it is considered as part of the string and not as a separator.

The provided variant will be initialized to a variant containing a linked list of variants where each item in the list is a variant containing a string.

Parameters
stringThe string that needs to be splitted
varthe top level variant, will be initiated to a variant containing a linked list of variants.
reasonwhen provided and parsing fails, will get filled with a human readable error string
Returns
one of the amxc_string_split_status_t statuses

Definition at line 555 of file amxc_string_split.c.

557  {
558  return amxc_string_split(string, var, amxc_build_csv_var_list, reason);
559 }
static amxc_string_split_status_t amxc_build_csv_var_list(amxc_llist_t *all, amxc_var_t *csv_list)
amxc_string_split_status_t amxc_string_split(const amxc_string_t *const string, amxc_var_t *var, amxc_string_split_builder_t fn, const char **reason)
Split a string in individual words or punctuation signs.
static amxc_var_t * var
Definition: test_issue_58.c:77

◆ amxc_string_get_from_llist()

amxc_string_t* amxc_string_get_from_llist ( const amxc_llist_t *const  llist,
const unsigned int  index 
)

Helper function to be used with amxc_string_split_llist.

It gets the reference to amxc_string_t object from an amxc_llist_t, it's not a copy.

The element is free'd when the list is deleted.

Taking a copy is the programmer's responsibility!

Parameters
llista pointer to the linked list structure
indexthe position in the list
Returns
A pointer to the string (amxc_string_t) at the index of the llist, when successful. NULL when failed to to index the llist

Definition at line 641 of file amxc_string_split.c.

642  {
643  amxc_string_t* retval = NULL;
644  amxc_llist_it_t* it = NULL;
645 
646  when_null(llist, exit);
647 
648  it = amxc_llist_get_at(llist, index);
649  when_null(it, exit);
650 
651  retval = amxc_string_from_llist_it(it);
652 exit:
653  return retval;
654 }
#define when_null(x, l)
Definition: amxc_macros.h:126
amxc_llist_it_t * amxc_llist_get_at(const amxc_llist_t *const llist, const unsigned int index)
Gets an item at a certain position of the linked list.
Definition: amxc_llist.c:222
#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
The linked list iterator structure.
Definition: amxc_llist.h:215
The string structure.
Definition: amxc_string.h:103
static amxc_htable_it_t it[2000]
static amxc_llist_t * llist

◆ amxc_string_get_text_from_llist()

const char* amxc_string_get_text_from_llist ( const amxc_llist_t *const  llist,
const unsigned int  index 
)

Helper function to be used with amxc_string_split_llist.

It gets the reference to the amxc_string_t object from an amxc_llist_t, it's not a copy.

The element is free'd when the list is deleted.

Taking a copy is the programmer's responsibility!

Parameters
llista pointer to the linked list structure
indexthe position in the list
Returns
Pointer to the string buffer at the index of the llist, when successful. NULL when failed to point to index the llist.

Definition at line 656 of file amxc_string_split.c.

657  {
658  const char* retval = NULL;
659  amxc_string_t* var_str = NULL;
660 
661  var_str = amxc_string_get_from_llist(llist, index);
662  when_null(var_str, exit);
663 
664  retval = amxc_string_get(var_str, 0);
665 exit:
666  return retval;
667 }
amxc_string_t * amxc_string_get_from_llist(const amxc_llist_t *const llist, const unsigned int index)
Helper function to be used with amxc_string_split_llist.
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_string_split()

amxc_string_split_status_t amxc_string_split ( const amxc_string_t *const  string,
amxc_var_t var,
amxc_string_split_builder_t  fn,
const char **  reason 
)

Split a string in individual words or punctuation signs.

This functions behaves exactly the same as amxc_string_split_word, with following differences:

  • instead of building a linked list of strings it builds a variant containing a linked list of variants where each item is a variant containing a string.
  • Provides the possibility to provide a callback function that can filter out or modify the items that are put in the list. See amxc_string_split_builder_t
Parameters
stringThe string that needs to be splitted
varthe top level variant, will be initiated to a variant containing a linked list of variants.
fna filter/build callback function or NULL
reasonwhen provided and parsing fails, will get filled with a human readable error string
Returns
one of the amxc_string_split_status_t statuses

Definition at line 521 of file amxc_string_split.c.

524  {
526  amxc_llist_t all_parts;
527 
528  amxc_llist_init(&all_parts);
529  when_null(string, exit);
530  when_null(var, exit);
531 
532  retval = amxc_string_split_words_internal(string,
533  &all_parts,
536  reason);
537 
538  if(fn != NULL) {
539  fn(&all_parts, var);
540  } else {
542  amxc_llist_for_each(it, (&all_parts)) {
545  amxc_string_delete(&part);
546  }
547  }
548 
549 exit:
551  return retval;
552 }
static amxc_string_split_status_t amxc_string_split_words_internal(const amxc_string_t *const string, amxc_llist_t *list, amxc_string_create_part_t create, amxc_string_check_delimiter_t check, const char **reason)
static int amxc_string_create_part(const amxc_string_t *const string, amxc_llist_t *const list, const size_t start_pos, const size_t length)
static bool amxc_need_to_add_delimiter(amxc_llist_t *list, const char delimiter)
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
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
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
#define AMXC_VAR_ID_LIST
Ambiorix Linked List variant id.
Definition: amxc_variant.h:206
#define amxc_var_add(type, var, data)
Convenience macro for adding a variant to composite variant type.
Definition: amxc_variant.h:618
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
The linked list structure.
Definition: amxc_llist.h:228

◆ amxc_string_split_to_llist()

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.

Splits a string into parts using a single character separator. A separator must be a punctuation sign except '[' or ']'. Alphanumeric characters are not allowed as a separator.

Parameters
stringThe string that needs to be splitted
listthe result list
separatorthe single character separator sign
Returns
one of the amxc_string_split_status_t statuses

Definition at line 569 of file amxc_string_split.c.

571  {
573  amxc_string_t* current = NULL;
574  amxc_llist_t parts_list;
575  bool in_sbrackets = false;
576 
577  amxc_llist_init(&parts_list);
578  when_null(string, exit);
579  when_null(list, exit);
580  when_true(isalnum(separator) != 0, exit);
581  when_true(separator == '[' || separator == ']', exit);
582 
583  retval = amxc_string_split_word(string, &parts_list, NULL);
584  when_failed(retval, exit);
585  amxc_trim_llist(&parts_list);
586 
587  amxc_llist_for_each(it, (&parts_list)) {
589  const char* txt_part = amxc_string_get(part, 0);
590  amxc_llist_append(list, &part->it);
591  if(amxc_string_text_length(part) == 1) {
592  if((separator == txt_part[0]) && !in_sbrackets) {
593  amxc_string_reset(part);
594  if(amxc_llist_it_get_previous(it) != NULL) {
595  current = part;
596  }
597  continue;
598  }
599  if(txt_part[0] == '[') {
600  if((amxc_string_text_length(current) > 0) && (isspace(separator) == 0)) {
601  amxc_string_append(current, txt_part, amxc_string_text_length(part));
602  amxc_string_delete(&part);
603  continue;
604  } else {
605  if(amxc_string_is_empty(current)) {
606  amxc_string_delete(&current);
607  }
608  current = part;
609  in_sbrackets = !in_sbrackets;
610  continue;
611  }
612  }
613  if((txt_part[0] == ']') && in_sbrackets) {
614  amxc_string_append(current, txt_part, amxc_string_text_length(part));
615  amxc_string_delete(&part);
616  in_sbrackets = !in_sbrackets;
617  current = NULL;
618  continue;
619  }
620  if(isspace(txt_part[0]) && !in_sbrackets) {
621  if(!amxc_string_is_empty(current)) {
622  amxc_string_append(current, txt_part, amxc_string_text_length(part));
623  }
624  amxc_string_delete(&part);
625  continue;
626  }
627  }
628  if(current == NULL) {
629  current = part;
630  } else {
631  amxc_string_append(current, txt_part, amxc_string_text_length(part));
632  amxc_string_delete(&part);
633  }
634  }
635 
636 exit:
638  return retval;
639 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_true(x, l)
Definition: amxc_macros.h:134
static void amxc_trim_llist(amxc_llist_t *const list)
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
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
amxc_string_split_status_t amxc_string_split_word(const amxc_string_t *const string, amxc_llist_t *list, const char **reason)
Split a string in individual words or punctuation signs.
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
void amxc_string_reset(amxc_string_t *const string)
Resets the buffer, reset the content to all 0.
Definition: amxc_string.c:203
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
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_llist_it_t it
Definition: amxc_string.h:108

◆ amxc_string_split_word()

amxc_string_split_status_t amxc_string_split_word ( const amxc_string_t *const  string,
amxc_llist_t list,
const char **  reason 
)

Split a string in individual words or punctuation signs.

This function splits a string in individual words or punctuation signs and puts each individual part in a list. Each part in the list will be a amxc_string_t

A sequence of characters the only consists out of alfa numeric symbols [0-9a-zA-Z] is considered as one single word.

When multiple space characters are encountered after each other only one single space is added to the list.

All space characters are always converted to ' '. So when a tab or new-line character is encountered it will be added to the list as a single ' '.

Brackets and quotes are taken into account, parsing fails when there are missing quotes or brackets.

Parameters
stringThe string that needs to be splitted
listthe linked list that gets filled
reasonwhen provided and parsing fails, will get filled with a human readable error string
Returns
one of the amxc_string_split_status_t statuses

Definition at line 499 of file amxc_string_split.c.

501  {
503 
504  when_null(list, exit);
505  when_null(string, exit);
506 
507  retval = amxc_string_split_words_internal(string,
508  list,
511  reason);
512  if(retval != 0) {
514  }
515 
516 exit:
517  return retval;
518 }

◆ amxc_string_ssv_to_var()

amxc_string_split_status_t amxc_string_ssv_to_var ( const amxc_string_t *const  string,
amxc_var_t var,
const char **  reason 
)

Split a string in individual parts assuming that the string contains space separated values.

Calls Split a string into a composite type.

This function takes into account square brackets (for lists in lists), double and single quotes. When a ' ' is between double or single quotes it is considered as part of the string and not as a separator.

The provided variant will be initialized to a variant containing a linked list of variants where each item in the list is a variant containing a string.

Parameters
stringThe string that needs to be splitted
varthe top level variant, will be initiated to a variant containing a linked list of variants.
reasonwhen provided and parsing fails, will get filled with a human readable error string
Returns
one of the amxc_string_split_status_t statuses

Definition at line 562 of file amxc_string_split.c.

564  {
565  return amxc_string_split(string, var, amxc_build_ssv_var_list, reason);
566 }
static amxc_string_split_status_t amxc_build_ssv_var_list(amxc_llist_t *all, amxc_var_t *ssv_list)