From 5fe90a08757bc364747186cc60ba2c9f21faa2b1 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Fri, 9 Dec 2022 18:02:14 +0100 Subject: [PATCH] Correction leak --- Makefile | 2 +- free.c | 23 +++++++++++++++++++++++ operations.c | 15 +++++++++++++++ parsing.c | 51 ++++++++++++++------------------------------------- push_swap.c | 17 +++++++++++++++-- push_swap.h | 8 ++++++++ tools.c | 29 +++++++++++++++++++++++++++++ tools_2.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 155 insertions(+), 40 deletions(-) create mode 100644 free.c create mode 100644 tools_2.c diff --git a/Makefile b/Makefile index 6c50937..39eb6bf 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ # # # **************************************************************************** # -SRCS = push_swap.c operations.c tools.c parsing.c +SRCS = push_swap.c operations.c tools.c tools_2.c parsing.c free.c OBJS = ${SRCS:.c=.o} CC = clang CFLAGS = -Wall -Wextra -Werror diff --git a/free.c b/free.c new file mode 100644 index 0000000..6d2fcfb --- /dev/null +++ b/free.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/09 15:50:52 by erey-bet #+# #+# */ +/* Updated: 2022/12/09 15:51:10 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void free_strstr(char **tmp) +{ + int i; + + i = 0; + while (tmp[i] != NULL) + free (tmp[i++]); + free(tmp); +} diff --git a/operations.c b/operations.c index 6ad3b03..d116eef 100644 --- a/operations.c +++ b/operations.c @@ -58,3 +58,18 @@ void rotate(t_stack *s, char *message) write(1, message, 1); write(1, "\n", 1); } + +void rrotate(t_stack *s, char *message) +{ + int i; + long rotate; + + i = (*s).len - 1; + rotate = (*s).list[(*s).len - 1]; + while (--i > -1) + (*s).list[i + 1] = (*s).list[i]; + (*s).list[0] = rotate; + write(1, "rr", 2); + write(1, message, 1); + write(1, "\n", 1); +} diff --git a/parsing.c b/parsing.c index 730eef7..818da4d 100644 --- a/parsing.c +++ b/parsing.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/07 19:23:59 by erey-bet #+# #+# */ -/* Updated: 2022/12/08 11:22:53 by erey-bet ### ########.fr */ +/* Updated: 2022/12/09 15:07:13 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,39 +25,21 @@ void choice(long *tab, int y) write(1, "ra\n", 3); } -int put_error(void) -{ - write(2, "Error\n", 6); - return (1); -} - -char **set_only_space(char *argv[], int i) +int put_tab(char **tmp, long **tab, int *y) { int x; x = 0; - while (argv[i][x]) - if (ft_strchr("\a\b\t\n\v\f\r", argv[i][x++]) != NULL) - argv[i][x - 1] = ' '; - return (argv); -} - -int if_there_is_double(long *tab, int y) -{ - int i; - int x; - - i = 0; - while (i < y) + while (tmp[x] != NULL) { - x = 0; - while (x < y) + if (ft_atoi_check(tmp[x])) + (*tab)[(*y)++] = ft_atoi(tmp[x]); + else { - if (tab[i] == tab[x] && i != x) - return (1); - x++; + free_strstr(tmp); + return (put_error()); } - i++; + x++; } return (0); } @@ -66,7 +48,6 @@ int parsing(int argc, char *argv[], long *tab) { int i; int y; - int x; char **tmp; i = 0; @@ -74,16 +55,12 @@ int parsing(int argc, char *argv[], long *tab) while (++i < argc) { argv = set_only_space(argv, i); - x = 0; tmp = ft_split(argv[i], ' '); - while (tmp[x] != NULL) - { - if (ft_atoi_check(tmp[x])) - tab[y++] = ft_atoi(tmp[x]); - else - return (put_error()); - x++; - } + if (tmp == NULL) + return (1); + if (put_tab(tmp, &tab, &y)) + return (1); + free_strstr(tmp); } if (if_there_is_double(tab, y) || (y < argc - 1)) return (put_error()); diff --git a/push_swap.c b/push_swap.c index d4c7ec8..f38336a 100644 --- a/push_swap.c +++ b/push_swap.c @@ -74,6 +74,7 @@ void push_swap_3(long *tab, int len) else swap(&sa, "a"); } + free(sa.list); } void push_swap_5(long *tab, int len) @@ -90,24 +91,36 @@ void push_swap_5(long *tab, int len) while (sb.len != 2) { while (get_min(sa.list, sa.len) != 0) - rotate(&sa, "a"); + { + if (get_min(sa.list, sa.len) < 3) + rotate(&sa, "a"); + else + rrotate(&sa, "a"); + } push(&sa, &sb, "b"); } push_swap_3(sa.list, sa.len); while (sb.len > 0) push(&sb, &sa, "a"); + free(sa.list); + free(sb.list); } int main(int argc, char *argv[]) { long *tab; - tab = malloc(sizeof(long) * (argc + 1)); + tab = malloc(sizeof(long) * (tab_malloc(argc, argv) + 1)); if (tab == NULL) return (1); if (argc > 1) + { if (parsing(argc, argv, tab) == 1) + { + free(tab); return (1); + } + } free(tab); return (0); } diff --git a/push_swap.h b/push_swap.h index 79310a1..ecc4b3c 100644 --- a/push_swap.h +++ b/push_swap.h @@ -33,14 +33,22 @@ void push_swap_3(long *tab, int len); void swap(t_stack *s, char *message); void push(t_stack *s_push, t_stack *s_receive, char *message); void rotate(t_stack *s, char *message); +void rrotate(t_stack *s, char *message); // Tools int get_min(long *tab, int len); long *get_index(long *tab, int len); int max_len_binary(t_stack s); int check_sa(t_stack s); +int tab_malloc(int argc, char *argv[]); +int put_error(void); +char **set_only_space(char *argv[], int i); +int if_there_is_double(long *tab, int y); // Parsing int parsing(int argc, char *argv[], long *tab); +// Free +void free_strstr(char **tmp); + #endif diff --git a/tools.c b/tools.c index 0ad4d53..51a35ba 100644 --- a/tools.c +++ b/tools.c @@ -84,3 +84,32 @@ int check_sa(t_stack s) } return (1); } + +int tab_malloc(int argc, char *argv[]) +{ + int x; + int y; + int check; + int count; + + y = 1; + count = 0; + while (y < argc) + { + x = 0; + check = 0; + while (argv[y][x]) + { + if (argv[y][x] >= '0' && argv[y][x] <= '9' && check == 0) + { + count++; + check = 1; + } + else if ((argv[y][x] < '0' || argv[y][x] > '9') && check == 1) + check = 0; + x++; + } + y++; + } + return (count); +} diff --git a/tools_2.c b/tools_2.c new file mode 100644 index 0000000..8014912 --- /dev/null +++ b/tools_2.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/09 15:54:00 by erey-bet #+# #+# */ +/* Updated: 2022/12/09 15:54:03 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int put_error(void) +{ + write(2, "Error\n", 6); + return (1); +} + +char **set_only_space(char *argv[], int i) +{ + int x; + + x = 0; + while (argv[i][x]) + if (ft_strchr("\a\b\t\n\v\f\r", argv[i][x++]) != NULL) + argv[i][x - 1] = ' '; + return (argv); +} + +int if_there_is_double(long *tab, int y) +{ + int i; + int x; + + i = 0; + while (i < y) + { + x = 0; + while (x < y) + { + if (tab[i] == tab[x] && i != x) + return (1); + x++; + } + i++; + } + return (0); +}