diff --git a/Makefile b/Makefile index 1aeffdf..d3dca84 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,12 @@ NAME = push_swap OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o))) DEP = $(OBJ:.o=.d) +#============================ +# BONUS CONFIG +#============================ + + + .PHONY: all clean fclean re all: $(NAME) diff --git a/bonus/GNL/get_next_line.c b/bonus/GNL/get_next_line.c new file mode 100644 index 0000000..aa3f21e --- /dev/null +++ b/bonus/GNL/get_next_line.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */ +/* Updated: 2025/11/28 13:47:15 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include + +static int del_before_nl(char buf[BUFFER_SIZE]) +{ + int nl_i; + int i; + char *temp; + + nl_i = index_of_nl(buf, BUFFER_SIZE); + if (nl_i >= 0) + { + temp = ft_substr(buf, nl_i + 1, BUFFER_SIZE - nl_i); + if (!temp) + return (-1); + ft_bzero(buf, BUFFER_SIZE); + i = 0; + while (temp[i]) + { + buf[i] = temp[i]; + i++; + } + free(temp); + } + return (1); +} + +static char *ft_read_one(char buf[BUFFER_SIZE], int fd, char *str) +{ + int temp; + + str = ft_strjoin_new(str, buf, BUFFER_SIZE); + if (!str) + return (NULL); + ft_bzero(buf, BUFFER_SIZE); + temp = read(fd, buf, BUFFER_SIZE); + if (temp < 0) + { + free(str); + return (NULL); + } + if ((!str || ft_strlen(str) == 0) && temp == 0) + { + free(str); + return (NULL); + } + if (temp == 0) + return (str); + return (str); +} + +static char *extract_all_nl(char buf[BUFFER_SIZE], int fd, char *str, int nl_i) +{ + int temp; + + if (!str) + return (NULL); + while (nl_i < 0) + { + str = ft_read_one(buf, fd, str); + if (!str) + return (NULL); + nl_i = index_of_nl(buf, BUFFER_SIZE); + if (nl_i < 0 && !ft_strlen(buf)) + return (str); + } + str = ft_strjoin_new(str, buf, nl_i); + if (!str) + return (NULL); + temp = del_before_nl(buf); + if (temp < 0) + { + free(str); + return (NULL); + } + return (str); +} + +char *get_next_line(int fd) +{ + static char buf[BUFFER_SIZE + 1]; + int nl_i; + int temp; + char *str; + + if (fd < 0 || BUFFER_SIZE <= 0) + return (NULL); + nl_i = index_of_nl(buf, BUFFER_SIZE); + if (nl_i >= 0) + { + str = ft_substr(buf, 0, nl_i + 1); + temp = del_before_nl(buf); + if (temp < 0) + return (free(str), NULL); + } + else + str = extract_all_nl(buf, fd, ft_substr("", 0, 1), nl_i); + return (str); +} diff --git a/bonus/GNL/get_next_line.h b/bonus/GNL/get_next_line.h new file mode 100644 index 0000000..7f43fa9 --- /dev/null +++ b/bonus/GNL/get_next_line.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */ +/* Updated: 2025/11/27 18:05:13 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif + +# include + +char *get_next_line(int fd); + +size_t ft_strlen(const char *s); +char *ft_strjoin_new(char const *s1, char const *s2, size_t limit); +void ft_bzero(void *s, size_t n); +int index_of_nl(char *str, int limit); +char *ft_substr(char const *s, unsigned int start, size_t len); + +#endif diff --git a/bonus/GNL/get_next_line_utils.c b/bonus/GNL/get_next_line_utils.c new file mode 100644 index 0000000..a84c3d5 --- /dev/null +++ b/bonus/GNL/get_next_line_utils.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */ +/* Updated: 2025/11/27 18:06:51 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include + +int index_of_nl(char *str, int limit) +{ + int i; + + if (!str) + return (-1); + i = 0; + while (i < limit && str[i]) + { + if (str[i] == '\n') + return (i); + i++; + } + return (-1); +} + +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s[i]) + i++; + return (i); +} + +char *ft_strjoin_new(char const *s1, char const *s2, size_t limit) +{ + char *str; + size_t i; + size_t j; + + str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!str) + return (NULL); + i = 0; + j = 0; + while (s1[i]) + { + str[i] = s1[i]; + i++; + } + while (j <= limit && s2[j]) + { + str[i + j] = s2[j]; + j++; + } + str[i + j] = '\0'; + free((void *) s1); + return (str); +} + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + char *sub_str; + size_t s_len; + + if (!s) + return (NULL); + s_len = ft_strlen(s); + if (start > s_len) + len = 0; + else if (s_len < (start + len)) + len = s_len - start; + sub_str = malloc(sizeof(char) * (len + 1)); + if (!sub_str) + return (NULL); + i = 0; + while (i < len) + { + sub_str[i] = s[start + i]; + i++; + } + sub_str[i] = '\0'; + return (sub_str); +} + +void ft_bzero(void *s, size_t n) +{ + while (n > 0) + { + *((unsigned char *) s) = '\0'; + s++; + n--; + } +} diff --git a/bonus/checker_bonus.c b/bonus/checker_bonus.c new file mode 100644 index 0000000..f025513 --- /dev/null +++ b/bonus/checker_bonus.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* checker_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 11:46:36 by dgaillet #+# #+# */ +/* Updated: 2026/01/09 14:40:06 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include "parsing.h" +#include "get_next_line.h" +#include + +static int apply_operation(t_stacks *stacks, char buf[1024]) +{ + if (!ft_strncmp("sa", buf, ft_strlen(buf))) + return (sa(stacks), 1); + if (!ft_strncmp("sb", buf, ft_strlen(buf))) + return (sb(stacks), 1); + if (!ft_strncmp("ss", buf, ft_strlen(buf))) + return (ss(stacks), 1); + if (!ft_strncmp("pa", buf, ft_strlen(buf))) + return (pa(stacks), 1); + if (!ft_strncmp("pb", buf, ft_strlen(buf))) + return (pb(stacks), 1); + if (!ft_strncmp("ra", buf, ft_strlen(buf))) + return (ra(stacks), 1); + if (!ft_strncmp("rb", buf, ft_strlen(buf))) + return (rb(stacks), 1); + if (!ft_strncmp("rr", buf, ft_strlen(buf))) + return (rr(stacks), 1); + if (!ft_strncmp("rra", buf, ft_strlen(buf))) + return (rra(stacks), 1); + if (!ft_strncmp("rrb", buf, ft_strlen(buf))) + return (rrb(stacks), 1); + if (!ft_strncmp("rrr", buf, ft_strlen(buf))) + return (rrr(stacks), 1); + return (0); +} + +static void tester(t_stacks *stacks) +{ + char *buf; + + buf = get_next_line(0); + while (buf) + { + if (!apply_operation(stacks, buf)) + break ; + free(buf); + buf = get_next_line(0); + } + if () +} + +int main(int argc, char **argv) +{ + t_stacks *stacks; + + +} diff --git a/bonus/ft_bzero.c b/bonus/ft_bzero.c new file mode 100644 index 0000000..95b2a0b --- /dev/null +++ b/bonus/ft_bzero.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 11:59:20 by dgaillet #+# #+# */ +/* Updated: 2026/01/09 11:59:46 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +void ft_bzero(void *s, unsigned int n) +{ + while (n > 0) + { + *((unsigned char *) s) = '\0'; + s++; + n--; + } +}