core: use charset split

This commit is contained in:
Camille Chauvet
2023-04-11 17:01:19 +00:00
parent d06eb570fc
commit 91ef62d8f9
5 changed files with 18 additions and 102 deletions

View File

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

View File

@ -1,75 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_quoted.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2023/02/15 14:25:56 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
size_t ft_seglen_quoted(const char *str, char c)
{
size_t len;
size_t i;
len = 0;
i = 0;
while (str[i] != 0)
{
while ((str[i] == c || ft_is_in_quote(str, i)) && str[i] != 0)
i++;
if (str[i] != 0)
len++;
while ((str[i] != c || ft_is_in_quote(str, i)) && str[i] != 0)
i++;
}
return (len);
}
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
{
size_t tab_index;
size_t let_i;
size_t start;
tab_index = 0;
let_i = 0;
start = 0;
if (tab == NULL || s == NULL)
return (NULL);
while (tab_index < len)
{
while ((s[let_i] == c || ft_is_in_quote(s, let_i)) && s[let_i] != 0)
let_i++;
start = let_i;
while ((s[let_i] != c || ft_is_in_quote(s, let_i)) && s[let_i] != 0)
let_i++;
tab[tab_index] = ft_substr(s, start, let_i - start);
if (tab[tab_index] == NULL)
return (ft_cancel((void *)tab, tab_index));
tab_index++;
}
return (tab);
}
char **ft_split_quoted(const char *s, char c)
{
size_t len;
char **tab;
if (s == NULL)
return (NULL);
len = ft_seglen_quoted(s, c);
tab = malloc((len + 1) * sizeof(char *));
if (tab == NULL)
return (NULL);
tab[len] = NULL;
if (ft_segsplitter(tab, len, s, c) == NULL)
return (NULL);
return (tab);
}