encore des segfault et travail sur les leaks de split en cas derreur
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/11/25 13:56:25 by hulamy #+# #+# */
|
/* Created: 2019/11/25 13:56:25 by hulamy #+# #+# */
|
||||||
/* Updated: 2019/11/28 15:04:39 by hulamy ### ########.fr */
|
/* Updated: 2019/12/10 23:53:40 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
@@ -1,78 +1,67 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_split.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2019/12/04 15:51:26 by hulamy #+# #+# */
|
|
||||||
/* Updated: 2019/12/09 21:27:55 by hulamy ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return an array of string with each word found in str, with c as separator
|
** return an array of string with each word found in str, with c as separator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
** #include <stdio.h>
|
|
||||||
** #include <stdlib.h>
|
|
||||||
** #include <unistd.h>
|
|
||||||
**
|
|
||||||
** size_t ft_strlen(const char *str)
|
|
||||||
** {
|
|
||||||
** size_t i;
|
|
||||||
**
|
|
||||||
** i = 0;
|
|
||||||
** while (str[i])
|
|
||||||
** i++;
|
|
||||||
** return (i);
|
|
||||||
** }
|
|
||||||
**
|
|
||||||
** char *ft_substr(char const *s, unsigned int start, size_t len)
|
|
||||||
** {
|
|
||||||
** char *str;
|
|
||||||
** size_t i;
|
|
||||||
**
|
|
||||||
** if (!s)
|
|
||||||
** return (NULL);
|
|
||||||
** if (ft_strlen(s) < start)
|
|
||||||
** return ("");
|
|
||||||
** if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
|
|
||||||
** return (NULL);
|
|
||||||
** i = 0;
|
|
||||||
** while (i < len && s[start])
|
|
||||||
** str[i++] = s[start++];
|
|
||||||
** str[i] = '\0';
|
|
||||||
** return (str);
|
|
||||||
** }
|
|
||||||
**
|
|
||||||
** char **ft_split(char const *s, char c);
|
|
||||||
** char **ft_strsplit(char const *s, char c);
|
|
||||||
**
|
|
||||||
** int main(void)
|
|
||||||
** {
|
|
||||||
** char **str;
|
|
||||||
** int i;
|
|
||||||
**
|
|
||||||
** char *s;
|
|
||||||
** char c;
|
|
||||||
**
|
|
||||||
** i = -1;
|
|
||||||
** s = NULL;
|
|
||||||
** c = 'c';
|
|
||||||
** str = ft_split(s, c);
|
|
||||||
** if (str)
|
|
||||||
** {
|
|
||||||
** printf("*str : '%p'\n", str[0]);
|
|
||||||
** while (str[++i])
|
|
||||||
** printf("str[%i] : '%s'\n", i, str[i]);
|
|
||||||
** }
|
|
||||||
** return (0);
|
|
||||||
** }
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "libft.h"
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
size_t ft_strlen(const char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return (NULL);
|
||||||
|
if (ft_strlen(s) < start)
|
||||||
|
return ("");
|
||||||
|
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (i < len && s[start])
|
||||||
|
str[i++] = s[start++];
|
||||||
|
str[i] = '\0';
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char c);
|
||||||
|
char **ft_strsplit(char const *s, char c);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char **str;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
char *s;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
s = "svh ile ifh oiuo";
|
||||||
|
c = ' ';
|
||||||
|
str = ft_split(s, c);
|
||||||
|
if (str)
|
||||||
|
{
|
||||||
|
printf("s : '%s'\n", s);
|
||||||
|
printf("*str : '%p'\n", str[0]);
|
||||||
|
while (str[++i])
|
||||||
|
printf("str[%i] : '%s'\n", i, str[i]);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//#include "libft.h"
|
||||||
|
|
||||||
static int count(char const *s, char c)
|
static int count(char const *s, char c)
|
||||||
{
|
{
|
||||||
@@ -81,17 +70,22 @@ static int count(char const *s, char c)
|
|||||||
|
|
||||||
i = -1;
|
i = -1;
|
||||||
words = 0;
|
words = 0;
|
||||||
while (s[++i])
|
while (s[++i] != '\0')
|
||||||
if (s[i] != c && ++words)
|
if (s[i] != c && ++words)
|
||||||
while (s[i + 1] && s[i + 1] != c)
|
while (s[i + 1] != '\0' && s[i + 1] != c)
|
||||||
i++;
|
i++;
|
||||||
return (words);
|
return (words);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ft_free(char **array)
|
void *ft_free(char **array, int w)
|
||||||
{
|
{
|
||||||
while (array)
|
int i;
|
||||||
free(array++);
|
|
||||||
|
i = 0;
|
||||||
|
while (1);
|
||||||
|
while (array[i] != NULL && i < w)
|
||||||
|
free(array[i++]);
|
||||||
|
free(array);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,19 +96,20 @@ char **ft_split(char const *s, char c)
|
|||||||
int len;
|
int len;
|
||||||
|
|
||||||
w = 0;
|
w = 0;
|
||||||
if (!s || !c || !(array = malloc(sizeof(char *) * (count(s, c) + 1))))
|
if (!s || !(array = (char **)malloc(sizeof(char *) * 1000 * (count(s, c) + 1))))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (*s)
|
while (*s != '\0')
|
||||||
{
|
{
|
||||||
len = 0;
|
len = 0;
|
||||||
if (*s != c)
|
if (*s != c)
|
||||||
{
|
{
|
||||||
while (s[len] && s[len] != c)
|
while (s[len] != '\0' && s[len] != c)
|
||||||
len++;
|
len++;
|
||||||
if (!(array[w++] = ft_substr(s, 0, len)))
|
if (w == 3 || !(array[w++] = ft_substr(s, 0, len)))
|
||||||
return (ft_free(array));
|
return (ft_free(array, w));
|
||||||
|
s += len - 1;
|
||||||
}
|
}
|
||||||
s += len ? len : 1;
|
s++;
|
||||||
}
|
}
|
||||||
array[w] = NULL;
|
array[w] = NULL;
|
||||||
return (array);
|
return (array);
|
||||||
|
|||||||
Reference in New Issue
Block a user