Skip to content

Commit

Permalink
workign on leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
SaladBunda committed Apr 8, 2024
1 parent 9a7bc2d commit e0447c2
Show file tree
Hide file tree
Showing 17 changed files with 790 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SRCS = main.c ft_split.c util_func1.c util_func2.c operations.c operations2.c operations3.c sort.c range.c extra_sort.c
SRCS = main.c ft_split.c util_func1.c util_func2.c operations.c operations2.c sort.c range.c extra_sort.c
# SRCB =
DIR = ./srcs/
BONUS_DIR = ./bonus/
Expand Down
4 changes: 2 additions & 2 deletions bonus/extra_sort.c → bonus/extra_sort_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: ael-maaz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/08 16:59:37 by ael-maaz #+# #+# */
/* Updated: 2024/04/08 16:59:38 by ael-maaz ### ########.fr */
/* Created: 2024/04/08 16:59:08 by ael-maaz #+# #+# */
/* Updated: 2024/04/08 16:59:16 by ael-maaz ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
102 changes: 102 additions & 0 deletions bonus/ft_split_bonus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-maaz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 16:02:02 by ael-maaz #+# #+# */
/* Updated: 2024/04/01 22:35:05 by ael-maaz ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static int word_c(char *str, char c)
{
int i;
int count;

i = 0;
count = 0;
while (str && str[i] == c && str[i])
i++;
while (str && str[i])
{
count++;
while (str[i] != c && str[i])
i++;
while (str[i] == c && str[i])
i++;
}
return (count);
}

static char **freef(char **tab, int j)
{
while (j >= 0)
free(tab[j--]);
free(tab);
return (NULL);
}

static int char_c(char *str, char c)
{
int i;

i = 0;
while (str[i] && str[i] != c)
i++;
return (i);
}

static char *strdup_i(const char *s1, char c, int *index)
{
char *p;
int i;
int j;
int word_size;

j = 0;
i = *index;
word_size = char_c((char *)&s1[i], c);
p = (char *)malloc(sizeof(char) * (word_size + 1));
if (!p)
return (NULL);
ft_memcpy(p, (char *)s1 + i, (size_t)word_size);
while (j < word_size)
{
j++;
(*index)++;
}
p[word_size] = '\0';
return (p);
}

char **ft_split(char const *s, char c)
{
char **tab;
int j;
int i;

i = 0;
j = 0;
if (!s)
return (NULL);
tab = malloc(sizeof(char *) * (word_c((char *)s, c) + 1));
if (!tab)
return (NULL);
while (s[i] && s[i] == c)
i++;
while (s[i] && j < word_c((char *)s, c))
{
tab[j] = strdup_i(s, c, &i);
if (!tab[j])
return (freef(tab, j));
j++;
while (s[i] && s[i] == c)
i++;
}
tab[word_c((char *)s, c)] = NULL;
return (tab);
}
118 changes: 118 additions & 0 deletions bonus/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-maaz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/01 16:58:38 by ael-maaz #+# #+# */
/* Updated: 2024/04/08 17:24:17 by ael-maaz ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

int fill_stack(char **av, int ac, t_stack *stack_a, int i)
{
int j;
int k;
char **arg;
int error;

error = 0;
k = stack_a->size - 1;
stack_a->top = -1;
while (++i < ac)
{
j = -1;
if (av[i][0] == 0)
error = 1;
arg = ft_split(av[i], ' ');
while (arg[++j])
{
stack_a->top++;
stack_a->stack[k--] = ft_atoi(arg[j], &error);
}
}
if (error == 0)
return (0);
else
return (1);
}

int duplicates(t_stack stack_a)
{
int i;
int j;

i = 0;
while (i < stack_a.size)
{
j = 0;
while (j < i)
{
if (stack_a.stack[j] == stack_a.stack[i])
return (0);
j++;
}
i++;
}
return (1);
}

int init_stacks(char **av, int ac, t_stack *stack_a, t_stack *stack_b)
{
int i;
int j;
char **arg;
int error;

i = 0;
stack_a->size = 0;
while (++i < ac)
{
j = -1;
arg = ft_split(av[i], ' ');
while (arg[++j])
stack_a->size++;
}
stack_a->stack = malloc(sizeof(int) * stack_a->size);
stack_b->size = stack_a->size;
stack_b->stack = malloc(sizeof(int) * stack_b->size);
stack_b->top = -1;
error = fill_stack(av, ac, stack_a, 0);
return (error);
}

// void print_stacks(t_stack a,t_stack b)
// {
// printf("-----------------stack a-----------\n");
// while(a.top >= 0)
// printf("%d\n", a.stack[a.top--]);
// printf("-----------------stack b------------\n");
// while(b.top >= 0)
// printf("%d\n", b.stack[b.top--]);
// }

int main(int ac, char **av)
{
t_stack a;
t_stack b;

if (ac > 1)
{
get_range(40);
if (init_stacks(av, ac, &a, &b) == 1)
{
write(2, "Error\n", 6);
return (0);
}
if (duplicates(a) == 0)
return (write(2, "Error\n", 6), 0);
if (is_sorted(&a) == 0)
return (0);
bubble_sort(&a);
range(&a, &b);
system("leaks push_swap");
}
}
61 changes: 61 additions & 0 deletions bonus/operations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-maaz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 02:34:02 by ael-maaz #+# #+# */
/* Updated: 2024/04/05 23:02:35 by ael-maaz ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void pb(t_stack *a, t_stack *b)
{
if (a->top > -1)
{
b->top++;
b->stack[b->top] = a->stack[a->top];
a->top--;
ft_putstr("pb\n");
}
}

void pa(t_stack *a, t_stack *b)
{
if (b->top > -1)
{
a->top++;
a->stack[a->top] = b->stack[b->top];
b->top--;
ft_putstr("pa\n");
}
}

void sa(t_stack *a)
{
int tmp;

if (a->top > 0)
{
tmp = a->stack[a->top];
a->stack[a->top] = a->stack[a->top -1];
a->stack[a->top -1] = tmp;
ft_putstr("sa\n");
}
}

void sb(t_stack *b)
{
int tmp;

if (b->top > 0)
{
tmp = b->stack[b->top];
b->stack[b->top] = b->stack[b->top - 1];
b->stack[b->top -1] = tmp;
ft_putstr("sb\n");
}
}
89 changes: 89 additions & 0 deletions bonus/operations2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* operations2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-maaz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 16:37:55 by ael-maaz #+# #+# */
/* Updated: 2024/04/05 23:04:30 by ael-maaz ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void rra(t_stack *a)
{
int tmp;
int i;

i = 0;
if (a->top > 0)
{
tmp = a->stack[0];
while (i < a->top)
{
a->stack[i] = a->stack[i + 1];
i++;
}
a->stack[a->top] = tmp;
ft_putstr("rra\n");
}
}

void rrb(t_stack *b)
{
int tmp;
int i;

i = 0;
if (b->top > 0)
{
tmp = b->stack[0];
while (i < b->top)
{
b->stack[i] = b->stack[i + 1];
i++;
}
b->stack[b->top] = tmp;
ft_putstr("rrb\n");
}
}

void ra(t_stack *a)
{
int tmp;
int i;

i = a->top;
if (a->top > 0)
{
tmp = a->stack[a->top];
while (i > 0)
{
a->stack[i] = a->stack[i - 1];
i--;
}
a->stack[0] = tmp;
ft_putstr("ra\n");
}
}

void rb(t_stack *b)
{
int tmp;
int i;

i = b->top;
if (b->top > 0)
{
tmp = b->stack[b->top];
while (i > 0)
{
b->stack[i] = b->stack[i - 1];
i--;
}
b->stack[0] = tmp;
ft_putstr("rb\n");
}
}
Loading

0 comments on commit e0447c2

Please sign in to comment.