diff options
| -rw-r--r-- | include/libft_io.h | 16 | ||||
| -rw-r--r-- | include/libft_str.h | 4 | ||||
| -rw-r--r-- | include/libft_util.h | 4 | ||||
| -rw-r--r-- | include/libft_vec.h | 3 | ||||
| -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 | ||||
| -rw-r--r-- | test/src/main.c | 1 | ||||
| -rw-r--r-- | test/src/runner/test_runner_str.c | 5 | ||||
| -rw-r--r-- | test/src/str/test_ft_strtof.c | 29 |
13 files changed, 187 insertions, 29 deletions
diff --git a/include/libft_io.h b/include/libft_io.h index 418b230..e59d70c 100644 --- a/include/libft_io.h +++ b/include/libft_io.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:35:43 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:09:11 by cacharle ### ########.fr */ +/* Updated: 2020/05/09 11:16:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,18 +29,18 @@ void ft_putnbr_fd(int n, int fd); char ft_getchar(void); -# ifndef FTNL_BUFFER_SIZE -# define FTNL_BUFFER_SIZE 32 +# ifndef FT_GETLINE_BUFFER_SIZE +# define FT_GETLINE_BUFFER_SIZE 32 # endif -# define FTNL_STATUS_LINE 1 -# define FTNL_STATUS_EOF 0 -# define FTNL_STATUS_ERROR -1 +# define FT_LINE 1 +# define FT_EOF 0 +# define FT_ERROR -1 /* -** get_next_line.c +** getline.c */ -int ft_next_line(int fd, char **line); +int ft_getline(int fd, char **line); #endif diff --git a/include/libft_str.h b/include/libft_str.h index 07a5fe0..83c4428 100644 --- a/include/libft_str.h +++ b/include/libft_str.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:22 by cacharle #+# #+# */ -/* Updated: 2020/05/09 12:29:21 by charles ### ########.fr */ +/* Updated: 2020/05/10 21:05:21 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,6 +64,8 @@ int ft_strcount(char *str, char c); char *ft_itoa(int n); int ft_atoi_strict(const char *s); long ft_strtol(const char *s, char **endptr, int base); +float ft_strtof(const char *nptr, char **endptr); +float ft_atof(const char *nptr); int ft_strcasecmp(const char *s1, const char *s2); int ft_strncasecmp(const char *s1, const char *s2, size_t n); size_t ft_strspn(const char *s, const char *charset); diff --git a/include/libft_util.h b/include/libft_util.h index 7a9e056..99e7a00 100644 --- a/include/libft_util.h +++ b/include/libft_util.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 17:51:36 by cacharle #+# #+# */ -/* Updated: 2020/02/27 17:52:16 by cacharle ### ########.fr */ +/* Updated: 2020/05/10 21:02:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,9 @@ # define LIBFT_UTIL_H # include <stdlib.h> +# include <stddef.h> void *ft_split_destroy(char **strs); +size_t ft_split_len(char **split); #endif diff --git a/include/libft_vec.h b/include/libft_vec.h index 8424bd5..a5f22dd 100644 --- a/include/libft_vec.h +++ b/include/libft_vec.h @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 18:57:16 by charles #+# #+# */ -/* Updated: 2020/04/04 15:55:29 by charles ### ########.fr */ +/* Updated: 2020/05/10 21:09:25 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,5 +47,6 @@ void ft_veciter(t_ftvec *vec, void (*f)(void *elem)); void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)); t_ftvec *ft_vecinsert(t_ftvec *vec, size_t i, void *elem); void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp); +void *ft_vectobuf32(t_ftvec *vec); #endif 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); +} diff --git a/test/src/main.c b/test/src/main.c index e962577..357e02e 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -34,6 +34,7 @@ static void run_all_test(void) RUN_TEST_GROUP(ft_strsjoinf); RUN_TEST_GROUP(ft_strsub); RUN_TEST_GROUP(ft_strsubf); + RUN_TEST_GROUP(ft_strtof); // ht RUN_TEST_GROUP(ft_htentry_new); diff --git a/test/src/runner/test_runner_str.c b/test/src/runner/test_runner_str.c index 8d3b68a..c2cf2a7 100644 --- a/test/src/runner/test_runner_str.c +++ b/test/src/runner/test_runner_str.c @@ -29,3 +29,8 @@ TEST_GROUP_RUNNER(ft_strsubf) { RUN_TEST_CASE(ft_strsubf, basic); } + +TEST_GROUP_RUNNER(ft_strtof) +{ + RUN_TEST_CASE(ft_strtof, basic); +} diff --git a/test/src/str/test_ft_strtof.c b/test/src/str/test_ft_strtof.c new file mode 100644 index 0000000..65290a9 --- /dev/null +++ b/test/src/str/test_ft_strtof.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_strtof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:41:59 by charles #+# #+# */ +/* Updated: 2020/05/10 21:58:21 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_strtof); + +TEST_SETUP(ft_strtof) +{} + +TEST_TEAR_DOWN(ft_strtof) +{} + +TEST(ft_strtof, basic) +{ + /* float foo; */ + + /* foo = ft_strtof("0.0", NULL); */ + /* TEST_ASSERT(0); */ +} |
