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 | |
| 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
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | get_next_line.c | 84 | ||||
| -rw-r--r-- | get_next_line.h | 33 | ||||
| -rw-r--r-- | get_next_line_utils.c | 36 |
4 files changed, 76 insertions, 79 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 08:11:00 by cacharle #+# #+# # -# Updated: 2019/10/08 11:52:21 by cacharle ### ########.fr # +# Updated: 2019/10/09 16:42:37 by cacharle ### ########.fr # # # # **************************************************************************** # 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; } diff --git a/get_next_line.h b/get_next_line.h index d4c866a..7aa51e9 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -1,15 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 16:24:58 by cacharle #+# #+# */ +/* Updated: 2019/10/09 16:42:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */ +/* Updated: 2019/10/09 16:24:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H # ifndef BUFFER_SIZE -# define BUFFER_SIZE (1 << 8) +# define BUFFER_SIZE (15) + # endif # define LINE_READ 1 -# define NO_LINE_READ 0 +# define END_OF_FILE 0 # define ERROR -1 - /* ** get_next_line.c */ @@ -21,8 +45,7 @@ int get_next_line(int fd, char **line); */ int find_newline(char *str); -char *strappend(char *dest, char *src); -char *ft_strcpy(char *dest, const char *src); +char *ft_strncpy(char *dest, const char *src, size_t n); char *ft_strcat(char *dest, const char *src); int ft_strlen(char *str); diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 754bf22..8bae2c1 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/08 14:57:48 by cacharle ### ########.fr */ +/* Updated: 2019/10/09 16:24:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,42 +25,18 @@ int find_newline(char *str) return (-1); } -char *strappend(char *dest, char *src) +char *ft_strncpy(char *dest, const char *src, size_t n) { - void *copy; - - if (dest == NULL) - { - if ((dest = (char*)malloc(sizeof(char) * (ft_strlen(src) + 1))) == NULL) - return (NULL); - ft_strcpy(dest, src); - return (dest); - } - if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL) - return (NULL); - ft_strcpy(copy, dest); - free(dest); - if ((dest = (char*)malloc(sizeof(char) - * (ft_strlen(dest) + ft_strlen(src) + 1))) == NULL) - return (NULL); - ft_strcpy(dest, copy); - /* dest[ft_strlen(dest) + ft_strlen(src)] = '\0'; */ - free(copy); - ft_strcat(dest, src); - return (dest); -} - -char *ft_strcpy(char *dest, const char *src) -{ - int i; + size_t i; i = 0; - while (src[i]) + while (src[i] && i < n) { dest[i] = src[i]; i++; } - dest[i] = '\0'; + while (i < n) + dest[i++] = '\0'; return (dest); } |
