From 0cc70e4eb5eb935901d69e55dddce114120dc5ec Mon Sep 17 00:00:00 2001 From: Angel Ortigosa Perez Date: Sat, 15 Nov 2025 11:12:17 +0100 Subject: [PATCH] gnl --- Makefile | 28 +++++++++ get_next_line.c | 73 +++++++++++++++++++++++ get_next_line.h | 32 ++++++++++ get_next_line_utils.c | 132 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 265 insertions(+) create mode 100644 Makefile create mode 100644 get_next_line.c create mode 100644 get_next_line.h create mode 100644 get_next_line_utils.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..50bdd61 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +CC = cc + +CFLAGS = -Wall -Wextra -Werror + +SRCS = get_next_line.c get_next_line_utils.c + +OBJS = $(SRCS:.c=.o) + +NAME = get_next_line.a + +all: $(NAME) + +$(NAME): $(OBJS) + ar rcs $(NAME) $(OBJS) + ranlib $(NAME) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/get_next_line.c b/get_next_line.c new file mode 100644 index 0000000..968efaa --- /dev/null +++ b/get_next_line.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/15 18:37:44 by aortigos #+# #+# */ +/* Updated: 2024/03/15 19:31:56 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +char *ft_read_to_left_str(int fd, char *left_str) +{ + char *buff; + int rd_bytes; + + buff = malloc((BUFFER_SIZE + 1) * sizeof(char)); + if (!buff) + return (NULL); + rd_bytes = 1; + while (!ft_strchr(left_str, '\n') && rd_bytes != 0) + { + rd_bytes = read(fd, buff, BUFFER_SIZE); + if (rd_bytes == -1) + { + free(left_str); + free(buff); + return (NULL); + } + buff[rd_bytes] = '\0'; + left_str = ft_strjoin(left_str, buff); + } + free(buff); + return (left_str); +} + +char *get_next_line(int fd) +{ + char *line; + static char *left_str; + + if (fd < 0 || BUFFER_SIZE <= 0) + { + return (0); + } + left_str = ft_read_to_left_str(fd, left_str); + if (!left_str) + { + free(left_str); + return (NULL); + } + line = ft_get_line(left_str); + left_str = ft_new_left_str(left_str); + return (line); +} + +/* +int main() +{ + int fd = open("test.txt", O_RDONLY); + char *line; + + while((line = get_next_line(fd)) != NULL) + { + printf("Linea leida: %s\n", line); + free(line); + } + close(fd); + return (0); +}*/ diff --git a/get_next_line.h b/get_next_line.h new file mode 100644 index 0000000..c7892e7 --- /dev/null +++ b/get_next_line.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/15 18:52:24 by aortigos #+# #+# */ +/* Updated: 2024/03/15 18:52:26 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 10000 +# endif + +char *get_next_line(int fd); +char *ft_read_to_left_str(int fd, char *left_str); +size_t ft_strlen(char *s); +char *ft_strchr(char *s, int c); +char *ft_strjoin(char *left_str, char *buff); +char *ft_get_line(char *left_str); +char *ft_new_left_str(char *left_str); + +#endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c new file mode 100644 index 0000000..538e574 --- /dev/null +++ b/get_next_line_utils.c @@ -0,0 +1,132 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/15 18:37:57 by aortigos #+# #+# */ +/* Updated: 2024/03/15 18:56:09 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +size_t ft_strlen(char *s) +{ + int i; + + i = 0; + if (!s) + return (0); + while (s[i] != '\0') + { + i++; + } + return (i); +} + +char *ft_strchr(char *s, int c) +{ + int i; + + i = 0; + if (!s) + return (0); + if (c == '\0') + return ((char *)&s[ft_strlen(s)]); + while (s[i] != '\0') + { + if (s[i] == (char) c) + { + return ((char *)&s[i]); + } + i++; + } + return (0); +} + +char *ft_strjoin(char *left_str, char *buff) +{ + size_t i; + size_t j; + char *str; + + if (!left_str) + { + left_str = (char *)malloc(1 * sizeof(char)); + left_str[0] = '\0'; + } + if (!left_str || !buff) + return (NULL); + str = malloc(sizeof(char) * ((ft_strlen(left_str)) + + (ft_strlen(buff))) + 1); + if (str == NULL) + return (NULL); + i = -1; + j = 0; + if (left_str) + while (left_str[++i] != '\0') + str[i] = left_str[i]; + while (buff[j] != '\0') + str[i++] = buff[j++]; + str[ft_strlen(left_str) + ft_strlen(buff)] = '\0'; + free(left_str); + return (str); +} + +char *ft_get_line(char *left_str) +{ + int i; + char *str; + + i = 0; + if (!left_str[i]) + return (NULL); + while (left_str[i] && left_str[i] != '\n') + i++; + str = (char *)malloc(sizeof(char) * (i + 2)); + if (!str) + return (NULL); + i = 0; + while (left_str[i] && left_str[i] != '\n') + { + str[i] = left_str[i]; + i++; + } + if (left_str[i] == '\n') + { + str[i] = left_str[i]; + i++; + } + str[i] = '\0'; + return (str); +} + +char *ft_new_left_str(char *left_str) +{ + int i; + int j; + char *str; + + i = 0; + while (left_str[i] && left_str[i] != '\n') + i++; + if (!left_str[i]) + { + free(left_str); + return (NULL); + } + str = (char *)malloc(sizeof(char) * (ft_strlen(left_str) - i + 1)); + if (!str) + { + return (NULL); + } + i++; + j = 0; + while (left_str[i]) + str[j++] = left_str[i++]; + str[j] = '\0'; + free(left_str); + return (str); +}