From a407fe611354d71330fc352fbc2a9fe268b4104f Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 19 Oct 2019 16:27:29 +0200 Subject: Removed dynamic rest Because if user doesnt read a file until the end, this would cause memory leak. All moulitest tests pass except for large file with certain buffer sizes greater than the line len and smaller than the file len. --- .gitignore | 5 ++++ Makefile | 11 +++++-- get_next_line.c | 82 ++++++++++++++++++++++----------------------------- get_next_line.h | 8 ++--- get_next_line_utils.c | 7 +++-- main.c | 8 +++-- 6 files changed, 63 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index 9bfa054..818f671 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,12 @@ a.out get_next_line *.ghc +t1 +t2 +testrand +testnl test test2 test3 *.dSYM + diff --git a/Makefile b/Makefile index 3542371..ab96c2c 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,14 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 08:11:00 by cacharle #+# #+# # -# Updated: 2019/10/10 11:20:17 by cacharle ### ########.fr # +# Updated: 2019/10/19 14:49:29 by cacharle ### ########.fr # # # # **************************************************************************** # $(RM) = rm -f CC = gcc -CCFLAGS = -Wall -Wextra -Werror +CCFLAGS = -Wall -Wextra -g NAME = get_next_line SRC = get_next_line.c get_next_line_utils.c main.c @@ -35,3 +35,10 @@ fclean: clean $(RM) $(NAME) re: fclean all + +compare: all + @./get_next_line Makefile > t1 + @cat -e Makefile | sed 's/^\(.*\)\$$/[\1]/' > t2 + @diff t1 t2 + + diff --git a/get_next_line.c b/get_next_line.c index 94ef3d8..36edac1 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/08 10:37:41 by cacharle #+# #+# */ -/* Updated: 2019/10/11 14:29:11 by cacharle ### ########.fr */ +/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */ +/* Updated: 2019/10/19 16:19:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,59 +14,56 @@ #include #include "get_next_line.h" -/* -** store rest in local_buf and read diff -** while no newline -> recursion -** newline -> store what is after newline -** allocate line according to stack depth -** cpy before newline at the end -** at each stack pop store local buf on the allocated line -*/ - int get_next_line(int fd, char **line) { int ret; int split_at; char buf[BUFFER_SIZE + 1]; - static char *rest = NULL; + static char rest[BUFFER_SIZE + 1] = {0}; if (fd < 0 || line == NULL) return (ERROR); - while ((ret = read(fd, buf, BUFFER_SIZE)) > 0 || !empty_rest(rest)) + int has_stuff = rest[0] != 0; + *line = put_rest(*line, rest); + if (rest[0] != '\0') + return (LINE_READ); + while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)// || rest[0] != '\0') { - buf[ret] = 0; - rest = ft_strappend(rest, buf); - if ((split_at = find_newline(rest)) == -1 && ret == 0) - split_at = ft_strlen(rest); + buf[ret] = '\0'; + split_at = find_newline(buf); if (split_at == -1) + { + *line = ft_strappend(*line, buf); continue ; - if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) - return (ERROR); - ft_strncpy(*line, rest, split_at); - (*line)[split_at] = '\0'; - return ((rest = shift_rest(rest, split_at)) == NULL - ? ERROR : LINE_READ); + } + buf[split_at] = '\0'; + ft_strncpy(rest, buf + split_at + 1, BUFFER_SIZE + 1); + *line = ft_strappend(*line, buf); + return LINE_READ; } - free(rest); - rest = NULL; + if (has_stuff) + return (LINE_READ); return (ret == -1 ? ERROR : END_OF_FILE); } -char *shift_rest(char *rest, int split_at) +char *put_rest(char *line, char *rest) { - char *copy; - int len; + int split_at; - len = ft_strlen(rest) - split_at + 1; - if ((copy = (char*)malloc(sizeof(char) * len)) == NULL) - return (NULL); - ft_strncpy(copy, rest + split_at + 1, len); - free(rest); - if ((rest = (char*)malloc(sizeof(char) * len)) == NULL) - return (NULL); - ft_strncpy(rest, copy, len); - free(copy); - return (rest); + split_at = find_newline(rest); + if (split_at == -1) + { + line = malloc(sizeof(char) * (ft_strlen(rest) + 1)); + ft_strcpy(line, rest); + /* line[ft_strlen(rest)] = 0; */ + rest[0] = '\0'; + return (line); + } + line = malloc(sizeof(char) * (split_at + 1)); + ft_strncpy(line, rest, split_at + 1); + line[split_at] = '\0'; + ft_strncpy(rest, rest + split_at + 1, BUFFER_SIZE); + return (line); } int find_newline(char *str) @@ -79,12 +76,3 @@ int find_newline(char *str) return (i); return (-1); } - -t_bool empty_rest(char *rest) -{ - if (rest == NULL) - return (TRUE); - if (rest[0] == 0) - return (TRUE); - return (FALSE); -} diff --git a/get_next_line.h b/get_next_line.h index 9f7aabd..619cb9b 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */ -/* Updated: 2019/10/11 14:27:22 by cacharle ### ########.fr */ +/* Updated: 2019/10/19 16:24:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ # define GET_NEXT_LINE_H # ifndef BUFFER_SIZE -# define BUFFER_SIZE 32 +# define BUFFER_SIZE 100000 # endif # define LINE_READ 1 @@ -26,14 +26,14 @@ typedef int t_bool; +#include /* ** get_next_line.c */ int get_next_line(int fd, char **line); -char *shift_rest(char *rest, int split_at); +char *put_rest(char *line, char *rest); int find_newline(char *str); -t_bool empty_rest(char *rest); /* ** get_next_line_utils.c - helper functions diff --git a/get_next_line_utils.c b/get_next_line_utils.c index e024c12..929e37b 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */ -/* Updated: 2019/10/11 14:29:34 by cacharle ### ########.fr */ +/* Updated: 2019/10/19 14:09:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,14 +47,15 @@ char *ft_strappend(char *dest, char *src) if ((dest = (char*)malloc(sizeof(char) * (ft_strlen(src) + 1))) == NULL) return (NULL); ft_strcpy(dest, src); + /* printf("%s - %s\n", dest, src); */ return (dest); } if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL) return (NULL); ft_strcpy(copy, dest); /* free(dest); */ - if ((dest = (char*)malloc(sizeof(char) - * (ft_strlen(dest) + ft_strlen(src) + 1))) == NULL) + dest = (char*)malloc(sizeof(char) * (ft_strlen(dest) + ft_strlen(src) + 1)); + if (dest == NULL) return (NULL); ft_strcpy(dest, copy); free(copy); diff --git a/main.c b/main.c index 91bca98..da6662f 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/10 09:27:41 by cacharle #+# #+# */ -/* Updated: 2019/10/10 11:04:31 by cacharle ### ########.fr */ +/* Updated: 2019/10/19 09:01:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,12 @@ int main(int argc, char **argv) char *line; int ret; + if (argc != 2) + { + printf("You forgot the filename"); + exit(1); + } printf("BUFFER_SIZE = %d\n", BUFFER_SIZE); - (void)argc; fd = open(argv[1], O_RDONLY); while ((ret = get_next_line(fd, &line)) == LINE_READ) { -- cgit