commit 99231c355b7d1848425fa74adeafbedfa0178844
parent 763480b939d25c2bdd8887a0b27d0aa12f9919ce
Author: vaplv <vaplv@free.fr>
Date: Wed, 24 Mar 2021 15:53:18 +0100
Update the cstr_to_list functions
Copy the submitted string in a 'struct str' data structure rather than
in a dynamic array.
Diffstat:
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/src/cstr_to_list.h b/src/cstr_to_list.h
@@ -23,7 +23,7 @@
#define CSTR_TO_LIST_H
#include "cstr.h"
-#include "dynamic_array_char.h"
+#include "str.h"
#endif /* CSTR_TO_LIST_H */
#else /* defined(CSTR_LIST_TYPE) */
@@ -44,33 +44,31 @@ CONCAT(cstr_to_list_, CSTR_LIST_SUFFIX)
size_t* length,
const size_t max_length) /* Maximum size of dst */
{
- struct darray_char buf;
+ struct str buf;
char delim[2] = {'\0', '\0'};
- size_t str_len;
size_t i;
char* tk;
+ char* tk_ctx;
res_T res = RES_OK;
+ str_init(NULL, &buf);
+
if(!str) {
- /* Avoid exit block since buf is not initialised */
- return RES_BAD_ARG;
+ res = RES_BAD_ARG;
+ goto error;
}
if(!dst && !length) { /* Useless invocation */
- /* Avoid exit block since buf is not initialised */
- return RES_OK;
+ goto exit;
}
/* Copy str in a temporary buffer to parse */
- darray_char_init(NULL, &buf);
- str_len = strlen(str);
- res = darray_char_resize(&buf, str_len + 1/*null char*/);
+ res = str_set(&buf, str);
if(res != RES_OK) goto error;
- strncpy(darray_char_data_get(&buf), str, str_len + 1/*null char*/);
/* Parse the string */
delim[0] = delimiter;
- tk = strtok(darray_char_data_get(&buf), delim);
- for(i=0; tk; tk = strtok(NULL, delim), ++i) {
+ tk = strtok_r(str_get(&buf), delim, &tk_ctx);
+ for(i=0; tk; tk = strtok_r(NULL, delim, &tk_ctx), ++i) {
if(dst) {
if(i >= max_length) {
res = RES_BAD_ARG;
@@ -88,7 +86,7 @@ CONCAT(cstr_to_list_, CSTR_LIST_SUFFIX)
*length = i;
exit:
- darray_char_release(&buf);
+ str_release(&buf);
return res;
error:
goto exit;