95 lines
2.0 KiB
C
95 lines
2.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* get_next_line_utils.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
|
|
/* Updated: 2023/01/05 13:06:37 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#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);
|
|
}
|
|
|
|
char *ft_strfjoin(char *s1, char *s2)
|
|
{
|
|
ssize_t i;
|
|
ssize_t j;
|
|
char *out;
|
|
|
|
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
|
|
if (out == NULL)
|
|
return (NULL);
|
|
i = 0;
|
|
if (s1 != NULL)
|
|
{
|
|
while (s1[i] != '\0')
|
|
{
|
|
out[i] = s1[i];
|
|
i++;
|
|
}
|
|
}
|
|
free(s1);
|
|
j = -1;
|
|
if (s2 != NULL)
|
|
{
|
|
while (s2[++j] != '\0')
|
|
out[i + j] = s2[j];
|
|
}
|
|
free(s2);
|
|
return (out);
|
|
}
|
|
|
|
char *ft_strndup(char *src, size_t n)
|
|
{
|
|
char *out;
|
|
size_t i;
|
|
|
|
if (src[0] == '\0')
|
|
return (NULL);
|
|
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);
|
|
}
|
|
|
|
ssize_t ft_strchri(char *str, char c)
|
|
{
|
|
size_t i;
|
|
|
|
if (str == NULL)
|
|
return (-1);
|
|
i = 0;
|
|
while (str[i] != c && str[i] != '\0')
|
|
i++;
|
|
if (str[i] == '\0')
|
|
return (-1);
|
|
return (i);
|
|
}
|