summaryrefslogtreecommitdiffstats
path: root/libft/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-24 16:33:51 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-24 16:33:51 +0200
commitd9af81e9c36b444ad90a5b435c9cecae628a3de1 (patch)
tree7731d3ec9dda16d7d58fc9cf1a577afa22f2ef4b /libft/src
parenttest commit (diff)
download42-minishell-d9af81e9c36b444ad90a5b435c9cecae628a3de1.tar.gz
42-minishell-d9af81e9c36b444ad90a5b435c9cecae628a3de1.tar.bz2
42-minishell-d9af81e9c36b444ad90a5b435c9cecae628a3de1.tar.xz
42-minishell-d9af81e9c36b444ad90a5b435c9cecae628a3de1.tar.zst
42-minishell-d9af81e9c36b444ad90a5b435c9cecae628a3de1.zip
Added strtok to libft
Diffstat (limited to 'libft/src')
-rw-r--r--libft/src/ft_strtok.c21
-rw-r--r--libft/src/ft_strtok_r.c100
2 files changed, 121 insertions, 0 deletions
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));
+}