42_minishell/utils/ft_split_charset_quoted.c

91 lines
2.2 KiB
C
Raw Normal View History

2023-04-11 11:49:02 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2023-04-11 13:01:19 -04:00
/* ft_split_charset_quoted.c :+: :+: :+: */
2023-04-11 11:49:02 -04:00
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */
2023-04-14 09:54:13 -04:00
/* Updated: 2023/04/14 15:53:49 by erey-bet ### ########.fr */
2023-04-11 11:49:02 -04:00
/* */
/* ************************************************************************** */
2023-04-11 13:01:19 -04:00
#include "utils.h"
2023-04-11 11:49:02 -04:00
2023-04-11 13:01:19 -04:00
int new_strs(char ***strs, const char *to_split, int *i, int *j)
2023-04-11 11:49:02 -04:00
{
2023-04-14 08:16:09 -04:00
if (ft_strlen((*strs)[(*i)]) <= 0)
return (0);
2023-04-11 11:49:02 -04:00
(*i)++;
(*j) = 0;
2023-04-11 13:01:19 -04:00
(*strs)[(*i)] = ft_calloc(sizeof(char),
(ft_strlen(to_split) + 1));
if (!(*strs)[(*i)])
2023-04-11 11:49:02 -04:00
return (1);
return (0);
}
2023-04-11 13:01:19 -04:00
int get_strs(const char *to_split, const char *charset, char ***strs, int *i)
2023-04-11 11:49:02 -04:00
{
int j;
int x;
int y;
2023-04-14 09:54:13 -04:00
int check;
2023-04-11 11:49:02 -04:00
x = -1;
j = 0;
2023-04-11 11:49:02 -04:00
while (to_split[++x])
{
y = -1;
2023-04-14 09:54:13 -04:00
check = 1;
2023-04-11 11:49:02 -04:00
while (charset[++y])
{
2023-04-11 13:01:19 -04:00
if (to_split[x] == charset[y] && !ft_is_in_quote(to_split, x))
2023-04-11 11:49:02 -04:00
{
2023-04-14 09:54:13 -04:00
check = 0;
if (new_strs(strs, to_split, i, &j))
return (1);
2023-04-11 11:49:02 -04:00
}
}
2023-04-14 09:54:13 -04:00
if (check)
(*strs)[(*i)][j++] = to_split[x];
2023-04-11 11:49:02 -04:00
}
return (0);
}
2023-04-14 09:54:13 -04:00
void free_set_null(char ***strs, int i)
{
if (ft_strlen((*strs)[i]) == 0)
{
free((*strs)[i]);
(*strs)[i] = NULL;
}
}
2023-04-11 13:01:19 -04:00
char **ft_split_charset_quoted(const char *to_split, const char *charset)
2023-04-11 11:49:02 -04:00
{
char **strs;
int i;
2023-04-11 13:01:19 -04:00
strs = ft_calloc(sizeof(char *), (ft_strlen(to_split) + 1));
2023-04-11 11:49:02 -04:00
if (!strs)
return (NULL);
i = 0;
2023-04-14 08:16:09 -04:00
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))
2023-04-11 11:49:02 -04:00
{
i = -1;
while (strs[++i])
free(strs[i]);
free(strs);
2023-04-11 13:01:19 -04:00
return (NULL);
2023-04-11 11:49:02 -04:00
}
2023-04-14 09:54:13 -04:00
free_set_null(&strs, i);
2023-04-11 11:49:02 -04:00
return (strs);
}