diff --git a/README.md b/README.md deleted file mode 100644 index 55a13e9..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# get_next_line \ No newline at end of file diff --git a/get_next_line.c b/get_next_line.c index 30e4532..20ab6ff 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -1,175 +1,100 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/14 02:40:53 by apommier #+# #+# */ -/* Updated: 2020/12/18 10:05:11 by apommier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "get_next_line.h" -#include -#include -#include -#include - -char *ft_strjoin(char *save, char *s2) -{ - char *dest; - int i; - int j; - - i = 0; - j = 0; - if (!save && !s2) - return(0); - if(!(dest = (char*)malloc(ft_strlen(save) + ft_strlen(s2) + 1))) - return (0); - while (save && save[i]) - { - dest[j] = save[i]; - j++; - i++; - } - i = 0; - while (s2 && s2[i]) - { - dest[j] = s2[i]; - j++; - i++; - } - dest[j] = 0; - return (dest); -} - -char *up_save(char **save, int fd, int *end) -{ - char *dest; - char *delete; - - dest = 0; - delete = *save; - if (!(dest = (char*)malloc(1 + BUFFER_SIZE))) - { - if (*save) - free(*save); - return (0); - } - *end = read(fd, dest, BUFFER_SIZE); - if (*end == -1) - return (0); - dest[*end] = 0; - if (!(*save = ft_strjoin(*save, dest))) - { - free(dest); - free(delete); - return (0); - } - free(delete); - free(dest); - return (*save); -} - -int is_line(char *save, int *end) -{ - int i; - - i = 0; - if (save == 0) - return (1); - if (*end < BUFFER_SIZE) - { - while (save[i] && save[i] != '\n') - i++; - if (save[i] == '\n') - *end = BUFFER_SIZE; - } - i = 0; - while (save[i]) - { - if (save[i] == '\n') - { - save[i] = 0; - return (1); - } - i++; - } - if (*end < BUFFER_SIZE) - return (1); - return (0); -} - -char *is_next_line(char **save, int *end, int fd) -{ - char *delete; - int i; - - i = 0; - delete = *save; - while (*save && (*save)[i]) - i++; - if (*save && !(*save = ft_strjoin(*save + i + 1, 0))) - { - free(delete); - return (0); - } - up_save(save, fd, end); - while (!is_line(*save, end)) - { - up_save(save, fd, end); - if (!(*save)) - { - free(delete); - return (0); - } - } - free(delete); - return (*save); -} - - -int get_next_line(int fd, char **line) -{ - int end; - static char **save; - - if (!save) - { - if(!(save = malloc(sizeof(char*)))) - return (-1); - *save = 0; - } - else if (*save == 0) - { - *line = *save; - free(*save); - free(save); - return (0); - } - end = BUFFER_SIZE; - if (fd < 0 || !line || BUFFER_SIZE < 1) - { - return (-1); - free(save); - } - is_next_line(save, &end, fd); - if (!(*save)) - return (-1); - free(*line); - *line = ft_strjoin(*save, 0); - if (!(*line)) - { - free(*save); - return (-1); - } - if (end < BUFFER_SIZE) - { - free(*save); - free(save); - return (0); - } - return (1); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kinou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/23 07:55:17 by kinou #+# #+# */ +/* Updated: 2021/11/01 10:21:35 by kinou ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +char *ft_free(char *save, int *end) +{ + if (!*end) + { + free(save); + free(end); + return (0); + } + free(end); + return (save); +} + +char *set_line(char *line, char *save) +{ + int i; + + i = 0; + line = ft_strjoin(save, 0); + while (line[i] && line[i] != '\n') + i++; + if (line[i] == '\n') + { + while (line[i++]) + line[i] = '\0'; + } + return (line); +} + +char *set_save(char *save) +{ + char *delete; + int i; + + i = 0; + delete = save; + while (save[i] && save[i] != '\n') + i++; + if (save[i] != '\n') + i = 0; + save = ft_strjoin((save + i + 1), 0); + free(delete); + return (save); +} + +char *next_line(char *save, int *end, int fd) +{ + char *delete; + char *dest; + + while (!ft_strchr(save, '\n') && *end > 0) + { + dest = ft_calloc(1, BUFFER_SIZE + 1); + *end = read(fd, dest, BUFFER_SIZE); + delete = save; + save = ft_strjoin(save, dest); + free(delete); + free(dest); + } + return (save); +} + +char *get_next_line(int fd) +{ + static char *save = NULL; + int *end; + char *line; + + line = 0; + if (fd < 0 || BUFFER_SIZE < 1) + return (0); + end = malloc(sizeof(int *)); + *end = 1; + if (save == NULL) + save = ft_calloc(1, 1); + save = next_line(save, end, fd); + line = set_line(line, save); + if (ft_strlen(line) > 0) + { + save = set_save(save); + save = ft_free(save, end); + return (line); + } + free(line); + save = ft_free(save, end); + return (0); +} diff --git a/get_next_line.h b/get_next_line.h index cd4ea69..eb41fd9 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,20 +6,20 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/14 05:58:49 by apommier #+# #+# */ -/* Updated: 2020/12/16 08:44:39 by apommier ### ########.fr */ +/* Updated: 2021/11/02 18:16:14 by kinou ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H -#include -#include -int get_next_line(int fd, char **line); -int is_line(char *save, int *end); -char *up_save(char **save, int fd, int *end); -char *ft_strjoin(char *savee, char *s2); +# include +# include + int ft_strlen(char *s1); -char *is_nex_line(char **save, int *end, int fd); +char *get_next_line(int fd); +char *ft_strjoin(char *savee, char *s2); +char *ft_strchr(const char *s, int c); +void *ft_calloc(size_t nmemb, size_t size); #endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 4d7c4da..2cb37d3 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -12,9 +12,10 @@ #include "get_next_line.h" -int ft_strlen(char *s1) +int ft_strlen(char *s1) { - int i; + int i; + i = 0; if (!s1) return (0); @@ -22,3 +23,64 @@ int ft_strlen(char *s1) i++; return (i); } + +char *ft_strjoin(char *save, char *s2) +{ + char *dest; + int i; + int j; + + i = 0; + j = 0; + if (!save && !s2) + return (0); + dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1); + while (save && save[i]) + { + dest[j] = save[i]; + j++; + i++; + } + i = 0; + while (s2 && s2[i]) + { + dest[j] = s2[i]; + j++; + i++; + } + dest[j] = 0; + return (dest); +} + +char *ft_strchr(const char *s, int c) +{ + char *str; + + if (!s) + return ("no s"); + str = (char *)s; + while ((*str != c) && (*str != 0)) + str++; + if (*str == c) + return ((char *)str); + else + return (0); +} + +void *ft_calloc(size_t nmemb, size_t size) +{ + char *new; + int i; + + i = 0; + new = malloc(size * nmemb); + if (new) + { + while (size * nmemb - i) + { + new[i] = 0; + i++; + } + } + return ((void *)new); +} diff --git a/texte b/texte deleted file mode 100644 index b26001b..0000000 --- a/texte +++ /dev/null @@ -1,9 +0,0 @@ -ijhgfddfghjkjhgfdl -dhjkjhlkgjgfh -cgbxf -gsdfg -dfgdgfdghdfhdg -dg -dhgdhdfjhdghdghd -hg -dhgdhgdhgdgh``