d
This commit is contained in:
parent
375ca0d803
commit
07565c2c6c
BIN
.get_next_line.h.swp
Normal file
BIN
.get_next_line.h.swp
Normal file
Binary file not shown.
@ -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"
|
||||
|
||||
char *ft_getstash(int fd)
|
||||
@ -50,8 +62,7 @@ char *ft_getline(int fd)
|
||||
free(stash);
|
||||
return (NULL);
|
||||
}
|
||||
ft_strcat(stash, buf);
|
||||
free(buf);
|
||||
ft_strfcat(stash, buf);
|
||||
if (stash[(i + 1) * BUFFER_SIZE -1] == '\0')
|
||||
break ;
|
||||
}
|
||||
@ -64,20 +75,18 @@ char *get_next_line(int fd)
|
||||
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);
|
||||
stash = ft_realloc(stash, ft_strchr(buf1, 0) + ft_strchr(stash, 0) + 2);
|
||||
if (stash == NULL)
|
||||
return (NULL);
|
||||
ft_strcat(stash, buf1);
|
||||
free(buf1);
|
||||
ft_strfcat(stash, buf1);
|
||||
}
|
||||
buf1 = ft_strndup(stash, ft_strchr(stash, '\n'));
|
||||
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, '\0'));
|
||||
buf1 = ft_strndup(stash, ft_strchr(stash, '\n') + 1);
|
||||
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strchr(stash, 0));
|
||||
free(stash);
|
||||
stash = buf2;
|
||||
if (stash == NULL || buf1 == NULL)
|
||||
|
@ -2,11 +2,10 @@
|
||||
# define GET_NEXT_LINE_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# 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_strfcat(char *dst, char *src);
|
||||
char *ft_strndup(char *src, size_t n);
|
||||
char *get_next_line(int fd);
|
||||
#endif
|
||||
|
@ -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"
|
||||
|
||||
void *ft_calloc(size_t nmemb, size_t size)
|
||||
@ -26,7 +38,7 @@ void *ft_realloc(void *ptr, size_t size)
|
||||
size_t i;
|
||||
|
||||
in = ptr;
|
||||
tab = ft_calloc(1, size);
|
||||
tab = ft_calloc(sizeof(char), size);
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
if (ptr == NULL)
|
||||
@ -41,7 +53,7 @@ void *ft_realloc(void *ptr, size_t size)
|
||||
return (tab);
|
||||
}
|
||||
|
||||
char *ft_strcat(char *dst, char *src)
|
||||
char *ft_strfcat(char *dst, char *src)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
@ -59,6 +71,7 @@ char *ft_strcat(char *dst, char *src)
|
||||
}
|
||||
}
|
||||
dst[i] = '\0';
|
||||
free(src);
|
||||
return (dst);
|
||||
}
|
||||
|
||||
|
10
main.c
10
main.c
@ -7,9 +7,11 @@
|
||||
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));
|
||||
printf("%s", get_next_line(fd));
|
||||
// printf("%s", get_next_line(fd));
|
||||
// printf("%s", get_next_line(fd));
|
||||
// printf("%s", get_next_line(fd));
|
||||
// printf("%s", get_next_line(fd));
|
||||
// printf("%s", get_next_line(fd));
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user