123 lines
2.4 KiB
C
123 lines
2.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_split.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/03/30 23:42:56 by tosuman #+# #+# */
|
|
/* Updated: 2023/03/30 23:42:57 by tosuman ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
|
|
{
|
|
char *orig_src;
|
|
|
|
orig_src = src;
|
|
while (size-- > 1 && *src)
|
|
*dest++ = *src++;
|
|
*dest = 0;
|
|
size = 0;
|
|
while (*orig_src++)
|
|
++size;
|
|
return (size);
|
|
}
|
|
|
|
char *ft_charstr(char *str, char to_find)
|
|
{
|
|
if (!to_find)
|
|
return (str);
|
|
while (*str)
|
|
{
|
|
if (*str == to_find)
|
|
return (str);
|
|
++str;
|
|
}
|
|
return (NULL);
|
|
}
|
|
|
|
int ft_strlen(char *str)
|
|
{
|
|
int size;
|
|
|
|
size = 0;
|
|
while (*str++)
|
|
++size;
|
|
return (size);
|
|
}
|
|
|
|
char **ft_split(char *str, char *charset)
|
|
{
|
|
int size;
|
|
int idx;
|
|
char **tab;
|
|
char *start;
|
|
|
|
size = ft_strlen(str);
|
|
tab = malloc(sizeof(char *) * size);
|
|
while (size--)
|
|
tab[size] = 0;
|
|
idx = 0;
|
|
while (*str)
|
|
{
|
|
start = str;
|
|
size = 0;
|
|
while (*str && ft_charstr(charset, *str++) == NULL)
|
|
++size;
|
|
if (start != --str)
|
|
{
|
|
tab[idx] = malloc(sizeof(char) * (size + 1));
|
|
ft_strlcpy(tab[idx], start, size + 1);
|
|
++idx;
|
|
}
|
|
++str;
|
|
}
|
|
return (tab);
|
|
}
|
|
|
|
/* ////
|
|
#include <unistd.h>
|
|
|
|
void ft_putstr(char *str)
|
|
{
|
|
write(1, "<", 1);
|
|
while (*str)
|
|
write(1, str++, 1);
|
|
write(1, ">", 1);
|
|
}
|
|
|
|
// strs must be a null terminated array
|
|
void ft_print_str_tab(char **strs, char *delim)
|
|
{
|
|
while (*strs)
|
|
{
|
|
ft_putstr(*strs++);
|
|
ft_putstr(delim);
|
|
}
|
|
}
|
|
|
|
void free_tab(char **tab)
|
|
{
|
|
int i;
|
|
|
|
i = -1;
|
|
while (tab[++i])
|
|
free(tab[i]);
|
|
free(tab);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
char **tab;
|
|
|
|
tab = ft_split(";0123:4567;89xx;1;1;1;1;1y2;", ":y;");
|
|
ft_print_str_tab(tab, "\n");
|
|
free_tab(tab);
|
|
return (0);
|
|
}
|
|
*/ ////
|