This commit is contained in:
Camille Chauvet 2022-10-26 02:27:09 +02:00
parent 375ca0d803
commit 07565c2c6c
7 changed files with 39 additions and 21 deletions

BIN
.get_next_line.h.swp Normal file

Binary file not shown.

BIN
a.out

Binary file not shown.

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/10/26 01:53:25 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
char *ft_getstash(int fd) char *ft_getstash(int fd)
@ -50,8 +62,7 @@ char *ft_getline(int fd)
free(stash); free(stash);
return (NULL); return (NULL);
} }
ft_strcat(stash, buf); ft_strfcat(stash, buf);
free(buf);
if (stash[(i + 1) * BUFFER_SIZE -1] == '\0') if (stash[(i + 1) * BUFFER_SIZE -1] == '\0')
break ; break ;
} }
@ -64,20 +75,18 @@ char *get_next_line(int fd)
char *buf1; char *buf1;
char *buf2; char *buf2;
buf1 = NULL;
if (ft_strchr(stash, '\n') == -1) if (ft_strchr(stash, '\n') == -1)
{ {
buf1 = ft_getline(fd); buf1 = ft_getline(fd);
if (buf1 == NULL) if (buf1 == NULL)
return (NULL); return (NULL);
stash = ft_realloc(stash, ft_strchr(buf1, '\0') + ft_strchr(stash, '\0') + 1); stash = ft_realloc(stash, ft_strchr(buf1, 0) + ft_strchr(stash, 0) + 2);
if (stash == NULL) if (stash == NULL)
return (NULL); return (NULL);
ft_strcat(stash, buf1); ft_strfcat(stash, buf1);
free(buf1);
} }
buf1 = ft_strndup(stash, ft_strchr(stash, '\n')); buf1 = ft_strndup(stash, ft_strchr(stash, '\n') + 1);
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, '\0')); buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, 0));
free(stash); free(stash);
stash = buf2; stash = buf2;
if (stash == NULL || buf1 == NULL) if (stash == NULL || buf1 == NULL)

View File

@ -2,11 +2,10 @@
# define GET_NEXT_LINE_H # define GET_NEXT_LINE_H
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# define BUFFER_SIZE 42
void *ft_calloc(size_t nmemb, size_t size); void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size); void *ft_realloc(void *ptr, size_t size);
char *ft_strcat(char *dst, char *src); char *ft_strfcat(char *dst, char *src);
char *ft_strndup(char *src, size_t n); char *ft_strndup(char *src, size_t n);
char *get_next_line(int fd); char *get_next_line(int fd);
#endif #endif

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/10/26 01:24:43 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
void *ft_calloc(size_t nmemb, size_t size) void *ft_calloc(size_t nmemb, size_t size)
@ -26,7 +38,7 @@ void *ft_realloc(void *ptr, size_t size)
size_t i; size_t i;
in = ptr; in = ptr;
tab = ft_calloc(1, size); tab = ft_calloc(sizeof(char), size);
if (tab == NULL) if (tab == NULL)
return (NULL); return (NULL);
if (ptr == NULL) if (ptr == NULL)
@ -41,7 +53,7 @@ void *ft_realloc(void *ptr, size_t size)
return (tab); return (tab);
} }
char *ft_strcat(char *dst, char *src) char *ft_strfcat(char *dst, char *src)
{ {
size_t i; size_t i;
size_t j; size_t j;
@ -59,6 +71,7 @@ char *ft_strcat(char *dst, char *src)
} }
} }
dst[i] = '\0'; dst[i] = '\0';
free(src);
return (dst); return (dst);
} }

10
main.c
View File

@ -7,9 +7,11 @@
int main(void) int main(void)
{ {
int fd = open("tester", O_RDONLY); int fd = open("tester", O_RDONLY);
printf("%s\n", get_next_line(fd)); printf("%s", get_next_line(fd));
printf("%s\n", get_next_line(fd)); // printf("%s", get_next_line(fd));
printf("%s\n", get_next_line(fd)); // printf("%s", get_next_line(fd));
printf("%s\n", get_next_line(fd)); // printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
} }

5
tester
View File

@ -1,5 +0,0 @@
test1
test2
test3