commit 375ca0d803559a5c22cc80111fa5f4595e668c82 Author: minimonster Date: Wed Oct 26 00:44:33 2022 +0200 d diff --git a/a.out b/a.out new file mode 100755 index 0000000..af04f43 Binary files /dev/null and b/a.out differ diff --git a/get_next_line.c b/get_next_line.c new file mode 100644 index 0000000..25ad771 --- /dev/null +++ b/get_next_line.c @@ -0,0 +1,90 @@ +#include "get_next_line.h" + +char *ft_getstash(int fd) +{ + char *str; + int readed; + + str = ft_calloc(BUFFER_SIZE, sizeof(char)); + if (str == NULL) + return (NULL); + readed = read(fd, str, BUFFER_SIZE); + if (readed < 0) + { + free(str); + return (NULL); + } + return (str); +} + +ssize_t ft_strchr(char *str, char c) +{ + size_t i; + + if (str == NULL) + return (-1); + i = 0; + while (str[i] != c && str[i] != '\0') + i++; + if (str[i] == '\0' && c != '\0') + return (-1); + return (i); +} + +char *ft_getline(int fd) +{ + char *stash; + char *buf; + size_t i; + + stash = NULL; + i = 0; + while (ft_strchr(stash + i * BUFFER_SIZE, '\n')) + { + stash = ft_realloc(stash, (i + 1) * BUFFER_SIZE); + if (stash == NULL) + return (NULL); + buf = ft_getstash(fd); + if (buf == NULL) + { + free(stash); + return (NULL); + } + ft_strcat(stash, buf); + free(buf); + if (stash[(i + 1) * BUFFER_SIZE -1] == '\0') + break ; + } + return (stash); +} + +char *get_next_line(int fd) +{ + static char *stash = NULL; + char *buf1; + char *buf2; + + buf1 = NULL; + if (ft_strchr(stash, '\n') == -1) + { + buf1 = ft_getline(fd); + if (buf1 == NULL) + return (NULL); + stash = ft_realloc(stash, ft_strchr(buf1, '\0') + ft_strchr(stash, '\0') + 1); + if (stash == NULL) + return (NULL); + ft_strcat(stash, buf1); + free(buf1); + } + buf1 = ft_strndup(stash, ft_strchr(stash, '\n')); + buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, '\0')); + free(stash); + stash = buf2; + if (stash == NULL || buf1 == NULL) + { + free(stash); + free(buf1); + return (NULL); + } + return (buf1); +} diff --git a/get_next_line.h b/get_next_line.h new file mode 100644 index 0000000..1bc3981 --- /dev/null +++ b/get_next_line.h @@ -0,0 +1,12 @@ +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H +# include +# include +# define BUFFER_SIZE 42 + +void *ft_calloc(size_t nmemb, size_t size); +void *ft_realloc(void *ptr, size_t size); +char *ft_strcat(char *dst, char *src); +char *ft_strndup(char *src, size_t n); +char *get_next_line(int fd); +#endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c new file mode 100644 index 0000000..a495aa9 --- /dev/null +++ b/get_next_line_utils.c @@ -0,0 +1,80 @@ +#include "get_next_line.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + char *tab; + size_t i; + + if (nmemb == 0 || size * nmemb / nmemb != size) + return (NULL); + tab = malloc(nmemb * size); + if (tab == NULL) + return (NULL); + i = 0; + while (i < size * nmemb) + { + tab[i] = 0; + i++; + } + return ((void *) tab); +} + +void *ft_realloc(void *ptr, size_t size) +{ + char *tab; + char *in; + size_t i; + + in = ptr; + tab = ft_calloc(1, size); + if (tab == NULL) + return (NULL); + if (ptr == NULL) + return (tab); + i = 0; + while (in[i] != 0 && i < size) + { + tab[i] = in[i]; + i++; + } + free(ptr); + return (tab); +} + +char *ft_strcat(char *dst, char *src) +{ + size_t i; + size_t j; + + j = 0; + while (dst[j] != '\0') + j++; + i = 0; + if (src != NULL) + { + while (src[i] != '\0') + { + dst[i + j] = src[i]; + i++; + } + } + dst[i] = '\0'; + return (dst); +} + +char *ft_strndup(char *src, size_t n) +{ + char *out; + size_t i; + + out = ft_calloc(n + 1, sizeof(char)); + if (out == NULL) + return (NULL); + i = 0; + while (src[i] != '\0' && i < n) + { + out[i] = src[i]; + i++; + } + return (out); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..6f2fbe0 --- /dev/null +++ b/main.c @@ -0,0 +1,15 @@ +#include "get_next_line.h" +#include + +#include +#include +#include +int main(void) +{ + int fd = open("tester", O_RDONLY); + printf("%s\n", get_next_line(fd)); + printf("%s\n", get_next_line(fd)); + printf("%s\n", get_next_line(fd)); + printf("%s\n", get_next_line(fd)); + +} diff --git a/tester b/tester new file mode 100644 index 0000000..4b64f4d --- /dev/null +++ b/tester @@ -0,0 +1,5 @@ +test1 +test2 +test3 + +