aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--ft_strcount.c24
-rw-r--r--ft_strjoin_free.c9
-rw-r--r--ft_strjoin_free_snd.c7
-rw-r--r--ft_strndup.c6
-rw-r--r--get_next_line.c115
-rw-r--r--get_next_line.h38
-rw-r--r--libft.h4
8 files changed, 191 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index f713b34..3360001 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/10/08 15:45:53 by cacharle #+# #+# #
-# Updated: 2019/11/05 22:16:32 by cacharle ### ########.fr #
+# Updated: 2019/11/20 04:13:32 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -28,8 +28,7 @@ SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \
ft_strncmp.c ft_strncpy.c ft_strnequ.c ft_strnew.c ft_strnstr.c \
ft_strrchr.c ft_split.c ft_strstr.c ft_substr.c ft_strtrim.c \
ft_tolower.c ft_toupper.c ft_strlcpy.c ft_calloc.c ft_strndup.c \
- ft_strjoin_free.c ft_strjoin_free_snd.c
-
+ ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c ft_strcount.c
OBJ = $(SRC:.c=.o)
INCLUDE = libft.h
diff --git a/ft_strcount.c b/ft_strcount.c
new file mode 100644
index 0000000..87e756d
--- /dev/null
+++ b/ft_strcount.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strcount.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/15 09:17:48 by cacharle #+# #+# */
+/* Updated: 2019/11/15 09:19:36 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+int ft_strcount(char *str, char c)
+{
+ int counter;
+
+ if (c == '\0')
+ return (1);
+ counter = 0;
+ while (*str)
+ if (*str++ == c)
+ counter++;
+ return (counter);
+}
diff --git a/ft_strjoin_free.c b/ft_strjoin_free.c
index 18ce83d..4050b77 100644
--- a/ft_strjoin_free.c
+++ b/ft_strjoin_free.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/05 21:32:48 by cacharle #+# #+# */
-/* Updated: 2019/11/05 22:17:03 by cacharle ### ########.fr */
+/* Updated: 2019/11/14 10:07:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,13 +17,10 @@ char *ft_strjoin_free(char const *s1, char const *s2, int free_nb)
{
char *joined;
- if (s1 == NULL || s2 == NULL)
+ if (s1 == NULL || s2 == NULL || free_nb < 0 || free_nb > 2)
return (NULL);
- if ((joined = (char*)malloc(sizeof(char)
- * (ft_strlen(s1) + ft_strlen(s2) + 1))) == NULL)
+ if ((joined = ft_strjoin(s1, s2)) == NULL)
return (NULL);
- joined = ft_strcpy(joined, s1);
- joined = ft_strcat(joined, s2);
if (free_nb == 1)
free((void*)s1);
if (free_nb == 2)
diff --git a/ft_strjoin_free_snd.c b/ft_strjoin_free_snd.c
index 46e36ae..0503211 100644
--- a/ft_strjoin_free_snd.c
+++ b/ft_strjoin_free_snd.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/05 22:12:56 by cacharle #+# #+# */
-/* Updated: 2019/11/05 22:17:15 by cacharle ### ########.fr */
+/* Updated: 2019/11/14 10:07:19 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,11 +19,8 @@ char *ft_strjoin_free_snd(char const *s1, char const *s2)
if (s1 == NULL || s2 == NULL)
return (NULL);
- if ((joined = (char*)malloc(sizeof(char)
- * (ft_strlen(s1) + ft_strlen(s2) + 1))) == NULL)
+ if ((joined = ft_strjoin(s1, s2)) == NULL)
return (NULL);
- joined = ft_strcpy(joined, s1);
- joined = ft_strcat(joined, s2);
free((void*)s2);
return (joined);
}
diff --git a/ft_strndup.c b/ft_strndup.c
index 0d4119a..0683dae 100644
--- a/ft_strndup.c
+++ b/ft_strndup.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/25 03:28:52 by cacharle #+# #+# */
-/* Updated: 2019/11/20 04:09:59 by cacharle ### ########.fr */
+/* Updated: 2019/11/20 04:15:44 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,9 @@
char *ft_strndup(const char *s1, size_t n)
{
+ char *clone;
+
if ((clone = ft_strnew(n)) == NULL)
return (NULL);
- return (ft_strncpy(clone, s, n));
+ return (ft_strncpy(clone, s1, n));
}
diff --git a/get_next_line.c b/get_next_line.c
new file mode 100644
index 0000000..f7d9c0e
--- /dev/null
+++ b/get_next_line.c
@@ -0,0 +1,115 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_next_line.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */
+/* Updated: 2019/11/04 00:00:16 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+
+/*
+** 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 GNL_EOF
+*/
+
+#define HAS_NEWLINE(str, split_at) ((split_at = find_newline(str)) != -1)
+
+int get_next_line(int fd, char **line)
+{
+ int split_at;
+ static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}};
+
+ if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE <= 0)
+ return (STATUS_ERROR);
+ if ((*line = ft_strdup("")) == NULL)
+ return (STATUS_ERROR);
+ if (rest[fd][0] == '\0')
+ return (read_line(fd, line, rest[fd]));
+ if (HAS_NEWLINE(rest[fd], split_at))
+ {
+ free(*line);
+ if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL)
+ return (STATUS_ERROR);
+ ft_strncpy(*line, rest[fd], split_at);
+ (*line)[split_at] = '\0';
+ ft_strcpy(rest[fd], rest[fd] + split_at + 1);
+ return (STATUS_LINE);
+ }
+ free(*line);
+ if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1))))
+ return (STATUS_ERROR);
+ ft_strcpy(*line, rest[fd]);
+ rest[fd][0] = '\0';
+ return (read_line(fd, line, rest[fd]));
+}
+
+int read_line(int fd, char **line, char *rest)
+{
+ int ret;
+ int split_at;
+ char *buf;
+
+ if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL)
+ return (free_return(line, NULL, STATUS_ERROR));
+ while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
+ {
+ buf[ret] = '\0';
+ if (HAS_NEWLINE(buf, split_at))
+ {
+ ft_strcpy(rest, buf + split_at + 1);
+ buf[split_at] = '\0';
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (free_return(&buf, NULL, STATUS_ERROR));
+ return (free_return(&buf, NULL, STATUS_LINE));
+ }
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (free_return(&buf, NULL, STATUS_ERROR));
+ }
+ if (ret == -1)
+ return (free_return(&buf, line, STATUS_ERROR));
+ return (free_return(&buf, NULL, ret));
+}
+
+int find_newline(char *str)
+{
+ int i;
+
+ i = -1;
+ while (str[++i])
+ if (str[i] == '\n')
+ return (i);
+ return (-1);
+}
+
+int free_return(char **ptr, char **ptr2, int ret)
+{
+ if (ptr != NULL)
+ {
+ free(*ptr);
+ *ptr = NULL;
+ }
+ if (ptr2 != NULL)
+ {
+ free(*ptr2);
+ *ptr2 = NULL;
+ }
+ return (ret);
+}
diff --git a/get_next_line.h b/get_next_line.h
new file mode 100644
index 0000000..c3962e3
--- /dev/null
+++ b/get_next_line.h
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_next_line.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */
+/* Updated: 2019/11/03 22:43:18 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef GET_NEXT_LINE_H
+# define GET_NEXT_LINE_H
+
+# include <unistd.h>
+# include <stdlib.h>
+# include <limits.h>
+# include "libft.h"
+
+# ifndef BUFFER_SIZE
+# define BUFFER_SIZE 32
+# endif
+
+# define STATUS_LINE 1
+# define STATUS_EOF 0
+# define STATUS_ERROR -1
+
+/*
+** get_next_line.c
+*/
+
+int get_next_line(int fd, char **line);
+int read_line(int fd, char **line, char *rest);
+int find_newline(char *str);
+int free_return(char **ptr, char **rest, int ret);
+
+#endif
diff --git a/libft.h b/libft.h
index e6d5cf9..6ac960b 100644
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */
-/* Updated: 2019/11/20 04:07:41 by cacharle ### ########.fr */
+/* Updated: 2019/11/20 04:13:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,7 @@
# include <stdlib.h>
# include <stddef.h>
# include <limits.h>
+# include "get_next_line.h"
# define TRUE 1
# define FALSE 0
@@ -76,6 +77,7 @@ char **ft_split(char const *s, char c);
char *ft_strjoin_free(char const *s1, char const *s2,
int free_nb);
char *ft_strjoin_free_snd(char const *s1, char const *s2);
+int ft_strcount(char *str, char c);
/*
** character