This commit is contained in:
Camille Chauvet
2023-01-19 17:33:26 +01:00
commit fcbe35befd
87 changed files with 2831 additions and 0 deletions

39
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
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

36
extra/extra.h Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* extra.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:03:10 by cchauvet #+# #+# */
/* Updated: 2023/01/18 19:21:29 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(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);
#endif

41
extra/ft_contain_only.c Normal file
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
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
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
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
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]);
}

42
extra/ft_strfjoin.c Normal file
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
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);
}

33
extra/ft_strmerger.c Normal file
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
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(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);
}

View File

31
extra/ft_tabrealloc.c Normal file
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);
}

BIN
extra/ft_ultoa.o Normal file

Binary file not shown.

89
extra/ft_ultoa_base.c Normal file
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);
}