GNL with static buf worked. Next step, find a way to catch multiple nl when they're in the same buf

This commit is contained in:
David Gailleton
2025-11-21 19:07:39 +01:00
parent e7d0e929b3
commit 2b7dad9c15
3 changed files with 174 additions and 4 deletions

View File

@@ -6,11 +6,93 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */ /* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */
/* Updated: 2025/11/21 17:21:22 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/21 19:06:31 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h"
#include <unistd.h>
static char *ft_read_line(int fd, char *dest, char buf[BUFFER_SIZE])
{
int readed;
int i;
readed = 1;
while (readed)
{
readed = read(fd, buf, BUFFER_SIZE);
if (readed < 0)
return (NULL);
i = index_of_nl(buf, BUFFER_SIZE);
if (i < 0)
dest = ft_strjoin_new(dest, buf, readed);
else
dest = ft_strjoin_new(dest, buf, i);
if (!dest)
return (NULL);
if (i >= 0)
break ;
}
return (dest);
}
static char *create_new_line(char buf[BUFFER_SIZE], char *base_str)
{
char *new_str;
int buf_len;
int nl_i;
if (!buf)
return (NULL);
buf_len = ft_strlen(buf);
nl_i = index_of_nl(buf, buf_len);
if (nl_i >= 0)
{
new_str = ft_substr(buf, nl_i, buf_len - nl_i);
free(base_str);
return (new_str);
}
return (NULL);
}
char *get_next_line(int fd) char *get_next_line(int fd)
{ {
return (0); static char buf[BUFFER_SIZE];
char *nl;
char *temp;
nl = malloc(sizeof(char));
if (!nl)
return (NULL);
temp = create_new_line(buf, nl);
if (temp)
nl = ft_read_line(fd, temp, buf);
else
{
nl[0] = '\0';
nl = ft_read_line(fd, nl, buf);
}
return (nl);
}
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
char *str;
int fd;
if (argc > 1)
{
fd = open(argv[1], O_RDONLY);
str = get_next_line(fd);
printf("%s", str);
free(str);
str = get_next_line(fd);
printf("%s", str);
free(str);
close(fd);
}
} }

View File

@@ -6,13 +6,24 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */ /* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */
/* Updated: 2025/11/21 17:22:38 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/21 18:57:22 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef GET_NEXT_LINE_H #ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H # define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
# include <stdlib.h>
char *get_next_line(int fd); char *get_next_line(int fd);
int index_of_nl(char *str, int limit);
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
size_t ft_strlen(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
#endif #endif

View File

@@ -6,7 +6,84 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */ /* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
/* Updated: 2025/11/21 17:21:49 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/21 18:57:06 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h"
int index_of_nl(char *str, int limit)
{
int i;
i = 0;
while (i < limit)
{
if (str[i] == '\n')
return (i);
i++;
}
return (-1);
}
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)
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
free((void *) s1);
return (str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *sub_str;
size_t s_len;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start > s_len)
len = 0;
else if (s_len < (start + len))
len = s_len - start;
sub_str = malloc(sizeof(char) * (len + 1));
if (!sub_str)
return (NULL);
i = 0;
while (i < len)
{
sub_str[i] = s[start + i];
i++;
}
sub_str[i] = '\0';
return (sub_str);
}