From 375ca0d803559a5c22cc80111fa5f4595e668c82 Mon Sep 17 00:00:00 2001 From: minimonster Date: Wed, 26 Oct 2022 00:44:33 +0200 Subject: [PATCH] d --- a.out | Bin 0 -> 20704 bytes get_next_line.c | 90 ++++++++++++++++++++++++++++++++++++++++++ get_next_line.h | 12 ++++++ get_next_line_utils.c | 80 +++++++++++++++++++++++++++++++++++++ main.c | 15 +++++++ tester | 5 +++ 6 files changed, 202 insertions(+) create mode 100755 a.out create mode 100644 get_next_line.c create mode 100644 get_next_line.h create mode 100644 get_next_line_utils.c create mode 100644 main.c create mode 100644 tester diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..af04f43348a29d10a26fb575cadc00b50fc2e27e GIT binary patch literal 20704 zcmeHPdvIJ=c|Z5=o_*-;w`3cqR~G ztHes-1MXEc&F|6xT-{FFl`vrF9zfD-rp^p-sU{1C93er{o2>=>n!`|-agd%QmciXH zVgh5x{Gz`QDWiqdz6jTv@i8pdque~$s^uT-tc3fF-hBqL9-*Z7IivSEqsMTX0zzZR z`J|lCw%+8^4ckcxjiFmFW%S%|vsM!f$p^JtUv(0HjK3X5uP&s_2sd;zSukXOe-1s$ z%OBc!nr|`wW}AnU$v;DrUePv~?p?RGZ7|U~n9k()weDNLu65noaBeufMuk9eg|Jag zZGHDH0W%XsGiem|0gbag;>|aFlJ8Exvu0K1lWV&cpZe$<-t46vPx)>m8H$^9NQN5O z;}Rj9`Zd@{M(5)m^fZm&OUdsI5uB}E1;*SNnDjfFiT-}jLo>*SXOXA)XR>oA=pk(G zwg7~g%@lQ>5Lf3J2}yxO(wMXYD%O|e8Wn;b~z zoMd+AO`8UXGs&It-oYfS4-5@wjP;lnoly`$+L&hxd%J9}2SZdk1aLin`N-d5+C72} z$3)97P6tW<7A-FLIK#j1i8-2a%p6CUe!$QJU7F8y#!p|b=~QFo^*32Y=UOMpsWQ5* zORYa$Mn|B`{}0?(`A7p}e& zEGoNA{ z)`yh!%|h7+zQBdoyKw2ke~;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O(wMkTD}LMjU@rl>02|j2 mAOGooi))BV@xDz8h{s*V*{|l(HR;UyZCkX&2V4p+RQxy0u@N=^ literal 0 HcmV?d00001 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 + +