2022-10-25 20:27:09 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* get_next_line_utils.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
|
2022-10-26 11:25:43 -04:00
|
|
|
/* Updated: 2022/10/26 16:33:23 by cchauvet ### ########.fr */
|
2022-10-25 20:27:09 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2022-10-25 18:44:33 -04:00
|
|
|
#include "get_next_line.h"
|
|
|
|
|
|
|
|
void *ft_calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
char *tab;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (nmemb == 0 || size * nmemb / nmemb != size)
|
|
|
|
return (NULL);
|
|
|
|
tab = malloc(nmemb * size);
|
|
|
|
if (tab == NULL)
|
|
|
|
return (NULL);
|
|
|
|
i = 0;
|
|
|
|
while (i < size * nmemb)
|
|
|
|
{
|
|
|
|
tab[i] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return ((void *) tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ft_realloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
char *tab;
|
|
|
|
char *in;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
in = ptr;
|
2022-10-25 20:27:09 -04:00
|
|
|
tab = ft_calloc(sizeof(char), size);
|
2022-10-25 18:44:33 -04:00
|
|
|
if (tab == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (ptr == NULL)
|
2022-10-26 11:25:43 -04:00
|
|
|
{
|
|
|
|
free(ptr);
|
2022-10-25 18:44:33 -04:00
|
|
|
return (tab);
|
2022-10-26 11:25:43 -04:00
|
|
|
}
|
2022-10-25 18:44:33 -04:00
|
|
|
i = 0;
|
|
|
|
while (in[i] != 0 && i < size)
|
|
|
|
{
|
|
|
|
tab[i] = in[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free(ptr);
|
|
|
|
return (tab);
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:25:43 -04:00
|
|
|
size_t ft_strlen(char *str)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return (0);
|
|
|
|
i = 0;
|
|
|
|
while (str[i] != '\0')
|
|
|
|
i++;
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ft_strfjoin(char *s1, char *s2)
|
2022-10-25 18:44:33 -04:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t j;
|
2022-10-26 11:25:43 -04:00
|
|
|
char *out;
|
2022-10-25 18:44:33 -04:00
|
|
|
|
2022-10-26 11:25:43 -04:00
|
|
|
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
|
|
|
|
if (out == NULL)
|
|
|
|
return (NULL);
|
2022-10-25 18:44:33 -04:00
|
|
|
i = 0;
|
2022-10-26 11:25:43 -04:00
|
|
|
if (s1 != NULL)
|
2022-10-25 18:44:33 -04:00
|
|
|
{
|
2022-10-26 11:25:43 -04:00
|
|
|
while (s1[i] != '\0')
|
2022-10-25 18:44:33 -04:00
|
|
|
{
|
2022-10-26 11:25:43 -04:00
|
|
|
out[i] = s1[i];
|
2022-10-25 18:44:33 -04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 11:25:43 -04:00
|
|
|
free(s1);
|
|
|
|
j = 0;
|
|
|
|
while (s2[j] != '\0')
|
|
|
|
{
|
|
|
|
out[i + j] = s2[j];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
free(s2);
|
|
|
|
return (out);
|
2022-10-25 18:44:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *ft_strndup(char *src, size_t n)
|
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
out = ft_calloc(n + 1, sizeof(char));
|
|
|
|
if (out == NULL)
|
|
|
|
return (NULL);
|
|
|
|
i = 0;
|
|
|
|
while (src[i] != '\0' && i < n)
|
|
|
|
{
|
|
|
|
out[i] = src[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (out);
|
|
|
|
}
|