Correction leak

This commit is contained in:
Etienne Rey-bethbeder 2022-12-09 18:02:14 +01:00
parent 4460a6ab2e
commit 5fe90a0875
8 changed files with 155 additions and 40 deletions

View file

@ -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

23
free.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

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

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,40 +25,22 @@ 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);
while (tmp[x] != NULL)
{
if (ft_atoi_check(tmp[x]))
(*tab)[(*y)++] = ft_atoi(tmp[x]);
else
{
free_strstr(tmp);
return (put_error());
}
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);
}
@ -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());

View file

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

View file

@ -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

29
tools.c
View file

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

50
tools_2.c Normal file
View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tools_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}