diff options
| -rw-r--r-- | get_next_line.c | 67 | ||||
| -rw-r--r-- | get_next_line.h | 6 | ||||
| -rw-r--r-- | get_next_line_utils.c | 14 | ||||
| -rw-r--r-- | main.c | 2 |
4 files changed, 48 insertions, 41 deletions
diff --git a/get_next_line.c b/get_next_line.c index 36edac1..0a5b001 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */ -/* Updated: 2019/10/19 16:19:25 by cacharle ### ########.fr */ +/* Updated: 2019/10/20 07:39:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,27 @@ #include <stdlib.h> #include "get_next_line.h" +/* +** if has rest: +** if rest has newline: +** push rest until newline in line, shift rest +** return LINE_READ +** else: +** push rest in line +** +** while can read fd in buf +** if buf has newline: +** push buf until newline in line +** push buf after newline in rest +** return LINE_READ +** push buf in line +** +** return END_OF_FILE +*/ + int get_next_line(int fd, char **line) { + t_bool had_rest; int ret; int split_at; char buf[BUFFER_SIZE + 1]; @@ -23,47 +42,43 @@ int get_next_line(int fd, char **line) if (fd < 0 || line == NULL) return (ERROR); - 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') + had_rest = put_rest(line, rest); + while (rest[0] == '\0' && (ret = read(fd, buf, BUFFER_SIZE)) > 0) { buf[ret] = '\0'; - split_at = find_newline(buf); - if (split_at == -1) + if ((split_at = find_newline(buf)) != -1) { + ft_strncpy(rest, buf + split_at + 1, BUFFER_SIZE); + buf[split_at] = '\0'; *line = ft_strappend(*line, buf); - continue ; + return (*line == 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; + if ((*line = ft_strappend(*line, buf)) == NULL) + return (ERROR); } - if (has_stuff) + if (had_rest) return (LINE_READ); return (ret == -1 ? ERROR : END_OF_FILE); } -char *put_rest(char *line, char *rest) +int put_rest(char **line, char *rest) { - int split_at; + int split_at; + t_bool had_rest; - split_at = find_newline(rest); - if (split_at == -1) + had_rest = rest[0] != '\0'; + if ((split_at = find_newline(rest)) == -1) { - line = malloc(sizeof(char) * (ft_strlen(rest) + 1)); - ft_strcpy(line, rest); - /* line[ft_strlen(rest)] = 0; */ + *line = malloc(sizeof(char) * (ft_strlen(rest) + 1)); + ft_strcpy(*line, rest); rest[0] = '\0'; - return (line); + return (had_rest); } - line = malloc(sizeof(char) * (split_at + 1)); - ft_strncpy(line, rest, split_at + 1); - line[split_at] = '\0'; + *line = malloc(sizeof(char) * (split_at + 1)); + ft_strncpy(*line, rest, split_at); + (*line)[split_at] = '\0'; ft_strncpy(rest, rest + split_at + 1, BUFFER_SIZE); - return (line); + return (had_rest); } int find_newline(char *str) diff --git a/get_next_line.h b/get_next_line.h index 619cb9b..15f9475 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */ -/* Updated: 2019/10/19 16:24:03 by cacharle ### ########.fr */ +/* Updated: 2019/10/20 07:39:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ # define GET_NEXT_LINE_H # ifndef BUFFER_SIZE -# define BUFFER_SIZE 100000 +# define BUFFER_SIZE 100 # endif # define LINE_READ 1 @@ -32,7 +32,7 @@ typedef int t_bool; */ int get_next_line(int fd, char **line); -char *put_rest(char *line, char *rest); +int put_rest(char **line, char *rest); int find_newline(char *str); /* diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 929e37b..9bc96d9 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */ -/* Updated: 2019/10/19 14:09:48 by cacharle ### ########.fr */ +/* Updated: 2019/10/20 07:39:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,19 +42,11 @@ char *ft_strappend(char *dest, char *src) { void *copy; - if (dest == NULL) - { - 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); */ - dest = (char*)malloc(sizeof(char) * (ft_strlen(dest) + ft_strlen(src) + 1)); + free(dest); + dest = (char*)malloc(sizeof(char) * (ft_strlen(copy) + ft_strlen(src) + 1)); if (dest == NULL) return (NULL); ft_strcpy(dest, copy); @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/10 09:27:41 by cacharle #+# #+# */ -/* Updated: 2019/10/19 09:01:18 by cacharle ### ########.fr */ +/* Updated: 2019/10/20 07:13:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ |
