This commit is contained in:
Camille Chauvet 2022-12-22 17:57:19 +01:00
commit 3256ce67f7
70 changed files with 2335 additions and 0 deletions

32
Makefile Normal file
View File

@ -0,0 +1,32 @@
OBJ = ${SRC:.c=.o}
NAME = libftx.a
LIBS = libft/libft.a gnl/get_next_line.a printf/ft_printf.a
CC = clang
FLAG = -Wall -Wextra -Werror
all: ${NAME}
${NAME}: ${OBJ}
make -C libft
make -C gnl
make -C printf
ar -rcT $(NAME) $(LIBS)
clean:
make -C libft clean
make -C gnl clean
make -C printf clean
fclean: clean
rm -f ${NAME}
make -C libft fclean
make -C printf fclean
make -C gnl fclean
re: fclean all
.PHONY: all clean fclean re

39
gnl/Makefile Normal file
View File

@ -0,0 +1,39 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/12/14 15:49:18 by cchauvet #+# #+# #
# Updated: 2022/12/14 16:08:19 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS = get_next_line.c get_next_line_utils.c
OBJS = ${SRCS:.c=.o}
NAME = get_next_line.a
CFLAGS = -Wall -Werror -Wextra
CC = clang
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
all: $(NAME)
$(NAME): $(OBJS)
ar -rc $(NAME) $(OBJS)
clean:
rm -f $(OBJS) $(BOBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all bonus clean fclean re

104
gnl/get_next_line.c Normal file
View File

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:37:48 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_getstash(int fd)
{
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 1)
{
free(str);
return (NULL);
}
return (str);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
stash = NULL;
buf = NULL;
while (ft_strchr(stash, '\n') == -1)
{
buf = ft_getstash(fd);
if (buf == NULL)
return (stash);
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
}
return (stash);
}
char *ft_getreturn(char *str)
{
int i;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
i = ft_strlen(str);
return (ft_strndup(str, i));
}
char *ft_getextra(char *str)
{
int i;
int j;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
return (NULL);
j = ft_strlen(str + i);
return (ft_strndup(str + i, j));
}
char *get_next_line(int fd)
{
static char *stash = NULL;
char *buf1;
char *buf2;
buf2 = stash;
if (ft_strchr(stash, '\n') == -1)
{
buf1 = ft_getline(fd);
buf2 = ft_strfjoin(stash, buf1);
if (buf2 == NULL)
{
free(buf1);
return (NULL);
}
}
buf1 = ft_getreturn(buf2);
stash = ft_getextra(buf2);
free(buf2);
if (buf1 == NULL)
{
free(stash);
free(buf1);
return (NULL);
}
return (buf1);
}

28
gnl/get_next_line.h Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2022/11/14 17:28:39 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
ssize_t ft_strchr(char *str, char c);
char *get_next_line(int fd);
#endif

