From 8131a5d26441c5152ab151b4bb49b561e5ca6e81 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 31 Jan 2020 10:44:30 +0100 Subject: hash table unit testing, norming --- src/io/ft_next_line.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/io') diff --git a/src/io/ft_next_line.c b/src/io/ft_next_line.c index 0f4cc2c..74afa71 100644 --- a/src/io/ft_next_line.c +++ b/src/io/ft_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:38:01 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:11:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit From 901402c99018422c994bdb297e3ba404969c88ea Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 30 Mar 2020 22:26:36 +0200 Subject: Added documentation for ht and lst --- src/io/ft_printf/internals/list.c | 4 ++-- src/io/ft_printf/internals/parse.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/io') diff --git a/src/io/ft_printf/internals/list.c b/src/io/ft_printf/internals/list.c index 99491f4..37f8013 100644 --- a/src/io/ft_printf/internals/list.c +++ b/src/io/ft_printf/internals/list.c @@ -18,7 +18,7 @@ t_flist *list_new(t_pformat *content) if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL) return (NULL); - lst->content = content; + lst->data = content; lst->next = NULL; return (lst); } @@ -47,7 +47,7 @@ void list_pop_front(t_flist **lst) if (lst == NULL || *lst == NULL) return ; tmp = (*lst)->next; - free((*lst)->content); + free((*lst)->data); free(*lst); *lst = tmp; } diff --git a/src/io/ft_printf/internals/parse.c b/src/io/ft_printf/internals/parse.c index 33928a0..4650481 100644 --- a/src/io/ft_printf/internals/parse.c +++ b/src/io/ft_printf/internals/parse.c @@ -28,7 +28,7 @@ int parse(const char *format, t_flist **flist) if ((tmp = list_new(parsed)) == NULL) return ((int)list_destroy(flist)); list_push_front(flist, tmp); - format += (*flist)->content->fmt_len; + format += (*flist)->data->fmt_len; } *flist = list_reverse(*flist); return (1); -- cgit 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 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/io/ft_next_line.c | 113 -------------------------------------------------- 2 files changed, 113 insertions(+), 113 deletions(-) create mode 100644 src/io/ft_getline.c delete mode 100644 src/io/ft_next_line.c (limited to 'src/io') 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])); +} diff --git a/src/io/ft_next_line.c b/src/io/ft_next_line.c deleted file mode 100644 index 74afa71..0000000 --- a/src/io/ft_next_line.c +++ /dev/null @@ -1,113 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:11:35 by cacharle ### ########.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) * (FTNL_BUFFER_SIZE + 1))) == NULL) - return (st_free_return(line, NULL, FTNL_STATUS_ERROR)); - while ((ret = read(fd, buf, FTNL_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, FTNL_STATUS_ERROR)); - return (st_free_return(&buf, NULL, FTNL_STATUS_LINE)); - } - if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FTNL_STATUS_ERROR)); - } - if (ret == -1) - return (st_free_return(&buf, line, FTNL_STATUS_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_next_line(int fd, char **line) -{ - int split_at; - static char rest[OPEN_MAX][FTNL_BUFFER_SIZE + 1] = {{0}}; - - if (fd < 0 || fd > OPEN_MAX || line == NULL || FTNL_BUFFER_SIZE <= 0) - return (FTNL_STATUS_ERROR); - if ((*line = ft_strdup("")) == NULL) - return (FTNL_STATUS_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 (FTNL_STATUS_ERROR); - ft_strncpy(*line, rest[fd], split_at); - (*line)[split_at] = '\0'; - ft_strcpy(rest[fd], rest[fd] + split_at + 1); - return (FTNL_STATUS_LINE); - } - free(*line); - if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) - return (FTNL_STATUS_ERROR); - ft_strcpy(*line, rest[fd]); - rest[fd][0] = '\0'; - return (st_read_line(fd, line, rest[fd])); -} -- cgit From a4b9cda7d6733f2b077f8586e3b3e69351e7dfba Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 11 May 2020 10:54:58 +0200 Subject: Added ft_getfile --- src/io/ft_getfile.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/io/ft_getfile.c (limited to 'src/io') diff --git a/src/io/ft_getfile.c b/src/io/ft_getfile.c new file mode 100644 index 0000000..891ea51 --- /dev/null +++ b/src/io/ft_getfile.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getfile.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 09:52:32 by charles #+# #+# */ +/* Updated: 2020/05/11 10:15:12 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_io.h" + +char *ft_getfile(int fd) +{ + char *s; + char buf[FT_GETFILE_BUFFER_SIZE + 1]; + int ret; + + if (fd < 0 || fd > OPEN_MAX) + return (NULL); + if ((s = ft_strdup("")) == NULL) + return (NULL); + while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((s = ft_strjoinf(s, buf, FT_STRJOINF_FST)) == NULL) + return (NULL); + } + if (ret == -1) + { + free(s); + return (NULL); + } + return (s); +} -- cgit From b9f000a80cbba38b8f21c9737a42f07573ec7b91 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 11 May 2020 16:14:38 +0200 Subject: Moved util/ft_split* in str, Added ft_memjoin and ft_memjoinf1, Modified ft_getfile so that it can read non-ascii file --- src/io/ft_getfile.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/io') diff --git a/src/io/ft_getfile.c b/src/io/ft_getfile.c index 891ea51..d3f697c 100644 --- a/src/io/ft_getfile.c +++ b/src/io/ft_getfile.c @@ -6,32 +6,35 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 09:52:32 by charles #+# #+# */ -/* Updated: 2020/05/11 10:15:12 by charles ### ########.fr */ +/* Updated: 2020/05/11 16:09:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_io.h" -char *ft_getfile(int fd) +/* +** \brief Read a file in a memory buffer +** \param fd File descriptor to read from +** \param mem Pointer to mem struct (buffer and buffer size) +** \return -1 on error, 0 otherwise +*/ + +int ft_getfile(int fd, t_ftmem *mem) { - char *s; - char buf[FT_GETFILE_BUFFER_SIZE + 1]; + char buf[FT_GETFILE_BUFFER_SIZE]; int ret; - if (fd < 0 || fd > OPEN_MAX) - return (NULL); - if ((s = ft_strdup("")) == NULL) - return (NULL); + if (fd < 0 || fd > OPEN_MAX || mem == NULL + || (mem->data = malloc(1)) == NULL) + return (-1); + mem->size = 0; while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0) { - buf[ret] = '\0'; - if ((s = ft_strjoinf(s, buf, FT_STRJOINF_FST)) == NULL) - return (NULL); + if ((mem->data = ft_memjoinf1(mem->data, mem->size, buf, ret)) == NULL) + return (-1); + mem->size += ret; } if (ret == -1) - { - free(s); - return (NULL); - } - return (s); + free(mem->data); + return (ret); } -- 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') 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') 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