Skip to content

Commit

Permalink
add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tolmvad committed Oct 13, 2021
1 parent af9115b commit db8fdf7
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
104 changes: 104 additions & 0 deletions functions/ft_split.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bcaffere <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/01 19:37:15 by bcaffere #+# #+# */
/* Updated: 2021/10/08 19:18:15 by bcaffere ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/pipex.h"

static size_t words_count(char *s, char c)
{
size_t i;
size_t j;

i = 0;
j = 0;
while (*s)
{
if (*s != c)
i++;
else if (*s == c && i != 0)
{
j++;
i = 0;
}
s++;
}
if (i != 0)
j++;
return (j);
}

static char *word(char *s, char c)
{
char *buf;

while (*s == c)
s++;
buf = s;
while (*buf && *buf != c)
buf++;
*buf = '\0';
return (ft_strdup(s));
}

static char **free_arr(char **arr, char *s)
{
size_t i;

i = 0;
while (arr[i])
{
free(arr[i]);
i++;
}
free(arr);
free(s);
return (NULL);
}

static char **worker(char **arr, char *s1, char c, size_t j)
{
size_t i;
char *str;

str = s1;
i = 0;
while (i < j)
{
if (*s1 != c)
{
arr[i] = word(s1, c);
if (!arr[i])
return (free_arr(arr, s1));
s1 = s1 + ft_strlen(arr[i]);
i++;
}
s1++;
}
arr[i] = NULL;
free(str);
return (arr);
}

char **ft_split(char const *s, char c)
{
char **w_arr;
char *s1;
size_t j;

s1 = ft_strdup(s);
if (!s1)
return (NULL);
j = words_count(s1, c);
w_arr = (char **)malloc(sizeof(char *) * (j + 1));
if (!w_arr)
return (NULL);
return (worker(w_arr, s1, c, j));
}
31 changes: 31 additions & 0 deletions functions/ft_strdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bcaffere <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/24 22:06:30 by bcaffere #+# #+# */
/* Updated: 2021/10/08 19:24:56 by bcaffere ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/pipex.h"

char *ft_strdup(const char *src)
{
char *cp;
size_t i;

i = 0;
cp = (char *)malloc(ft_strlen(src) + 1);
if (!cp)
return (NULL);
while (src[i])
{
cp[i] = src[i];
i++;
}
cp[i] = '\0';
return (cp);
}
39 changes: 39 additions & 0 deletions functions/ft_strjoin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bcaffere <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/18 19:26:05 by bcaffere #+# #+# */
/* Updated: 2021/10/08 20:21:52 by bcaffere ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/pipex.h"

char *ft_strjoin(char const *s1, char const *s2)
{
char *j_str;
size_t i;
size_t j;

j_str = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!j_str)
return (NULL);
i = 0;
while (s1[i] != '\0')
{
j_str[i] = s1[i];
i++;
}
j = 0;
while (s2[j] != '\0')
{
j_str[i] = s2[j];
i++;
j++;
}
j_str[i] = '\0';
return (j_str);
}
26 changes: 26 additions & 0 deletions functions/ft_strncmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bcaffere <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/19 19:41:42 by bcaffere #+# #+# */
/* Updated: 2021/10/08 17:46:35 by bcaffere ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/pipex.h"

int ft_strncmp(const char *s1, const char *s2, size_t n)
{
while ((*s1 || *s2) && (n > 0))
{
if (*s1 != *s2)
return ((unsigned char)*s1 - (unsigned char)*s2);
s1++;
s2++;
n--;
}
return (0);
}

0 comments on commit db8fdf7

Please sign in to comment.