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/dstr/ft_dstrnew.c | 6 ++- src/io/ft_getline.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ src/io/ft_next_line.c | 113 ------------------------------------------------ src/str/ft_atof.c | 18 ++++++++ src/str/ft_strtof.c | 44 +++++++++++++++++++ src/util/ft_split_len.c | 23 ++++++++++ src/vec/ft_vectobuf32.c | 29 +++++++++++++ 7 files changed, 232 insertions(+), 114 deletions(-) create mode 100644 src/io/ft_getline.c delete mode 100644 src/io/ft_next_line.c create mode 100644 src/str/ft_atof.c create mode 100644 src/str/ft_strtof.c create mode 100644 src/util/ft_split_len.c create mode 100644 src/vec/ft_vectobuf32.c (limited to 'src') diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c index 8ae4a64..c280dd1 100644 --- a/src/dstr/ft_dstrnew.c +++ b/src/dstr/ft_dstrnew.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:54:52 by charles #+# #+# */ -/* Updated: 2020/04/04 19:50:38 by charles ### ########.fr */ +/* Updated: 2020/05/09 12:57:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,13 @@ t_ftdstr *ft_dstrnew(char *from) { t_ftdstr *dstr; + dstr = NULL; if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL || (dstr->str = ft_strdup(from)) == NULL) + { + free(dstr); return (NULL); + } dstr->length = ft_strlen(from); dstr->capacity = dstr->length + 1; return (dstr); 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])); -} diff --git a/src/str/ft_atof.c b/src/str/ft_atof.c new file mode 100644 index 0000000..74d5a42 --- /dev/null +++ b/src/str/ft_atof.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 21:03:50 by charles #+# #+# */ +/* Updated: 2020/05/10 21:04:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +float ft_atof(const char *nptr) +{ + return (ft_strtof(nptr, NULL)); +} diff --git a/src/str/ft_strtof.c b/src/str/ft_strtof.c new file mode 100644 index 0000000..f147394 --- /dev/null +++ b/src/str/ft_strtof.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/09 12:09:13 by charles #+# #+# */ +/* Updated: 2020/05/09 12:23:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Extract a float from a string +** \param nptr String to extract from +** \param endptr If not NULL pointer to last character +** of the extraction is placed in *endptr +** \return Extracted float value +** \note This function doesn't try mimic the original perfectly +** (no hexadecimal, exponent, NaN, inf handling) +*/ + +float ft_strtof(const char *nptr, char **endptr) +{ + float n; + bool is_neg; + const char *tmp; + + while (ft_isspace(*nptr)) + nptr++; + is_neg = *nptr == '-'; + if (*nptr == '-' || *nptr == '+') + nptr++; + n = (float)ft_strtol(nptr, (char**)&nptr, 10); + if (*nptr == '.') + nptr++; + tmp = nptr; + n += (float)ft_strtol(nptr, (char**)&nptr, 10) / (10 * (nptr - tmp)); + if (endptr != NULL) + *endptr = (char*)nptr; + return (is_neg ? -n : n); +} diff --git a/src/util/ft_split_len.c b/src/util/ft_split_len.c new file mode 100644 index 0000000..090a0c9 --- /dev/null +++ b/src/util/ft_split_len.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 20:58:46 by charles #+# #+# */ +/* Updated: 2020/05/10 21:02:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_util.h" + +size_t ft_split_len(char **split) +{ + size_t count; + + count = 0; + while (split[count] != NULL) + count++; + return (count); +} diff --git a/src/vec/ft_vectobuf32.c b/src/vec/ft_vectobuf32.c new file mode 100644 index 0000000..d152d37 --- /dev/null +++ b/src/vec/ft_vectobuf32.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vectobuf32.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 21:09:35 by charles #+# #+# */ +/* Updated: 2020/05/10 21:14:31 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +void *ft_vectobuf32(t_ftvec *vec) +{ + uint32_t *buf; + size_t i; + + if ((buf = malloc(sizeof(uint32_t) * vec->size)) == NULL) + return (NULL); + i = 0; + while (i < vec->size) + { + buf[i] = *(uint32_t*)&vec->data[i]; + i++; + } + return (buf); +} -- cgit