From 47ca76eb98d578cc78a6315a5f98504a8241cfec Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 3 Nov 2019 23:38:32 +0100 Subject: changed rest bufs back to not malloc'd, empty line on EOF --- .gitignore | 2 +- README.md | 2 +- get_next_line.c | 33 ++++++++++++++++++--------------- get_next_line.h | 4 ++-- get_next_line_utils.c | 4 +--- main.c | 6 +++++- 6 files changed, 28 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 25bfce3..3092d15 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ test2 test3 large_file.txt *.dSYM - +42TESTERS-GNL diff --git a/README.md b/README.md index 969790a..6a6d37b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # get_next_line -get_next_line project from school 42 +get_next_line project of school 42 diff --git a/get_next_line.c b/get_next_line.c index 62878b8..602bbb7 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */ -/* Updated: 2019/11/03 00:33:16 by cacharle ### ########.fr */ +/* Updated: 2019/11/04 00:00:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,15 +39,17 @@ int get_next_line(int fd, char **line) { int split_at; - static char *rest[OPEN_MAX] = {NULL}; + static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}}; if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE <= 0) return (STATUS_ERROR); - *line = NULL; - if (rest[fd] == NULL || rest[fd][0] == 0) - return (read_line(fd, line, &rest[fd])); + if ((*line = ft_strdup("")) == NULL) + return (STATUS_ERROR); + if (rest[fd][0] == '\0') + return (read_line(fd, line, rest[fd])); if (HAS_NEWLINE(rest[fd], split_at)) { + free(*line); if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) return (STATUS_ERROR); ft_strncpy(*line, rest[fd], split_at); @@ -55,38 +57,39 @@ int get_next_line(int fd, char **line) ft_strcpy(rest[fd], rest[fd] + split_at + 1); return (STATUS_LINE); } + free(*line); if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) return (STATUS_ERROR); ft_strcpy(*line, rest[fd]); - free(rest[fd]); - rest[fd] = NULL; - return (read_line(fd, line, &rest[fd])); + rest[fd][0] = '\0'; + return (read_line(fd, line, rest[fd])); } -int read_line(int fd, char **line, char **rest) +int read_line(int fd, char **line, char *rest) { int ret; int split_at; char *buf; if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL) - return (STATUS_ERROR); + return (free_return(line, NULL, STATUS_ERROR)); while ((ret = read(fd, buf, BUFFER_SIZE)) > 0) { buf[ret] = '\0'; if (HAS_NEWLINE(buf, split_at)) { - free(*rest); - *rest = ft_strdup(buf + split_at + 1); + ft_strcpy(rest, buf + split_at + 1); buf[split_at] = '\0'; if ((*line = ft_strappend(*line, buf)) == NULL) - return (free_return(&buf, rest, STATUS_ERROR)); + return (free_return(&buf, NULL, STATUS_ERROR)); return (free_return(&buf, NULL, STATUS_LINE)); } if ((*line = ft_strappend(*line, buf)) == NULL) - return (free_return(&buf, rest, STATUS_ERROR)); + return (free_return(&buf, NULL, STATUS_ERROR)); } - return (free_return(&buf, rest, ret)); + if (ret == -1) + return (free_return(&buf, line, STATUS_ERROR)); + return (free_return(&buf, NULL, ret)); } int find_newline(char *str) diff --git a/get_next_line.h b/get_next_line.h index 6a6ccca..7a2dd27 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/11/03 00:29:53 by cacharle ### ########.fr */ +/* Updated: 2019/11/03 22:43:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ typedef int t_bool; */ int get_next_line(int fd, char **line); -int read_line(int fd, char **line, char **rest); +int read_line(int fd, char **line, char *rest); int find_newline(char *str); int free_return(char **ptr, char **rest, int ret); diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 096fb84..637842c 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/11/03 00:26:34 by cacharle ### ########.fr */ +/* Updated: 2019/11/03 22:57:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,6 @@ char *ft_strappend(char *dest, char *src) void *copy; int dest_len; - if (dest == NULL) - return (ft_strdup(src)); dest_len = ft_strlen(dest); if ((copy = (char*)malloc(sizeof(char) * (dest_len + 1))) == NULL) return (NULL); diff --git a/main.c b/main.c index ff09b7b..04896f2 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/10 09:27:41 by cacharle #+# #+# */ -/* Updated: 2019/11/02 22:42:58 by cacharle ### ########.fr */ +/* Updated: 2019/11/03 22:18:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,11 @@ int main(int argc, char **argv) if (ret == -1) printf("error\n"); else if (ret == 0) + { + printf("at EOF: [%s]\n", line); + free(line); printf("EOF\n"); + } close(fd); return (0); } -- cgit