aboutsummaryrefslogtreecommitdiff
path: root/src/get_next_line/get_next_line.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-15 07:56:02 +0100
committerCharles <sircharlesaze@gmail.com>2020-01-15 08:49:32 +0100
commit49ad59b0e773e92e93fb69ede889c781ca53e680 (patch)
tree5ad230ddcc2198555e2b56ca84945809c137285d /src/get_next_line/get_next_line.c
parent5c6d9b3ba3e4946b2a527d10109f0e82284e64c5 (diff)
downloadlibft-49ad59b0e773e92e93fb69ede889c781ca53e680.tar.gz
libft-49ad59b0e773e92e93fb69ede889c781ca53e680.tar.bz2
libft-49ad59b0e773e92e93fb69ede889c781ca53e680.zip
Move *.o -> src/, *.h -> include/ and *.o -> build
Diffstat (limited to 'src/get_next_line/get_next_line.c')
-rw-r--r--src/get_next_line/get_next_line.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/get_next_line/get_next_line.c b/src/get_next_line/get_next_line.c
new file mode 100644
index 0000000..e51f152
--- /dev/null
+++ b/src/get_next_line/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: 2020/01/15 07:26:50 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "get_next_line.h"
+
+#define HAS_NEWLINE(str, split_at) ((split_at = gnl_find_newline(str)) != -1)
+
+static int gnl_find_newline(char *str)
+{
+ int i;
+
+ i = -1;
+ while (str[++i])
+ if (str[i] == '\n')
+ return (i);
+ return (-1);
+}
+
+static int gnl_free_return(char **ptr, char **ptr2, int ret)
+{
+ if (ptr != NULL)
+ {
+ free(*ptr);
+ *ptr = NULL;
+ }
+ if (ptr2 != NULL)
+ {
+ free(*ptr2);
+ *ptr2 = NULL;
+ }
+ return (ret);
+}
+
+static int gnl_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 (gnl_free_return(line, NULL, GNL_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 (gnl_free_return(&buf, NULL, GNL_STATUS_ERROR));
+ return (gnl_free_return(&buf, NULL, GNL_STATUS_LINE));
+ }
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (gnl_free_return(&buf, NULL, GNL_STATUS_ERROR));
+ }
+ if (ret == -1)
+ return (gnl_free_return(&buf, line, GNL_STATUS_ERROR));
+ return (gnl_free_return(&buf, NULL, ret));
+}
+
+/*
+** 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
+*/
+
+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 (GNL_STATUS_ERROR);
+ if ((*line = ft_strdup("")) == NULL)
+ return (GNL_STATUS_ERROR);
+ if (rest[fd][0] == '\0')
+ return (gnl_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 (GNL_STATUS_ERROR);
+ ft_strncpy(*line, rest[fd], split_at);
+ (*line)[split_at] = '\0';
+ ft_strcpy(rest[fd], rest[fd] + split_at + 1);
+ return (GNL_STATUS_LINE);
+ }
+ free(*line);
+ if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1))))
+ return (GNL_STATUS_ERROR);
+ ft_strcpy(*line, rest[fd]);
+ rest[fd][0] = '\0';
+ return (gnl_read_line(fd, line, rest[fd]));
+}