Archived
1
0
This commit is contained in:
Adam Joly
2024-01-04 16:36:32 +01:00
parent 32983af238
commit fb3fae1e1a
27 changed files with 432 additions and 261 deletions

View File

@ -6,12 +6,11 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/23 06:51:37 by adjoly ### ########.fr */
/* Updated: 2024/01/04 00:45:17 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "../libft/libft.h"
char check_line(char *res, char *buf)
{
@ -57,11 +56,11 @@ char *get_next_line(int fd)
if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023)
return (NULL);
if (!buf)
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc(1, 1);
buf = ft_calloc_gnl(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc_gnl(1, 1);
while (buf)
{
res = ft_strjoinf(res, buf);
res = ft_strjoin_gnl(res, buf);
if (!res)
return (NULL);
if (check_line(res, buf))

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2023/12/23 06:51:07 by adjoly ### ########.fr */
/* Updated: 2024/01/04 00:38:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,8 +21,8 @@
# endif
char *get_next_line(int fd);
char *ft_strjoinf(char *s1, char *s2);
// size_t ft_strlen(char *s);
// void *ft_calloc(size_t nmemb, size_t size);
char *ft_strjoin_gnl(char *s1, char *s2);
size_t ft_strlen_gnl(char *s);
void *ft_calloc_gnl(size_t nmemb, size_t size);
#endif

Binary file not shown.

View File

@ -1,74 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/15 05:51:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char check_line(char *res, char *buf)
{
int i;
int j;
i = 0;
j = 0;
while (res[i] && res[i] != '\n')
i++;
if (res[i] == '\n')
{
i++;
while (res[i])
{
buf[j] = res[i];
i++;
j++;
}
buf[j] = 0;
res[i - j] = 0;
return (1);
}
return (0);
}
char *ft_read_error(char **buf, char *res)
{
free(*buf);
*buf = NULL;
if (res[0] != 0)
return (res);
free(res);
return (NULL);
}
char *get_next_line(int fd)
{
static char *buf[1024];
char *res;
ssize_t bytes_read;
if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023)
return (NULL);
if (!buf[fd])
buf[fd] = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc(1, 1);
while (buf[fd] && res)
{
res = ft_strjoin(res, buf[fd]);
if (!res)
return (NULL);
if (check_line(res, buf[fd]))
return (res);
bytes_read = read(fd, buf[fd], BUFFER_SIZE);
if (bytes_read < 1)
return (ft_read_error(&buf[fd], res));
buf[fd][bytes_read] = 0;
}
return (NULL);
}

View File

@ -1,28 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2023/12/15 05:43:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
# include <unistd.h>
# include <stdlib.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
# endif
char *get_next_line(int fd);
char *ft_strjoin(char *s1, char *s2);
size_t ft_strlen(char *s);
void *ft_calloc(size_t nmemb, size_t size);
#endif

View File

@ -6,24 +6,24 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/23 06:52:24 by adjoly ### ########.fr */
/* Updated: 2024/01/04 00:47:44 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "../libft/libft.h"
// size_t ft_strlen(char *s)
// size_t ft_strlen_gnl(char *s)
// {
// size_t i;
//
// i = 0;
// while (s[i])
// i++;
// return (i);
// }
char *ft_strjoinf(char *s1, char *s2)
char *ft_strjoin_gnl(char *s1, char *s2)
{
char *result;
size_t i;
@ -33,7 +33,7 @@ char *ft_strjoinf(char *s1, char *s2)
j = 0;
if (!s2)
return (NULL);
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
result = ft_calloc_gnl((ft_strlen(s1) + ft_strlen(s2) + 1), 1);
if (result == NULL)
return (NULL);
while (s1[i])
@ -52,7 +52,7 @@ char *ft_strjoinf(char *s1, char *s2)
return (result);
}
void *ft_calloc(size_t nmemb, size_t size)
void *ft_calloc_gnl(size_t nmemb, size_t size)
{
char *result;
size_t i;

View File

@ -1,75 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/15 05:42:57 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strjoin(char *s1, char *s2)
{
char *result;
size_t i;
size_t j;
i = 0;
j = 0;
if (!s2)
return (NULL);
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (result == NULL)
return (NULL);
while (s1[i])
{
result[i] = s1[i];
i++;
}
while (s2[j])
{
result[i] = s2[j];
i++;
j++;
}
free(s1);
result[i] = '\0';
return (result);
}
void *ft_calloc(size_t nmemb, size_t size)
{
char *result;
size_t i;
i = 0;
if (nmemb == 0 || size == 0)
return (malloc(1));
if (((unsigned long long)(size * nmemb) > 4294967295))
return (NULL);
if ((int)size < 0 && (int)nmemb < 0)
return (NULL);
result = malloc(size * nmemb);
if (!result)
return (NULL);
while (i < (size * nmemb))
{
*(unsigned char *)(result + i) = '\0';
i++;
}
return (result);
}