mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-05 23:18:08 +01:00
128 lines
3.2 KiB
C
128 lines
3.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* get_next_line.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/01 15:08:34 by jguelen #+# #+# */
|
|
/* Updated: 2025/01/06 14:37:50 by jguelen ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
#include "get_next_line.h"
|
|
|
|
/*Finds the next \n in the buffer and sets *copy_til to it and returns 1 if
|
|
found. If no \n is found, returns 0 and sets copy_til to buffer->end if
|
|
the buffer is not empty*/
|
|
static int find_eol(t_buffer *buffer, char **copy_til)
|
|
{
|
|
size_t i;
|
|
|
|
i = 0;
|
|
if (buffer->start == NULL)
|
|
return (0);
|
|
while (buffer->start + i <= buffer->end)
|
|
{
|
|
if (buffer->start[i] == '\n')
|
|
{
|
|
*copy_til = buffer->start + i;
|
|
return (1);
|
|
}
|
|
i++;
|
|
}
|
|
*copy_til = buffer->end;
|
|
return (0);
|
|
}
|
|
|
|
static int line_aggregation(t_buffer *buffer, char **copy_til, char **line,
|
|
size_t *len)
|
|
{
|
|
char *new;
|
|
size_t old_len;
|
|
|
|
old_len = *len;
|
|
if (!buffer->start)
|
|
return (1);
|
|
*len += *copy_til - buffer->start + 1;
|
|
new = malloc((*len + 1) * sizeof(char));
|
|
if (!new)
|
|
{
|
|
free(*line);
|
|
line = NULL;
|
|
return (0);
|
|
}
|
|
if (*line)
|
|
ft_memcpy(new, *line, old_len);
|
|
ft_memcpy(new + old_len, buffer->start, *len - old_len);
|
|
new[*len] = '\0';
|
|
buffer->start = *copy_til + 1;
|
|
if (buffer->start > buffer->end)
|
|
buffer->start = NULL;
|
|
free(*line);
|
|
*line = new;
|
|
return (1);
|
|
}
|
|
|
|
static char *ft_read_line(int fd, t_buffer *buffer, char **line)
|
|
{
|
|
char *copy_til;
|
|
size_t line_length;
|
|
ssize_t bytes_read;
|
|
|
|
line_length = 0;
|
|
while (!find_eol(buffer, ©_til))
|
|
{
|
|
if (!line_aggregation(buffer, ©_til, line, &line_length))
|
|
return (NULL);
|
|
bytes_read = read(fd, buffer->buffer, BUFFER_SIZE);
|
|
if (bytes_read == -1)
|
|
return (buffer->start = NULL, free(*line), NULL);
|
|
buffer->start = buffer->buffer;
|
|
buffer->end = buffer->start + bytes_read - 1;
|
|
if (buffer->end < buffer->start)
|
|
buffer->start = NULL;
|
|
if (bytes_read < BUFFER_SIZE)
|
|
break ;
|
|
}
|
|
find_eol(buffer, ©_til);
|
|
if (!line_aggregation(buffer, ©_til, line, &line_length))
|
|
return (NULL);
|
|
return (*line);
|
|
}
|
|
|
|
char *get_next_line(int fd)
|
|
{
|
|
static t_buffer buffer = {{0}, NULL, NULL};
|
|
char *line;
|
|
|
|
if (fd < 0 || fd >= FD_MAX || BUFFER_SIZE <= 0)
|
|
return (NULL);
|
|
line = NULL;
|
|
return (ft_read_line(fd, &buffer, &line));
|
|
}
|
|
/**
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
|
|
int main(void)
|
|
{
|
|
int fd;
|
|
char *line;
|
|
|
|
fd = open("../TestGNL/giant_line_nl.txt", O_RDONLY);
|
|
line = "";
|
|
while (line != NULL)
|
|
{
|
|
printf("%s", line);
|
|
line = get_next_line(fd);
|
|
}
|
|
line = get_next_line(fd);
|
|
printf("%s", line);
|
|
line = get_next_line(fd);
|
|
printf("%s", line);
|
|
close(fd);
|
|
return (0);
|
|
}**/
|