Compare commits

..

10 Commits

Author SHA1 Message Date
Camille Chauvet
7ca0dfbc54 kekw 2022-10-28 19:52:15 +02:00
Camille Chauvet
3626bdb901 kekw 2022-10-28 19:23:50 +02:00
Camille Chauvet
83d25dbefd kekw 2022-10-28 18:29:59 +02:00
Camille Chauvet
816da7bb1c d 2022-10-28 17:18:11 +02:00
Camille Chauvet
1c6e823928 d 2022-10-28 17:14:04 +02:00
Camille Chauvet
9fd933c69f final 2022-10-21 15:32:43 +02:00
Camille Chauvet
57bd2453ee test 2022-10-12 19:00:31 +02:00
Camille Chauvet
93d2a3ce3b d 2022-10-05 21:32:52 +02:00
Camille Chauvet
80df47ddb4 d 2022-10-05 21:04:45 +02:00
Camille Chauvet
26b7aed203 final maybe 2022-10-04 23:01:49 +02:00
18 changed files with 174 additions and 245 deletions

View File

@ -1,4 +1,4 @@
# **************************************************************************** #
:# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
@ -6,9 +6,12 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/10/04 19:41:01 by cchauvet ### ########.fr #
# Updated: 2022/10/28 19:51:46 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = gcc
SRCS = ft_isalpha.c \
ft_isdigit.c \
ft_isalnum.c \
@ -54,46 +57,31 @@ BSRCS = ft_lstnew.c \
ft_lstiter.c \
ft_lstmap.c
ESRCS = ft_strndup.c
OBJS = $(SRCS:.c=.o)
OBJS = ${SRCS:.c=.o}
BOBJS = $(BSRCS:.c=.o)
BOBJS = ${BSRCS:.c=.o}
NAME = libft.a
EOBJS = ${ESRCS:.c=.o}
NAME = libft
CFLAG = -Wall -Werror -Wextra
CFLAGS = -Wall -Werror -Wextra
%.o: %.c libft.h
${CC} ${CFLAGS} -c -o $@ $<
$(CC) $(CFLAGS) -c -o $@ $<
all: ${NAME}
all: $(NAME)
${NAME}: ${OBJS}
ar -rc ${NAME}.a ${OBJS}
$(NAME): $(OBJS)
ar -rc $(NAME) $(OBJS)
bonus: ${OBJS} ${BOBJS} ${EOBJS}
ar -rc ${NAME}.a ${OBJS} ${BOBJS}
extra:
ar -rc ${NAME}.a ${OBJS} ${BOBJS} ${EOBJS}
bonus: $(OBJS) $(BOBJS)
ar -rc $(NAME) $(OBJS) $(BOBJS)
clean:
rm -f ${OBJS} ${BOBJS} ${EOBJS}
rm -f $(OBJS) $(BOBJS)
fclean: clean
rm -f ${NAME}.a
rm -f $(NAME)
re: fclean all
.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)
.PHONY: all bonus clean fclean re

BIN
a.out

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:09:01 by cchauvet #+# #+# */
/* Updated: 2022/10/04 14:56:44 by cchauvet ### ########.fr */
/* Updated: 2022/10/28 17:31:12 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 11:15:04 by cchauvet #+# #+# */
/* Updated: 2022/09/27 11:32:40 by cchauvet ### ########.fr */
/* Updated: 2022/10/28 17:31:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,9 @@
int ft_isalnum(int c)
{
if ((('9' >= c && c >= '0')
|| ('z' >= c && c >= 'a')
|| ('Z' >= c && c >= 'A')))
if (('9' >= c && c >= '0')
|| ('z' >= c && c >= 'a')
|| ('Z' >= c && c >= 'A'))
return (1);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 23:58:34 by cchauvet #+# #+# */
/* Updated: 2022/10/04 14:06:22 by cchauvet ### ########.fr */
/* Updated: 2022/10/26 18:06:00 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */

View File

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

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 *))
{
if (lst == NULL || lst == NULL)
if (del == NULL || lst == NULL)
return ;
if (lst != NULL)
{

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 *))
{
while (lst->next == NULL)
if (f == NULL)
return ;
while (lst != NULL)
{
f(lst);
f(lst->content);
lst = lst->next;
}
}

View File

