42_minishell/utils/ft_split_charset_quoted.c
Etienne Rey-bethbeder 425fbd15cd _
2023-04-14 14:16:09 +02:00

95 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_charset_quoted.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */
/* Updated: 2023/04/14 14:14:25 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
int new_strs(char ***strs, const char *to_split, int *i, int *j)
{
if (ft_strlen((*strs)[(*i)]) <= 0)
return (0);
(*i)++;
(*j) = 0;
(*strs)[(*i)] = ft_calloc(sizeof(char),
(ft_strlen(to_split) + 1));
if (!(*strs)[(*i)])
return (1);
return (0);
}
int get_strs(const char *to_split, const char *charset, char ***strs, int *i)
{
int j;
int x;
int y;
x = -1;
j = 0;
while (to_split[++x])
{
y = -1;
while (charset[++y])
{
if (to_split[x] == charset[y] && !ft_is_in_quote(to_split, x))
{
y = 0;
if (to_split[x + 1])
{
x++;
if (new_strs(strs, to_split, i, &j))
return (1);
}
}
}
(*strs)[(*i)][j++] = to_split[x];
}
return (0);
}
char **ft_split_charset_quoted(const char *to_split, const char *charset)
{
char **strs;
int i;
strs = ft_calloc(sizeof(char *), (ft_strlen(to_split) + 1));
if (!strs)
return (NULL);
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))
{
i = -1;
while (strs[++i])
free(strs[i]);
free(strs);
return (NULL);
}
return (strs);
}
/*#include <stdio.h>
int main(int argc, char **argv)
{
char **strs;
if (argc == 3)
strs = ft_split_charset_quoted(argv[1], argv[2]);
int i = -1;
while (strs[++i])
printf("%s\n", strs[i]);
return (0);
}*/