problemes affichage

This commit is contained in:
ReverseSky 2024-10-09 03:35:09 +02:00
parent bdd7e86f3c
commit 5fd959eeb8
54 changed files with 1849 additions and 63 deletions

30
Libft/Makefile Normal file
View file

@ -0,0 +1,30 @@
NAME = libft.a
SRC = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c ft_isascii.c \
ft_isdigit.c ft_isprint.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c \
ft_memset.c ft_strchr.c ft_strdup.c ft_strlcat.c ft_strlcpy.c ft_strlen.c \
ft_strncmp.c ft_strrchr.c ft_tolower.c ft_toupper.c ft_strnstr.c ft_substr.c\
ft_strjoin.c ft_strtrim.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_split.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 ft_hexa.c ft_printf.c\
ft_put.c get_next_line.c get_next_line_utils.c
OBJS = ${SRC:.c=.o}
CC = gcc
CFLAGS = -Wall -Werror -Wextra
all: $(NAME)
$(NAME): ${OBJS}
ar rc ${NAME} ${OBJS}
clean :
rm -f ${OBJS}
fclean : clean
rm -f $(NAME)
re : fclean all

34
Libft/ft_atoi.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 14:30:22 by grobledo #+# #+# */
/* Updated: 2023/02/04 14:30:24 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int i;
int is_neg;
int number;
i = 0;
is_neg = 1;
number = 0;
while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
is_neg *= -1;
i++;
}
while (str [i] >= '0' && str[i] <= '9')
number = (number * 10) + str[i++] - 48;
return (number * is_neg);
}

26
Libft/ft_bzero.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 14:32:10 by grobledo #+# #+# */
/* Updated: 2023/02/08 14:32:22 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
unsigned char *str;
size_t i;
i = 0;
str = s;
while (i < n)
{
str[i] = 0;
i++;
}
}

29
Libft/ft_calloc.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 19:35:13 by grobledo #+# #+# */
/* Updated: 2023/02/08 19:35:23 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *str;
size_t tmp;
tmp = size * count;
if (size == 0 || count == 0)
return (malloc(0));
if (tmp / count != size)
return (NULL);
str = malloc(size * count);
if (str == NULL)
return (NULL);
ft_bzero (str, size * count);
return (str);
}

64
Libft/ft_hexa.c Normal file
View file

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 09:38:27 by grobledo #+# #+# */
/* Updated: 2023/04/04 09:38:28 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_hexa(unsigned int args, char format, int count)
{
char *base;
char *shiftbase;
base = "0123456789abcdef";
shiftbase = "0123456789ABCDEF";
if (format == 'x')
{
if (args >= 16)
count = ft_hexa(args / 16, format, count);
count = ft_putcharf(base[args % 16], count);
return (count);
}
if (format == 'X')
{
if (args >= 16)
count = ft_hexa(args / 16, format, count);
count = ft_putcharf(shiftbase[args % 16], count);
return (count);
}
return (0);
}
int ft_starthexa(unsigned int args, char format, int count)
{
if (!args && (format == 'x' || format == 'X'))
return (ft_putcharf('0', count));
count = ft_hexa(args, format, count);
return (count);
}
int ft_startpointer(unsigned long long int args, char format, int count)
{
if (!args)
return (ft_putstrf("(nil)", count));
count = ft_putstrf("0x", count);
count = ft_pointer(args, format, count);
return (count);
}
int ft_pointer(unsigned long long int args, char format, int count)
{
char *base;
base = "0123456789abcdef";
if (args >= 16)
count = ft_pointer(args / 16, format, count);
count = ft_putcharf(base[args % 16], count);
return (count);
}

19
Libft/ft_isalnum.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 18:17:39 by grobledo #+# #+# */
/* Updated: 2023/02/02 18:17:41 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if (ft_isalpha(c) || ft_isdigit(c))
return (1);
return (0);
}

19
Libft/ft_isalpha.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 17:36:30 by grobledo #+# #+# */
/* Updated: 2023/02/02 17:36:34 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

19
Libft/ft_isascii.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 18:20:38 by grobledo #+# #+# */
/* Updated: 2023/02/02 18:20:40 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

19
Libft/ft_isdigit.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 17:48:20 by grobledo #+# #+# */
/* Updated: 2023/02/02 17:48:22 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

19
Libft/ft_isprint.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 18:27:24 by grobledo #+# #+# */
/* Updated: 2023/02/02 18:27:25 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

62
Libft/ft_itoa.c Normal file
View file

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/19 19:32:47 by grobledo #+# #+# */
/* Updated: 2023/02/19 19:32:48 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int countsize(int nb)
{
long int temp;
int size;
size = 0;
temp = nb;
if (nb < 0)
{
temp = -nb;
size = size +1;
}
while (temp > 9)
{
temp = (temp / 10);
size++;
}
size++;
return (size);
}
char *ft_itoa(int nb)
{
long int temp;
char *res;
int size;
if (nb == -2147483648)
return (ft_strdup("-2147483648"));
temp = nb;
size = countsize(nb);
res = ft_calloc(size + 1, sizeof (char));
if (!res)
return (NULL);
if (nb < 0)
{
res[0] = '-';
temp *= -1;
}
if (nb == 0)
res[0] = '0';
while (temp > 0)
{
res[size - 1] = (temp % 10 + 48);
size--;
temp /= 10;
}
return (res);
}

30
Libft/ft_lstadd_back.c Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 11:38:44 by grobledo #+# #+# */
/* Updated: 2023/02/23 11:38:46 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *temp;
if (!new)
return ;
if (!*lst)
{
*lst = new;
return ;
}
else
{
temp = ft_lstlast(*lst);
temp->next = new;
}
}

18
Libft/ft_lstadd_front.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 23:42:28 by grobledo #+# #+# */
/* Updated: 2023/02/21 23:42:30 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
new->next = *lst;
*lst = new;
}

30
Libft/ft_lstclear.c Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 22:05:11 by grobledo #+# #+# */
/* Updated: 2023/02/23 22:05:12 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *next;
t_list *current;
if (lst == NULL || del == NULL || *lst == NULL)
return ;
current = *lst;
while (current != NULL)
{
next = current->next;
del(current->content);
free(current);
current = next;
}
*lst = NULL;
}

22
Libft/ft_lstdelone.c Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 20:15:05 by grobledo #+# #+# */
/* Updated: 2023/02/23 20:15:06 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void*))
{
if (!lst)
return ;
if (!del)
return ;
del(lst->content);
free(lst);
}

27
Libft/ft_lstiter.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 22:42:44 by grobledo #+# #+# */
/* Updated: 2023/02/23 22:42:45 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
t_list *node;
node = lst;
if (lst == NULL || f == NULL)
return ;
node = lst;
while (node != NULL)
{
f(node->content);
node = node->next;
}
}

21
Libft/ft_lstlast.c Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 10:27:02 by grobledo #+# #+# */
/* Updated: 2023/02/23 10:27:07 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

41
Libft/ft_lstmap.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/24 11:59:12 by grobledo #+# #+# */
/* Updated: 2023/02/24 11:59:13 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(f)(void *), void (*del)(void *))
{
t_list *new;
t_list *current;
if (!lst || !f ||!del)
return (NULL);
new = ft_lstnew(NULL);
if (!new)
return (NULL);
current = new;
while (lst)
{
current->content = f(lst->content);
if (lst->next)
{
current->next = ft_lstnew(NULL);
if (!current)
{
ft_lstclear(&new, del);
return (NULL);
}
current = current ->next;
}
lst = lst->next;
}
return (new);
}

24
Libft/ft_lstnew.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 19:40:32 by grobledo #+# #+# */
/* Updated: 2023/02/21 22:58:42 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *new;
new = malloc(sizeof(t_list));
if (new == 0)
return (NULL);
new->content = content;
new->next = NULL;
return (new);
}

25
Libft/ft_lstsize.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 10:14:39 by grobledo #+# #+# */
/* Updated: 2023/02/23 10:14:41 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
i = 0;
while (lst)
{
lst = lst->next;
i++;
}
return (i);
}

26
Libft/ft_memchr.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 20:08:44 by grobledo #+# #+# */
/* Updated: 2023/02/08 20:08:45 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *str, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if ((((unsigned char *)str)[i]) == (unsigned char)c)
return ((void *)str + i);
i++;
}
return (NULL);
}

26
Libft/ft_memcmp.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 20:15:03 by grobledo #+# #+# */
/* Updated: 2023/02/08 20:15:05 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++;
}
return (0);
}

28
Libft/ft_memcpy.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 15:04:22 by grobledo #+# #+# */
/* Updated: 2023/02/08 15:04:24 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
int i;
if (!dest && !src)
return (NULL);
i = 0;
while (n != 0)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
n--;
}
return ((void *)dest);
}

39
Libft/ft_memmove.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/10 10:32:23 by grobledo #+# #+# */
/* Updated: 2023/02/10 10:32:25 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
if (!dest && !src)
return (NULL);
i = 0;
if (dest < src)
{
while (n--)
{
(((unsigned char *)dest)[i]) = (((unsigned char *)src)[i]);
i++;
}
}
else
{
i = i + n - 1;
while (n--)
{
(((unsigned char *)dest)[i]) = (((unsigned char *)src)[i]);
i--;
}
}
return ((void *)dest);
}

26
Libft/ft_memset.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 18:40:30 by grobledo #+# #+# */
/* Updated: 2023/02/02 18:40:33 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *str, int c, size_t n)
{
int i;
i = 0;
while (n != 0)
{
(((unsigned char *)str)[i]) = c;
i++;
n--;
}
return ((void *)str);
}

63
Libft/ft_printf.c Normal file
View file

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/13 10:48:34 by grobledo #+# #+# */
/* Updated: 2023/03/13 10:48:35 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_format(const char format, va_list args, int count)
{
if (format == 'c')
count = ft_putcharf((char)va_arg(args, int), count);
else if (format == 's')
count = ft_putstrf(va_arg(args, char *), count);
else if (format == 'd')
count = ft_putnbrf(va_arg(args, int), count);
else if (format == 'i')
count = ft_putnbrf(va_arg(args, int), count);
else if (format == 'u')
count = ft_putnbrposf(va_arg(args, unsigned int), count);
else if (format == 'x')
count = ft_starthexa (va_arg(args, unsigned int), 'x', count);
else if (format == 'X')
count = ft_starthexa (va_arg(args, unsigned int), 'X', count);
else if (format == 'p')
count = ft_startpointer
(va_arg(args, unsigned long long int), 'p', count);
else if (format == '%')
count = ft_putcharf('%', count);
return (count);
}
int ft_printf(const char *format, ...)
{
va_list args;
int i;
int count;
va_start(args, format);
i = 0;
count = 0;
while (format[i])
{
if (format[i] != '%')
{
write(1, &format[i], 1);
count++;
}
else
{
i++;
count = ft_format(format[i], args, count);
}
i++;
}
va_end(args);
return (count);
}

75
Libft/ft_put.c Normal file
View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/13 10:26:43 by grobledo #+# #+# */
/* Updated: 2023/04/13 10:26:45 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_putcharf(char c, int count)
{
write(1, &c, 1);
count++;
return (count);
}
int ft_putnbrf(int n, int count)
{
if (n == -2147483648)
{
(write(1, "-2147483648", 11));
count += 11;
return (count);
}
if (n < 0)
{
count = ft_putcharf('-', count);
n *= -1;
count = ft_putnbrf(n, count);
}
else if (n >= 10)
{
count = ft_putnbrf(n / 10, count);
count = ft_putnbrf(n % 10, count);
}
else
{
count = ft_putcharf(n + '0', count);
}
return (count);
}
int ft_putnbrposf(unsigned int n, int count)
{
if (n >= 10)
{
count = ft_putnbrposf(n / 10, count);
count = ft_putnbrposf(n % 10, count);
}
else
{
count = ft_putcharf(n + '0', count);
}
return (count);
}
int ft_putstrf(char *s, int count)
{
int i;
if (!s)
return (ft_putstrf("(null)", count));
i = 0;
while (s[i])
{
write(1, &s[i], 1);
i++;
count++;
}
return (count);
}

17
Libft/ft_putchar_fd.c Normal file
View file

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 23:21:04 by grobledo #+# #+# */
/* Updated: 2023/02/20 23:21:05 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

27
Libft/ft_putendl_fd.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 23:36:08 by grobledo #+# #+# */
/* Updated: 2023/02/20 23:36:09 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
int i;
if (!s)
return ;
i = 0;
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
ft_putchar_fd('\n', fd);
}

33
Libft/ft_putnbr_fd.c Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 23:43:01 by grobledo #+# #+# */
/* Updated: 2023/02/20 23:43:02 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
{
write(fd, "-2147483648", 11);
}
else if (n < 0)
{
ft_putchar_fd('-', fd);
n *= -1;
ft_putnbr_fd(n, fd);
}
else if (n >= 10)
{
ft_putnbr_fd(n / 10, fd);
ft_putnbr_fd(n % 10, fd);
}
else
ft_putchar_fd(n + '0', fd);
}

26
Libft/ft_putstr_fd.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 23:29:21 by grobledo #+# #+# */
/* Updated: 2023/02/20 23:29:26 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
int i;
if (!s)
return ;
i = 0;
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
}

78
Libft/ft_split.c Normal file
View file

@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 11:11:19 by grobledo #+# #+# */
/* Updated: 2023/02/21 18:03:11 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_countword(const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] != '\0')
j++;
while (s[i] != c && s[i])
i++;
}
return (j);
}
char *ft_strdup_modif(const char *s, char c, size_t i)
{
size_t k;
char *dest;
k = 0;
while (s[i + k] && s[i + k] != c)
k++;
dest = ft_calloc(sizeof(char), (k + 1));
k = 0;
if (dest == 0)
return (NULL);
while (s[i + k] && s[i + k] != c)
{
dest[k] = s[i + k];
k++;
}
return (dest);
}
char **ft_split(const char *s, char c)
{
size_t i;
size_t j;
char **res;
if (!s)
return (NULL);
res = ft_calloc(sizeof (char *), (ft_countword(s, c) + 1));
if (!res)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] == '\0')
break ;
res[j] = ft_strdup_modif(s, c, i);
j++;
while (s[i] != c && s[i])
i++;
}
return (res);
}

28
Libft/ft_strchr.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 12:59:29 by grobledo #+# #+# */
/* Updated: 2023/02/04 12:59:31 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *str, int tofind)
{
int i;
i = 0;
while (str[i])
{
if ((unsigned char)str[i] == (unsigned char)tofind)
return ((char *)&str[i]);
i++;
}
if (tofind == '\0')
return ((char *)&str[i]);
return (0);
}

25
Libft/ft_strdup.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 20:55:55 by grobledo #+# #+# */
/* Updated: 2023/02/13 21:30:44 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *src)
{
size_t size;
char *dest;
size = ft_strlen(src);
dest = (char *)malloc(size + 1);
if (dest == 0)
return (NULL);
ft_strlcpy(dest, src, size + 1);
return (dest);
}

26
Libft/ft_striteri.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 22:29:06 by grobledo #+# #+# */
/* Updated: 2023/02/20 22:29:07 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
size_t i;
if (!s || !(*f))
return ;
i = 0;
while (i < ft_strlen(s))
{
(*f)(i, &s[i]);
i++;
}
}

41
Libft/ft_strjoin.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 16:41:00 by grobledo #+# #+# */
/* Updated: 2023/02/14 16:41:02 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t j;
char *dest;
if (!s1 || !s2)
return (NULL);
dest = ft_calloc ((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof (char));
if (dest == 0)
return (NULL);
i = 0;
j = 0;
while (s1[i] || s2[j])
{
while (i < ft_strlen(s1))
{
dest[i] = s1[i];
i++;
}
while (j < ft_strlen(s2))
{
dest[i + j] = s2[j];
j++;
}
}
return (dest);
}

37
Libft/ft_strlcat.c Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 19:33:00 by grobledo #+# #+# */
/* Updated: 2023/02/02 19:33:01 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strlcat(char *dest, const char *src, size_t size)
{
unsigned int i;
unsigned int j;
unsigned int dlen;
unsigned int slen;
if ((dest == NULL || src == NULL) && size == 0)
return (0);
i = 0;
j = ft_strlen(dest);
dlen = j;
slen = ft_strlen(src);
if (size <= dlen)
return (slen + size);
while (src[i] != '\0' && i < size - dlen - 1)
{
dest[j] = src[i];
i++;
j++;
}
dest[j] = '\0';
return (dlen + slen);
}

28
Libft/ft_strlcpy.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 19:33:21 by grobledo #+# #+# */
/* Updated: 2023/02/02 19:33:24 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strlcpy(char *dest, const char *src, size_t size)
{
unsigned int i;
i = 0;
if (size == 0)
return (ft_strlen(src));
while (src[i] && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (ft_strlen(src));
}

24
Libft/ft_strlen.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 18:31:38 by grobledo #+# #+# */
/* Updated: 2023/02/02 18:31:39 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
{
i++;
}
return (i);
}

33
Libft/ft_strmapi.c Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 21:51:01 by grobledo #+# #+# */
/* Updated: 2023/02/20 21:51:02 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
size_t dlen;
char *dest;
if (!s || !(*f))
return (NULL);
i = 0;
dlen = ft_strlen(s);
dest = ft_calloc(dlen + 1, sizeof (char));
if (!dest)
return (NULL);
while (i < ft_strlen(s))
{
dest[i] = (*f)(i, s[i]);
i++;
}
return (dest);
}

26
Libft/ft_strncmp.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 14:33:04 by grobledo #+# #+# */
/* Updated: 2023/02/04 14:33:06 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t size)
{
size_t i;
i = 0;
while ((s1[i] || s2[i]) && (i < size))
{
if (s1[i] != s2[i])
return (((unsigned char)(s1[i]) - ((unsigned char)s2[i])));
i++;
}
return (0);
}

38
Libft/ft_strnstr.c Normal file
View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/13 22:07:12 by grobledo #+# #+# */
/* Updated: 2023/02/13 22:07:14 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
if ((big == NULL || little == NULL) && !len)
return (0);
i = 0;
j = 0;
if (little[i] == '\0')
return ((char *)big);
while (big[i] && i < len)
{
if (big[i] == little[j])
{
while (big[i + j] == little[j] && (i + j) < len && little[j])
j++;
if (little[j] == '\0')
return ((char *)&big[i]);
j = 0;
}
i++;
}
return (NULL);
}

28
Libft/ft_strrchr.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 14:00:32 by grobledo #+# #+# */
/* Updated: 2023/02/13 21:32:54 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *str, int tofind)
{
int i;
i = 0;
while (str[i])
i++;
while (i >= 0)
{
if ((unsigned char)str[i] == (unsigned char)tofind)
return ((char *)&str[i]);
i--;
}
return (0);
}

40
Libft/ft_strtrim.c Normal file
View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/18 18:34:05 by grobledo #+# #+# */
/* Updated: 2023/02/18 18:34:08 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(const char *s1, const char *set)
{
size_t i;
size_t j;
size_t k;
size_t dlen;
char *dest;
if (!s1)
return (NULL);
i = 0;
j = ft_strlen(s1);
k = -1;
while (ft_strchr(set, s1[i]) != 0 && s1[i])
i++;
if (!s1[i])
return (ft_strdup(""));
while (ft_strchr(set, s1[j]) != 0 && j > 0)
j--;
dlen = (j - i);
dest = ft_calloc ((dlen + 2), sizeof (char));
if (dest == 0)
return (NULL);
while (++k <= dlen)
dest[k] = s1[i + k];
return (dest);
}

39
Libft/ft_substr.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:52:48 by grobledo #+# #+# */
/* Updated: 2023/02/14 14:52:51 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(const char *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *dest;
if (!s)
return (NULL);
i = start;
j = 0;
if (start >= ft_strlen(s))
return (ft_calloc(1, 1));
if (ft_strlen(s + start) < len)
dest = (char *)ft_calloc((ft_strlen(s + start) + 1), sizeof(char));
else
dest = (char *)ft_calloc((len + 1), sizeof(char));
if (dest == 0)
return (NULL);
while (j < len && s[i])
{
dest[j] = s[i];
i++;
j++;
}
return (dest);
}

19
Libft/ft_tolower.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 20:49:29 by grobledo #+# #+# */
/* Updated: 2023/02/02 20:49:30 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 65 && c <= 90)
c += 32;
return (c);
}

19
Libft/ft_toupper.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 20:19:23 by grobledo #+# #+# */
/* Updated: 2023/02/02 20:19:25 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 97 && c <= 122)
c -= 32;
return (c);
}

110
Libft/get_next_line.c Normal file
View file

@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/05 20:41:22 by grobledo #+# #+# */
/* Updated: 2023/05/05 20:41:25 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_join( char *buffer, char *buff_read)
{
char *temp;
temp = ft_strjoing(buffer, buff_read);
free (buffer);
return (temp);
}
char *ft_nextline(char *buffer)
{
int i;
int j;
char *nextline;
i = 0;
while (buffer[i] && buffer[i] != '\n')
i++;
if (buffer[i] == '\n')
i++;
if (!buffer[i])
{
free (buffer);
return (NULL);
}
j = 0;
nextline = ft_calloc(ft_strleng(buffer) - i + 1, sizeof(char));
while (buffer[i])
{
nextline[j++] = buffer[i++];
}
free (buffer);
return (nextline);
}
char *ft_line(char *buffer)
{
char *line;
int i;
i = 0;
if (!buffer)
return (NULL);
while (buffer[i] && buffer[i] != '\n')
i++;
line = ft_calloc(i + 2, sizeof(char));
i = 0;
while (buffer[i] && buffer[i] != '\n')
{
line[i] = buffer[i];
i++;
}
if (buffer[i] && buffer[i] == '\n')
line[i] = '\n';
return (line);
}
char *ft_readfile(int fd, char *buffer)
{
int byte_read;
char *buff_read;
buff_read = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
byte_read = 1;
while (byte_read > 0)
{
byte_read = read(fd, buff_read, BUFFER_SIZE);
if (byte_read == 0)
break ;
if (byte_read == -1)
{
free(buff_read);
return (NULL);
}
buff_read[byte_read] = 0;
buffer = ft_join(buffer, buff_read);
if (ft_strrchr(buff_read, '\n'))
break ;
}
free (buff_read);
return (buffer);
}
char *get_next_line(int fd)
{
char *line;
static char *buffer = NULL;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
return (NULL);
buffer = ft_readfile(fd, buffer);
if (!buffer)
return (NULL);
line = ft_line(buffer);
buffer = ft_nextline(buffer);
return (line);
}

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/05 20:41:39 by grobledo #+# #+# */
/* Updated: 2023/05/05 20:41:41 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strleng(const char *str)
{
size_t i;
i = 0;
while (str && str[i])
{
i++;
}
return (i);
}
char *ft_strjoing(char const *s1, char const *s2)
{
size_t i;
size_t j;
char *dest;
if (!s2)
return (NULL);
dest = ft_calloc ((ft_strleng(s1) + ft_strleng(s2) + 1), sizeof (char));
if (dest == 0)
return (NULL);
i = 0;
j = 0;
while (s2[j])
{
while (i < ft_strleng(s1))
{
dest[i] = s1[i];
i++;
}
while (j < ft_strleng(s2))
{
dest[i + j] = s2[j];
j++;
}
}
return (dest);
}

89
Libft/libft.h Normal file
View file

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/10 12:14:34 by grobledo #+# #+# */
/* Updated: 2023/02/13 21:31:19 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <unistd.h>
# include <strings.h>
# include <stdlib.h>
# include <stdarg.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 10000
# endif
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_atoi(const char *str);
void ft_bzero(void *s, size_t n);
void *ft_calloc(size_t count, 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 *str, 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 *str, int c, size_t n);
char *ft_strchr(const char *str, int tofind);
char *ft_strdup(const char *src);
int ft_strlcat(char *dest, const char *src, size_t size);
int ft_strlcpy(char *dest, const char *src, size_t size);
size_t ft_strlen(const char *str);
int ft_strncmp(const char *s1, const char *s2, size_t size);
char *ft_strrchr(const char *str, int tofind);
int ft_tolower(int c);
int ft_toupper(int c);
char *ft_strnstr(const char *big, const char *little, size_t len);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(const char *s1, const char *set);
char *ft_itoa(int nb);
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);
char **ft_split(const char *s, char c);
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_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 *));
int ft_hexa(unsigned int args, char format, int count);
int ft_format(const char format, va_list args, int count);
int ft_printf(const char *format, ...);
int ft_putstrf(char *s, int count);
int ft_putcharf(char c, int count);
int ft_putnbrposf(unsigned int n, int count);
int ft_putnbrf(int n, int count);
int ft_starthexa(unsigned int args, char format, int count);
int ft_pointer(unsigned long long int args, char format, int count);
int ft_startpointer(unsigned long long int args, char format, int count);
char *get_next_line(int fd);
size_t ft_strleng(const char *str);
char *ft_strjoing(char const *s1, char const *s2);
#endif

View file

@ -6,7 +6,7 @@
# By: greg <greg@student.42.fr> +#+ +:+ +#+ # # By: greg <greg@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/07/29 13:08:42 by greg #+# #+# # # Created: 2024/07/29 13:08:42 by greg #+# #+# #
# Updated: 2024/10/03 17:00:43 by greg ### ########.fr # # Updated: 2024/10/09 03:07:50 by greg ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -24,15 +24,15 @@ CC = gcc -g
CFLAGS = -Wall -Werror -Wextra CFLAGS = -Wall -Werror -Wextra
obj : ${OBJS}
all: $(NAME) all: $(NAME)
obj : ${OBJS}
$(NAME): ${OBJS} $(NAME): ${OBJS}
make -C ${LIBFT} make -C ${LIBFT}
make -C ${MLX} make -C ${MLX}
gcc ${OBJS} Libft/libft.a Minilibx/libmlx.a -lX11 -lXext -o cub3d gcc ${OBJS} Libft/libft.a Minilibx/libmlx.a -lX11 -lXext -o cub3d -lm
clean : clean :
rm -f ${OBJS} rm -f ${OBJS}

1
Minilibx Submodule

@ -0,0 +1 @@
Subproject commit 7dc53a411a7d4ae286c60c6229bd1e395b0efb82

152
algo.c
View file

@ -6,11 +6,41 @@
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */ /* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/03 18:59:06 by greg ### ########.fr */ /* Updated: 2024/10/09 03:30:08 by greg ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "algo.h" #include "algo.h"
#include <math.h>
#include <stdio.h>
int worldMap[mapWidth][mapHeight]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
static int initalgo(t_ray *ray) static int initalgo(t_ray *ray)
@ -21,20 +51,60 @@ static int initalgo(t_ray *ray)
ray->dirY = 0; ray->dirY = 0;
ray->planeX = 0; ray->planeX = 0;
ray->planeY = 0.66; // FOV de 66 degres ray->planeY = 0.66; // FOV de 66 degres
ray->currtime = 0;
ray->oldtime = 0;
ray->movespeed = 0.1; ray->movespeed = 0.1;
ray->rotspeed = 0.1; ray->rotspeed = 0.1;
return (0); return (0);
} }
static int keypress(int keycode, t_ray *ray, int **worldMap)
{
//move forward if no wall in front of you
if (keycode == 119)
{
if(worldMap[(int)(ray->posX + ray->dirX * ray->movespeed)][(int)(ray->posY)] == 0)
ray->posX += ray->dirX * ray->movespeed;
if(worldMap[(int)(ray->posX)][((int)ray->posY + (int)ray->dirY * (int)ray->movespeed)] == 0)
ray->posY += ray->dirY * ray->movespeed;
}
//move backwards if no wall behind you
if (keycode == 115)
{
if(worldMap[(int)(ray->posX - ray->dirX * ray->movespeed)][(int)(ray->posY)] == 0)
ray->posX -= ray->dirX * ray->movespeed;
if(worldMap[(int)(ray->posX)][((int)ray->posY - (int)ray->dirY * (int)ray->movespeed)] == 0)
ray->posY -= ray->dirY * ray->movespeed;
}
//rotate to the right
if (keycode == 100)
{
//both camera direction and camera plane must be rotated
ray->oldDirX = ray->dirX;
ray->dirX = ray->dirX * cos(-ray->rotspeed) - ray->dirY * sin(-ray->rotspeed);
ray->dirY = ray->oldDirX * sin(-ray->rotspeed) + ray->dirY * cos(-ray->rotspeed);
ray->oldPlaneX = ray->planeX;
ray->planeX = ray->planeX * cos(-ray->rotspeed) - ray->planeY * sin(-ray->rotspeed);
ray->planeY = ray->oldPlaneX * sin(-ray->rotspeed) + ray->planeY * cos(-ray->rotspeed);
}
//rotate to the left
if (keycode == 97)
{
//both camera direction and camera plane must be rotated
ray->oldDirX = ray->dirX;
ray->dirX = ray->dirX * cos(ray->rotspeed) - ray->dirY * sin(ray->rotspeed);
ray->dirY = ray->oldDirX * sin(ray->rotspeed) + ray->dirY * cos(ray->rotspeed);
ray->oldPlaneX = ray->planeX;
ray->planeX = ray->planeX * cos(ray->rotspeed) - ray->planeY * sin(ray->rotspeed);
ray->planeY = ray->oldPlaneX * sin(ray->rotspeed) + ray->planeY * cos(ray->rotspeed);
}
return (0);
}
int algo() int main(int /*argc*/, char */*argv*/[])
{ {
t_ray ray; t_ray ray;
int x; int x;
double width; double width;
int height; double height;
// quelle box sur la map on est // quelle box sur la map on est
int mapX; int mapX;
int mapY; int mapY;
@ -43,12 +113,17 @@ int algo()
int side; int side;
initalgo(&ray); initalgo(&ray);
// init image;
x = 0; x = 0;
width = 640; width = 640;
height = 480; height = 480;
void *mlx_ptr;
void *win_ptr;
mlx_ptr = mlx_init(); // Initialise la connexion à la MLX
win_ptr = mlx_new_window(mlx_ptr, width, height, "cub3d");
while (x < width) while (x < width)
{ {
// position et direction du rayon // position et direction du rayon
@ -118,67 +193,28 @@ int algo()
if(drawStart < 0) if(drawStart < 0)
drawStart = 0; drawStart = 0;
int drawEnd; int drawEnd;
drawEnd = lineHeight / 2 + height / 2;
if(drawEnd >= height) if(drawEnd >= height)
drawEnd = height - 1; drawEnd = height - 1;
int y = drawStart;
/*mlx fontction color while (y <= drawEnd)
{
give x and y sides different brightness mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0xFF0000);
if (side == 1) {color = color / 2;} y++;
}
x++;
}
//draw the pixels of the stripe as a vertical line //draw the pixels of the stripe as a vertical line
verLine(x, drawStart, drawEnd, color); */ //verLine(x, drawStart, drawEnd, color); */
//timing for input to dissociate speed from speed processor (same speed on all pc)
ray.oldtime = ray.currtime;
ray.currtime = getTicks();
// put image to window // put image to window
// destroy old image // destroy old image
mlx_hook(win_ptr, 2, 1L<<0, keypress, &ray);
// hook event
readKeys();
//move forward if no wall in front of you
if (keyDown(move_up))
{
if(worldMap[(ray.posX + ray.dirX * ray.movespeed)][(int)(ray.posY)] == 0)
ray.posX += ray.dirX * ray.movespeed;
if(worldMap[(ray.posX)][((int)ray.posY + (int)ray.dirY * (int)ray.movespeed)] == 0)
ray.posY += ray.dirY * ray.movespeed;
}
//move backwards if no wall behind you
if (keyDown(moove_down))
{
if(worldMap[(ray.posX - ray.dirX * ray.movespeed)][(int)(ray.posY)] == 0)
ray.posX -= ray.dirX * ray.movespeed;
if(worldMap[(ray.posX)][((int)ray.posY - (int)ray.dirY * (int)ray.movespeed)] == 0)
ray.posY -= ray.dirY * ray.movespeed;
}
//rotate to the right
if (keyDown(moove_right))
{
//both camera direction and camera plane must be rotated
ray.oldDirX = ray.dirX;
ray.dirX = ray.dirX * cos(-ray.rotspeed) - ray.dirY * sin(-ray.rotspeed);
ray.dirY = ray.oldDirX * sin(-ray.rotspeed) + ray.dirY * cos(-ray.rotspeed);
ray.oldPlaneX = ray.planeX;
ray.planeX = ray.planeX * cos(-ray.rotspeed) - ray.planeY * sin(-ray.rotspeed);
ray.planeY = ray.oldPlaneX * sin(-ray.rotspeed) + ray.planeY * cos(-ray.rotspeed);
}
//rotate to the left
if (keyDown(moove_left))
{
//both camera direction and camera plane must be rotated
ray.oldDirX = ray.dirX;
ray.dirX = ray.dirX * cos(ray.rotspeed) - ray.dirY * sin(ray.rotspeed);
ray.dirY = ray.oldDirX * sin(ray.rotspeed) + ray.dirY * cos(ray.rotspeed);
ray.oldPlaneX = ray.planeX;
ray.planeX = ray.planeX * cos(ray.rotspeed) - ray.planeY * sin(ray.rotspeed);
ray.planeY = ray.oldPlaneX * sin(ray.rotspeed) + ray.planeY * cos(ray.rotspeed);
}
}
return (0); return (0);
} }

6
algo.h
View file

@ -6,7 +6,7 @@
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */ /* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/03 18:59:43 by greg ### ########.fr */ /* Updated: 2024/10/09 02:51:53 by greg ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,10 @@
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
#include "Minilibx/mlx.h"
#include "Minilibx/mlx_int.h"
#define mapWidth 24
#define mapHeight 24
typedef struct s_ray typedef struct s_ray
{ {