diff options
Diffstat (limited to 'get_next_line.c')
| -rw-r--r-- | get_next_line.c | 82 |
1 files changed, 35 insertions, 47 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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 <stdlib.h> #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); -} |
