diff options
| author | Dominik Kaiser | 2024-04-12 16:55:57 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-04-12 16:55:57 +0200 |
| commit | 358e300bc148d2a223e1cfa2a3c49da6c0ba968c (patch) | |
| tree | f79772df1f0d5590b71c3ca1e70370ec86540aab | |
| parent | 0560de9d3932735737ac9118c626a7ced0eedfd2 (diff) | |
| download | libft-358e300bc148d2a223e1cfa2a3c49da6c0ba968c.tar.gz libft-358e300bc148d2a223e1cfa2a3c49da6c0ba968c.zip | |
Add get_next_line to libft
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | get_next_line.c | 84 | ||||
| -rw-r--r-- | get_next_line.h | 26 | ||||
| -rw-r--r-- | get_next_line_utils.c | 60 | ||||
| -rw-r--r-- | libft.h | 3 |
5 files changed, 176 insertions, 3 deletions
@@ -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 index 0000000..46edb73 --- /dev/null +++ b/get_next_line.c @@ -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 index 0000000..63c477b --- /dev/null +++ b/get_next_line.h @@ -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 index 0000000..61171be --- /dev/null +++ b/get_next_line_utils.c @@ -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); +} @@ -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> |
