diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | Makefile | 19 | ||||
| -rw-r--r-- | author | 1 | ||||
| -rw-r--r-- | get_next_line.c | 109 | ||||
| -rw-r--r-- | get_next_line.h | 25 | ||||
| -rw-r--r-- | get_next_line_utils.c | 93 | ||||
| m--------- | libft | 0 |
8 files changed, 191 insertions, 60 deletions
@@ -1,3 +1,4 @@ *.o a.out get_next_line +*.ghc diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 92ebdd9..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "libft"] - path = libft - url = https://github.com/HappyTramp/libft @@ -6,34 +6,27 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 08:11:00 by cacharle #+# #+# # -# Updated: 2019/10/08 08:21:06 by cacharle ### ########.fr # +# Updated: 2019/10/08 11:52:21 by cacharle ### ########.fr # # # # **************************************************************************** # -LIBFTPATH = ./libft +$(RM) = rm -f CC = gcc -CCFLAGS = -Wall -Wextra #-Werror -LDFALGS = -L./libft -lft -INCLFLAGS = -I./libft +CCFLAGS = -Wall -Wextra #-Werror -D BUFFER_SIZE=32 NAME = get_next_line -SRC = get_next_line.c +SRC = get_next_line.c get_next_line_utils.c OBJ = $(SRC:.c=.o) INCLUDE = get_next_line.h -$(RM) = rm -f - -# Makefile must not relink ?? - all: $(NAME) $(NAME): $(OBJ) $(INCLUDE) - make -C $(LIBFTPATH) - $(CC) $(LDFLAGS) $(CCFLAGS) -o $(NAME) $(OBJ) + $(CC) $(CCFLAGS) -o $(NAME) $(OBJ) %.o: %.c - $(CC) $(INCLFLAGS) $(CCFLAGS) -c -o $@ $< + $(CC) $(CCFLAGS) -c -o $@ $< clean: $(RM) $(OBJ) @@ -1 +0,0 @@ -cacharle 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; } diff --git a/get_next_line.h b/get_next_line.h index a70ea24..d4c866a 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -1,8 +1,29 @@ #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H -# define BUFF_SIZE (1 << 8) +# ifndef BUFFER_SIZE +# define BUFFER_SIZE (1 << 8) +# endif -int get_next_line(const int fd, char **line); +# define LINE_READ 1 +# define NO_LINE_READ 0 +# define ERROR -1 + + +/* +** get_next_line.c +*/ + +int get_next_line(int fd, char **line); + +/* +** get_next_line_utils.c - helper functions +*/ + +int find_newline(char *str); +char *strappend(char *dest, char *src); +char *ft_strcpy(char *dest, const char *src); +char *ft_strcat(char *dest, const char *src); +int ft_strlen(char *str); #endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c new file mode 100644 index 0000000..754bf22 --- /dev/null +++ b/get_next_line_utils.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */ +/* Updated: 2019/10/08 14:57:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "get_next_line.h" + +#include <stdio.h> +int find_newline(char *str) +{ + int i; + + i = -1; + while (str[++i]) + if (str[i] == '\n') + return (i); + return (-1); +} + +char *strappend(char *dest, char *src) +{ + void *copy; + + if (dest == NULL) + { + if ((dest = (char*)malloc(sizeof(char) * (ft_strlen(src) + 1))) == NULL) + return (NULL); + ft_strcpy(dest, src); + return (dest); + } + if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL) + return (NULL); + ft_strcpy(copy, dest); + free(dest); + if ((dest = (char*)malloc(sizeof(char) + * (ft_strlen(dest) + ft_strlen(src) + 1))) == NULL) + return (NULL); + ft_strcpy(dest, copy); + /* dest[ft_strlen(dest) + ft_strlen(src)] = '\0'; */ + free(copy); + ft_strcat(dest, src); + return (dest); +} + +char *ft_strcpy(char *dest, const char *src) +{ + int i; + + i = 0; + while (src[i]) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + 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; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} diff --git a/libft b/libft deleted file mode 160000 -Subproject 9a2b208985ac7d4644c718ada74770b98eeb459 |
