bonus, first implementation

This commit is contained in:
Camille Chauvet 2022-11-14 15:45:44 +01:00
parent 87432b195e
commit 66aa7a2268
4 changed files with 126 additions and 12 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 16:01:01 by cchauvet ### ########.fr */
/* Updated: 2022/11/14 15:37:48 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -1,7 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:40:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# 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);

104
get_next_line_bonus.h Normal file
View File

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:45:00 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_getstash(int fd)
{
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 1)
{
free(str);
return (NULL);
}
return (str);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
stash = NULL;
buf = NULL;
while (ft_strchr(stash, '\n') == -1)
{
buf = ft_getstash(fd);
if (buf == NULL)
return (stash);
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
}
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[1024] = NULL;
char *buf1;
char *buf2;
buf2 = stash[fd];
if (ft_strchr(stash[fd], '\n') == -1)
{
buf1 = ft_getline(fd);
buf2 = ft_strfjoin(stash[fd], buf1);
if (buf2 == NULL)
{
free(buf1);
return (NULL);
}
}
buf1 = ft_getreturn(buf2);
stash[fd] = ft_getextra(buf2);
free(buf2);
if (buf1 == NULL)
{
free(stash[fd]);
free(buf1);
return (NULL);
}
return (buf1);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/10/28 14:38:10 by cchauvet ### ########.fr */
/* Updated: 2022/11/14 15:39:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,6 +30,7 @@ void *ft_calloc(size_t nmemb, size_t size)
}
return ((void *) tab);
}
size_t ft_strlen(char *str)
{
size_t i;
@ -44,8 +45,8 @@ size_t ft_strlen(char *str)
char *ft_strfjoin(char *s1, char *s2)
{
size_t i;
size_t j;
ssize_t i;
ssize_t j;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
@ -61,14 +62,11 @@ char *ft_strfjoin(char *s1, char *s2)
}
}
free(s1);
j = 0;
j = -1;
if (s2 != NULL)
{
while (s2[j] != '\0')
{
while (s2[++j] != '\0')
out[i + j] = s2[j];
j++;
}
}
free(s2);
return (out);
@ -106,4 +104,3 @@ ssize_t ft_strchr(char *str, char c)
return (-1);
return (i);
}