Made everything pass norminette (split functions and files).
authorLukas Jiriste <ljiriste@student.42prague.com>
Mon, 11 Sep 2023 16:40:22 +0000 (18:40 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Mon, 11 Sep 2023 16:40:22 +0000 (18:40 +0200)
Lightly tested (single file, multiple files, empty file).
Everything seems to work and not leak.

get_next_line.c
get_next_line.h
get_next_line_utils.c [new file with mode: 0644]

index d75f4d12275c64f7c990f9c8dcbcf9b3b1f64bf9..8d431e79826c5f6eb14f3aba042c505946499f34 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/08/28 00:01:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/08/28 00:32:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/11 18:39:13 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <unistd.h>
 #include "get_next_line.h"
 
-void   *ft_memmove(void *dest, const void *src, size_t n)
-{
-       size_t  i;
-
-       if (dest > src)
-       {
-               i = n;
-               while (i > 0)
-               {
-                       --i;
-                       *((unsigned char *)dest + i) = *((unsigned char *)src + i);
-               }
-       }
-       else if (dest < src)
-       {
-               i = 0;
-               while (i < n)
-               {
-                       *((unsigned char *)dest + i) = *((unsigned char *)src + i);
-                       ++i;
-               }
-       }
-       return (dest);
-}
-
-t_list *ft_lstnew(void *content)
-{
-       t_list  *res;
-
-       res = malloc(sizeof(t_list));
-       if (res == NULL)
-               return (res);
-       res->content = content;
-       res->next = NULL;
-       return (res);
-}
-
-t_list *new_ft_node(int fd)
-{
-       t_list  *node;
-       t_ft    *thread;
-
-       thread = malloc(sizeof(t_ft));
-       if (thread == NULL)
-               return (NULL);
-       node = ft_lstnew(thread);
-       if (node == NULL)
-       {
-               free(thread);
-               return (NULL);
-       }
-       thread->buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
-       if (thread->buffer == NULL)
-       {
-               free(thread);
-               free(node);
-               return (NULL);
-       }
-       (thread->buffer)[0] = '\0';
-       thread->fd = fd;
-       return (node);
-}
-
 // How to free?
 // static in get_next_line? So it could be passed to select AND delete
-char   *select_file_buffer(int fd, t_list **list)
+static char    *select_file_buffer(int fd, t_list **list)
 {
        t_list  *local_head;
 
@@ -91,7 +28,7 @@ char  *select_file_buffer(int fd, t_list **list)
                return (((t_ft *)((*list)->content))->buffer);
        }
        local_head = *list;
-       while (local_head->next || ((t_ft *)(local_head->content))->fd != fd)
+       while (local_head->next && ((t_ft *)(local_head->content))->fd != fd)
                local_head = local_head->next;
        if (((t_ft *)(local_head->content))->fd == fd)
                return (((t_ft *)(local_head->content))->buffer);
@@ -101,7 +38,7 @@ char *select_file_buffer(int fd, t_list **list)
        return (((t_ft *)(local_head->next->content))->buffer);
 }
 
-void   delete_file_node(int fd, t_list **list)
+static void    delete_file_node(int fd, t_list **list)
 {
        t_list  *cur;
        t_list  *prev;
@@ -123,30 +60,26 @@ void       delete_file_node(int fd, t_list **list)
        return ;
 }
 
-void   ft_strncat_alloc(char **dest, char *src, int n)
+/*     Concatenates res and buffer up to newline.
+       Shifts buffer by the transfered characters.
+       Returns 0 when buffer is empty (and needs filling with read).
+       Also returns 1 when newline is transfered
+       unless buffer empty (to delete node).
+       Otherwise returns true.
+*/
+static int     transfer_string(char **res, char *buffer)
 {
-       int             size;
-       char    *temp;
+       int     i;
+       int     nl;
 
-       if (n <= 0)
-               return ;
-       temp = *dest;
-       size = 0;
-       if (temp)
-               while (temp[size])
-                       ++size;
-       size += n + 1;
-       *dest = malloc(size);
-       if (*dest == NULL)
-       {
-               free(temp);
-               return ;
-       }
-       ft_memmove(*dest, temp, size - n - 1);
-       ft_memmove(*dest + size - n - 1, src, n);
-       (*dest)[size - 1] = '\0';
-       free(temp);
-       return ;
+       i = 0;
+       while (buffer[i] && buffer[i] != '\n')
+               ++i;
+       nl = (buffer[i] == '\n');
+       i += nl;
+       ft_strncat_alloc(res, buffer, i);
+       ft_memmove(buffer, buffer + i, BUFFER_SIZE + 1 - i);
+       return (buffer[0] && nl);
 }
 
 char   *get_next_line(int fd)
