bonus started
This commit is contained in:
49
get_next_line_bonus.c
Normal file
49
get_next_line_bonus.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/24 15:55:25 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/24 17:22:10 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
static char *get_lines(int fd, char *str)
|
||||
{
|
||||
char buf[BUFFER_SIZE];
|
||||
int res;
|
||||
|
||||
res = read(fd, buf, BUFFER_SIZE);
|
||||
while (res > 0)
|
||||
{
|
||||
if (!str)
|
||||
str = ft_strdup(buf);
|
||||
else
|
||||
str = ft_strjoin_new(str, buf, res);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
ft_bzero(buf, BUFFER_SIZE);
|
||||
res = read(fd, buf, BUFFER_SIZE);
|
||||
}
|
||||
if (res < 0)
|
||||
{
|
||||
if (str)
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *strs[1024];
|
||||
|
||||
if (!strs[fd])
|
||||
strs[fd] = get_lines(fd, strs[fd]);
|
||||
|
||||
}
|
||||
29
get_next_line_bonus.h
Normal file
29
get_next_line_bonus.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_bonus.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/24 15:56:45 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/24 17:22:18 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_BONUS_H
|
||||
# define GET_NEXT_LINE_BONUS_H
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 42
|
||||
# endif
|
||||
|
||||
# include <stdlib.h>
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
||||
size_t ft_strlen(const char *s);
|
||||
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
|
||||
char *ft_strdup(const char *s);
|
||||
void ft_bzero(void *s, size_t n);
|
||||
|
||||
#endif
|
||||
77
get_next_line_utils_bonus.c
Normal file
77
get_next_line_utils_bonus.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/24 16:04:39 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/24 16:12:37 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
|
||||
{
|
||||
char *str;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (s1[i])
|
||||
{
|
||||
str[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (j <= limit && s2[j])
|
||||
{
|
||||
str[i + j] = s2[j];
|
||||
j++;
|
||||
}
|
||||
str[i + j] = '\0';
|
||||
free((void *) s1);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strdup(const char *s)
|
||||
{
|
||||
char *dest;
|
||||
int i;
|
||||
|
||||
dest = malloc(sizeof(char) *(ft_strlen(s) + 1));
|
||||
if (!dest)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
dest[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
||||
|
||||
void ft_bzero(void *s, size_t n)
|
||||
{
|
||||
while (n > 0)
|
||||
{
|
||||
*((unsigned char *) s) = '\0';
|
||||
s++;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user