libamxc  1.10.3
C Generic Data Containers
amxc_string_join.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 <ctype.h>
58 
59 #include <amxc/amxc_variant.h>
60 #include <amxc/amxc_string_join.h>
61 #include <amxc/amxc_macros.h>
62 
70  const amxc_llist_t* list,
71  char separator) {
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 }
98 
100  const amxc_var_t* const var,
101  const char* separator,
102  const char* end,
103  bool remove) {
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 }
157 
159  const amxc_var_t* const var,
160  const char* separator) {
161  return amxc_string_join_var_until(string, var, separator, NULL, false);
162 }
163 
165  const amxc_var_t* const var) {
166  return amxc_string_join_var(string, var, ",");
167 }
168 
170  const amxc_var_t* const var) {
171  return amxc_string_join_var(string, var, " ");
172 }
#define when_true(x, l)
Definition: amxc_macros.h:134
#define when_null(x, l)
Definition: amxc_macros.h:126
Ambiorix variant API header file.
#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
#define amxc_llist_for_each(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:253
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.
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.
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.
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.
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...
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
#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 string structure.
Definition: amxc_string.h:103
The variant struct definition.
Definition: amxc_variant.h:861
static amxc_htable_it_t it[2000]
static amxc_var_t * var
Definition: test_issue_58.c:77