@ -6,8 +6,34 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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,94 +5,88 @@
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:50 by cchauvet #+# #+# */
/* Updated: 2022/10/04 21:24:29 by cchauvet ### ########.fr */
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2022/10/28 19:23:45 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_seglen(const char *s, char c)
{
size_t len;
size_t i;
if (*s == 0)
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)
static void *ft_cancel(char **tab, size_t len)
{
size_t i;
i = 0;
while (i < len)
if (tab != NULL)
{
free(tab[i]);
i++;
i = 0;
while (i < len)
{
free(tab[i]);
i++;
}
free(tab);
}
free(tab);
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 j;
ssize_t start;
size_t len;
if (tab == NULL)
return (NULL);
i = 0;
j = 0;
while (i < len)
if (s == NULL)
return (0);
len = 0;
while (*s != 0)
{
while (s[j] == c)
j++;
start = j;
while (s[j] != c)
j++;
tab[i] = ft_substr(s, start, j - start);
if (tab[i] == NULL)
return (ft_cancel(len, tab));
i++;
while (*s == c && *s != 0)
s++;
if (*s != 0)
len++;
while (*s != c && *s != 0)
s++;
}
return (len);
}
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);
}
char **ft_split(const char *s, char c)
{
char **tab;
size_t len;
char **tab;
if (s == NULL)
return (NULL);
len = ft_seglen(s, c);
tab = malloc(sizeof(char *) * (len));
tab = malloc((len + 1) * sizeof(char *));
if (tab == NULL)
return (NULL);
if (c == 0 || !*s)
{
tab[0] = ft_strdup(s);
if (tab[0] == NULL)
return (NULL);
}
else
tab = ft_segmentator(s, c, len - 1, tab);
tab[len - 1] = NULL;
tab[len] = NULL;
if (ft_segsplitter(tab, len, s, c) == NULL)
return (NULL);
return (tab);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
while (*s != c)
while (*s != (char) c)
{
if (*s == 0)
return (NULL);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
size_t i;
out = malloc(sizeof(char) * (ft_strlen(s) + 1));
out = ft_calloc((ft_strlen(s) + 1), sizeof(char));
if (out == NULL)
return (NULL);
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 +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
while (i-- > 0)
if (s[i] == c)
if (s[i] == (char) c)
return ((char *) s + i);
return (NULL);
}

View File

@ -1,15 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:16:27 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:17:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *));

View File

@ -6,29 +6,30 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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;
char *sub;
ssize_t size;
char *ptr;
if (s == NULL || !*s)
if (s == NULL)
return (NULL);
max = ft_strlen(s);
if (max > len)
max = len;
if (start >= ft_strlen(s))
return (ft_strdup(""));
sub = malloc((max + 1) * sizeof(char));
if (sub == NULL)
size = ft_strlen(s);
size -= start;
if (size < 0)
size = 0;
if ((size_t)size > len)
size = len;
ptr = malloc((size + 1) * sizeof(char));
if (ptr == NULL)
return (NULL);
sub[max] = 0;
while (max-- > 0)
sub[max] = s[max + start];
return (sub);
ptr[size] = '\0';
while (size-- > 0)
ptr[size] = s[start + size];
return (ptr);
}

76
libft.h
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2022/10/04 13:45:02 by cchauvet ### ########.fr */
/* Updated: 2022/10/21 15:02:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,50 +15,48 @@
# include <stdlib.h>
# include <unistd.h>
int ft_atoi(const char *nptr);
void ft_bzero(void *s, size_t n);
void *ft_calloc(size_t nmemb, size_t size);
int ft_isalnum(int c);
int ft_isalpha(int c);
int ft_isascii(int c);
int ft_isdigit(int c);
int ft_isprint(int c);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void *ft_memset(void *s, int c, size_t n);
char *ft_strchr(const char *s, int c);
char *ft_strdup(const char *s);
size_t ft_strlcat(char *dst, const char *src, size_t size);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlen(const char *s);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
char *ft_strrchr(const char *s, int c);
int ft_tolower(int c);
int ft_toupper(int c);
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);
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*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
char *ft_strdup(const char *s);
char *ft_strndup(const char *s, size_t n);
void *ft_calloc(size_t nmemb, size_t size);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
int ft_toupper(int c);
int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memrchr(const void *s, int c, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
char *ft_substr(const char *s, unsigned int start, size_t len);
char *ft_strjoin(const char *s1, const char *s2);
char *ft_strtrim(const char *s1, const char *set);
char *ft_itoa(int n);
char **ft_split(const char *s, char c);
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int f);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);

34
main.c
View File

@ -1,34 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 13:56:50 by cchauvet #+# #+# */
/* Updated: 2022/10/04 21:20:44 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);
while (*splitted != NULL)
{
puts(*splitted);
free(*splitted);
splitted++;
}
return (0);
}