2023-04-11 11:49:02 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* ft_split_charset.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */
|
2023-04-11 12:49:41 -04:00
|
|
|
/* Updated: 2023/04/11 18:49:24 by erey-bet ### ########.fr */
|
2023-04-11 11:49:02 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-04-11 12:49:41 -04:00
|
|
|
int ft_strlen(char *s)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (s[i])
|
|
|
|
i++;
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:49:02 -04:00
|
|
|
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));
|
2023-04-11 12:49:41 -04:00
|
|
|
if (!(*strs)[(*i)])
|
2023-04-11 11:49:02 -04:00
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:49:41 -04:00
|
|
|
int get_strs(char *to_split, char *charset, char ***strs, int *i)
|
2023-04-11 11:49:02 -04:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
|
|
|
x = -1;
|
2023-04-11 12:49:41 -04:00
|
|
|
j = 0;
|
|
|
|
(*strs)[(*i)] = malloc(sizeof(char) * (ft_strlen(to_split) + 1));
|
|
|
|
if (!(*strs)[(*i)])
|
2023-04-11 11:49:02 -04:00
|
|
|
return (1);
|
|
|
|
while (to_split[++x])
|
|
|
|
{
|
|
|
|
y = -1;
|
|
|
|
while (charset[++y])
|
|
|
|
{
|
|
|
|
if (to_split[x] == charset[y])
|
|
|
|
{
|
|
|
|
y = 0;
|
|
|
|
x++;
|
2023-04-11 12:49:41 -04:00
|
|
|
if (ft_strlen((*strs)[(*i)]) > 0)
|
|
|
|
if (new_strs(strs, to_split, i, &j))
|
2023-04-11 11:49:02 -04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 12:49:41 -04:00
|
|
|
(*strs)[(*i)][j++] = to_split[x];
|
2023-04-11 11:49:02 -04:00
|
|
|
}
|
|
|
|
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;
|
2023-04-11 12:49:41 -04:00
|
|
|
if (get_strs(to_split, charset, &strs, &i))
|
2023-04-11 11:49:02 -04:00
|
|
|
{
|
|
|
|
i = -1;
|
|
|
|
while (strs[++i])
|
|
|
|
free(strs[i]);
|
|
|
|
free(strs);
|
|
|
|
}
|
2023-04-11 12:49:41 -04:00
|
|
|
strs[i + 1] = NULL;
|
2023-04-11 11:49:02 -04:00
|
|
|
return (strs);
|
|
|
|
}
|
2023-04-11 12:49:41 -04:00
|
|
|
|
|
|
|
/*int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char **strs;
|
|
|
|
if (argc == 3)
|
|
|
|
strs = ft_split_charset(argv[1], argv[2]);
|
|
|
|
int i = -1;
|
|
|
|
while (strs[++i])
|
|
|
|
printf("%s\n", strs[i]);
|
|
|
|
return (0);
|
|
|
|
}*/
|