piscine/c08/ex04/ft_strs_to_tab.c
Willem Haffmans 607ce08c18 all
2024-09-10 00:18:01 +02:00

86 lines
1.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strs_to_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 15:04:35 by whaffman #+# #+# */
/* Updated: 2024/06/22 14:43:23 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_stock_str.h"
int ft_strlen(char *str)
{
int i;
i = 0;
while (*str++)
i++;
return (i);
}
char *ft_strncpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
i = 0;
while (src[i] != '\0' && i < n)
{
dest[i] = src[i];
i++;
}
while (i < n)
{
dest[i] = '\0';
i++;
}
return (dest);
}
char *ft_strdup(char *src)
{
int str_size;
char *dest;
str_size = ft_strlen(src) + 1;
dest = (char *) malloc(str_size * sizeof(char));
if (dest)
ft_strncpy(dest, src, str_size);
return (dest);
}
struct s_stock_str *ft_strs_to_tab(int ac, char **av)
{
int i;
t_stock_str *result;
result = (t_stock_str *) malloc ((ac + 1) * sizeof(t_stock_str));
if (!result)
return ((void *) 0);
i = 0;
while (i < ac)
{
result[i].size = ft_strlen(av[i]);
result[i].str = av[i];
result[i].copy = ft_strdup(av[i]);
i++;
}
result[i].str = 0;
return (result);
}
#ifdef DEBUG
int main(int argc, char **argv)
{
struct s_stock_str *a;
a = ft_strs_to_tab(argc, argv);
}
#endif