74 lines
1.9 KiB
C
74 lines
1.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_split_charset.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */
|
|
/* Updated: 2023/04/11 17:48:13 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
int new_strs(char ***strs, char *to_split, int *i, int *j)
|
|
{
|
|
(*i)++;
|
|
(*j) = 0;
|
|
(*strs)[(*i)] = malloc(sizeof(char)
|
|
* (ft_strlen(to_split) + 1));
|
|
if ((*strs)[(*i)])
|
|
return (1);
|
|
return (0);
|
|
}
|
|
|
|
int get_strs(char *to_split, char *charset, char ***strs, int i)
|
|
{
|
|
int j;
|
|
int x;
|
|
int y;
|
|
|
|
x = -1;
|
|
(*strs)[i] = malloc(sizeof(char) * (ft_strlen(to_split) + 1));
|
|
if (!(*strs)[i])
|
|
return (1);
|
|
while (to_split[++x])
|
|
{
|
|
y = -1;
|
|
while (charset[++y])
|
|
{
|
|
if (to_split[x] == charset[y])
|
|
{
|
|
y = 0;
|
|
x++;
|
|
if (ft_strlen((*strs)[i]) > 0)
|
|
if (new_strs(strs, to_split, &i, &j))
|
|
return (1);
|
|
}
|
|
}
|
|
(*strs)[i][j++] = to_split[x];
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
char **ft_split_charset(char *to_split, char *charset)
|
|
{
|
|
char **strs;
|
|
int i;
|
|
|
|
strs = malloc(sizeof(char *) * (ft_strlen(to_split) + 1));
|
|
if (!strs)
|
|
return (NULL);
|
|
i = 0;
|
|
if (get_strs(to_split, charset, &strs, i))
|
|
{
|
|
i = -1;
|
|
while (strs[++i])
|
|
free(strs[i]);
|
|
free(strs);
|
|
}
|
|
return (strs);
|
|
}
|