diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-10 22:01:15 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-10 22:01:15 +0200 |
| commit | d3fb362c2e0b83cc9754a05ae5bc4a68a5f9269d (patch) | |
| tree | 0caac15f6a52282d83acffa3154fd007fc3db444 /src | |
| parent | 02abc030a68cb2fdd2f21c96db830ec8cb9176ad (diff) | |
| download | libft-d3fb362c2e0b83cc9754a05ae5bc4a68a5f9269d.tar.gz libft-d3fb362c2e0b83cc9754a05ae5bc4a68a5f9269d.tar.bz2 libft-d3fb362c2e0b83cc9754a05ae5bc4a68a5f9269d.zip | |
Added ft_strtof, ft_atof, ft_vectobuf32, ft_split_len (not tested)
Diffstat (limited to 'src')
| -rw-r--r-- | src/dstr/ft_dstrnew.c | 6 | ||||
| -rw-r--r-- | src/io/ft_getline.c (renamed from src/io/ft_next_line.c) | 34 | ||||
| -rw-r--r-- | src/str/ft_atof.c | 18 | ||||
| -rw-r--r-- | src/str/ft_strtof.c | 44 | ||||
| -rw-r--r-- | src/util/ft_split_len.c | 23 | ||||
| -rw-r--r-- | src/vec/ft_vectobuf32.c | 29 |
6 files changed, 136 insertions, 18 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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_next_line.c b/src/io/ft_getline.c index 74afa71..4cf3962 100644 --- a/src/io/ft_next_line.c +++ b/src/io/ft_getline.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_next_line.c :+: :+: :+: */ +/* ft_getline.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:11:35 by cacharle ### ########.fr */ +/* Updated: 2020/05/09 11:19:12 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,9 +44,9 @@ static int st_read_line(int fd, char **line, char *rest) 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) + 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) @@ -54,14 +54,14 @@ static int st_read_line(int fd, char **line, char *rest) 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)); + 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, FTNL_STATUS_ERROR)); + return (st_free_return(&buf, NULL, FT_ERROR)); } if (ret == -1) - return (st_free_return(&buf, line, FTNL_STATUS_ERROR)); + return (st_free_return(&buf, line, FT_ERROR)); return (st_free_return(&buf, NULL, ret)); } @@ -83,30 +83,30 @@ static int st_read_line(int fd, char **line, char *rest) ** return FTNL_EOF */ -int ft_next_line(int fd, char **line) +int ft_getline(int fd, char **line) { int split_at; - static char rest[OPEN_MAX][FTNL_BUFFER_SIZE + 1] = {{0}}; + static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; - if (fd < 0 || fd > OPEN_MAX || line == NULL || FTNL_BUFFER_SIZE <= 0) - return (FTNL_STATUS_ERROR); + if (fd < 0 || fd > OPEN_MAX || line == NULL || FT_GETLINE_BUFFER_SIZE <= 0) + return (FT_ERROR); if ((*line = ft_strdup("")) == NULL) - return (FTNL_STATUS_ERROR); + 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 (FTNL_STATUS_ERROR); + return (FT_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); + return (FT_LINE); } free(*line); if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) - return (FTNL_STATUS_ERROR); + return (FT_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} |
