Skip to content

Commit

Permalink
25 percent done
Browse files Browse the repository at this point in the history
  • Loading branch information
ablaamim committed Apr 4, 2022
1 parent 490250a commit 8815f15
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 6 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ablaamim <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/04/03 15:08:35 by ablaamim #+# #+# #
# Updated: 2022/04/04 14:49:16 by ablaamim ### ########.fr #
# Updated: 2022/04/04 17:37:45 by ablaamim ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -23,7 +23,12 @@ SRC = ./sources/main.c \
./sources/lib_utils.c \
./sources/ft_split_and_array_free.c \
./sources/ft_parser.c \
./sources/lib_utils_1.c
./sources/lib_utils_1.c \
./sources/lib_mem_utils.c \
./sources/exit.c \
./sources/ft_printf.c \
./sources/debug.c \
./sources/ft_stacks_constructor.c

OBJ = $(patsubst %.c,%.o,$(SRC))

Expand Down
44 changes: 43 additions & 1 deletion includes/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 14:26:35 by ablaamim #+# #+# */
/* Updated: 2022/04/04 14:42:16 by ablaamim ### ########.fr */
/* Updated: 2022/04/04 17:37:21 by ablaamim ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,28 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>

typedef struct s_node
{
int nbr;
int index;
int keep_a;
void *next;
} t_node;

typedef struct s_stack
{
t_node *head;
int size;
bool markup_head;
} t_stack;

typedef struct s_stacks
{
t_stack a;
t_stack b;
} t_stacks;

/*
* Parsing functions
Expand All @@ -24,9 +46,23 @@ char **ft_args_unified(int argc, char **argv);
void ft_arguments_validator(int argc, char **argv);
void ft_array_free(void **array);
bool ft_parser(char **argv);

/*
* Linked list util functions
*/
t_node *ft_create_node(int numb);

/*
* Stack util functions
*/
void ft_stacks_constructor(char **argv, t_stacks *stacks);
void exit_program(t_stacks *stacks);
void print_stacks(t_stacks stacks);

/*
* Libft utils
*/
int ft_printf(const char *fmt, ...);
char *ft_strdup(char *str);
int ft_strlen(char *str);
void ft_putchar(char c);
Expand All @@ -37,4 +73,10 @@ char **ft_split(char const *s, char c);
int ft_isdigit(int c);
int ft_atoi(char *str);
int ft_strcmp(char *s1, char *s2);
void *ft_memcpy(void *dst, const void *src, size_t n);
void *ft_calloc(size_t count, size_t size);
void ft_bzero(void *s, size_t n);
void *ft_memmove(void *dst, const void *src, size_t len);
void *ft_memset(void *s, int c, size_t n);

#endif
41 changes: 41 additions & 0 deletions sources/debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/04 17:30:25 by ablaamim #+# #+# */
/* Updated: 2022/04/04 17:39:57 by ablaamim ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/push_swap.h"

void print_stacks(t_stacks stacks)
{
int i;

i = 0;
ft_printf("\nsize:\t%d\t%d\n-----\nstack:", stacks.a.size, stacks.b.size);
while (i < stacks.a.size || i < stacks.b.size)
{
if (i < stacks.a.size)
{
ft_printf("\t%d", stacks.a.head->nbr);
if (stacks.a.head->next)
stacks.a.head = stacks.a.head->next;
}
else
ft_printf("\t");
if (i < stacks.b.size)
{
ft_printf("\t%d", stacks.b.head->nbr);
if (stacks.b.head->next)
stacks.b.head = stacks.b.head->next;
}
ft_printf("\n");
i++;
}
ft_printf("\t_\t_\n\ta\tb\n> ");
}
45 changes: 45 additions & 0 deletions sources/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/04 17:07:00 by ablaamim #+# #+# */
/* Updated: 2022/04/04 17:14:18 by ablaamim ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/push_swap.h"

void ft_stack_free(t_node **lst, int size)
{
t_node *to_free;
int i;

to_free = *lst;
if (!lst || *lst)
return ;
i = 0x0;
while (i < size)
{
*lst = to_free->next;
free(to_free);
to_free = *lst;
i++;
}
*lst = NULL;
}

void ft_clear_stacks(t_stacks *stacks)
{
ft_stack_free(&stacks->a.head, stacks->a.size);
ft_stack_free(&stacks->b.head, stacks->a.size);
}

void exit_program(t_stacks *stacks)
{
write(2, "Error\n", 6);
ft_clear_stacks(stacks);
exit(EXIT_FAILURE);
}
78 changes: 78 additions & 0 deletions sources/ft_printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/04 17:22:13 by ablaamim #+# #+# */
/* Updated: 2022/04/04 17:28:14 by ablaamim ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/push_swap.h"

int ft_putchar_printf(char c)
{
return (write(1, &c, 1));
}

int ft_putstr_printf(char *str)
{
int len;

len = 0x0;
if (str == NULL)
{
len += write(1, "(null)", 6);
return (len);
}
while (*str)
len += ft_putchar_printf(*str++);
return (len);
}
#define hex "0123456789abcdef"
int ft_putnbr_printf(long long nb, int base)
{
int len = 0x0;
if (nb < 0)
{
nb = -nb;
len+=ft_putchar_printf('-')
;}
if (nb >= base)
len += ft_putnbr_printf(nb / base, base);
len += ft_putchar_printf(hex[nb % base]);
return (len);
}
int ft_vaprintf(va_list ap, const char *fmt)
{
int len = 0x0; char c;
while (*fmt)
{
c = *fmt++;
if (c != '%')
len += ft_putchar_printf(c);
else
{
c = *fmt++;
if (c == 's')
len += ft_putstr_printf(va_arg(ap, char *));
if (c == 'd')
len+=ft_putnbr_printf((long long)va_arg(ap, int), 10);
}
}
return (len);
}

int ft_printf(const char *fmt, ...)
{
int len;
va_list ap;

len = 0x0;
va_start(ap, fmt);
len += ft_vaprintf(ap, fmt);
va_end(ap);
return (len);
}
58 changes: 58 additions & 0 deletions sources/ft_stacks_constructor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_stacks_constructor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/04 16:48:45 by ablaamim #+# #+# */
/* Updated: 2022/04/04 17:22:00 by ablaamim ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/push_swap.h"

t_node *ft_create_node(int numb)
{
t_node *elem;

elem = ft_calloc(0x1, sizeof(t_node));
if (!elem)
return (0x0);
elem->nbr = numb;
elem->next = NULL;
return (elem);
}

void ft_nodeadd_back(t_node **lst, t_node *new)
{
t_node *tmp;

tmp = *lst;
if (!new)
return ;
if (!*lst)
*lst = new;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
}

void ft_stacks_constructor(char **argv, t_stacks *stacks)
{
t_node *new_node;

stacks->a = (t_stack) {0};
stacks->b = (t_stack) {0};
while (argv[stacks->a.size])
{
new_node = ft_create_node(ft_atoi(argv[stacks->a.size]));
if (!new_node)
exit_program(stacks);
ft_nodeadd_back(&stacks->a.head, new_node);
(stacks->a.size)++;
}
}
Loading

0 comments on commit 8815f15

Please sign in to comment.