aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
diff options
context:
space:
mode:
Diffstat (limited to 'get_next_line.c')
-rw-r--r--get_next_line.c109
1 files changed, 68 insertions, 41 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 39305f5..b6f1a41 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -1,53 +1,80 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_next_line.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/08 10:37:41 by cacharle #+# #+# */
+/* Updated: 2019/10/08 15:35:43 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include <unistd.h>
#include <stdlib.h>
-#include "libft.h"
#include "get_next_line.h"
-static unsigned int cursor;
-
-/* static void *ft_memcat(void *ptr, void *tail, */
-/* unsigned int size, unsigned int tail_size); */
-
-int get_next_line(const int fd, char **line)
-{
- /* unsigned int newline_i; */
- /* char buf[BUFF_SIZE]; */
- /* */
- /* if (read(fd, buf, BUFF_SIZE) < 0) */
- /* return (-1); */
- /* if ((*line = ft_strnew(0)) == NULL) */
- /* return (-1); */
- /* newline_i = 0; */
- /* while (buf[newline_i]) */
- /* { */
- /* while (buf[newline_i] != '\n') */
- /* newline_i++; */
- /* ft_memcat(*line, buf, 0, newline_i); */
- /* if (read(fd, buf, BUFF_SIZE) < 0) */
- /* return (-1); */
- /* } */
- return (0);
-}
-/* */
-/* static void *ft_memcat(void *ptr, void *tail, */
-/* unsigned int size, unsigned int tail_size) */
+/* static char *split_newline(char *buf, char **line) */
/* { */
-/* void *copy; */
+/* int split_index; */
/* */
-/* if ((copy = malloc(size)) == NULL) */
-/* return (NULL); */
-/* ft_memcpy(copy, ptr, size); */
-/* free(ptr); */
-/* if ((ptr = malloc(size + tail_size)) == NULL) */
-/* return (NULL); */
-/* ft_memcpy(ptr, copy, size); */
-/* free(copy); */
-/* ft_memcpy(ptr + size, tail, tail_size); */
-/* return (ptr); */
+/* split_index = find_newline(buf); */
+/* if (split_index != -1) */
+/* buf[split_index] = '\0'; */
+/* ft_strappend(*line, buf); */
+/* if (split_index == -1) */
+/* return (NULL); */
+/* return (buf + split_index); */
/* } */
+#include <stdio.h>
+int get_next_line(int fd, char **line)
+{
+ int slice_index;
+ static char buf[BUFFER_SIZE + 1] = "";
+
+ /* *line = NULL; */
+ if (buf[0] == '\0')
+ {
+ if (read(fd, buf, BUFFER_SIZE) == 0)
+ return (NO_LINE_READ);
+ }
+ /* printf("%s\n", *line); */
+ slice_index = find_newline(buf);
+ if (slice_index == -1)
+ {
+ *line = strappend(*line, buf);
+ buf[0] = '\0';
+ return (get_next_line(fd, line));
+ }
+ buf[slice_index] = '\0';
+ *line = strappend(*line, buf);
+ ft_strcpy(buf, buf + slice_index + 1);
+ return (LINE_READ);
+}
+
+#include <stdio.h>
+#include <fcntl.h>
int main()
{
- printf("test\n");
+ int fd = open("Makefile", O_RDONLY);
+ char *line = NULL;
+ int ret = 1;
+ while (get_next_line(fd, &line) == 1)
+ {
+ printf("> %s\n", line);
+ free(line);
+ line = NULL;
+ }
+ /* free(line); */
+
+ /* char c; */
+ /* while (read(fd, &c, 1) > 0) */
+ /* printf("%c", c); */
+ /* while (read(fd, &c, 1) > 0) */
+ /* printf("%c", c); */
+ /* while (read(fd, &c, 1) > 0) */
+ /* printf("%c", c); */
+ close(fd);
return 0;
}