106
gnl/get_next_line_utils.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:39:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *ft_calloc(size_t nmemb, size_t size)
{
char *tab;
size_t i;
if (nmemb == 0 || size * nmemb / nmemb != size)
return (NULL);
tab = malloc(nmemb * size);
if (tab == NULL)
return (NULL);
i = 0;
while (i < size * nmemb)
{
tab[i] = 0;
i++;
}
return ((void *) tab);
}
size_t ft_strlen(char *str)
{
size_t i;
if (str == NULL)
return (0);
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_strfjoin(char *s1, char *s2)
{
ssize_t i;
ssize_t j;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
if (s1 != NULL)
{
while (s1[i] != '\0')
{
out[i] = s1[i];
i++;
}
}
free(s1);
j = -1;
if (s2 != NULL)
{
while (s2[++j] != '\0')
out[i + j] = s2[j];
}
free(s2);
return (out);
}
char *ft_strndup(char *src, size_t n)
{
char *out;
size_t i;
if (src[0] == '\0')
return (NULL);
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (src[i] != '\0' && i < n)
{
out[i] = src[i];
i++;
}
return (out);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}

87
libft/Makefile Normal file
View File

@ -0,0 +1,87 @@
:# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/10/28 19:51:46 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = gcc
SRCS = ft_isalpha.c \
ft_isdigit.c \
ft_isalnum.c \
ft_isascii.c \
ft_isprint.c \
ft_strlen.c \
ft_memset.c \
ft_bzero.c \
ft_memcpy.c \
ft_memmove.c \
ft_strlcpy.c \
ft_strlcat.c \
ft_toupper.c \
ft_tolower.c \
ft_strchr.c \
ft_strrchr.c \
ft_strncmp.c \
ft_memchr.c \
ft_memcmp.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_split.c \
ft_itoa.c \
ft_strmapi.c \
ft_striteri.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c
BSRCS = ft_lstnew.c \
ft_lstadd_front.c \
ft_lstsize.c \
ft_lstlast.c \
ft_lstadd_back.c \
ft_lstdelone.c \
ft_lstclear.c \
ft_lstiter.c \
ft_lstmap.c
OBJS = $(SRCS:.c=.o)
BOBJS = $(BSRCS:.c=.o)
NAME = libft.a
CFLAGS = -Wall -Werror -Wextra
%.o: %.c libft.h
$(CC) $(CFLAGS) -c -o $@ $<
all: $(NAME)
$(NAME): $(OBJS)
ar -rc $(NAME) $(OBJS)
bonus: $(OBJS) $(BOBJS)
ar -rc $(NAME) $(OBJS) $(BOBJS)
clean:
rm -f $(OBJS) $(BOBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all bonus clean fclean re

39
libft/ft_atoi.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
/* Updated: 2022/09/28 18:15:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
char *str;
int out;
int sign;
str = (char *) nptr;
out = 0;
while (*str == ' ' || *str == '\f' || *str == '\v' || *str == '\t'
|| *str == '\r' || *str == '\n')
str++;
sign = 1;
if (*str == '-' || *str == '+')
{
if (*str == '-')
sign = -1;
str++;
}
while (ft_isdigit(*str))
{
out = 10 * out + *str - 48;
str++;
}
return (out * sign);
}

18
libft/ft_bzero.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 09:40:13 by cchauvet #+# #+# */
/* Updated: 2022/09/27 09:41:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
ft_memset(s, 0, n);
}

26
libft/ft_calloc.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:09:01 by cchauvet #+# #+# */
/* Updated: 2022/10/28 17:31:12 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *tab;
if (nmemb != 0 && (size_t)(nmemb * size) / nmemb != size)
return (NULL);
tab = malloc(nmemb * size);
if (tab == NULL)
return (NULL);
ft_bzero(tab, nmemb * size);
return (tab);
}

22
libft/ft_isalnum.c Normal file
View File

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

20
libft/ft_isalpha.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 11:15:04 by cchauvet #+# #+# */
/* Updated: 2022/09/27 11:04:58 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if (('z' >= c && c >= 'a') || ('Z' >= c && c >= 'A'))
return (1);
return (0);
}

18
libft/ft_isascii.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 11:57:55 by cchauvet #+# #+# */
/* Updated: 2022/09/26 13:33:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
if (c >= 0 && 127 >= c)
return (1);
return (0);
}

20
libft/ft_isdigit.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 11:15:04 by cchauvet #+# #+# */
/* Updated: 2022/09/27 11:09:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
if ('9' >= c && c >= '0')
return (1);
return (0);
}

18
libft/ft_isprint.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 11:52:31 by cchauvet #+# #+# */
/* Updated: 2022/09/28 15:05:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (32 <= c && c < 127);
}

55
libft/ft_itoa.c Normal file
View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/09/29 18:07:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nb_digit(int n)
{
int out;
out = 0;
while (n)
{
n /= 10;
out++;
}
return (out);
}
char *ft_itoa(int n)
{
char *out;
unsigned int nb[2];
if (!n)
return (ft_strdup("0"));
nb[0] = ft_nb_digit(n);
if (n < 0)
{
nb[1] = -n;
nb[0]++;
}
else
nb[1] = n;
out = malloc(sizeof(char) * (nb[0] + 1));
if (out == NULL)
return (NULL);
out[nb[0]--] = 0;
if (n < 0)
out[0] = '-';
while (nb[1])
{
out[nb[0]--] = nb[1] % 10 + 48;
nb[1] /= 10;
}
return (out);
}

26
libft/ft_lstadd_back.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 23:58:34 by cchauvet #+# #+# */
/* Updated: 2022/10/26 18:06:00 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (lst == NULL || new == NULL)
return ;
last = ft_lstlast(*lst);
if (last == NULL)
*lst = new;
else
last->next = new;
}

21
libft/ft_lstadd_front.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 23:50:03 by cchauvet #+# #+# */
/* Updated: 2022/09/30 01:08:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (new == NULL)
return ;
new->next = *lst;
*lst = new;
}

27
libft/ft_lstclear.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:01:05 by cchauvet #+# #+# */
/* Updated: 2022/10/21 14:55:37 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *next;
if (lst == NULL || del == NULL)
return ;
while (*lst != NULL)
{
next = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = next;
}
}

25
libft/ft_lstdelone.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:02:55 by cchauvet #+# #+# */
/* Updated: 2022/10/05 21:01:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (del == NULL || lst == NULL)
return ;
if (lst != NULL)
{
if (lst->content != NULL)
del(lst->content);
free(lst);
}
}

24
libft/ft_lstiter.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:05:43 by cchauvet #+# #+# */
/* Updated: 2022/10/05 20:58:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (f == NULL)
return ;
while (lst != NULL)
{
f(lst->content);
lst = lst->next;
}
}

22
libft/ft_lstlast.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 23:55:52 by cchauvet #+# #+# */
/* Updated: 2022/09/30 01:20:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (lst == NULL)
return (NULL);
while (lst->next != NULL)
lst = lst->next;
return (lst);
}

39
libft/ft_lstmap.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */
/* 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);
}

25
libft/ft_lstnew.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 23:41:40 by cchauvet #+# #+# */
/* Updated: 2022/09/29 23:47:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *new;
new = malloc(sizeof(t_list));
if (new == NULL)
return (NULL);
new->next = NULL;
new->content = content;
return (new);
}

26
libft/ft_lstsize.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 01:18:08 by cchauvet #+# #+# */
/* Updated: 2022/09/30 01:18:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int count;
count = 0;
while (lst != NULL)
{
count++;
lst = lst->next;
}
return (count);
}

31
libft/ft_memchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 13:40:09 by cchauvet #+# #+# */
/* Updated: 2022/09/27 13:59:03 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *tab;
size_t i;
unsigned char tofind;
tab = (unsigned char *) s;
tofind = (unsigned char) c;
i = 0;
while (i < n)
{
if (tab[i] == tofind)
return (tab + i);
i++;
}
return (NULL);
}

29
libft/ft_memcmp.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 14:15:37 by cchauvet #+# #+# */
/* Updated: 2022/09/27 15:10:56 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *str1;
unsigned char *str2;
size_t i;
if (n == 0)
return (0);
str1 = (unsigned char *) s1;
str2 = (unsigned char *) s2;
i = 0;
while (str1[i] == str2[i] && i < n - 1)
i++;
return (str1[i] - str2[i]);
}

32
libft/ft_memcpy.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 09:42:11 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:50:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
char *tab1;
char *tab2;
size_t i;
if (dest == NULL && src == NULL)
return (dest);
tab1 = dest;
tab2 = (void *) src;
i = 0;
while (i < n)
{
tab1[i] = tab2[i];
i++;
}
return (tab1);
}

37
libft/ft_memmove.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 11:44:12 by cchauvet #+# #+# */
/* Updated: 2022/09/28 16:40:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
char *tab1;
char *tab2;
if (dest == NULL && src == NULL)
return (dest);
tab1 = dest;
tab2 = (void *) src;
if (tab1 < tab2)
ft_memcpy(dest, src, n);
else
{
i = n;
while (0 < i)
{
i--;
tab1[i] = tab2[i];
}
}
return (dest);
}

28
libft/ft_memset.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 09:29:51 by cchauvet #+# #+# */
/* Updated: 2022/09/27 09:48:23 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
char *tab;
size_t i;
tab = s;
i = 0;
while (i < n)
{
tab[i] = c;
i++;
}
return (s);
}

18
libft/ft_putchar_fd.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:24:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

21
libft/ft_putendl_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:43:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
if (s == NULL)
return ;
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

34
libft/ft_putnbr_fd.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:28:27 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:39:25 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
{
ft_putnbr_fd(-2, fd);
ft_putnbr_fd(147483648, fd);
}
else if (n < 0)
{
ft_putchar_fd('-', fd);
ft_putnbr_fd(-n, fd);
}
else if (n > 9)
{
ft_putnbr_fd(n / 10, fd);
ft_putnbr_fd(n % 10, fd);
}
else
ft_putchar_fd(n + 48, fd);
}

21
libft/ft_putstr_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:40:34 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
if (s == NULL)
return ;
while (*s)
write(fd, s++, 1);
}

92
libft/ft_split.c Normal file
View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2022/10/28 19:23:45 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;
if (s == NULL)
return (0);
len = 0;
while (*s != 0)
{
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)
{
size_t len;
char **tab;
if (s == NULL)
return (NULL);
len = ft_seglen(s, c);
tab = malloc((len + 1) * sizeof(char *));
if (tab == NULL)
return (NULL);
tab[len] = NULL;
if (ft_segsplitter(tab, len, s, c) == NULL)
return (NULL);
return (tab);
}

24
libft/ft_strchr.c Normal file
View File

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

31
libft/ft_strdup.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
/* Updated: 2022/10/04 23:24:11 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s)
{
char *out;
size_t i;
out = ft_calloc((ft_strlen(s) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (s[i])
{
out[i] = s[i];
i++;
}
out[i] = 0;
return (out);
}

27
libft/ft_striteri.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:21:01 by cchauvet #+# #+# */
/* Updated: 2022/10/04 15:01:00 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
size_t i;
if (s == NULL)
return ;
i = 0;
while (s[i])
{
f(i, s + i);
i++;
}
}

38
libft/ft_strjoin.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 11:20:27 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:30:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(const char *s1, const char *s2)
{
char *out;
ssize_t i;
ssize_t j;
size_t len_s1;
size_t len_s2;
if (s1 == NULL || s2 == NULL)
return (NULL);
len_s1 = ft_strlen(s1);
len_s2 = ft_strlen(s2);
out = malloc(sizeof(char) * (len_s1 + len_s2 + 1));
if (out == NULL)
return (NULL);
i = -1;
while (++i < (ssize_t) len_s1)
out[i] = s1[i];
j = -1;
while (++j < (ssize_t) len_s2)
out[j + i] = s2[j];
out[i + j] = 0;
return (out);
}

25
libft/ft_strlcat.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 17:02:13 by cchauvet #+# #+# */
/* Updated: 2022/09/28 17:11:29 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t len_dest;
if (size == 0)
return (ft_strlen(src));
len_dest = ft_strlen(dest);
if (len_dest >= size)
return (ft_strlen(src) + (size));
return (len_dest + ft_strlcpy(dest + len_dest, src, size - len_dest));
}

28
libft/ft_strlcpy.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:24:15 by cchauvet #+# #+# */
/* Updated: 2022/09/28 17:23:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (i + 1 < size && src[i])
{
dst[i] = src[i];
i++;
}
if (size)
dst[i] = 0;
return (ft_strlen(src));
}

22
libft/ft_strlen.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 12:03:59 by cchauvet #+# #+# */
/* Updated: 2022/09/27 09:02:11 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_strlen(const char *s)
{
size_t length;
length = 0;
while (s[length])
length++;
return (length);
}

33
libft/ft_strmapi.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:01:18 by cchauvet #+# #+# */
/* Updated: 2022/09/30 00:53:55 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *out;
size_t i;
if (s == NULL)
return (NULL);
out = malloc((ft_strlen(s) + 1) * sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (s[i])
{
out[i] = f(i, s[i]);
i++;
}
out[i] = 0;
return (out);
}

25
libft/ft_strncmp.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 08:30:26 by cchauvet #+# #+# */
/* Updated: 2022/09/28 17:29:56 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
if (n == 0)
return (0);
i = 0;
while (s1[i] == s2[i] && i < n - 1 && s1[i] && s2[i])
i++;
return ((unsigned char) s1[i] - (unsigned char) s2[i]);
}

35
libft/ft_strnstr.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 13:39:33 by cchauvet #+# #+# */
/* Updated: 2022/09/29 21:10:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
if (!*little || (little == big && ft_strlen(little) <= len))
return ((char *) big);
i = 0;
while (i < len && big[i])
{
j = 0;
while (big[i + j] == little[j] && i + j < len)
{
if (!little[j + 1])
return ((char *) big + i);
j++;
}
i++;
}
return (NULL);
}

24
libft/ft_strrchr.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */
/* Updated: 2022/10/05 20:50:49 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
size_t i;
i = ft_strlen(s) + 1;
while (i-- > 0)
if (s[i] == (char) c)
return ((char *) s + i);
return (NULL);
}

44
libft/ft_strtrim.c Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 11:33:53 by cchauvet #+# #+# */
/* Updated: 2022/09/29 18:03:20 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_is_in(const char c, const char *charset)
{
while (*charset)
{
if (c == *charset)
return (1);
charset++;
}
return (0);
}
char *ft_strtrim(const char *s1, const char *set)
{
size_t start;
size_t stop;
char *str;
if (s1 == NULL || set == NULL)
return (NULL);
if (!*s1 || !*s1)
return (ft_strdup(""));
start = 0;
while (ft_is_in(s1[start], set))
start++;
stop = ft_strlen(s1) - 1;
while (ft_is_in(s1[stop], set))
stop--;
str = ft_substr(s1, start, stop - start + 1);
return (str);
}

35
libft/ft_substr.c Normal file
View File

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

18
libft/ft_tolower.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 12:38:07 by cchauvet #+# #+# */
/* Updated: 2022/09/28 16:41:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
return (c - ('A' - 'a') * (c >= 'A' && 'Z' >= c));
}

18
libft/ft_toupper.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 12:38:07 by cchauvet #+# #+# */
/* Updated: 2022/09/28 15:08:58 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
return (c + ('A' - 'a') * (c >= 'a' && 'z' >= c));
}

70
libft/libft.h Normal file
View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2022/10/21 15:02:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# 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;
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif

73
libftx.h Normal file
View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libftx.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2022/12/14 16:05:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFTX_H
# define LIBFTX_H
# include <stdlib.h>
# include <unistd.h>
int ft_printf(const char *format, ...);
char *get_next_line(int fd);
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;
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *nouveau);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *nouveau);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif

36
printf/Makefile Normal file
View File

@ -0,0 +1,36 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/12/14 16:15:10 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS = ft_dprintul_base.c ft_isdigit.c ft_dprintul.c ft_dprintx.c ft_dprintflag.c ft_skipflag.c ft_vdprintf.c ft_dprintl_base.c ft_dprintX.c ft_dprintptr.c ft_strlen.c ft_putstr_fd.c ft_dprintarg.c ft_printf.c ft_putchar_fd.c ft_isarg.c
OBJS = ${SRCS:.c=.o}
NAME = ft_printf.a
CFLAG = -Wall -Werror -Wextra
%.o: %.c
${CC} ${CFLAGS} -c -o $@ $<
all: ${NAME}
${NAME}: ${OBJS}
ar -rc ${NAME} ${OBJS}
clean:
rm -f ${OBJS} ${BOBJS}
fclean: clean
rm -f ${NAME}
re: fclean all
.PHONY: all clean fclean re

18
printf/ft_dprintX.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintX.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 17:44:38 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:22:43 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprint_upperx(int fd, unsigned int n)
{
return (ft_dprintul_base(fd, n, "0123456789ABCDEF"));
}

34
printf/ft_dprintarg.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintarg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:54:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintarg(int fd, int arg, va_list args)
{
if (arg == 'i' || arg == 'd')
return (ft_dprintl_base(fd, va_arg(args, int), "0123456789"));
if (arg == 'X')
return (ft_dprint_upperx(fd, va_arg(args, unsigned int)));
if (arg == 'x')
return (ft_dprintx(fd, va_arg(args, unsigned int)));
if (arg == 'u')
return (ft_dprintul(fd, va_arg(args, unsigned int)));
if (arg == 'c')
return (ft_putchar_fd(fd, va_arg(args, int)));
if (arg == 's')
return (ft_putstr_fd(fd, va_arg(args, char *)));
if (arg == '%')
return (ft_putchar_fd(fd, '%'));
if (arg == 'p')
return (ft_dprintptr(fd, va_arg(args, void *)));
return (0);
}

23
printf/ft_dprintflag.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintflag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 16:30:39 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:59:55 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintflag(int fd, const char *flag, va_list va)
{
if (ft_skipflag(flag) == 0)
return (-1);
flag++;
while (ft_isdigit(flag[0]))
flag += 1;
return (ft_dprintarg(fd, flag[0], va));
}

34
printf/ft_dprintl_base.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintl_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
/* Updated: 2022/10/21 15:54:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintl_base(int fd, long long int n, char *base)
{
int i;
unsigned long nb;
if (base == NULL)
return (-1);
i = 0;
if (n < 0)
{
nb = -n;
i += ft_putchar_fd(fd, '-');
}
else
nb = n;
i += ft_dprintul_base(fd, nb, base);
if (i < 1)
return (-1);
return (i);
}

27
printf/ft_dprintptr.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
/* Updated: 2022/10/12 15:22:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintptr(int fd, void *ptr)
{
unsigned long address;
int i;
if (ptr == NULL)
return (ft_putstr_fd(fd, "(nil)"));
address = (unsigned long) ptr;
i = 2;
ft_putstr_fd(fd, "0x");
i += ft_dprintul_base(fd, address, "0123456789abcdef");
return (i);
}

18
printf/ft_dprintul.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 15:46:14 by cchauvet #+# #+# */
/* Updated: 2022/10/21 15:53:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintul(int fd, unsigned long long n)
{
return (ft_dprintul_base(fd, n, "0123456789"));
}

81
printf/ft_dprintul_base.c Normal file
View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintul_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/10/23 14:23:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t ft_str_size(unsigned long long n, size_t base_size)
{
size_t size;
size = 1;
if (n == 0)
return (2);
while (n != 0)
{
n = n / base_size;
size++;
}
return (size);
}
static int ft_isdup(char *str)
{
char c;
size_t i;
while (*str != 0)
{
c = *str;
i = 1;
while (str[i] != 0)
{
if (str[i] == c)
return (1);
i++;
}
str++;
}
return (0);
}
static size_t ft_base_size(char *base)
{
size_t len;
if (ft_isdup(base))
return (0);
len = ft_strlen(base);
if (len < 2)
return (0);
return (len);
}
int ft_dprintul_base(int fd, unsigned long long n, char *base)
{
size_t base_size;
int str_size;
if (base == NULL)
return (-1);
base_size = ft_base_size(base);
if (base_size == 0)
return (-1);
str_size = ft_str_size(n, base_size);
if (n > base_size - 1)
{
ft_dprintul_base(fd, n / base_size, base);
ft_putchar_fd(fd, base[n % base_size]);
}
else
ft_putchar_fd(fd, base[n]);
return (str_size - 1);
}

18
printf/ft_dprintx.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 17:44:38 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:04:56 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintx(int fd, unsigned int n)
{
return (ft_dprintul_base(fd, n, "0123456789abcdef"));
}

29
printf/ft_isarg.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isarg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 22:37:41 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:09:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_isarg(int c)
{
char *flags;
if (c == '\0')
return (1);
flags = "cspdiuxX%";
while (*flags)
{
if (*flags == c)
return (1);
flags++;
}
return (0);
}

20
printf/ft_isdigit.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 16:34:14 by cchauvet #+# #+# */
/* Updated: 2022/10/11 16:35:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_isdigit(int c)
{
if (c <= '9' && c >= '0')
return (1);
return (0);
}

24
printf/ft_printf.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 22:01:28 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:34:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *format, ...)
{
va_list va;
int i;
va_start(va, format);
i = ft_vdprintf(1, format, va);
va_end(va);
return (i);
}

40
printf/ft_printf.h Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2022/11/07 19:41:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <unistd.h>
# include <stdarg.h>
size_t ft_strlen(const char *s);
char *ft_strchr(const char *s, int c);
int ft_isdigit(int c);
int ft_skipflag(const char *str);
int ft_isarg(int c);
int ft_dprintptr(int fd, void *ptr);
int ft_dprintl_base(int fd, long long n, char *base);
int ft_dprintul_base(int fd, unsigned long long n, char *base);
int ft_dprintul(int fd, unsigned long long n);
int ft_dprintx(int fd, unsigned int n);
int ft_dprint_upperx(int fd, unsigned int n);
int ft_dprintflag(int fd, const char *flag, va_list va);
int ft_dprintarg(int fd, int c, va_list va);
int ft_printf(const char *format, ...);
int ft_vdprintf(int fd, const char *format, va_list va);
int ft_putchar_fd(int fd, char c);
int ft_putstr_fd(int fd, char *str);
#endif

19
printf/ft_putchar_fd.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
/* Updated: 2022/10/12 15:21:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar_fd(int fd, char c)
{
write(fd, &c, 1);
return (1);
}

25
printf/ft_putstr_fd.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:51:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putstr_fd(int fd, char *str)
{
int i;
if (str == NULL)
str = "(null)";
i = 0;
while (str[i] != '\0')
ft_putchar_fd(fd, str[i++]);
return (i);
}

30
printf/ft_skipflag.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_skipflag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 14:47:52 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:14:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_skipflag(const char *str)
{
size_t i;
i = 0;
if (str[i] != '%')
return (0);
i++;
if (str[i] == '\0')
return (-1);
while (ft_isdigit(str[i]))
i++;
if (ft_isarg(str[i]))
return (i + 1);
return (0);
}

25
printf/ft_strlen.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:39:05 by cchauvet #+# #+# */
/* Updated: 2022/10/11 00:01:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen(const char *s)
{
size_t i;
if (s == NULL)
return (0);
i = 0;
while (s[i])
i++;
return (i);
}

54
printf/ft_vdprintf.c Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vdprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 14:34:28 by cchauvet #+# #+# */
/* Updated: 2022/12/11 19:23:34 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_dprintseg(int fd, const char *str)
{
int i;
i = 0;
while (str[i] != '\0' && ft_skipflag(str + i) == 0)
{
ft_putchar_fd(fd, str[i]);
i++;
}
return (i);
}
int ft_vdprintf(int fd, const char *format, va_list va)
{
int i;
int upi;
if (format == NULL)
return (-1);
i = 0;
while (format[0] != 0)
{
if (ft_skipflag(format) == 0)
{
upi = ft_dprintseg(fd, format);
format += upi;
i += upi;
}
else
{
upi = ft_dprintflag(fd, format, va);
if (upi == -1)
return (-1);
format += ft_skipflag(format);
i += upi;
}
}
return (i);
}

BIN
printf/libftprintf.a Normal file

Binary file not shown.