diff options
Diffstat (limited to 'src/io/ft_getline.c')
| -rw-r--r-- | src/io/ft_getline.c | 77 |
1 files changed, 23 insertions, 54 deletions
diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c index d59aa63..067e66c 100644 --- a/src/io/ft_getline.c +++ b/src/io/ft_getline.c @@ -6,63 +6,34 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/05/12 17:56:59 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:32:50 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int st_find_newline(char *str) -{ - int i; - - i = -1; - while (str[++i]) - if (str[i] == '\n') - return (i); - return (-1); -} - -static int st_free_return(char **ptr, char **ptr2, int ret) -{ - if (ptr != NULL) - { - free(*ptr); - *ptr = NULL; - } - if (ptr2 != NULL) - { - free(*ptr2); - *ptr2 = NULL; - } - return (ret); -} - static int st_read_line(int fd, char **line, char *rest) { - int ret; - int split_at; - char *buf; + int ret; + char *cut; + static char buf[FT_GETLINE_BUFFER_SIZE + 1] = {'\0'}; - if ((buf = malloc(sizeof(char) * (FT_GETLINE_BUFFER_SIZE + 1))) == NULL) - return (st_free_return(line, NULL, FT_ERROR)); while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0) { buf[ret] = '\0'; - if ((split_at = st_find_newline(buf)) != -1) + if ((cut = ft_strchr(buf, '\n')) != NULL) { - ft_strcpy(rest, buf + split_at + 1); - buf[split_at] = '\0'; - if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FT_ERROR)); - return (st_free_return(&buf, NULL, FT_LINE)); + ft_strcpy(rest, cut + 1); + *cut = '\0'; } if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FT_ERROR)); + return (FT_ERROR); + if (cut != NULL) + return (FT_LINE); } if (ret == -1) - return (st_free_return(&buf, line, FT_ERROR)); - return (st_free_return(&buf, NULL, ret)); + free(line); + return (ret); } /* @@ -85,29 +56,27 @@ static int st_read_line(int fd, char **line, char *rest) int ft_getline(int fd, char **line) { - int split_at; - static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; + char *cut; + static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{'\0'}}; if (fd < 0 || fd > OPEN_MAX || line == NULL) return (FT_ERROR); - if ((*line = ft_strdup("")) == NULL) - return (FT_ERROR); if (rest[fd][0] == '\0') + { + if ((*line = ft_strdup("")) == NULL) + return (FT_ERROR); return (st_read_line(fd, line, rest[fd])); - if ((split_at = st_find_newline(rest[fd])) != -1) + } + if ((cut = ft_strchr(rest[fd], '\n')) != NULL) { - free(*line); - if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + *cut = '\0'; + if ((*line = ft_strdup(rest[fd])) == NULL) return (FT_ERROR); - ft_strncpy(*line, rest[fd], split_at); - (*line)[split_at] = '\0'; - ft_strcpy(rest[fd], rest[fd] + split_at + 1); + ft_strmove(rest[fd], cut + 1); return (FT_LINE); } - free(*line); - if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + if ((*line = ft_strdup(rest[fd])) == NULL) return (FT_ERROR); - ft_strcpy(*line, rest[fd]); rest[fd][0] = '\0'; return (st_read_line(fd, line, rest[fd])); } |
