aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-10 10:20:32 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-10 10:20:32 +0200
commit2df0d3e47a9deb4676b4fd82a1924da4181ef9d9 (patch)
tree3b8e8800557754983b5da44757424b43670232bc
parente1e832e8f63046bdea329a8f82b6d88131c1d09e (diff)
downloadget_next_line-2df0d3e47a9deb4676b4fd82a1924da4181ef9d9.tar.gz
get_next_line-2df0d3e47a9deb4676b4fd82a1924da4181ef9d9.tar.bz2
get_next_line-2df0d3e47a9deb4676b4fd82a1924da4181ef9d9.zip
WIP: not getting last part if buff size too high
-rw-r--r--.gitignore1
-rw-r--r--Makefile8
-rw-r--r--get_next_line.c97
-rw-r--r--get_next_line.h19
-rw-r--r--get_next_line_utils.c28
-rw-r--r--main.c39
6 files changed, 98 insertions, 94 deletions
diff --git a/.gitignore b/.gitignore
index 8063ede..85157eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
a.out
get_next_line
*.ghc
+test
diff --git a/Makefile b/Makefile
index 616c560..6cd00ec 100644
--- a/Makefile
+++ b/Makefile
@@ -6,17 +6,17 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/10/08 08:11:00 by cacharle #+# #+# #
-# Updated: 2019/10/09 16:42:37 by cacharle ### ########.fr #
+# Updated: 2019/10/10 09:56:42 by cacharle ### ########.fr #
# #
# **************************************************************************** #
$(RM) = rm -f
CC = gcc
-CCFLAGS = -Wall -Wextra #-Werror -D BUFFER_SIZE=32
+CCFLAGS = -Wall -Wextra -Werror
NAME = get_next_line
-SRC = get_next_line.c get_next_line_utils.c
+SRC = get_next_line.c get_next_line_utils.c main.c
OBJ = $(SRC:.c=.o)
INCLUDE = get_next_line.h
@@ -25,7 +25,7 @@ all: $(NAME)
$(NAME): $(OBJ) $(INCLUDE)
$(CC) $(CCFLAGS) -o $(NAME) $(OBJ)
-%.o: %.c
+%.o: %.c $(INCLUDE)
$(CC) $(CCFLAGS) -c -o $@ $<
clean:
diff --git a/get_next_line.c b/get_next_line.c
index 25cfc90..082cd6f 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 10:37:41 by cacharle #+# #+# */
-/* Updated: 2019/10/09 16:50:40 by cacharle ### ########.fr */
+/* Updated: 2019/10/10 10:18:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,65 +14,64 @@
#include <stdlib.h>
#include "get_next_line.h"
-#include <stdio.h>
-
-// if has rest
-// store it in local_buf and read diff
-// else
-// read full local buf
-// while no newline -> recursion
-// newline -> store what is after newline
-// allocate line according to stack depth
-// cpy before newline at the end
-// at each stack pop store local buf on the allocated line
-
-
-int read_after(int fd, char *buf, char *rest_buf)
-{
- int offset = ft_strlen(rest_buf);
- ft_strncpy(buf, rest_buf, offset);
- rest_buf[0] = 0;
- return (read(fd, buf + offset, BUFFER_SIZE - offset));
-}
+/*
+** store rest in local_buf and read diff
+** while no newline -> recursion
+** newline -> store what is after newline
+** allocate line according to stack depth
+** cpy before newline at the end
+** at each stack pop store local buf on the allocated line
+*/
+#include <stdio.h>
int get_next_line(int fd, char **line)
{
- int split_at;
- char local_buf[BUFFER_SIZE + 1];
- static int buf_count = 0;
+ int ret;
+ int split_at;
+ char local_buf[BUFFER_SIZE + 1];
+ static int line_len = 0;
static char rest_buf[BUFFER_SIZE + 1] = "";
local_buf[0] = '\0';
- if (read_after(fd, local_buf, rest_buf) == 0)
- return (END_OF_FILE);
- split_at = find_newline(local_buf);
- if (split_at == -1)
- {
- buf_count++;
- int ret = get_next_line(fd, line);
- buf_count--;
- ft_strncpy(*line + (buf_count * BUFFER_SIZE), local_buf, BUFFER_SIZE);
+ if ((ret = read_after(fd, local_buf, rest_buf)) <= 0)
return (ret);
+ if ((split_at = find_newline(local_buf)) != -1)
+ {
+ ft_strncpy(rest_buf, local_buf + split_at + 1,
+ ft_strlen(local_buf) - split_at);
+ if ((*line = malloc(sizeof(char) * (line_len + split_at + 1))) == NULL)
+ return (ERROR);
+ ft_strncpy(*line + line_len, local_buf, split_at);
+ (*line)[line_len + split_at] = '\0';
+ return (LINE_READ);
}
- ft_strncpy(rest_buf, local_buf + split_at + 1, ft_strlen(local_buf) - split_at);
- *line = malloc(sizeof(char) * (buf_count * BUFFER_SIZE + split_at + 1));
- ft_strncpy(*line + (buf_count * BUFFER_SIZE), local_buf, split_at);
- (*line)[buf_count * BUFFER_SIZE + split_at] = '\0';
- return (LINE_READ);
+ line_len += BUFFER_SIZE;
+ if ((ret = get_next_line(fd, line)) == -1)
+ return (ERROR);
+ line_len -= BUFFER_SIZE;
+ ft_strncpy(*line + line_len, local_buf, BUFFER_SIZE);
+ return (ret);
}
-#include <stdio.h>
-#include <fcntl.h>
-int main()
+int read_after(int fd, char *buf, char *rest_buf)
{
- int fd = open("Makefile", O_RDONLY);
- char *line = NULL;
- /* int ret = 1; */
- while (get_next_line(fd, &line) == 1)
+ int offset;
+ int ret;
+
+ offset = ft_strlen(rest_buf);
+ ft_strncpy(buf, rest_buf, offset);
+ /* printf(">%s<\n", buf); */
+ rest_buf[0] = '\0';
+ if ((ret = read(fd, buf + offset, BUFFER_SIZE - offset)) == -1)
+ return (ERROR);
+ if (ret == 0)
+ return (0);
+ if (ret < BUFFER_SIZE - offset)
{
- printf(">%s\n", line);
- free(line);
+ /* buf[offset + ret - 1] = '\0'; */
+ /* printf(">eof? %s<\n", buf); */
}
- close(fd);
- return 0;
+ else
+ buf[BUFFER_SIZE] = '\0';
+ return (ret);
}
diff --git a/get_next_line.h b/get_next_line.h
index 7aa51e9..6d55f61 100644
--- a/get_next_line.h
+++ b/get_next_line.h
@@ -5,20 +5,8 @@
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
-/* Created: 2019/10/09 16:24:58 by cacharle #+# #+# */
-/* Updated: 2019/10/09 16:42:51 by cacharle ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* get_next_line.h :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */
-/* Updated: 2019/10/09 16:24:26 by cacharle ### ########.fr */
+/* Updated: 2019/10/10 10:19:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -27,7 +15,6 @@
# ifndef BUFFER_SIZE
# define BUFFER_SIZE (15)
-
# endif
# define LINE_READ 1
@@ -39,14 +26,14 @@
*/
int get_next_line(int fd, char **line);
+int read_after(int fd, char *buf, char *rest_buf);
/*
** get_next_line_utils.c - helper functions
*/
int find_newline(char *str);
-char *ft_strncpy(char *dest, const char *src, size_t n);
-char *ft_strcat(char *dest, const char *src);
+char *ft_strncpy(char *dest, const char *src, int n);
int ft_strlen(char *str);
#endif
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
index 8bae2c1..c48c62f 100644
--- a/get_next_line_utils.c
+++ b/get_next_line_utils.c
@@ -6,14 +6,10 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */
-/* Updated: 2019/10/09 16:24:20 by cacharle ### ########.fr */
+/* Updated: 2019/10/10 09:29:53 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
-#include <stdlib.h>
-#include "get_next_line.h"
-
-#include <stdio.h>
int find_newline(char *str)
{
int i;
@@ -25,9 +21,9 @@ int find_newline(char *str)
return (-1);
}
-char *ft_strncpy(char *dest, const char *src, size_t n)
+char *ft_strncpy(char *dest, const char *src, int n)
{
- size_t i;
+ int i;
i = 0;
while (src[i] && i < n)
@@ -40,24 +36,6 @@ char *ft_strncpy(char *dest, const char *src, size_t n)
return (dest);
}
-char *ft_strcat(char *dest, const char *src)
-{
- int i;
- int j;
-
- i = 0;
- while (dest[i])
- i++;
- j = 0;
- while (src[j])
- {
- dest[i + j] = src[j];
- j++;
- }
- dest[i + j] = '\0';
- return (dest);
-}
-
int ft_strlen(char *str)
{
int counter;
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..160c66b
--- /dev/null
+++ b/main.c
@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/10 09:27:41 by cacharle #+# #+# */
+/* Updated: 2019/10/10 09:56:06 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include "get_next_line.h"
+
+int main(int argc, char **argv)
+{
+ int fd;
+ char *line;
+ int ret;
+
+ printf("BUFFER_SIZE = %d\n", BUFFER_SIZE);
+ (void)argc;
+ fd = open(argv[1], O_RDONLY);
+ while ((ret = get_next_line(fd, &line)) == LINE_READ)
+ {
+ printf("[%s]\n", line);
+ free(line);
+ }
+ if (ret == -1)
+ printf("error\n");
+ else if (ret == 0)
+ printf("EOF\n");
+ close(fd);
+ return 0;
+}