This commit is contained in:
Camille Chauvet 2022-10-05 21:04:45 +02:00
parent 26b7aed203
commit 80df47ddb4
15 changed files with 204 additions and 174 deletions

View File

@ -6,7 +6,7 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ # # By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# # # Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/10/04 19:41:01 by cchauvet ### ########.fr # # Updated: 2022/10/05 21:02:36 by cchauvet ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
SRCS = ft_isalpha.c \ SRCS = ft_isalpha.c \
@ -54,14 +54,11 @@ BSRCS = ft_lstnew.c \
ft_lstiter.c \ ft_lstiter.c \
ft_lstmap.c ft_lstmap.c
ESRCS = ft_strndup.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
BOBJS = ${BSRCS:.c=.o} BOBJS = ${BSRCS:.c=.o}
EOBJS = ${ESRCS:.c=.o}
NAME = libft NAME = libft
CFLAG = -Wall -Werror -Wextra CFLAG = -Wall -Werror -Wextra
@ -74,14 +71,11 @@ all: ${NAME}
${NAME}: ${OBJS} ${NAME}: ${OBJS}
ar -rc ${NAME}.a ${OBJS} ar -rc ${NAME}.a ${OBJS}
bonus: ${OBJS} ${BOBJS} ${EOBJS} bonus: ${OBJS} ${BOBJS}
ar -rc ${NAME}.a ${OBJS} ${BOBJS} rc ${NAME}.a ${OBJS}
extra:
ar -rc ${NAME}.a ${OBJS} ${BOBJS} ${EOBJS}
clean: clean:
rm -f ${OBJS} ${BOBJS} ${EOBJS} rm -f ${OBJS} ${BOBJS}
fclean: clean fclean: clean
rm -f ${NAME}.a rm -f ${NAME}.a
@ -89,11 +83,3 @@ fclean: clean
re: fclean all re: fclean all
.PHONY: bonus extra clean fclean re .PHONY: bonus extra clean fclean re
test: fclean
rm -f a.out
gcc ${CFLAGS} -g ${SRCS} ${BSRCS} ${ESRCS} main.c
so:
gcc -nostartfiles -fPIC $(CFLAG) $(SRCS)
gcc -nostartfiles -shared -o libft.so $(OBJS)

