97 lines
2.7 KiB
C
Executable file
97 lines
2.7 KiB
C
Executable file
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* get_next_line_utils.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/10/17 16:43:37 by thrieg #+# #+# */
|
|
/* Updated: 2025/02/16 19:06:55 by thrieg ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "get_next_line.h"
|
|
#include "../libft.h"
|
|
|
|
//allocates and returns a new t_list node, setting next as NULL, allocates
|
|
//BUFFER_SIZE bytes for the buffer. returns NULL if any allocation fails
|
|
//index is set to BUFFER_SIZE and nb_chars to 0
|
|
t_gnl_list *ft_gnl_create_bufflist(int fd)
|
|
{
|
|
t_gnl_list *list;
|
|
|
|
list = malloc(sizeof(t_gnl_list));
|
|
if (!list)
|
|
return (NULL);
|
|
list->buffer = malloc(sizeof(char) * BUFFER_SIZE);
|
|
if (!list->buffer)
|
|
return (free(list), NULL);
|
|
list->fd = fd;
|
|
list->index = BUFFER_SIZE;
|
|
list->nb_chars = 0;
|
|
list->next = NULL;
|
|
return (list);
|
|
}
|
|
|
|
t_gnl_vector *ft_gnl_create_vector(size_t size)
|
|
{
|
|
t_gnl_vector *ret;
|
|
|
|
ret = malloc(sizeof(t_gnl_vector));
|
|
if (!ret)
|
|
return (NULL);
|
|
ret->buffer = malloc(sizeof(char) * size);
|
|
if (!ret->buffer)
|
|
{
|
|
free(ret);
|
|
return (NULL);
|
|
}
|
|
ret->index = 0;
|
|
ret->size = size;
|
|
ret->finished = 0;
|
|
return (ret);
|
|
}
|
|
|
|
//append size bytes at src to the vector dest
|
|
//returns NULL if the allocation fail or dest otherwise
|
|
t_gnl_vector *ft_gnl_vector_concat(
|
|
t_gnl_vector *dest,
|
|
void *src,
|
|
size_t size)
|
|
{
|
|
char *buff;
|
|
|
|
if (!dest || !src)
|
|
return (NULL);
|
|
if ((dest->index + size) >= dest->size)
|
|
{
|
|
buff = malloc(sizeof(char) * ((dest->size + size) * 2));
|
|
if (!buff)
|
|
return (NULL);
|
|
ft_memcpy(buff, dest->buffer, dest->index);
|
|
dest->size = (dest->size + size) * 2;
|
|
free(dest->buffer);
|
|
dest->buffer = buff;
|
|
}
|
|
ft_memcpy(dest->buffer + dest->index, src, size);
|
|
dest->index += size;
|
|
return (dest);
|
|
}
|
|
|
|
//duplicates the content of a vector into an appropriately sized string
|
|
//returns NULL if allocation fails or vec is NULL, or vec->buffer is NULL
|
|
char *ft_gnl_vtoc(t_gnl_vector *vec)
|
|
{
|
|
char *ret;
|
|
|
|
if (!vec)
|
|
return (NULL);
|
|
if (!vec->buffer)
|
|
return (NULL);
|
|
ret = malloc(sizeof(char) * (vec->index + 1));
|
|
if (!ret)
|
|
return (NULL);
|
|
ft_memcpy(ret, vec->buffer, vec->index);
|
|
ret[vec->index] = '\0';
|
|
return (ret);
|
|
}
|