diff options
Diffstat (limited to 'get_next_line.c')
| -rw-r--r-- | get_next_line.c | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/get_next_line.c b/get_next_line.c index 25cfc90..082cd6f 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/08 10:37:41 by cacharle #+# #+# */ -/* Updated: 2019/10/09 16:50:40 by cacharle ### ########.fr */ +/* Updated: 2019/10/10 10:18:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,65 +14,64 @@ #include <stdlib.h> #include "get_next_line.h" -#include <stdio.h> - -// if has rest -// store it in local_buf and read diff -// else -// read full local buf -// 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 read_after(int fd, char *buf, char *rest_buf) -{ - int offset = ft_strlen(rest_buf); - ft_strncpy(buf, rest_buf, offset); - rest_buf[0] = 0; - return (read(fd, buf + offset, BUFFER_SIZE - offset)); -} +/* +** 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 +*/ +#include <stdio.h> int get_next_line(int fd, char **line) { - int split_at; - char local_buf[BUFFER_SIZE + 1]; - static int buf_count = 0; + int ret; + int split_at; + char local_buf[BUFFER_SIZE + 1]; + static int line_len = 0; static char rest_buf[BUFFER_SIZE + 1] = ""; local_buf[0] = '\0'; - if (read_after(fd, local_buf, rest_buf) == 0) - return (END_OF_FILE); - split_at = find_newline(local_buf); - if (split_at == -1) - { - buf_count++; - int ret = get_next_line(fd, line); - buf_count--; - ft_strncpy(*line + (buf_count * BUFFER_SIZE), local_buf, BUFFER_SIZE); + if ((ret = read_after(fd, local_buf, rest_buf)) <= 0) return (ret); + if ((split_at = find_newline(local_buf)) != -1) + { + ft_strncpy(rest_buf, local_buf + split_at + 1, + ft_strlen(local_buf) - split_at); + if ((*line = malloc(sizeof(char) * (line_len + split_at + 1))) == NULL) + return (ERROR); + ft_strncpy(*line + line_len, local_buf, split_at); + (*line)[line_len + split_at] = '\0'; + return (LINE_READ); } - ft_strncpy(rest_buf, local_buf + split_at + 1, ft_strlen(local_buf) - split_at); - *line = malloc(sizeof(char) * (buf_count * BUFFER_SIZE + split_at + 1)); - ft_strncpy(*line + (buf_count * BUFFER_SIZE), local_buf, split_at); - (*line)[buf_count * BUFFER_SIZE + split_at] = '\0'; - return (LINE_READ); + line_len += BUFFER_SIZE; + if ((ret = get_next_line(fd, line)) == -1) + return (ERROR); + line_len -= BUFFER_SIZE; + ft_strncpy(*line + line_len, local_buf, BUFFER_SIZE); + return (ret); } -#include <stdio.h> -#include <fcntl.h> -int main() +int read_after(int fd, char *buf, char *rest_buf) { - int fd = open("Makefile", O_RDONLY); - char *line = NULL; - /* int ret = 1; */ - while (get_next_line(fd, &line) == 1) + int offset; + int ret; + + offset = ft_strlen(rest_buf); + ft_strncpy(buf, rest_buf, offset); + /* printf(">%s<\n", buf); */ + rest_buf[0] = '\0'; + if ((ret = read(fd, buf + offset, BUFFER_SIZE - offset)) == -1) + return (ERROR); + if (ret == 0) + return (0); + if (ret < BUFFER_SIZE - offset) { - printf(">%s\n", line); - free(line); + /* buf[offset + ret - 1] = '\0'; */ + /* printf(">eof? %s<\n", buf); */ } - close(fd); - return 0; + else + buf[BUFFER_SIZE] = '\0'; + return (ret); } |
