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> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */ /* 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 "get_next_line.h"
#include <stdlib.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; static int num_allocs = 0;
@ -33,110 +28,48 @@ static void *xmalloc(size_t size)
#define malloc(x) xmalloc(x) #define malloc(x) xmalloc(x)
*/ */
char *shorten_buffer(char *buffer) char *read_at_least_one_line(char *buffer, int fd)
{ {
char *outbuf; char *read_buffer;
char *addr_of_newline; int bytes_read;
size_t index_after_newline;
size_t outlen;
addr_of_newline = ft_strchr(buffer, '\n'); read_buffer = malloc((BUFFER_SIZE + 1 ) * sizeof(char));
if (addr_of_newline == NULL) if (read_buffer == 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;
return (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); free(read_buffer);
return (outbuf); return (buffer);
} }
char *cleanup(char **buffer, char *read_buffer) #include <stdio.h>
{
free(read_buffer);
read_buffer = *buffer;
if (ft_strlen(*buffer) == 0)
{
free(*buffer);
read_buffer = NULL;
}
*buffer = NULL;
return (read_buffer);
}
char *get_next_line(int fd) char *get_next_line(int fd)
{ {
static char *buffer = NULL; static char *buffer = NULL;
char *read_buffer; char *out;
int num_bytes_read; size_t line_length;
if (setup(&buffer, &read_buffer, fd) != 0) if (fd < 0)
return (NULL); return (NULL);
num_bytes_read = 1; buffer = read_at_least_one_line(buffer, fd);
while (num_bytes_read != 0) if (buffer == NULL)
{ return (NULL);
if (ft_strchr(buffer, '\n')) line_length = 0;
return (prepare_output_buffer(&buffer, read_buffer)); while (buffer[line_length] != '\0' || buffer[line_length] != '\n')
num_bytes_read = read(fd, read_buffer, BUFFER_SIZE); line_length++;
if (num_bytes_read < 0) out = ft_substr(buffer, 0, line_length + 1);
{ buffer = ft_substr(buffer, line_length, ft_strlen(buffer) - line_length);
free(read_buffer); return (out);
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));
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:23:21 by kcolin #+# #+# */ /* Created: 2024/10/23 20:23:21 by kcolin #+# #+# */
/* Updated: 2024/10/23 20:24:50 by kcolin ### ########.fr */ /* Updated: 2024/11/06 16:51:35 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,11 @@
# error BUFFER_SIZE must be at least 1 # error BUFFER_SIZE must be at least 1
# endif # endif
size_t ft_strlen(const char *s);
char *ft_strjoin(char *s1, char *s2);
char *ft_strchr(const char *s, int c);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *get_next_line(int fd); char *get_next_line(int fd);
#endif #endif

View file

@ -6,17 +6,61 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/01 12:31:58 by kcolin #+# #+# */ /* Created: 2024/11/01 12:31:58 by kcolin #+# #+# */
/* Updated: 2024/11/01 12:37:32 by kcolin ### ########.fr */ /* Updated: 2024/11/06 17:37:39 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
#include <stdlib.h> #include <stdlib.h>
size_t ft_strlen(const char *s)
{
size_t i;
if (s == NULL)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strjoin(char *s1, char *s2)
{
char *out;
size_t i;
size_t j;
if (s1 == NULL)
{
s1 = malloc(1);
s1[0] = '\0';
}
out = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (out == NULL)
return (NULL);
i = 0;
j = 0;
while (s1[i] != '\0')
{
out[i] = s1[i];
i++;
}
while (s2[j] != '\0')
{
out[i] = s2[j];
i++;
j++;
}
return (out);
}
char *ft_strchr(const char *s, int c) char *ft_strchr(const char *s, int c)
{ {
size_t i; size_t i;
if (s == NULL)
return (0);
i = 0; i = 0;
while (s[i] != '\0') while (s[i] != '\0')
{ {
@ -29,76 +73,22 @@ char *ft_strchr(const char *s, int c)
return (0); return (0);
} }
size_t ft_strlen(const char *s) char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (src[i] != 0)
{
if (i < size)
dst[i] = src[i];
i++;
}
if (size > 0)
{
if (size - 1 > i)
dst[i] = '\0';
else
dst[size - 1] = '\0';
}
return (i);
}
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t dst_len;
size_t src_len;
size_t i;
dst_len = 0;
while (dst[dst_len] != '\0')
dst_len++;
src_len = 0;
while (src[src_len] != '\0')
src_len++;
if (size <= dst_len)
return (size + src_len);
i = 0;
while (src[i] != '\0' && i < size - dst_len - 1)
{
dst[dst_len + i] = src[i];
i++;
}
dst[dst_len + i] = '\0';
return (dst_len + src_len);
}
char *ft_strjoin_and_free_originals(char const *s1, char const *s2)
{ {
char *out; char *out;
size_t len; size_t i;
len = ft_strlen(s1) + ft_strlen(s2) + 1; i = 0;
out = malloc(len); if (s[i] == '\0' || start > ft_strlen(s) || len == 0)
if (out == NULL)
{
free((void *)s1);
free((void *)s2);
return (NULL); return (NULL);
out = malloc((len + 1) * sizeof(char));
if (out == NULL)
return (NULL);
while (i < len)
{
out[i] = s[start + i];
i++;
} }
ft_strlcpy(out, s1, len); out[i] = '\0';
ft_strlcat(out, s2, len);
free((void *)s1);
return (out); return (out);
} }