From d3fb362c2e0b83cc9754a05ae5bc4a68a5f9269d Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 10 May 2020 22:01:15 +0200 Subject: Added ft_strtof, ft_atof, ft_vectobuf32, ft_split_len (not tested) --- src/io/ft_getline.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/io/ft_getline.c (limited to 'src/io/ft_getline.c') diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c new file mode 100644 index 0000000..4cf3962 --- /dev/null +++ b/src/io/ft_getline.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ +/* Updated: 2020/05/09 11:19:12 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; + + 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) + { + 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)); + } + if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) + return (st_free_return(&buf, NULL, FT_ERROR)); + } + if (ret == -1) + return (st_free_return(&buf, line, FT_ERROR)); + return (st_free_return(&buf, NULL, ret)); +} + +/* +** 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 FTNL_EOF +*/ + +int ft_getline(int fd, char **line) +{ + int split_at; + static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; + + if (fd < 0 || fd > OPEN_MAX || line == NULL || FT_GETLINE_BUFFER_SIZE <= 0) + return (FT_ERROR); + if ((*line = ft_strdup("")) == NULL) + return (FT_ERROR); + if (rest[fd][0] == '\0') + return (st_read_line(fd, line, rest[fd])); + if ((split_at = st_find_newline(rest[fd])) != -1) + { + free(*line); + if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + return (FT_ERROR); + ft_strncpy(*line, rest[fd], split_at); + (*line)[split_at] = '\0'; + ft_strcpy(rest[fd], rest[fd] + split_at + 1); + return (FT_LINE); + } + free(*line); + if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + return (FT_ERROR); + ft_strcpy(*line, rest[fd]); + rest[fd][0] = '\0'; + return (st_read_line(fd, line, rest[fd])); +} -- cgit From 966eb29634a84496e0851ef2b5a7d64f413d33ed Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 19:10:14 +0200 Subject: Added ft_veciter_ret --- src/io/ft_getline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/io/ft_getline.c') diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c index 4cf3962..d59aa63 100644 --- a/src/io/ft_getline.c +++ b/src/io/ft_getline.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/05/09 11:19:12 by charles ### ########.fr */ +/* Updated: 2020/05/12 17:56:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,7 +88,7 @@ int ft_getline(int fd, char **line) int split_at; static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; - if (fd < 0 || fd > OPEN_MAX || line == NULL || FT_GETLINE_BUFFER_SIZE <= 0) + if (fd < 0 || fd > OPEN_MAX || line == NULL) return (FT_ERROR); if ((*line = ft_strdup("")) == NULL) return (FT_ERROR); -- cgit From d4aeabeac0a8bd1665b14aacf487df17198c1ab9 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 21:40:41 +0200 Subject: Fixing/refactoring ft_getline, fixed/normed ft_memchr, added ft_strmove --- src/io/ft_getline.c | 77 ++++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 54 deletions(-) (limited to 'src/io/ft_getline.c') 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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])); } -- cgit