commit 503104de653912581ac73d0c80f951531e2db973 Author: Raphaƫl <35407363+EniumRaphael@users.noreply.github.com> Date: Wed Oct 30 12:53:45 2024 +0100 update: pushing the last version of gnl diff --git a/get_next_line.c b/get_next_line.c new file mode 100644 index 0000000..b4c2928 --- /dev/null +++ b/get_next_line.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 17:12:02 by rparodi #+# #+# */ +/* Updated: 2023/11/22 13:31:13 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +char *ft_check(char *memory, int fd) +{ + char *buffer; + int bytescopy; + + bytescopy = 1; + buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1)); + if (!buffer) + return (NULL); + while (ft_strchr(memory, '\n') <= 0 && bytescopy) + { + bytescopy = read(fd, buffer, BUFFER_SIZE); + if (bytescopy == -1) + return (ft_free(buffer)); + buffer[bytescopy] = '\0'; + memory = ft_strjoin(memory, buffer, 0, -1); + } + free(buffer); + return (memory); +} + +char *file_converted(int fd) +{ + size_t i; + int read_bits; + char *str; + char c; + + i = 0; + c = 1; + read_bits = 1; + str = (char *)malloc(sizeof(char) * BUFFER_SIZE); + if (!str) + return (NULL); + while (read_bits != 0 && i < BUFFER_SIZE) + { + read_bits = read(fd, &c, 1); + str[i] = c; + i++; + } + str[i] = '\0'; + return (str); +} + +char *ft_free(char *str) +{ + free(str); + return (NULL); +} + +char *get_next_line(int fd) +{ + static char *memory = NULL; + char *line; + + if (fd < 0 || BUFFER_SIZE <= 0) + return (NULL); + memory = ft_check(memory, fd); + if (!memory) + return (ft_free(memory)); + line = ft_get_line(memory); + memory = ft_get_next(memory); + return (line); +} diff --git a/get_next_line.h b/get_next_line.h new file mode 100644 index 0000000..3f76342 --- /dev/null +++ b/get_next_line.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 17:12:04 by rparodi #+# #+# */ +/* Updated: 2023/11/23 18:15:34 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 420 +# endif + +# include +# include + +char *ft_get_line(char *str); +char *get_next_line(int fd); +int ft_strchr(char *str, char c); +char *ft_get_next(char *str); +char *ft_get_line(char *str); +char *ft_strjoin(char *s1, char *s2, int i, int j); +size_t ft_strlen(char const *str); +char *file_converted(int fd); +char *ft_free(char *str); +char *ft_check(char *memory, int fd); + +#endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c new file mode 100644 index 0000000..b4ad6c4 --- /dev/null +++ b/get_next_line_utils.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 17:14:47 by rparodi #+# #+# */ +/* Updated: 2023/11/22 13:35:03 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +size_t ft_strlen(char const *str) +{ + size_t i; + + i = 0; + if (str) + { + while (str[i]) + i++; + } + return (i); +} + +char *ft_strjoin(char *s1, char *s2, int i, int j) +{ + char *temp; + + if (!s1) + { + s1 = (char *)malloc(1); + s1[i] = '\0'; + } + if (!s2 || !s1) + return (NULL); + temp = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!temp) + return (NULL); + while (s1[i]) + { + temp[i] = s1[i]; + i++; + } + while (s2[++j]) + temp[i + j] = s2[j]; + temp[i + j] = '\0'; + free(s1); + return (temp); +} + +char *ft_get_line(char *str) +{ + char *temp; + int i; + + i = 0; + if (!str[i]) + return (NULL); + while (str[i] && str[i] != '\n') + i++; + temp = (char *)malloc((i + 2)); + if (!temp) + return (NULL); + i = 0; + while (str[i] && str[i] != '\n') + { + temp[i] = str[i]; + i++; + } + if (str[i] == '\n') + { + temp[i] = str[i]; + i++; + } + temp[i] = '\0'; + return (temp); +} + +char *ft_get_next(char *str) +{ + char *temp; + int i; + int j; + + i = 0; + while (str[i] && str[i] != '\n') + i++; + if (!str[i]) + return (ft_free(str)); + temp = (char *)malloc((ft_strlen(str) - i)); + if (!temp) + return (NULL); + j = i + 1; + while (str[++i]) + temp[i - j] = str[i]; + temp[i - j] = '\0'; + free(str); + return (temp); +} + +int ft_strchr(char *str, char c) +{ + int i; + + i = 0; + if (str) + { + while (str[i]) + { + if (str[i] == c) + return (i); + i++; + } + } + return (-1); +}