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)
ar rcs $(NAME) $(OBJ_FILES)
clean:
- rm -f $(OBJ_FILES) $(BONUS_FILES)
+ rm -f $(OBJ_FILES) $(BONUS_OBJ)
fclean: clean
rm -f $(NAME)
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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);
+}
/* 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 */
/* */
/* ************************************************************************** */
# define LIBFT_H
# include "ft_printf.h"
+# include "get_next_line.h"
# include <stdlib.h>
# include <string.h>
# include <unistd.h>