From 91ef62d8f927dca8d3e37325b5a544c05cb5dc50 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 11 Apr 2023 17:01:19 +0000 Subject: [PATCH] core: use charset split --- Makefile | 2 +- parse/parse.c | 6 +- redirection/redirection.c | 4 +- .../ft_split_charset_quoted.c | 33 +++----- utils/ft_split_quoted.c | 75 ------------------- 5 files changed, 18 insertions(+), 102 deletions(-) rename ft_split_charset.c => utils/ft_split_charset_quoted.c (69%) delete mode 100644 utils/ft_split_quoted.c diff --git a/Makefile b/Makefile index 4a20c38..8488d99 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/parse/parse.c b/parse/parse.c index f5cc5c2..2eac22e 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -6,7 +6,7 @@ /* By: cchauvet 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"); diff --git a/ft_split_charset.c b/utils/ft_split_charset_quoted.c similarity index 69% rename from ft_split_charset.c rename to utils/ft_split_charset_quoted.c index e10d006..f5e83c7 100644 --- a/ft_split_charset.c +++ b/utils/ft_split_charset_quoted.c @@ -1,38 +1,29 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_split_charset.c :+: :+: :+: */ +/* ft_split_charset_quoted.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -#include +#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); } diff --git a/utils/ft_split_quoted.c b/utils/ft_split_quoted.c deleted file mode 100644 index e11ebd2..0000000 --- a/utils/ft_split_quoted.c +++ /dev/null @@ -1,75 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_split_quoted.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet