mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
adding file split strlen substr for the special parsing
This commit is contained in:
93
parsing/ft_split.c
Normal file
93
parsing/ft_split.c
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_split.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/11 15:34:04 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 14:55:21 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static size_t count_words(char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t len;
|
||||||
|
size_t tester;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
len = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
tester = 0;
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
tester++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (tester > 0)
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t len_word(size_t i, char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void free_tab(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
free(tab[i++]);
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
char **big_tab;
|
||||||
|
|
||||||
|
big_tab = malloc((count_words(s, c) + 1) * sizeof(char *));
|
||||||
|
if (!big_tab)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
if (s[i])
|
||||||
|
{
|
||||||
|
big_tab[j] = ft_substr(s, i, len_word(i, s, c));
|
||||||
|
if (!big_tab[j])
|
||||||
|
return (free_tab(big_tab), NULL);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
big_tab[j] = 0;
|
||||||
|
return (big_tab);
|
||||||
|
}
|
||||||
23
parsing/ft_strlen.c
Normal file
23
parsing/ft_strlen.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/06 13:25:17 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 11:24:39 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
size_t ft_strlen(const char *s)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
40
parsing/ft_substr.c
Normal file
40
parsing/ft_substr.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_substr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/11 13:46:41 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 13:42:37 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
size_t len_s;
|
||||||
|
size_t i;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return (0);
|
||||||
|
len_s = ft_strlen(s);
|
||||||
|
if (len_s < start)
|
||||||
|
len = 0;
|
||||||
|
if (len > len_s - start)
|
||||||
|
len = len_s - start;
|
||||||
|
tmp = malloc((len + 1) * sizeof(char));
|
||||||
|
if (!tmp)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
tmp[i] = s[start];
|
||||||
|
i++;
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
tmp[i] = '\0';
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
@@ -14,14 +14,21 @@
|
|||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static t_stack *parsing(int argc, char **argv)
|
static int wich_mod(int mod)
|
||||||
|
{
|
||||||
|
if (mod == 0 || mod == 3)
|
||||||
|
return (1);
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_stack *parsing(int argc, char **argv, int mod)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int stock;
|
int stock;
|
||||||
t_stack *first;
|
t_stack *first;
|
||||||
t_stack *new;
|
t_stack *new;
|
||||||
|
|
||||||
i = 1;
|
i = wich_mod(mod);
|
||||||
first = NULL;
|
first = NULL;
|
||||||
while (i < argc)
|
while (i < argc)
|
||||||
{
|
{
|
||||||
@@ -40,7 +47,14 @@ static t_stack *parsing(int argc, char **argv)
|
|||||||
return (first);
|
return (first);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_stacks *init_big_stacks(int argc, char **argv)
|
static t_stack *special_parsing(int argc, char **argv, int mod)
|
||||||
|
{
|
||||||
|
t_stacks *piles;
|
||||||
|
t_stack *first;
|
||||||
|
t_stack *new;
|
||||||
|
}
|
||||||
|
|
||||||
|
t_stacks *init_piles(int argc, char **argv, int mod)
|
||||||
{
|
{
|
||||||
t_stacks *stacks;
|
t_stacks *stacks;
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
@@ -50,7 +64,7 @@ t_stacks *init_big_stacks(int argc, char **argv)
|
|||||||
stacks->b = NULL;
|
stacks->b = NULL;
|
||||||
if (!stacks)
|
if (!stacks)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
a = parsing(argc, argv);
|
a = parsing(argc, argv, mod);
|
||||||
stacks->a = a;
|
stacks->a = a;
|
||||||
return (stacks);
|
return (stacks);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user