got big reimplement

i am le tired
This commit is contained in:
Khaïs COLIN 2024-11-06 17:57:34 +01:00
parent 7431326fa7
commit 607a121ced
3 changed files with 95 additions and 167 deletions

View file

@ -6,18 +6,13 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
/* Updated: 2024/11/01 17:02:27 by kcolin ### ########.fr */
/* Updated: 2024/11/06 17:33:37 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_and_free_originals(char const *s1, char const *s2);
/*
static int num_allocs = 0;
@ -33,110 +28,48 @@ static void *xmalloc(size_t size)
#define malloc(x) xmalloc(x)
*/
char *shorten_buffer(char *buffer)
char *read_at_least_one_line(char *buffer, int fd)
{
char *outbuf;
char *addr_of_newline;
size_t index_after_newline;
size_t outlen;
char *read_buffer;
int bytes_read;
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);
}
/*
** returns 1 on error, 0 on success
*/
int setup(char **buffer, char **read_buffer, int fd)
{
if (fd < 0)
return (1);
if (*buffer == NULL)
{
*buffer = malloc(1 * sizeof(char));
if (*buffer == NULL)
return (1);
*buffer[0] = '\0';
}
*read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (*read_buffer == NULL)
{
free(*buffer);
return (1);
}
return (0);
}
char *prepare_output_buffer(char **buffer, char *read_buffer)
{
char *outbuf;
outbuf = malloc((ft_strchr(*buffer, '\n') - *buffer + 2));
if (outbuf == NULL)
{
free(read_buffer);
free(*buffer);
*buffer = NULL;
read_buffer = malloc((BUFFER_SIZE + 1 ) * sizeof(char));
if (read_buffer == NULL)
return (NULL);
bytes_read = 1;
while (!ft_strchr(buffer, '\n') && bytes_read > 0)
{
bytes_read = read(fd, read_buffer, BUFFER_SIZE);
if (bytes_read < 0)
{
free(buffer);
free(read_buffer);
return (NULL);
}
read_buffer[bytes_read] = '\0';
buffer = ft_strjoin(buffer, read_buffer);
}
ft_strlcpy(outbuf, *buffer, ft_strchr(*buffer, '\n') - *buffer + 2);
*buffer = shorten_buffer(*buffer);
free(read_buffer);
return (outbuf);
return (buffer);
}
char *cleanup(char **buffer, char *read_buffer)
{
free(read_buffer);
read_buffer = *buffer;
if (ft_strlen(*buffer) == 0)
{
free(*buffer);
read_buffer = NULL;
}
*buffer = NULL;
return (read_buffer);
}
#include <stdio.h>
char *get_next_line(int fd)
{
static char *buffer = NULL;
char *read_buffer;
int num_bytes_read;
char *out;
size_t line_length;
if (setup(&buffer, &read_buffer, fd) != 0)
if (fd < 0)
return (NULL);
num_bytes_read = 1;
while (num_bytes_read != 0)
{
if (ft_strchr(buffer, '\n'))
return (prepare_output_buffer(&buffer, read_buffer));
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';
buffer = ft_strjoin_and_free_originals(buffer, read_buffer);
if (buffer == NULL)
return (NULL);
}
return (cleanup(&buffer, read_buffer));
buffer = read_at_least_one_line(buffer, fd);
if (buffer == NULL)
return (NULL);
line_length = 0;
while (buffer[line_length] != '\0' || buffer[line_length] != '\n')
line_length++;
out = ft_substr(buffer, 0, line_length + 1);
buffer = ft_substr(buffer, line_length, ft_strlen(buffer) - line_length);
return (out);
}