From a4838cc0b434e0d583aa15b80c438398b8c1316f Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 29 Jun 2023 01:27:17 +0200 Subject: [PATCH] add: split_quoted --- src/utils/is_in_quote.c | 4 +-- src/utils/split_quoted.c | 78 ++++++++++++++++++++++++++++++++++++++++ src/utils/utils.h | 3 +- 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/utils/split_quoted.c diff --git a/src/utils/is_in_quote.c b/src/utils/is_in_quote.c index 543a13b..def74ea 100644 --- a/src/utils/is_in_quote.c +++ b/src/utils/is_in_quote.c @@ -1,10 +1,10 @@ #include "utils.h" -int is_in_quote(const char *str) +int is_in_quote(const char *str, size_t pos) { int out = 0; - for (size_t i = 0; str[i] != '\0'; i++) + for (size_t i = 0; str[i] != '\0' && i < pos; i++) { if (str[i] == '\"') { diff --git a/src/utils/split_quoted.c b/src/utils/split_quoted.c new file mode 100644 index 0000000..b482ee1 --- /dev/null +++ b/src/utils/split_quoted.c @@ -0,0 +1,78 @@ +#include "utils.h" +#include +#include +#include +#include + +static size_t get_len(const char *str, const char* charset) +{ + size_t len = 0; + size_t i = 0; + + while (str[i] != '\0') + { + while (str[i] != '\0' && (is_in_quote(str, i) || strchr(charset, str[i]) != NULL)) + i++; + if (str[i] == '\0') + return len; + len++; + while (str[i] != '\0' && (is_in_quote(str, i) || strchr(charset, str[i]) == NULL)) + i++; + } + return (len); +} + +void free_tab(char **tab) +{ + for (size_t i = 0; tab[i] != NULL; i++) + free(tab[i]); + free(tab); +} + +int fill_tab(char **tab, const char* str, const char* charset) +{ + const char *start; + size_t len = 0; + size_t i = 0; + + while (str[i] != '\0') + { + while (str[i] != '\0' && (is_in_quote(str, i) || strchr(charset, str[i]) != NULL)) + i++; + if (str[i] == '\0') + return len; + start = str + i; + while (str[i] != '\0' && (is_in_quote(str, i) || strchr(charset, str[i]) == NULL)) + i++; + tab[len] = strndup(start, (str + i) - start); + if (tab[len] == NULL) + { + free_tab(tab); + return 1; + } + len++; + } +} + +char **split_quoted_charset(const char *str, const char *charset) +{ + size_t len = get_len(str, charset); + char **tab = malloc((len + 1) * sizeof(char *)); + if (tab == NULL) + return NULL; + tab[len] = NULL; + if (fill_tab(tab, str, charset)) + return NULL; + return (tab); +} + +int main(int ac, char** av) +{ + size_t i = 0; + char** tab; + if (ac != 2) + return 1; + tab = split_quoted_charset(av[1], " \t"); + while ( tab[i] ) printf( "%s\n", tab[i++] ); + free_tab(tab); +} diff --git a/src/utils/utils.h b/src/utils/utils.h index 3c322fa..05ebfb7 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -1,5 +1,6 @@ #pragma once #include +#include -int is_in_quote(const char *str); +int is_in_quote(const char *str, size_t pos);