Compare commits

..

No commits in common. "7ca0dfbc542795f8ecdb007f66a3193942a593e3" and "d4bc0e05a540943c02f69b3769d405c23dc6c1b6" have entirely different histories.

18 changed files with 243 additions and 172 deletions

View File

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

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/28 17:31:12 by cchauvet ### ########.fr */
/* Updated: 2022/10/04 14:56:44 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/10/28 17:31:44 by cchauvet ### ########.fr */
/* Updated: 2022/09/27 11:32:40 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/26 18:06:00 by cchauvet ### ########.fr */
/* Updated: 2022/10/04 14:06:22 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/10/21 14:55:37 by cchauvet ### ########.fr */
/* Updated: 2022/09/30 01:34:44 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/10/05 21:01:41 by cchauvet ### ########.fr */
/* Updated: 2022/09/30 01:26:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (del == NULL || lst == NULL)
if (lst == 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/10/05 20:58:18 by cchauvet ### ########.fr */
/* Updated: 2022/09/30 00:33:06 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,11 +14,9 @@
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (f == NULL)
return ;
while (lst != NULL)
while (lst->next == NULL)
{
f(lst->content);
f(lst);
lst = lst->next;
}
}

View File

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

31
ft_strndup.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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/05 20:50:49 by cchauvet ### ########.fr */
/* Updated: 2022/10/04 15:03:56 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] == (char) c)
if (s[i] == c)
return ((char *) s + i);
return (NULL);
}

15
ft_strteri.c Normal file
View File

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

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/21 15:02:44 by cchauvet ### ########.fr */
/* Updated: 2022/10/04 13:45:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,48 +15,50 @@
# 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 Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}