Some things work

This commit is contained in:
Khaïs COLIN 2024-10-31 11:42:56 +01:00
parent 60514beb45
commit b6e406cc7d
2 changed files with 99 additions and 56 deletions

View file

@ -6,33 +6,14 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
/* Updated: 2024/10/24 12:34:49 by kcolin ### ########.fr */
/* Updated: 2024/10/26 10:27:01 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
static char *ft_realloc(char *ptr, size_t new_size, size_t old_size)
{
char *outptr;
size_t i;
outptr = malloc(new_size);
if (outptr != NULL)
{
i = 0;
while (i < old_size)
{
outptr[i] = ptr[i];
i++;
}
}
free(ptr);
return (outptr);
}
static char *ft_strchr(const char *s, int c)
char *ft_strchr(const char *s, int c)
{
int i;
@ -48,55 +29,117 @@ static char *ft_strchr(const char *s, int c)
return (0);
}
size_t ft_strlen(const char *s)
{
int 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(char const *s1, char const *s2)
{
char *out;
size_t len;
len = ft_strlen(s1) + ft_strlen(s2) + 1;
out = malloc(len);
if (out == NULL)
return (NULL);
ft_strlcpy(out, s1, len);
ft_strlcat(out, s2, len);
return (out);
}
char *get_next_line(int fd)
{
static char *buffer = NULL;
static size_t buf_size = BUFFER_SIZE;
static size_t length = 0;
int num_bytes_read;
char *newline_addr;
char *out;
long i;
static char *buffer = "";
char *read_buffer;
int num_bytes_read;
size_t length;
char *outbuf;
if (buffer == NULL)
{
buffer = malloc(BUFFER_SIZE * sizeof(char));
buf_size = BUFFER_SIZE;
length = 0;
}
if (buffer == NULL)
return (NULL);
read_buffer = malloc(BUFFER_SIZE * sizeof(char));
num_bytes_read = 1;
length = 0;
while (num_bytes_read != 0)
{
if (length == buf_size)
// DONE: if newline found,
if (ft_strchr(buffer, '\n'))
{
buffer = ft_realloc(buffer, sizeof(char) * buf_size * 1.5, buf_size);
if (buffer == NULL)
// DONE: copy string until newline to new buffer,
outbuf = malloc((ft_strchr(buffer, '\n') - buffer + 2) * sizeof(char));
if (outbuf == NULL)
return (NULL);
buf_size *= 1.5;
ft_strlcpy(outbuf, buffer, ft_strchr(buffer, '\n') - buffer + 2);
// TODO: shorten current buffer,
buffer += ft_strchr(buffer, '\n') - buffer + 1;
// DONE: and return.
return (outbuf);
}
num_bytes_read = read(fd, buffer + length, buf_size - length);
num_bytes_read = read(fd, read_buffer + length, BUFFER_SIZE - length - 1);
if (num_bytes_read < 0)
{
free(read_buffer);
free(buffer);
buffer = NULL;
return (NULL);
}
read_buffer[num_bytes_read] = '\0';
length += num_bytes_read;
newline_addr = ft_strchr(buffer, '\n');
if (newline_addr != NULL)
{
out = malloc((newline_addr - buffer + 1) * sizeof(char));
out[(newline_addr - buffer)] = '\0';
i = 0;
while (i < (newline_addr - buffer))
{
out[i] = buffer[i];
i++;
}
return (out);
}
// DONE: join with big buffer
// FIXME: correctly free buffer
buffer = ft_strjoin(buffer, read_buffer);
}
free(read_buffer);
return (buffer);
}