diff --git a/Makefile b/Makefile index 401c1dd..457aa10 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: ablaamim +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 # # # # **************************************************************************** # @@ -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)) diff --git a/includes/push_swap.h b/includes/push_swap.h index aaae29f..d12f4aa 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -6,7 +6,7 @@ /* By: ablaamim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -16,6 +16,28 @@ #include #include #include +#include + +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 @@ -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); @@ -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 diff --git a/sources/debug.c b/sources/debug.c new file mode 100644 index 0000000..294e489 --- /dev/null +++ b/sources/debug.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* debug.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ablaamim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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> "); +} diff --git a/sources/exit.c b/sources/exit.c new file mode 100644 index 0000000..05fd94d --- /dev/null +++ b/sources/exit.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ablaamim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/sources/ft_printf.c b/sources/ft_printf.c new file mode 100644 index 0000000..5511d1c --- /dev/null +++ b/sources/ft_printf.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ablaamim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/sources/ft_stacks_constructor.c b/sources/ft_stacks_constructor.c new file mode 100644 index 0000000..c2a3d99 --- /dev/null +++ b/sources/ft_stacks_constructor.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stacks_constructor.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ablaamim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)++; + } +} diff --git a/sources/lib_mem_utils.c b/sources/lib_mem_utils.c new file mode 100644 index 0000000..f269dd5 --- /dev/null +++ b/sources/lib_mem_utils.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lib_mem_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ablaamim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/04 16:59:56 by ablaamim #+# #+# */ +/* Updated: 2022/04/04 17:01:56 by ablaamim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + unsigned char *pd; + unsigned char *ps; + + if (dst == NULL && src == NULL) + return (NULL); + if (dst == src) + return (dst); + i = 0; + pd = (unsigned char *) dst; + ps = (unsigned char *) src; + while (n > 0) + { + pd[i] = ps[i]; + i++; + n--; + } + return (dst); +} + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + size_t i; + unsigned char *dstc; + unsigned char *srcc; + + if (dst == NULL && src == NULL) + return (NULL); + dstc = (unsigned char *)dst; + srcc = (unsigned char *)src; + i = 1; + if (srcc < dstc) + { + while (i <= len) + { + dstc[len - i] = srcc[len - i]; + i++; + } + } + else + ft_memcpy(dstc, srcc, len); + return (dst); +} + +void *ft_memset(void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + ((unsigned char *)s)[i] = c; + i++; + } + return (s); +} + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, '\0', n); +} + +void *ft_calloc(size_t count, size_t size) +{ + void *ptr; + + ptr = malloc(count * size); + if (ptr == NULL) + return (NULL); + ft_bzero(ptr, (count * size)); + return (ptr); +} diff --git a/sources/lib_utils_1.c b/sources/lib_utils_1.c index f469a40..f2f641a 100644 --- a/sources/lib_utils_1.c +++ b/sources/lib_utils_1.c @@ -6,7 +6,7 @@ /* By: ablaamim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/04 13:32:51 by ablaamim #+# #+# */ -/* Updated: 2022/04/04 14:41:31 by ablaamim ### ########.fr */ +/* Updated: 2022/04/04 16:59:49 by ablaamim ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/sources/main.c b/sources/main.c index c840812..21a269e 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: ablaamim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/02 14:28:28 by ablaamim #+# #+# */ -/* Updated: 2022/04/03 15:19:43 by ablaamim ### ########.fr */ +/* Updated: 2022/04/04 17:35:40 by ablaamim ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,12 @@ int main(int argc, char **argv) { - char **splited_args; + char **splited_args; + t_stacks stacks; splited_args = ft_args_unified(argc, argv); ft_arguments_validator(argc, splited_args); + ft_stacks_constructor(splited_args, &stacks); + print_stacks(stacks); return (EXIT_SUCCESS); }