42_cub3d/read_all_text.c
2024-10-15 18:23:16 +02:00

95 lines
2.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_all_text.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 16:18:35 by mcolonna #+# #+# */
/* Updated: 2024/10/15 16:28:22 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "read_all_text.h"
#include "map.h"
#include "libft.h"
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
/// @brief Concatenate *dest and src and put the result in *dest.
///
/// *dest is alloc'd to contain the result value.
///
/// the old *dest is freed only if the function succeds.
///
/// src doesn't have to be nul-terminated, instead n is the size of src.
///
/// @return true if success, false if error.
static bool strconcat(char **dest, char *src, int n)
{
const int len_dest = ft_strlen(*dest);
char *old_dest;
int i;
old_dest = *dest;
*dest = malloc((len_dest + n + 1) * sizeof(char));
if (!*dest)
return (false);
i = -1;
while (old_dest[++i])
(*dest)[i] = old_dest[i];
while (i < len_dest + n)
{
(*dest)[i] = src[i - len_dest];
i++;
}
(*dest)[i] = '\0';
free(old_dest);
return (true);
}
/// @brief If the string isn't empty and isn't ended by a '\\n', add one and
/// return the result.
///
/// str is either returned directly, or another freeable pointer is returned
/// and str is freed.
///
/// @return Return the result or NULL if error.
static char *add_endline_if_necessary(char *str)
{
char *endline;
if (str[0] == '\0' || str[ft_strlen(str) - 1] == '\n')
return (str);
endline = malloc(1 * sizeof(char));
if (!endline)
return (free(str), NULL);
endline[0] = '\n';
if (!strconcat(&str, endline, 1))
return (free(str), NULL);
return (str);
}
char *read_all_text(int fd)
{
char buf[BUFFER_SIZE];
int n;
char *r;
r = malloc(sizeof(char));
if (!r)
return (NULL);
r[0] = '\0';
n = 1;
while (n)
{
n = read(fd, buf, BUFFER_SIZE);
if (n < 0 || (n && !strconcat(&r, buf, n)))
{
free(r);
return (NULL);
}
}
return (add_endline_if_necessary(r));
}