This commit is contained in:
Camille Chauvet 2022-11-08 14:49:12 +01:00
parent 386bef0a64
commit d4f57e8f7b
6 changed files with 56 additions and 63 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/10/28 13:37:18 by cchauvet ### ########.fr */
/* Updated: 2022/10/28 16:01:01 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ char *ft_getstash(int fd)
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 0)
if (readed < 1)
{
free(str);
return (NULL);
@ -29,40 +29,18 @@ char *ft_getstash(int fd)
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')
return (-1);
return (i);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
size_t i;
stash = NULL;
buf = NULL;
i = 0;
while (ft_strchr(stash + i, '\n') == -1)
while (ft_strchr(stash, '\n') == -1)
{
i = i + ft_strlen(buf);
buf = ft_getstash(fd);
if (buf == NULL)
return (NULL);
if (buf[0] == '\0')
{
free(buf);
break ;
}
return (stash);
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
@ -70,6 +48,32 @@ char *ft_getline(int fd)
return (stash);
}
char *ft_getreturn(char *str)
{
int i;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
i = ft_strlen(str);
return(ft_strndup(str, i));
}
char *ft_getextra(char *str)
{
int i;
int j;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
return (NULL);
j = ft_strlen(str + i);
return(ft_strndup(str + i, j));
}
char *get_next_line(int fd)
{
static char *stash = NULL;
@ -80,16 +84,17 @@ char *get_next_line(int fd)
if (ft_strchr(stash, '\n') == -1)
{
buf1 = ft_getline(fd);
if (buf1 == NULL)
return (NULL);
buf2 = ft_strfjoin(stash, buf1);
if (buf2 == NULL)
{
free(buf1);
return (NULL);
}
}
buf1 = ft_strndup(buf2, ft_strchr(buf2, '\n') + 1);
stash = ft_strndup(buf2 + ft_strchr(buf2, '\n') + 1, ft_strlen(buf2));
buf1 = ft_getreturn(buf2);
stash = ft_getextra(buf2);
free(buf2);
if (stash == NULL || buf1 == NULL)
if (buf1 == NULL)
{
free(stash);
free(buf1);

View File

@ -3,12 +3,13 @@
# include <stdlib.h>
# include <unistd.h>
# define BUFFER_SIZE 10
//# define BUFFER_SIZE 40
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
ssize_t ft_strchr(char *str, char c);
char *get_next_line(int fd);
#endif

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/10/26 23:55:03 by cchauvet ### ########.fr */
/* Updated: 2022/10/28 14:38:10 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -79,6 +79,8 @@ char *ft_strndup(char *src, size_t n)
char *out;
size_t i;
if (src[0] == '\0')
return (NULL);
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
@ -90,3 +92,18 @@ char *ft_strndup(char *src, size_t n)
}
return (out);
}
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')
return (-1);
return (i);
}

@ -1 +0,0 @@
Subproject commit 2e28a6ce9454666c6670645a60a1b9b4404d0c65

26
main.c
View File

@ -1,26 +0,0 @@
#include "get_next_line.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd = open("tester", O_RDONLY);
char *str;
str = get_next_line(fd);
printf("%s", str);
free(str);
str = get_next_line(fd);
printf("%s", str);
free(str);
str = get_next_line(fd);
printf("%s", str);
free(str);
// 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));
}

3
tester
View File

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