This commit is contained in:
Etienne Rey-bethbeder 2023-04-14 14:16:09 +02:00
parent d3fa573054
commit 425fbd15cd

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */ /* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */
/* Updated: 2023/04/11 16:59:15 by cchauvet ### ########.fr */ /* Updated: 2023/04/14 14:14:25 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,8 @@
int new_strs(char ***strs, const char *to_split, int *i, int *j) int new_strs(char ***strs, const char *to_split, int *i, int *j)
{ {
if (ft_strlen((*strs)[(*i)]) <= 0)
return (0);
(*i)++; (*i)++;
(*j) = 0; (*j) = 0;
(*strs)[(*i)] = ft_calloc(sizeof(char), (*strs)[(*i)] = ft_calloc(sizeof(char),
@ -31,9 +33,6 @@ int get_strs(const char *to_split, const char *charset, char ***strs, int *i)
x = -1; x = -1;
j = 0; j = 0;
(*strs)[(*i)] = ft_calloc(sizeof(char), (ft_strlen(to_split) + 1));
if (!(*strs)[(*i)])
return (1);
while (to_split[++x]) while (to_split[++x])
{ {
y = -1; y = -1;
@ -42,12 +41,14 @@ int get_strs(const char *to_split, const char *charset, char ***strs, int *i)
if (to_split[x] == charset[y] && !ft_is_in_quote(to_split, x)) if (to_split[x] == charset[y] && !ft_is_in_quote(to_split, x))
{ {
y = 0; y = 0;
if (to_split[x + 1])
{
x++; x++;
if (ft_strlen((*strs)[(*i)]) > 0)
if (new_strs(strs, to_split, i, &j)) if (new_strs(strs, to_split, i, &j))
return (1); return (1);
} }
} }
}
(*strs)[(*i)][j++] = to_split[x]; (*strs)[(*i)][j++] = to_split[x];
} }
return (0); return (0);
@ -62,6 +63,12 @@ char **ft_split_charset_quoted(const char *to_split, const char *charset)
if (!strs) if (!strs)
return (NULL); return (NULL);
i = 0; i = 0;
strs[0] = ft_calloc(sizeof(char), (ft_strlen(to_split) + 1));
if (!strs[0])
{
free(strs);
return (NULL);
}
if (get_strs(to_split, charset, &strs, &i)) if (get_strs(to_split, charset, &strs, &i))
{ {
i = -1; i = -1;
@ -73,11 +80,13 @@ char **ft_split_charset_quoted(const char *to_split, const char *charset)
return (strs); return (strs);
} }
/*int main(int argc, char **argv) /*#include <stdio.h>
int main(int argc, char **argv)
{ {
char **strs; char **strs;
if (argc == 3) if (argc == 3)
strs = ft_split_charset(argv[1], argv[2]); strs = ft_split_charset_quoted(argv[1], argv[2]);
int i = -1; int i = -1;
while (strs[++i]) while (strs[++i])
printf("%s\n", strs[i]); printf("%s\n", strs[i]);