@@ -155,33 +88,25 @@ char       *get_next_line(int fd)
        char                    *buffer;
        char                    *res;
        int                             i;
-       int                             nl_found;
 
        res = NULL;
        buffer = select_file_buffer(fd, &list);
-       i = 0;
-       nl_found = 0;
-       while (!nl_found)
+       while (!transfer_string(&res, buffer))
        {
-               while (buffer[i] && buffer[i] != '\n')
-                       ++i;
-               nl_found = (buffer[i] == '\n');
-               i += nl_found;
-               ft_strncat_alloc(&res, buffer, i);
-               if (buffer[i] == '\0')
+               if (buffer[0] == '\0')
                {
                        i = read(fd, buffer, BUFFER_SIZE);
                        buffer[i] = '\0';
                        if (i <= 0)
                                delete_file_node(fd, &list);
                        if (i < 0)
+                       {
+                               free(res);
                                return (NULL);
+                       }
                        if (i == 0)
                                return (res);
                }
-               else if (i > 0)
-                       ft_memmove(buffer, buffer + i, BUFFER_SIZE + 1 - i);
-               i = 0;
        }
        return (res);
 }
index 662a6d6ee978582955c234edc6352843e65ffe2c..3e15b4dd4775b300c383b0cd8de7f5a321ee0379 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/08/28 11:24:52 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/08/28 11:29:18 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/11 15:41:02 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,7 +14,7 @@
 # define GET_NEXT_LINE_H
 
 # ifndef BUFFER_SIZE
-#  define BUFFER_SIZE 81
+#  define BUFFER_SIZE 80
 # endif
 
 typedef struct s_file_thread
@@ -31,4 +31,11 @@ typedef struct s_list
 
 char   *get_next_line(int fd);
 
+t_list *new_ft_node(int fd);
+t_list *ft_lstnew(void *content);
+void   *ft_memmove(void *dest, const void *src, size_t n);
+void   ft_strncat_alloc(char **dest, char *src, int n);
+
+char   *get_next_line(int fd);
+
 #endif
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
new file mode 100644 (file)
index 0000000..ec03e25
--- /dev/null
@@ -0,0 +1,108 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   get_next_line_utils.c                              :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/09/11 13:36:53 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/09/11 15:22:45 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+/*     Functions in this file may (and should) be substituted by Libft functions
+       after this project is integrated into Libft
+*/
+
+#include <stdlib.h>    // malloc etc.
+#include "get_next_line.h"
+
+// This function may be useful enough to add to Libft
+void   ft_strncat_alloc(char **dest, char *src, int n)
+{
+       int             size;
+       char    *temp;
+
+       if (n <= 0)
+               return ;
+       temp = *dest;
+       size = 0;
+       if (temp)
+               while (temp[size])
+                       ++size;
+       size += n + 1;
+       *dest = malloc(size);
+       if (*dest == NULL)
+       {
+               free(temp);
+               return ;
+       }
+       ft_memmove(*dest, temp, size - n - 1);
+       ft_memmove(*dest + size - n - 1, src, n);
+       (*dest)[size - 1] = '\0';
+       free(temp);
+       return ;
+}
+
+void   *ft_memmove(void *dest, const void *src, size_t n)
+{
+       size_t  i;
+
+       if (dest > src)
+       {
+               i = n;
+               while (i > 0)
+               {
+                       --i;
+                       *((unsigned char *)dest + i) = *((unsigned char *)src + i);
+               }
+       }
+       else if (dest < src)
+       {
+               i = 0;
+               while (i < n)
+               {
+                       *((unsigned char *)dest + i) = *((unsigned char *)src + i);
+                       ++i;
+               }
+       }
+       return (dest);
+}
+
+t_list *ft_lstnew(void *content)
+{
+       t_list  *res;
+
+       res = malloc(sizeof(t_list));
+       if (res == NULL)
+               return (res);
+       res->content = content;
+       res->next = NULL;
+       return (res);
+}
+
+t_list *new_ft_node(int fd)
+{
+       t_list  *node;
+       t_ft    *thread;
+
+       thread = malloc(sizeof(t_ft));
+       if (thread == NULL)
+               return (NULL);
+       node = ft_lstnew(thread);
+       if (node == NULL)
+       {
+               free(thread);
+               return (NULL);
+       }
+       thread->buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
+       if (thread->buffer == NULL)
+       {
+               free(thread);
+               free(node);
+               return (NULL);
+       }
+       (thread->buffer)[0] = '\0';
+       thread->fd = fd;
+       return (node);
+}