Skip to content

Commit

Permalink
Added function to free the array strings in ft_split
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Yeow committed Apr 5, 2024
1 parent 6992714 commit f85edea
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ MEMSRCS = $(addprefix $(MEMDIR)$(FT_PRE), $(MEMSRC))
STRDIR = $(SRCDIR)str_utils/
STRSRC = \
atoi \
free_ft_split \
itoa \
split \
strchr \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ TODOs:
1. Add get_next_line
2. Add ft_printf
3. Error management for the functions.
4. Add a free function for ft_split.
3 changes: 2 additions & 1 deletion includes/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/15 16:11:56 by myeow #+# #+# */
/* Updated: 2024/03/31 19:11:17 by myeow ### ########.fr */
/* Updated: 2024/04/05 19:04:15 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,6 +45,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
void ft_free_ft_split(char **str_array);
char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f) (unsigned int, char));
void ft_striteri(char *s, void (*f) (unsigned int, char*));
Expand Down
23 changes: 23 additions & 0 deletions srcs/str_utils/ft_free_ft_split.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free_ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 18:59:36 by myeow #+# #+# */
/* Updated: 2024/04/05 19:02:20 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>

void ft_free_ft_split(char **str_array)
{
int i;

i = -1;
while (str_array[++i])
free(str_array[i]);
free(str_array);
}
60 changes: 48 additions & 12 deletions srcs/str_utils/ft_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: myeow <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 23:18:06 by myeow #+# #+# */
/* Updated: 2024/03/28 19:30:16 by myeow ### ########.fr */
/* Updated: 2024/04/05 20:08:21 by myeow ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,30 +43,66 @@ static char *ft_strdupsplit(char const *s, unsigned int start, unsigned int end)
return (str);
}

char **ft_split(char const *s, char c)
static int ft_split_fail_malloc(char **array, char *array_str)
{
if (array_str)
return (0);
else
{
ft_free_ft_split(array);
return (1);
}
}

static char **ft_split_append(char const *s, char c,
char **array, unsigned int i)
{
char **array;
unsigned int start;
unsigned int i;
size_t a;

if (!s)
return (0);
array = (char **)malloc(sizeof(char *) * (ft_countwords(s, c) + 1));
if (!array)
return (0);
i = 0;
while (s[i] == c && c)
++i;
start = i--;
a = -1;
while (s[++i])
{
if (s[i] != c && (s[i + 1] == c || !(s[i + 1])))
{
array[++a] = ft_strdupsplit(s, start, i);
if (ft_split_fail_malloc(array, array[a]))
return (0);
}
if (s[i] == c && s[i + 1] != c && s[i + 1])
start = i + 1;
}
array[++a] = 0;
return (array);
}

char **ft_split(char const *s, char c)
{
char **array;
unsigned int i;

if (!s)
return (0);
array = (char **)malloc(sizeof(char *) * (ft_countwords(s, c) + 1));
if (!array)
return (0);
i = 0;
while (s[i] == c && c)
++i;
return (ft_split_append(s, c, array, i));
}
/*
#include <stdio.h>
int main()
{
char *str = "q q q q qq ";
char **array_str = ft_split(str, ' ');
int i = -1;
while (array_str[++i])
printf("[%s]\n", array_str[i]);
ft_free_ft_split(array_str);
return (0);
}
*/

0 comments on commit f85edea

Please sign in to comment.