42_get_next_line/get_next_line_utils.c

107 lines
2.1 KiB
C
Raw Normal View History

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-11-14 09:45:44 -05:00
/* Updated: 2022/11/14 15:39:50 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);
}
2022-11-14 09:45:44 -05:00
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
{
2022-11-14 09:45:44 -05:00
ssize_t i;
ssize_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);
2022-11-14 09:45:44 -05:00
j = -1;
2022-10-28 07:41:06 -04:00
if (s2 != NULL)
2022-10-26 11:25:43 -04:00
{
2022-11-14 09:45:44 -05:00
while (s2[++j] != '\0')
2022-10-28 07:41:06 -04:00
out[i + j] = s2[j];
2022-10-26 11:25:43 -04:00
}
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;
2022-11-08 08:49:12 -05:00
if (src[0] == '\0')
return (NULL);
2022-10-25 18:44:33 -04:00
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);
}
2022-11-08 08:49:12 -05:00
ssize_t ft_strchr(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);
}