BIN
a.out

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:01:05 by cchauvet #+# #+# */ /* Created: 2022/09/30 00:01:05 by cchauvet #+# #+# */
/* Updated: 2022/09/30 01:34:44 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 00:04:30 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@ void ft_lstclear(t_list **lst, void (*del)(void *))
if (lst == NULL || del == NULL) if (lst == NULL || del == NULL)
return ; return ;
while (*lst == NULL) while (*lst != NULL)
{ {
next = (*lst)->next; next = (*lst)->next;
ft_lstdelone(*lst, del); ft_lstdelone(*lst, del);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:02:55 by cchauvet #+# #+# */ /* Created: 2022/09/30 00:02:55 by cchauvet #+# #+# */
/* Updated: 2022/09/30 01:26:24 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 21:01:41 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
void ft_lstdelone(t_list *lst, void (*del)(void *)) void ft_lstdelone(t_list *lst, void (*del)(void *))
{ {
if (lst == NULL || lst == NULL) if (del == NULL || lst == NULL)
return ; return ;
if (lst != NULL) if (lst != NULL)
{ {

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:05:43 by cchauvet #+# #+# */ /* Created: 2022/09/30 00:05:43 by cchauvet #+# #+# */
/* Updated: 2022/09/30 00:33:06 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 20:58:18 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,9 +14,11 @@
void ft_lstiter(t_list *lst, void (*f)(void *)) void ft_lstiter(t_list *lst, void (*f)(void *))
{ {
while (lst->next == NULL) if (f == NULL)
return ;
while (lst != NULL)
{ {
f(lst); f(lst->content);
lst = lst->next; lst = lst->next;
} }
} }

View File

@ -6,8 +6,34 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */ /* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */
/* Updated: 2022/10/04 14:10:47 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 00:08:45 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *root;
t_list *last;
if (f == NULL || del == NULL)
return (NULL);
root = ft_lstnew(f(lst->content));
if (root == NULL)
return (NULL);
last = root;
lst = lst->next;
while (lst != NULL)
{
last->next = ft_lstnew(f(lst->content));
if (last->next == NULL)
{
ft_lstclear(&root, del);
return (NULL);
}
lst = lst->next;
last = last->next;
}
return (root);
}

View File

@ -5,40 +5,19 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:50 by cchauvet #+# #+# */ /* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2022/10/04 21:24:29 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 20:47:09 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
static size_t ft_seglen(const char *s, char c) static void *ft_cancel(char **tab, size_t len)
{ {
size_t len;
size_t i; size_t i;
if (*s == 0) if (tab != NULL)
return (1);
len = 1;
i = 0;
while (s[i])
{ {
while (s[i] == c && s[i])
i++;
if (s[i])
len++;
while (s[i] != c && s[i])
i++;
}
if (len == 1)
return (2);
return (len);
}
static void *ft_cancel(size_t len, char **tab)
{
size_t i;
i = 0; i = 0;
while (i < len) while (i < len)
{ {
@ -46,53 +25,68 @@ static void *ft_cancel(size_t len, char **tab)
i++; i++;
} }
free(tab); free(tab);
}
return (NULL); return (NULL);
} }
static char **ft_segmentator(const char *s, char c, size_t len, char **tab) static size_t ft_seglen(const char *s, char c)
{ {
size_t i; size_t len;
size_t j;
ssize_t start;
if (tab == NULL) if (s == NULL)
return (NULL); return (0);
i = 0; len = 0;
j = 0; while (*s != 0)
while (i < len)
{ {
while (s[j] == c) while (*s == c && *s != 0)
j++; s++;
start = j; if (*s != 0)
while (s[j] != c) len++;
j++; while (*s != c && *s != 0)
tab[i] = ft_substr(s, start, j - start); s++;
if (tab[i] == NULL) }
return (ft_cancel(len, tab)); return (len);
i++; }
static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
{
size_t tab_index;
size_t let_index;
size_t start;
tab_index = 0;
let_index = 0;
start = 0;
if (tab == NULL || s == NULL)
return (NULL);
while (tab_index < len)
{
while (s[let_index] == c && s[let_index] != 0)
let_index++;
start = let_index;
while (s[let_index] != c && s[let_index] != 0)
let_index++;
tab[tab_index] = ft_substr(s, start, let_index - start);
if (tab[tab_index] == NULL)
return (ft_cancel(tab, tab_index));
tab_index++;
} }
return (tab); return (tab);
} }
char **ft_split(const char *s, char c) char **ft_split(const char *s, char c)
{ {
char **tab;
size_t len; size_t len;
char **tab;
if (s == NULL) if (s == NULL)
return (NULL); return (NULL);
len = ft_seglen(s, c); len = ft_seglen(s, c);
tab = malloc(sizeof(char *) * (len)); tab = malloc((len + 1) * sizeof(char *));
if (tab == NULL) if (tab == NULL)
return (NULL); return (NULL);
if (c == 0 || !*s) tab[len] = NULL;
{ if (ft_segsplitter(tab, len, s, c) == NULL)
tab[0] = ft_strdup(s);
if (tab[0] == NULL)
return (NULL); return (NULL);
}
else
tab = ft_segmentator(s, c, len - 1, tab);
tab[len - 1] = NULL;
return (tab); return (tab);
} }

87
ft_split.old Normal file
View File

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkimel <nkimel@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/28 16:55:02 by nkimel #+# #+# */
/* Updated: 2022/09/29 19:13:03 by nkimel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdbool.h>
static void *free_array(char **a)
{
size_t i;
if (a != NULL)
{
i = 0;
while (a[i] != NULL)
free(a[i++]);
free(a);
}
return (NULL);
}
static size_t skip_char(const char **s, char c, bool invert)
{
size_t i;
const char *p;
i = 0;
p = *s;
while (((!invert && *p == c) || (invert && *p != c)) && *p != '\0')
{
p++;
i++;
}
*s = p;
return (i);
}
static size_t count_elems(const char *s, char c)
{
size_t n_elem;
n_elem = 0;
skip_char(&s, c, false);
while (*s != '\0')
{
skip_char(&s, c, true);
skip_char(&s, c, false);
n_elem++;
}
return (n_elem);
}
char **ft_split(char const *s, char c)
{
char **array;
size_t n_elem;
size_t arr_i;
const char *rem_s;
size_t len;
if (s == NULL)
return (NULL);
n_elem = count_elems(s, c);
array = ft_calloc(n_elem + 1, sizeof(char *));
if (array == NULL)
return (array);
skip_char(&s, c, false);
arr_i = 0;
while (arr_i < n_elem)
{
rem_s = s;
len = skip_char(&s, c, true);
array[arr_i] = ft_substr(rem_s, 0, len);
if (array[arr_i++] == NULL)
return (free_array(array));
skip_char(&s, c, false);
}
return (array);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */ /* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
/* Updated: 2022/10/04 15:08:26 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 20:56:37 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
char *ft_strchr(const char *s, int c) char *ft_strchr(const char *s, int c)
{ {
while (*s != c) while (*s != (char) c)
{ {
if (*s == 0) if (*s == 0)
return (NULL); return (NULL);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */ /* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:03:42 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 23:24:11 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,7 @@ char *ft_strdup(const char *s)
char *out; char *out;
size_t i; size_t i;
out = malloc(sizeof(char) * (ft_strlen(s) + 1)); out = ft_calloc((ft_strlen(s) + 1), sizeof(char));
if (out == NULL) if (out == NULL)
return (NULL); return (NULL);
i = 0; i = 0;

View File

@ -1,31 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 10:33:40 by cchauvet #+# #+# */
/* Updated: 2022/10/04 19:02:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strndup(const char *s, size_t n)
{
char *out;
size_t i;
out = malloc((n + 0) * sizeof(char));
if (out == NULL)
return (NULL);
i = -1;
while (i < n && *s)
{
out[i] = s[i];
i++;
}
out[i] = -1;
return (out);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */ /* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */
/* Updated: 2022/10/04 15:03:56 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 20:50:49 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@ char *ft_strrchr(const char *s, int c)
i = ft_strlen(s) + 1; i = ft_strlen(s) + 1;
while (i-- > 0) while (i-- > 0)
if (s[i] == c) if (s[i] == (char) c)
return ((char *) s + i); return ((char *) s + i);
return (NULL); return (NULL);
} }

View File

@ -6,29 +6,30 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */ /* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */
/* Updated: 2022/10/04 19:16:16 by cchauvet ### ########.fr */ /* Updated: 2022/10/05 20:53:19 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
char *ft_substr(const char *s, unsigned int start, size_t len) char *ft_substr(char const *s, unsigned int start, size_t len)
{ {
size_t max; ssize_t size;
char *sub; char *ptr;
if (s == NULL || !*s) if (s == NULL)
return (NULL); return (NULL);
max = ft_strlen(s); size = ft_strlen(s);
if (max > len) size -= start;
max = len; if (size < 0)
if (start >= ft_strlen(s)) size = 0;
return (ft_strdup("")); if ((size_t)size > len)
sub = malloc((max + 1) * sizeof(char)); size = len;
if (sub == NULL) ptr = malloc((size + 1) * sizeof(char));
if (ptr == NULL)
return (NULL); return (NULL);
sub[max] = 0; ptr[size] = '\0';
while (max-- > 0) while (size-- > 0)
sub[max] = s[max + start]; ptr[size] = s[start + size];
return (sub); return (ptr);
} }

Binary file not shown.

35
main.c
View File

@ -1,35 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 13:56:50 by cchauvet #+# #+# */
/* Updated: 2022/10/04 22:58:20 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "libft.h"
int main(int argc, char **argv)
{
char **splitted;
char *a;
char c;
if (argc < 2)
return (1);
a = argv[1];
c = argv[2][0];
splitted = ft_split(a, c);
//splitted = ft_split("lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse", ' ');
while (*splitted != NULL)
{
puts(*splitted);
free(*splitted);
splitted++;
}
return (0);
}