OUBLIE PAS D'ENLEVER LE MAIN DANS FT_SPLIT_CHARSET.C

This commit is contained in:
Etienne Rey-bethbeder 2023-04-11 18:49:41 +02:00
parent 1ef3fa115e
commit d06eb570fc

View File

@ -6,33 +6,42 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */ /* Created: 2023/04/11 14:50:26 by erey-bet #+# #+# */
/* Updated: 2023/04/11 17:48:13 by erey-bet ### ########.fr */ /* Updated: 2023/04/11 18:49:24 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
int ft_strlen(char *s)
{
int i = 0;
while (s[i])
i++;
return (i);
}
int new_strs(char ***strs, char *to_split, int *i, int *j) int new_strs(char ***strs, char *to_split, int *i, int *j)
{ {
(*i)++; (*i)++;
(*j) = 0; (*j) = 0;
(*strs)[(*i)] = malloc(sizeof(char) (*strs)[(*i)] = malloc(sizeof(char)
* (ft_strlen(to_split) + 1)); * (ft_strlen(to_split) + 1));
if ((*strs)[(*i)]) if (!(*strs)[(*i)])
return (1); return (1);
return (0); return (0);
} }
int get_strs(char *to_split, char *charset, char ***strs, int i) int get_strs(char *to_split, char *charset, char ***strs, int *i)
{ {
int j; int j;
int x; int x;
int y; int y;
x = -1; x = -1;
(*strs)[i] = malloc(sizeof(char) * (ft_strlen(to_split) + 1)); j = 0;
if (!(*strs)[i]) (*strs)[(*i)] = malloc(sizeof(char) * (ft_strlen(to_split) + 1));
if (!(*strs)[(*i)])
return (1); return (1);
while (to_split[++x]) while (to_split[++x])
{ {
@ -43,12 +52,12 @@ int get_strs(char *to_split, char *charset, char ***strs, int i)
{ {
y = 0; y = 0;
x++; x++;
if (ft_strlen((*strs)[i]) > 0) if (ft_strlen((*strs)[(*i)]) > 0)
if (new_strs(strs, to_split, &i, &j)) if (new_strs(strs, to_split, i, &j))
return (1); return (1);
} }
} }
(*strs)[i][j++] = to_split[x]; (*strs)[(*i)][j++] = to_split[x];
} }
return (0); return (0);
} }
@ -62,12 +71,24 @@ char **ft_split_charset(char *to_split, char *charset)
if (!strs) if (!strs)
return (NULL); return (NULL);
i = 0; i = 0;
if (get_strs(to_split, charset, &strs, i)) if (get_strs(to_split, charset, &strs, &i))
{ {
i = -1; i = -1;
while (strs[++i]) while (strs[++i])
free(strs[i]); free(strs[i]);
free(strs); free(strs);
} }
strs[i + 1] = NULL;
return (strs); return (strs);
} }
/*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);
}*/