diff options
| author | Cabergs Charles <cacharle@e-r5-p7.s19.be> | 2019-10-09 16:53:13 +0200 |
|---|---|---|
| committer | Cabergs Charles <cacharle@e-r5-p7.s19.be> | 2019-10-09 16:53:13 +0200 |
| commit | e1e832e8f63046bdea329a8f82b6d88131c1d09e (patch) | |
| tree | e22e9daa275a55def8cdeaebe5c22c60cb08e3e3 /get_next_line.c | |
| parent | 7a7262aa245258236f769c1baa5de24a3abb9b15 (diff) | |
| download | get_next_line-e1e832e8f63046bdea329a8f82b6d88131c1d09e.tar.gz get_next_line-e1e832e8f63046bdea329a8f82b6d88131c1d09e.tar.bz2 get_next_line-e1e832e8f63046bdea329a8f82b6d88131c1d09e.zip | |
WIP: removed free
Doesnt work with some buffer sizes
Diffstat (limited to 'get_next_line.c')
| -rw-r--r-- | get_next_line.c | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/get_next_line.c b/get_next_line.c index b6f1a41..25cfc90 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/08 15:35:43 by cacharle ### ########.fr */ +/* Updated: 2019/10/09 16:50:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,42 +14,50 @@ #include <stdlib.h> #include "get_next_line.h" -/* static char *split_newline(char *buf, char **line) */ -/* { */ -/* int split_index; */ -/* */ -/* split_index = find_newline(buf); */ -/* if (split_index != -1) */ -/* buf[split_index] = '\0'; */ -/* ft_strappend(*line, buf); */ -/* if (split_index == -1) */ -/* return (NULL); */ -/* return (buf + split_index); */ -/* } */ - #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)); +} + int get_next_line(int fd, char **line) { - int slice_index; - static char buf[BUFFER_SIZE + 1] = ""; + int split_at; + char local_buf[BUFFER_SIZE + 1]; + static int buf_count = 0; + static char rest_buf[BUFFER_SIZE + 1] = ""; - /* *line = NULL; */ - if (buf[0] == '\0') + 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) { - if (read(fd, buf, BUFFER_SIZE) == 0) - return (NO_LINE_READ); + buf_count++; + int ret = get_next_line(fd, line); + buf_count--; + ft_strncpy(*line + (buf_count * BUFFER_SIZE), local_buf, BUFFER_SIZE); + return (ret); } - /* printf("%s\n", *line); */ - slice_index = find_newline(buf); - if (slice_index == -1) - { - *line = strappend(*line, buf); - buf[0] = '\0'; - return (get_next_line(fd, line)); - } - buf[slice_index] = '\0'; - *line = strappend(*line, buf); - ft_strcpy(buf, buf + slice_index + 1); + 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); } @@ -59,22 +67,12 @@ int main() { int fd = open("Makefile", O_RDONLY); char *line = NULL; - int ret = 1; + /* int ret = 1; */ while (get_next_line(fd, &line) == 1) { - printf("> %s\n", line); + printf(">%s\n", line); free(line); - line = NULL; } - /* free(line); */ - - /* char c; */ - /* while (read(fd, &c, 1) > 0) */ - /* printf("%c", c); */ - /* while (read(fd, &c, 1) > 0) */ - /* printf("%c", c); */ - /* while (read(fd, &c, 1) > 0) */ - /* printf("%c", c); */ close(fd); return 0; } |
