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

@ -1,4 +1,4 @@
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c ./utils/ft_get_executable.c ./utils/fd.c
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_charset_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c ./utils/ft_get_executable.c ./utils/fd.c
BUILTINS_SRC = builtins/pwd.c \
builtins/export.c \

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:38 by cchauvet #+# #+# */
/* Updated: 2023/04/04 13:50:18 by alouis-j ### ########.fr */
/* Updated: 2023/04/11 16:17:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ static int ft_args_parse(char *cmd_str, t_cmd *cmd)
char **tab;
size_t i;
tab = ft_split_quoted(cmd_str, ' ');
tab = ft_split_charset_quoted(cmd_str, "\t ");
if (tab == NULL)
return (1);
i = 0;
@ -102,7 +102,7 @@ int ft_cmds_parser(t_data *data, const char *line)
char **tab;
ssize_t i;
tab = ft_split_quoted(line, '|');
tab = ft_split_charset_quoted(line, "|");
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:00:52 by alouis-j ### ########.fr */
/* Updated: 2023/04/11 16:19:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -98,7 +98,7 @@ int ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str)
cmd->fd_in[1] = -1;
cmd->fd_out[0] = -1;
cmd->fd_out[1] = -1;
tab = ft_split_quoted(cmd_str, ' ');
tab = ft_split_charset_quoted(cmd_str, " ");
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");

View File

@ -1,38 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_charset.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/11 18:49:24 by erey-bet ### ########.fr */
/* Updated: 2023/04/11 16:59:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
int ft_strlen(char *s)
{
int i = 0;
while (s[i])
i++;
return (i);
}
int new_strs(char ***strs, char *to_split, int *i, int *j)
int new_strs(char ***strs, const char *to_split, int *i, int *j)
{
(*i)++;
(*j) = 0;
(*strs)[(*i)] = malloc(sizeof(char)
* (ft_strlen(to_split) + 1));
(*strs)[(*i)] = ft_calloc(sizeof(char),
(ft_strlen(to_split) + 1));
if (!(*strs)[(*i)])
return (1);
return (0);
}
int get_strs(char *to_split, char *charset, char ***strs, int *i)
int get_strs(const char *to_split, const char *charset, char ***strs, int *i)
{
int j;
int x;
@ -40,7 +31,7 @@ int get_strs(char *to_split, char *charset, char ***strs, int *i)
x = -1;
j = 0;
(*strs)[(*i)] = malloc(sizeof(char) * (ft_strlen(to_split) + 1));
(*strs)[(*i)] = ft_calloc(sizeof(char), (ft_strlen(to_split) + 1));
if (!(*strs)[(*i)])
return (1);
while (to_split[++x])
@ -48,7 +39,7 @@ int get_strs(char *to_split, char *charset, char ***strs, int *i)
y = -1;
while (charset[++y])
{
if (to_split[x] == charset[y])
if (to_split[x] == charset[y] && !ft_is_in_quote(to_split, x))
{
y = 0;
x++;
@ -62,12 +53,12 @@ int get_strs(char *to_split, char *charset, char ***strs, int *i)
return (0);
}
char **ft_split_charset(char *to_split, char *charset)
char **ft_split_charset_quoted(const char *to_split, const char *charset)
{
char **strs;
int i;
strs = malloc(sizeof(char *) * (ft_strlen(to_split) + 1));
strs = ft_calloc(sizeof(char *), (ft_strlen(to_split) + 1));
if (!strs)
return (NULL);
i = 0;
@ -77,8 +68,8 @@ char **ft_split_charset(char *to_split, char *charset)
while (strs[++i])
free(strs[i]);
free(strs);
return (NULL);
}
strs[i + 1] = NULL;
return (strs);
}

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);
}