add: libftx

This commit is contained in:
Camille Chauvet 2023-04-26 11:02:44 +00:00
parent ad754bb579
commit 537283e940
87 changed files with 2922 additions and 3 deletions

View File

@ -1,21 +1,26 @@
SRCS = main.c
OBJS = ${SRCS:.c=.o}
CC = clang
LIBS = libftx/libftx.a
CFLAGS = -g -Wall -Wextra -Werror
NAME = cub3D
all: ${NAME}
${NAME}: ${OBJS}
make -C libftx all
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS}
clean:
make -C libftx clean
rm -f ${OBJS}
fclean:
rm -f ${OBJS} ${NAME}
fclean: clean
make -C libftx fclean
rm -f ${NAME}
re: fclean all
re: fclean
make all
.PHONY: all clean fclean re coffee bozo

35
libftx/Makefile Normal file
View File

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

39
libftx/extra/Makefile Normal file
View File

@ -0,0 +1,39 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/01/19 12:55:43 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = clang
SRCS = ft_contain_only.c ft_freer.c ft_is_in.c ft_random_generator.c ft_strchri.c ft_strcmp.c ft_strfjoin.c ft_strgen.c ft_strmerger.c ft_strndup.c ft_tabrealloc.c ft_ultoa_base.c ft_swap.c
OBJS = $(SRCS:.c=.o)
NAME = extra.a
CFLAGS = -Wall -Werror -Wextra -g
%.o: %.c extra.h
$(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

40
libftx/extra/extra.h Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* extra.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
/* Updated: 2023/01/19 17:35:09 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef EXTRA_H
# define EXTRA_H
# include <stdarg.h>
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include "../libft/libft.h"
char *ft_ultoa_base(unsigned long long n, char *base);
char *get_next_line(int fd);
size_t ft_random_generator(size_t start, size_t stop);
void ft_freer_tab_ultimate(size_t len, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
char *ft_strndup(const char *src, size_t n);
ssize_t ft_strchri(char *str, char c);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
int ft_strcmp(char *s1, char *s2);
void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b);
#endif

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_contain_only.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/12 16:28:20 by cchauvet #+# #+# */
/* Updated: 2023/01/12 16:31:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_contain_only(char *str, char c)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
if (str[i] != c)
return (0);
i++;
}
return (1);
}
int ft_contain_only_str(char *str, char *to_find)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
if (!ft_is_in(to_find, str[i]))
return (0);
i++;
}
return (1);
}

58
libftx/extra/ft_freer.c Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_freer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 15:01:40 by cchauvet #+# #+# */
/* Updated: 2023/01/09 17:57:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
void ft_freer_ultimate(size_t len, ...)
{
va_list va;
size_t i;
i = 0;
va_start(va, len);
while (i < len)
{
free(va_arg(va, char *));
i++;
}
va_end(va);
}
void ft_freer_tab(char **tab)
{
size_t i;
if (tab == NULL)
return ;
i = 0;
while (tab[i] != NULL)
{
free(tab[i]);
i++;
}
free(tab);
}
void ft_freer_tab_ultimate(size_t len, ...)
{
va_list va;
size_t i;
i = 0;
va_start(va, len);
while (i < len)
{
ft_freer_tab(va_arg(va, char **));
i++;
}
va_end(va);
}

27
libftx/extra/ft_is_in.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_in.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 20:29:53 by cchauvet #+# #+# */
/* Updated: 2023/01/08 12:15:08 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_is_in(char *str, char c)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] == c)
return (1);
i++;
}
return (0);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_random_generator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 18:43:27 by cchauvet #+# #+# */
/* Updated: 2023/01/09 20:09:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
size_t ft_random_generator(size_t start, size_t stop)
{
int fd;
char *line;
int sum;
size_t i;
fd = open("/dev/random", O_RDONLY);
if (fd == -1)
return (0);
line = ft_calloc(sizeof(char), 10001);
if (line == NULL)
return (0);
read(fd, line, 10000);
sum = 0;
i = 0;
while (i < ft_strlen(line))
{
sum += (unsigned int) line[i];
i++;
}
free(line);
return (sum % (stop - start));
}

27
libftx/extra/ft_strchri.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 19:22:58 by cchauvet #+# #+# */
/* Updated: 2023/01/10 18:30:35 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
ssize_t ft_strchri(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);
}

23
libftx/extra/ft_strcmp.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 19:20:47 by cchauvet #+# #+# */
/* Updated: 2023/01/05 13:20:20 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i] && s1[i] != '\0')
i++;
return (s1[i] - s2[i]);
}

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strfjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:06:04 by cchauvet #+# #+# */
/* Updated: 2023/01/10 18:29:59 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
char *ft_strfjoin(char *s1, char *s2)
{
ssize_t i;
ssize_t y;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
if (s1 != NULL)
{
i = -1;
while (s1[++i] != '\0')
out[i] = s1[i];
}
free(s1);
y = 0;
if (s2 != NULL)
{
y = -1;
while (s2[++y] != '\0')
out[i + y] = s2[y];
}
free(s2);
out[i + y] = '\0';
return (out);
}

31
libftx/extra/ft_strgen.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strgen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/08 12:32:52 by cchauvet #+# #+# */
/* Updated: 2023/01/09 18:04:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
char *ft_strgen(char c, size_t len)
{
char *out;
size_t i;
out = ft_calloc((len + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (i < len)
{
out[i] = c;
i++;
}
out[len] = '\0';
return (out);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmerger.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 15:09:15 by cchauvet #+# #+# */
/* Updated: 2023/01/04 15:24:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
char *ft_strmerger(size_t arg_len, ...)
{
va_list va;
char *out;
char *temp;
va_start(va, arg_len);
out = ft_strjoin(va_arg(va, char *), va_arg(va, char *));
while (arg_len > 2)
{
temp = ft_strjoin(out, va_arg(va, char *));
free(out);
if (temp == NULL)
return (NULL);
out = temp;
arg_len--;
}
return (out);
}

32
libftx/extra/ft_strndup.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2023/01/05 19:27:03 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
char *ft_strndup(const 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);
}

40
libftx/extra/ft_swap.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:43:25 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:43:26 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
void ft_swap(void *a, void *b)
{
void *c;
c = a;
a = b;
b = c;
}
void ft_swap_char(char *a, char *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
void ft_swap_int(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tabrealloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 18:58:48 by cchauvet #+# #+# */
/* Updated: 2023/01/10 18:30:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size)
{
char **new;
size_t i;
new = ft_calloc(new_size, sizeof(char *));
if (new == NULL)
return (NULL);
i = 0;
while (i < current_size)
{
new[i] = tab[i];
i++;
}
free(tab);
return (new);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2023/01/19 13:01:09 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.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);
}
char *ft_ultoa_base(unsigned long long n, char *base)
{
size_t base_size;
int str_size;
char *out;
size_t i;
if (base == NULL)
return (NULL);
base_size = ft_base_size(base);
if (base_size == 0)
return (NULL);
str_size = ft_str_size(n, base_size);
out = ft_calloc(str_size + 1, sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
if (n == 0)
out[0] = base[0];
while (n != 0)
{
out[str_size - 2 - i] = base[n % base_size];
n /= base_size;
i++;
}
out[str_size - 1] = '\0';
return (out);
}

39
libftx/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: 2023/01/05 19:22:40 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS = get_next_line.c
OBJS = ${SRCS:.c=.o}
NAME = get_next_line.a
CFLAGS = -Wall -Werror -Wextra -g
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 clean fclean re

104
libftx/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: 2023/01/05 13:07:10 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_strchri(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_strchri(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_strchri(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_strchri(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);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2023/01/10 18:26:56 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
# include "../libft/libft.h"
# include "../extra/extra.h"
char *get_next_line(int fd);
#endif

81
libftx/libft/Makefile Normal file
View File

@ -0,0 +1,81 @@
:# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/01/05 17:44:37 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = clang
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 \
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)
NAME = libft.a
CFLAGS = -Wall -Werror -Wextra -g
%.o: %.c libft.h
$(CC) $(CFLAGS) -c -o $@ $<
all: $(NAME)
$(NAME): $(OBJS)
ar -rc $(NAME) $(OBJS)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

39
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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: 2023/01/09 13:51:09 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);
}

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;
}

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;
}

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;
}
}

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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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);
}

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: 2023/02/17 16:23:01 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

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: 2023/02/17 16:15:07 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);
}

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);
}

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
libftx/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: 2023/01/06 19:33:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_cancel(void **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((void **)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
libftx/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
libftx/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);
}

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
libftx/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
libftx/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
libftx/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));
}

25
libftx/libft/ft_strlen.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 12:03:59 by cchauvet #+# #+# */
/* Updated: 2023/01/05 17:46:12 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t length;
if (s == NULL)
return (0);
length = 0;
while (s[length])
length++;
return (length);
}

33
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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
libftx/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));
}

56
libftx/libft/libft.h Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/02/17 16:22:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <unistd.h>
# include "./list.h"
void *ft_cancel(void **tab, size_t len);
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);
#endif

32
libftx/libft/list.h Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:43:53 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:43:54 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_H
# define LIST_H
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

80
libftx/libftx.h Normal file
View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libftx.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:28:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFTX_H
# define LIBFTX_H
# include <stdlib.h>
# include <unistd.h>
# include <stdarg.h>
# include "./libft/libft.h"
char *ft_ultoa_base(unsigned long long n, char *base);
int ft_printf(const char *format, ...);
int ft_eprintf(const char *format, ...);
int ft_dprintf(int fd, const char *format, ...);
int ft_dprintf(int fd, const char *format, ...);
char *get_next_line(int fd);
size_t ft_random_generator(size_t start, size_t stop);
void ft_freer_tab_ultimate(size_t len, ...);
void ft_freer_ultimate(size_t len, ...);
char *ft_strgen(char c, size_t len);
int ft_is_in(char *str, char c);
char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size);
int ft_contain_only_str(char *str, char *to_find);
int ft_contain_only(char *str, char c);
char *ft_strfjoin(char *s1, char *s2);
char *ft_strmerger(size_t arg_len, ...);
int ft_strcmp(char *s1, char *s2);
ssize_t ft_strchri(char *str, char c);
char *ft_strndup(const char *src, size_t n);
void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b);
/* void *ft_cancel(void **tab, size_t len); */
/* 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); */
#endif

39
libftx/printf/Makefile Normal file
View File

@ -0,0 +1,39 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2023/02/01 16:19:19 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
CC = clang
SRCS = ft_dprintarg.c ft_dprintflag.c ft_dprintl_base.c ft_dprintptr.c ft_dprintstrtab.c ft_dprintul_base.c ft_dprintul.c ft_dprintx.c ft_dprintX.c ft_isarg.c ft_isdigit.c ft_printf.c ft_putchar_fd.c ft_putstr_fd.c ft_skipflag.c ft_strlen.c ft_vdprintf.c ft_eprintf.c
OBJS = ${SRCS:.c=.o}
NAME = ft_printf.a
CFLAG = -Wall -Werror -Wextra -g
%.o: %.c
${CC} ${CFLAG} -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

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"));
}

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintarg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
/* Updated: 2023/02/17 16:29:56 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_p(fd, va_arg(args, int)));
if (arg == 'S')
return (ft_dprintstrtab(fd, va_arg(args, char **)));
if (arg == 's')
return (ft_putstr_fd_p(fd, va_arg(args, char *)));
if (arg == '%')
return (ft_putchar_fd_p(fd, '%'));
if (arg == 'p')
return (ft_dprintptr(fd, va_arg(args, void *)));
return (0);
}

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));
}

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: 2023/02/17 16:30:25 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_p(fd, '-');
}
else
nb = n;
i += ft_dprintul_base(fd, nb, base);
if (i < 1)
return (-1);
return (i);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
/* Updated: 2023/02/03 12:53:45 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_p(fd, "(nil)"));
address = (unsigned long) ptr;
i = 2;
ft_putstr_fd_p(fd, "0x");
i += ft_dprintul_base(fd, address, "0123456789abcdef");
return (i);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintstrtab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 17:26:55 by cchauvet #+# #+# */
/* Updated: 2023/02/17 16:30:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintstrtab(int fd, char **tab)
{
size_t index;
int i;
i = 0;
index = 0;
while (tab[index] != NULL)
{
i += ft_putstr_fd_p(fd, tab[index]) + 1;
ft_putchar_fd_p(fd, '\n');
index++;
}
return (i);
}

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"));
}

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: 2023/02/17 16:31:15 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_p(fd, base[n % base_size]);
}
else
ft_putchar_fd_p(fd, base[n]);
return (str_size - 1);
}

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"));
}

View File

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

29
libftx/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: 2023/01/05 18:26:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_isarg(int c)
{
char *flags;
if (c == '\0')
return (1);
flags = "cspdiuxX%S";
while (*flags)
{
if (*flags == c)
return (1);
flags++;
}
return (0);
}

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);
}

35
libftx/printf/ft_printf.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 22:01:28 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:27: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);
}
int ft_dprintf(int fd, const char *format, ...)
{
va_list va;
int i;
va_start(va, format);
i = ft_vdprintf(fd, format, va);
va_end(va);
return (i);
}

43
libftx/printf/ft_printf.h Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:27:51 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_dprintstrtab(int fd, char **tab);
int ft_printf(const char *format, ...);
int ft_dprintf(int fd, const char *format, ...);
int ft_eprintf(const char *format, ...);
int ft_vdprintf(int fd, const char *format, va_list va);
int ft_putchar_fd_p(int fd, char c);
int ft_putstr_fd_p(int fd, char *str);
#endif

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: 2023/02/17 16:28:59 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar_fd_p(int fd, char c)
{
write(fd, &c, 1);
return (1);
}

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: 2023/02/17 16:28:25 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putstr_fd_p(int fd, char *str)
{
int i;
if (str == NULL)
str = "(null)";
i = 0;
while (str[i] != '\0')
ft_putchar_fd_p(fd, str[i++]);
return (i);
}

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
libftx/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);
}

View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vdprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 14:34:28 by cchauvet #+# #+# */
/* Updated: 2023/02/17 16:31:29 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_p(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);
}