Correction leak
This commit is contained in:
parent
4460a6ab2e
commit
5fe90a0875
2
Makefile
2
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}
|
OBJS = ${SRCS:.c=.o}
|
||||||
CC = clang
|
CC = clang
|
||||||
CFLAGS = -Wall -Wextra -Werror
|
CFLAGS = -Wall -Wextra -Werror
|
||||||
|
|
23
free.c
Normal file
23
free.c
Normal 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);
|
||||||
|
}
|
15
operations.c
15
operations.c
|
@ -58,3 +58,18 @@ void rotate(t_stack *s, char *message)
|
||||||
write(1, message, 1);
|
write(1, message, 1);
|
||||||
write(1, "\n", 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);
|
||||||
|
}
|
||||||
|
|
51
parsing.c
51
parsing.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/12/07 19:23:59 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);
|
write(1, "ra\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
int put_error(void)
|
int put_tab(char **tmp, long **tab, int *y)
|
||||||
{
|
|
||||||
write(2, "Error\n", 6);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char **set_only_space(char *argv[], int i)
|
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
x = 0;
|
x = 0;
|
||||||
while (argv[i][x])
|
while (tmp[x] != NULL)
|
||||||
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;
|
if (ft_atoi_check(tmp[x]))
|
||||||
while (x < y)
|
(*tab)[(*y)++] = ft_atoi(tmp[x]);
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if (tab[i] == tab[x] && i != x)
|
free_strstr(tmp);
|
||||||
return (1);
|
return (put_error());
|
||||||
x++;
|
|
||||||
}
|
}
|
||||||
i++;
|
x++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -66,7 +48,6 @@ int parsing(int argc, char *argv[], long *tab)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int y;
|
int y;
|
||||||
int x;
|
|
||||||
char **tmp;
|
char **tmp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
@ -74,16 +55,12 @@ int parsing(int argc, char *argv[], long *tab)
|
||||||
while (++i < argc)
|
while (++i < argc)
|
||||||
{
|
{
|
||||||
argv = set_only_space(argv, i);
|
argv = set_only_space(argv, i);
|
||||||
x = 0;
|
|
||||||
tmp = ft_split(argv[i], ' ');
|
tmp = ft_split(argv[i], ' ');
|
||||||
while (tmp[x] != NULL)
|
if (tmp == NULL)
|
||||||
{
|
return (1);
|
||||||
if (ft_atoi_check(tmp[x]))
|
if (put_tab(tmp, &tab, &y))
|
||||||
tab[y++] = ft_atoi(tmp[x]);
|
return (1);
|
||||||
else
|
free_strstr(tmp);
|
||||||
return (put_error());
|
|
||||||
x++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (if_there_is_double(tab, y) || (y < argc - 1))
|
if (if_there_is_double(tab, y) || (y < argc - 1))
|
||||||
return (put_error());
|
return (put_error());
|
||||||
|
|
17
push_swap.c
17
push_swap.c
|
@ -74,6 +74,7 @@ void push_swap_3(long *tab, int len)
|
||||||
else
|
else
|
||||||
swap(&sa, "a");
|
swap(&sa, "a");
|
||||||
}
|
}
|
||||||
|
free(sa.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_swap_5(long *tab, int len)
|
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 (sb.len != 2)
|
||||||
{
|
{
|
||||||
while (get_min(sa.list, sa.len) != 0)
|
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(&sa, &sb, "b");
|
||||||
}
|
}
|
||||||
push_swap_3(sa.list, sa.len);
|
push_swap_3(sa.list, sa.len);
|
||||||
while (sb.len > 0)
|
while (sb.len > 0)
|
||||||
push(&sb, &sa, "a");
|
push(&sb, &sa, "a");
|
||||||
|
free(sa.list);
|
||||||
|
free(sb.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
long *tab;
|
long *tab;
|
||||||
|
|
||||||
tab = malloc(sizeof(long) * (argc + 1));
|
tab = malloc(sizeof(long) * (tab_malloc(argc, argv) + 1));
|
||||||
if (tab == NULL)
|
if (tab == NULL)
|
||||||
return (1);
|
return (1);
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
|
{
|
||||||
if (parsing(argc, argv, tab) == 1)
|
if (parsing(argc, argv, tab) == 1)
|
||||||
|
{
|
||||||
|
free(tab);
|
||||||
return (1);
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
free(tab);
|
free(tab);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,14 +33,22 @@ void push_swap_3(long *tab, int len);
|
||||||
void swap(t_stack *s, char *message);
|
void swap(t_stack *s, char *message);
|
||||||
void push(t_stack *s_push, t_stack *s_receive, char *message);
|
void push(t_stack *s_push, t_stack *s_receive, char *message);
|
||||||
void rotate(t_stack *s, char *message);
|
void rotate(t_stack *s, char *message);
|
||||||
|
void rrotate(t_stack *s, char *message);
|
||||||
|
|
||||||
// Tools
|
// Tools
|
||||||
int get_min(long *tab, int len);
|
int get_min(long *tab, int len);
|
||||||
long *get_index(long *tab, int len);
|
long *get_index(long *tab, int len);
|
||||||
int max_len_binary(t_stack s);
|
int max_len_binary(t_stack s);
|
||||||
int check_sa(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
|
// Parsing
|
||||||
int parsing(int argc, char *argv[], long *tab);
|
int parsing(int argc, char *argv[], long *tab);
|
||||||
|
|
||||||
|
// Free
|
||||||
|
void free_strstr(char **tmp);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
29
tools.c
29
tools.c
|
@ -84,3 +84,32 @@ int check_sa(t_stack s)
|
||||||
}
|
}
|
||||||
return (1);
|
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
50
tools_2.c
Normal 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);
|
||||||
|
}
|
Loading…
Reference in a new issue