]> git.dkaiser.de - 42/libft.git/commitdiff
Add get_next_line to libft
authorDominik Kaiser <dkaiser@2-F-4.42heilbronn.de>
Fri, 12 Apr 2024 14:55:57 +0000 (16:55 +0200)
committerDominik Kaiser <dkaiser@2-F-4.42heilbronn.de>
Fri, 12 Apr 2024 14:55:57 +0000 (16:55 +0200)
Makefile
get_next_line.c [new file with mode: 0644]
get_next_line.h [new file with mode: 0644]
get_next_line_utils.c [new file with mode: 0644]
libft.h

index 6bf4a1a40bbb5179157aa6bdf3b454a4ac631b46..7bab2675ec516def56a05d0442b74357e1178d97 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -39,7 +39,9 @@ SRC_FILES = ft_atoi.c \
                        ft_printf.c \
                        ft_printnbr.c \
                        ft_printhex.c \
-                       ft_printaddr.c
+                       ft_printaddr.c \
+                       get_next_line.c \
+                       get_next_line_utils.c
 
 
 OBJ_FILES = $(SRC_FILES:.c=.o)
@@ -62,7 +64,7 @@ $(NAME): $(OBJ_FILES)
        ar rcs $(NAME) $(OBJ_FILES)
 
 clean:
-       rm -f $(OBJ_FILES) $(BONUS_FILES)
+       rm -f $(OBJ_FILES) $(BONUS_OBJ)
 
 fclean: clean
        rm -f $(NAME)
diff --git a/get_next_line.c b/get_next_line.c
new file mode 100644 (file)
index 0000000..46edb73
--- /dev/null
@@ -0,0 +1,84 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 14:13:51 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/25 15:26:21 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+
+static int     get_next_line_len(char *buf, int pos)
+{
+       int     len;
+
+       len = 0;
+       while (pos + len < BUFFER_SIZE)
+       {
+               if (buf[pos + len] == '\n')
+               {
+                       len++;
+                       break ;
+               }
+               if (!buf[pos + len])
+                       break ;
+               len++;
+       }
+       return (len);
+}
+
+static void    get_next_line_rec(int fd, char *buf, char **result, int pos)
+{
+       int     len;
+       int     i;
+       int     readlen;
+
+       len = get_next_line_len(buf, pos);
+       *result = str_add_buffer(*result, buf + pos, len);
+       if (pos + len == BUFFER_SIZE && buf[BUFFER_SIZE - 1] == '\n')
+               return (clear_buffer(buf, pos));
+       i = 0;
+       while (i < len)
+               buf[pos + i++] = '\0';
+       if (pos + len == BUFFER_SIZE && *result)
+       {
+               readlen = read(fd, buf, BUFFER_SIZE);
+               if (readlen < 0)
+               {
+                       clear_buffer(buf, 0);
+                       free(*result);
+                       *result = NULL;
+               }
+               else if (readlen > 0)
+                       get_next_line_rec(fd, buf, result, 0);
+       }
+}
+
+char   *get_next_line(int fd)
+{
+       static char     buf[BUFFER_SIZE];
+       int                     i;
+       char            *result;
+       int                     readlen;
+
+       i = 0;
+       readlen = 0;
+       result = NULL;
+       while (i < BUFFER_SIZE && !buf[i])
+               i++;
+       if (i == BUFFER_SIZE)
+       {
+               readlen = read(fd, buf, BUFFER_SIZE);
+               if (readlen < 0)
+                       clear_buffer(buf, 0);
+               else if (readlen > 0)
+                       return (get_next_line(fd));
+               return (NULL);
+       }
+       get_next_line_rec(fd, buf, &result, i);
+       return (result);
+}
diff --git a/get_next_line.h b/get_next_line.h
new file mode 100644 (file)
index 0000000..63c477b
--- /dev/null
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line.h                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 14:14:07 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/25 14:19:27 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef GET_NEXT_LINE_H
+# define GET_NEXT_LINE_H
+# include <stdlib.h>
+# include <unistd.h>
+
+# ifndef BUFFER_SIZE
+#  define BUFFER_SIZE 42
+# endif
+
+int            ft_strlen(const char *str);
+char   *get_next_line(int fd);
+void   clear_buffer(char *buf, int start);
+char   *str_add_buffer(char *old_str, char *buf, int buf_len);
+#endif // GET_NEXT_LINE_H
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
new file mode 100644 (file)
index 0000000..61171be
--- /dev/null
@@ -0,0 +1,60 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line_utils.c                              :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/15 14:14:59 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/26 10:49:08 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+
+int    ft_strlen(const char *str)
+{
+       int     len;
+
+       if (!str)
+               return (0);
+       len = 0;
+       while (str[len])
+               len++;
+       return (len);
+}
+
+void   clear_buffer(char *buf, int start)
+{
+       while (start < BUFFER_SIZE)
+               buf[start++] = '\0';
+}
+
+char   *str_add_buffer(char *old_str, char *buf, int buf_len)
+{
+       char    *result;
+       int             len;
+       int             i;
+
+       if (!old_str)
+               len = buf_len;
+       else
+               len = ft_strlen(old_str) + buf_len;
+       result = malloc(sizeof(char) * (len + 1));
+       if (!result)
+       {
+               free(old_str);
+               return (NULL);
+       }
+       result[len] = '\0';
+       i = 0;
+       while (old_str && old_str[i])
+       {
+               result[i] = old_str[i];
+               i++;
+       }
+       while (i < len)
+               result[i++] = *(buf++);
+       free(old_str);
+       return (result);
+}
diff --git a/libft.h b/libft.h
index 0e35f359d1541fdf054c5957231db90983f0b0b3..e0c169149467459a8b597b43ce5b4ce65caf71ad 100644 (file)
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/10 16:37:54 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/12 16:44:02 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/12 16:50:28 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,6 +14,7 @@
 # define LIBFT_H
 
 # include "ft_printf.h"
+# include "get_next_line.h"
 # include <stdlib.h>
 # include <string.h>
 # include <unistd.h>