diff options
Diffstat (limited to 'libft')
-rw-r--r-- | libft/Makefile | 4 | ||||
-rw-r--r-- | libft/include/libft.h | 10 | ||||
-rw-r--r-- | libft/src/ft_strtok.c | 21 | ||||
-rw-r--r-- | libft/src/ft_strtok_r.c | 100 |
4 files changed, 134 insertions, 1 deletions
diff --git a/libft/Makefile b/libft/Makefile index 81cc2fd..6ada2e1 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -15,7 +15,7 @@ OBJS_DIR = obj/ #==============================================================================# INCS = libft.h #------------------------------------------------------------------------------# -SRCS_NAME = ft_memset.c +SRCS_NAME = ft_memset.c SRCS_NAME += ft_bzero.c SRCS_NAME += ft_memcpy.c SRCS_NAME += ft_memccpy.c @@ -41,6 +41,8 @@ SRCS_NAME += ft_strncmp.c SRCS_NAME += ft_strlcpy.c SRCS_NAME += ft_strlcat.c SRCS_NAME += ft_strnstr.c +SRCS_NAME += ft_strtok_r.c +SRCS_NAME += ft_strtok.c SRCS_NAME += ft_atoi.c SRCS_NAME += ft_calloc.c SRCS_NAME += ft_nrealloc.c diff --git a/libft/include/libft.h b/libft/include/libft.h index 7019d6b..96615fd 100644 --- a/libft/include/libft.h +++ b/libft/include/libft.h @@ -65,6 +65,14 @@ typedef struct s_printflist char *output; } t_printflist; +typedef struct s_stok +{ + char *spanp; + char *ns; + int32_t c; + int32_t sc; +} t_stok; + /* ** VOID */ @@ -115,6 +123,8 @@ char *ft_nstr(size_t size); char *ft_strsubst(char *str, const char *pattern, const char *subst); +char *ft_strtok_r(char *s, const char *delim, char **last); +char *ft_strtok(char *s, const char *delim); char **ft_split(const char *s, char c); /* diff --git a/libft/src/ft_strtok.c b/libft/src/ft_strtok.c new file mode 100644 index 0000000..e4fde5e --- /dev/null +++ b/libft/src/ft_strtok.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtok.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:07:12 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:07:12 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> + +char + *ft_strtok(char *s, const char *delim) +{ + static char *last; + + return (ft_strtok_r(s, delim, &last)); +} diff --git a/libft/src/ft_strtok_r.c b/libft/src/ft_strtok_r.c new file mode 100644 index 0000000..f3411cc --- /dev/null +++ b/libft/src/ft_strtok_r.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtok_r.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:07:12 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:07:12 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> + +static t_stok + ft_cont(char *s, const char *delim) +{ + t_stok stok; + t_bool inc; + + stok.c = *s++; + stok.spanp = (char *)delim; + inc = TRUE; + while ((stok.sc = *stok.spanp) != '\0') + { + if (stok.c == stok.sc) + { + stok.c = *s++; + stok.spanp = (char*)delim; + inc = FALSE; + } + if (inc == TRUE) + stok.spanp++; + else + inc = TRUE; + } + stok.ns = s; + return (stok); +} + +static char + *ft_scan_ret(char *s, char **last, char *tok, t_stok stok) +{ + if ((stok.sc = *stok.spanp++) == stok.c) + { + if (stok.c == '\0') + s = NULL; + else + s[-1] = '\0'; + *last = s; + return (tok); + } + while (stok.sc != '\0') + { + if ((stok.sc = *stok.spanp++) == stok.c) + { + if (stok.c == '\0') + s = NULL; + else + s[-1] = '\0'; + *last = s; + return (tok); + } + } + return (NULL); +} + +static char + *ft_scan_tok(char *s, const char *delim, char **last, t_stok stok) +{ + char *tok; + char *ret; + + tok = s - 1; + while (TRUE) + { + stok.c = *s++; + stok.spanp = (char *)delim; + if ((ret = ft_scan_ret(s, last, tok, stok)) != NULL) + { + return (ret); + } + } +} + +char + *ft_strtok_r(char *s, const char *delim, char **last) +{ + t_stok stok; + + if (s == NULL && (s = *last) == NULL) + return (NULL); + stok = ft_cont(s, delim); + s = stok.ns; + if (stok.c == '\0') + *last = NULL; + if (stok.c == '\0') + return (NULL); + return (ft_scan_tok(s, delim, last, stok)); +} |