get_next_line/get_next_line.c
2024-11-01 12:35:30 +01:00

130 lines
3.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
/* Updated: 2024/11/01 12:34:14 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
char *ft_strchr(const char *s, int c);
size_t ft_strlen(const char *s);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
char *ft_strjoin_free_s1(char const *s1, char const *s2);
/*
static int num_allocs = 0;
static void *xmalloc(size_t size)
{
if (FAIL_AFTER > 0 && num_allocs++ >= FAIL_AFTER)
{
return 0;
}
return malloc(size);
}
#define malloc(x) xmalloc(x)
*/
char *shorten_buffer(char *buffer)
{
char *outbuf;
char *addr_of_newline;
size_t index_after_newline;
size_t outlen;
addr_of_newline = ft_strchr(buffer, '\n');
if (addr_of_newline == NULL)
{
buffer[0] = '\0';
return (buffer);
}
else
{
index_after_newline = addr_of_newline + 1 - buffer;
outlen = ft_strlen(buffer + index_after_newline) + 1;
outbuf = malloc(outlen * sizeof(char));
if (outbuf != NULL)
ft_strlcpy(outbuf, buffer + index_after_newline, outlen);
}
free(buffer);
return (outbuf);
}
char *get_next_line(int fd)
{
static char *buffer = NULL;
char *read_buffer;
int num_bytes_read;
char *outbuf;
if (buffer == NULL)
{
buffer = malloc(1 * sizeof(char));
if (buffer == NULL)
return (NULL);
buffer[0] = '\0';
}
read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (read_buffer == NULL)
{
free(buffer);
return (NULL);
}
num_bytes_read = 1;
while (num_bytes_read != 0)
{
// DONE: if newline found,
if (ft_strchr(buffer, '\n'))
{
// DONE: copy string until newline to new buffer,
outbuf = malloc((ft_strchr(buffer, '\n') - buffer + 2));
if (outbuf == NULL)
{
free(read_buffer);
free(buffer);
buffer = NULL;
return (NULL);
}
ft_strlcpy(outbuf, buffer, ft_strchr(buffer, '\n') - buffer + 2);
// DONE: shorten current buffer,
// buffer += ft_strchr(buffer, '\n') - buffer + 1;
buffer = shorten_buffer(buffer);
// DONE: and return.
free(read_buffer);
return (outbuf);
}
num_bytes_read = read(fd, read_buffer, BUFFER_SIZE);
if (num_bytes_read < 0)
{
free(read_buffer);
free(buffer);
buffer = NULL;
return (NULL);
}
read_buffer[num_bytes_read] = '\0';
// DONE: join with big buffer
buffer = ft_strjoin_free_s1(buffer, read_buffer);
if (buffer == NULL)
{
free(read_buffer);
return (NULL);
}
}
free(read_buffer);
outbuf = buffer;
if (ft_strlen(buffer) == 0)
{
free(buffer);
outbuf = NULL;
}
buffer = NULL;
return (outbuf);
}