Libft and ftprintf added

This commit is contained in:
Angel Ortigosa Perez
2025-11-15 10:31:47 +01:00
parent 5c40a00540
commit a4c1881930
54 changed files with 1809 additions and 105 deletions

36
lib/libft/Makefile Normal file
View File

@@ -0,0 +1,36 @@
NAME = libft.a
SRC = ft_strlen.c ft_strlcpy.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strnstr.c ft_memset.c ft_memcpy.c ft_memmove.c ft_memchr.c ft_memcmp.c ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c ft_tolower.c ft_toupper.c ft_strncmp.c ft_atoi.c ft_bzero.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
SRC_B = ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c
OBJ = $(SRC:.c=.o)
OBJS_B = ${SRC_B:.c=.o}
CC = cc
CFLAGS = -Wall -Wextra -Werror
all: $(NAME)
$(NAME): $(OBJ)
ar rc $(NAME) $(OBJ)
ranlib $(NAME)
bonus: $(OBJ) $(OBJS_B)
ar rc $(NAME) $(OBJ) $(OBJS_B)
ranlib $(NAME)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJ) $(OBJS_B)
fclean: clean
rm -f $(NAME)
re: fclean bonus
.PHONY: bonus all clean fclean re

41
lib/libft/ft_atoi.c Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 20:46:43 by aortigos #+# #+# */
/* Updated: 2023/10/18 20:41:03 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int sign;
int result;
int i;
sign = 1;
result = 0;
i = 0;
while ((str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
|| (str[i] == '\v' || str[i] == '\r' || str[i] == '\f'))
i++;
if (str[i] == '-')
{
sign = -1;
}
if (str[i] == '-' || str[i] == '+')
{
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + (str[i] - '0');
i++;
}
return (sign * result);
}

18
lib/libft/ft_bzero.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/12 15:17:03 by aortigos #+# #+# */
/* Updated: 2023/10/12 15:22:29 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *ptr, size_t num)
{
ft_memset(ptr, 0, num);
}

26
lib/libft/ft_calloc.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 15:16:49 by aortigos #+# #+# */
/* Updated: 2023/09/24 15:18:05 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *ptr;
ptr = malloc(count * size);
if (!(ptr))
{
return (NULL);
}
ft_bzero(ptr, count * size);
return (ptr);
}

18
lib/libft/ft_isalnum.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 17:52:29 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:40:16 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isdigit(c) || ft_isalpha(c));
}

18
lib/libft/ft_isalpha.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/19 18:25:08 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:39:20 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

18
lib/libft/ft_isascii.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 17:55:43 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:39:27 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

18
lib/libft/ft_isdigit.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 17:47:47 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:39:55 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

18
lib/libft/ft_isprint.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 17:57:36 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:39:39 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (c >= 32 && c <= 126);
}

65
lib/libft/ft_itoa.c Normal file
View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 15:48:26 by aortigos #+# #+# */
/* Updated: 2023/10/22 16:08:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_array(char *x, unsigned int number, long int len)
{
while (number > 0)
{
x[len--] = 48 + (number % 10);
number = number / 10;
}
return (x);
}
static long int ft_len(int n)
{
int len;
len = 0;
if (n <= 0)
len = 1;
while (n != 0)
{
len++;
n = n / 10;
}
return (len);
}
char *ft_itoa(int n)
{
char *x;
long int len;
unsigned int number;
int sign;
sign = 1;
len = ft_len(n);
x = (char *)malloc(sizeof(char) * (len + 1));
if (!(x))
return (NULL);
x[len--] = '\0';
if (n == 0)
x[0] = '0';
if (n < 0)
{
sign *= -1;
number = n * -1;
x[0] = '-';
}
else
number = n;
x = ft_array(x, number, len);
return (x);
}

View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 20:27:06 by aortigos #+# #+# */
/* Updated: 2023/10/22 20:37:18 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *i;
if (lst)
{
if (*lst)
{
i = ft_lstlast(*lst);
i->next = new;
}
else
*lst = new;
}
}

View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 20:09:38 by aortigos #+# #+# */
/* Updated: 2023/10/22 20:14:37 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (!new || !lst)
return ;
new->next = *lst;
*lst = new;
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/23 16:28:57 by aortigos #+# #+# */
/* Updated: 2023/10/23 17:30:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *tmp;
if (!del || !lst || !*lst)
return ;
while (lst && *lst)
{
tmp = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = tmp;
}
}

View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 20:40:10 by aortigos #+# #+# */
/* Updated: 2023/10/23 16:25:03 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!del)
return ;
if (lst)
{
(*del)(lst->content);
free(lst);
}
}

View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/23 17:19:33 by aortigos #+# #+# */
/* Updated: 2023/10/23 17:21:49 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void*))
{
if (!f)
return ;
while (lst)
{
(*f)(lst->content);
lst = lst->next;
}
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 20:25:15 by aortigos #+# #+# */
/* Updated: 2023/10/22 20:37:37 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst)
{
if (!lst->next)
return (lst);
lst = lst->next;
}
return (lst);
}

View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/23 17:25:38 by aortigos #+# #+# */
/* Updated: 2023/10/24 18:15:50 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(f)(void *), void (*del)(void *))
{
t_list *x;
t_list *new;
void *set;
if (!lst || !f || !del)
return (NULL);
x = NULL;
while (lst)
{
set = f(lst->content);
new = ft_lstnew(set);
if (!new)
{
del(set);
ft_lstclear(&x, del);
return (NULL);
}
ft_lstadd_back(&x, new);
lst = lst->next;
}
return (x);
}

View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 20:07:04 by aortigos #+# #+# */
/* Updated: 2023/10/22 20:08:16 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *x;
x = malloc(sizeof(t_list));
if (!(x))
return (NULL);
x->content = content;
x->next = NULL;
return (x);
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 20:23:20 by aortigos #+# #+# */
/* Updated: 2023/10/22 20:37:47 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
i = 0;
while (lst)
{
lst = lst->next;
i++;
}
return (i);
}

29
lib/libft/ft_memchr.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 19:00:54 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:41:42 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((const unsigned char *)s)[i] == (unsigned char)c)
{
return ((void *)&((const unsigned char *)s)[i]);
}
i++;
}
return (NULL);
}

29
lib/libft/ft_memcmp.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 19:17:43 by aortigos #+# #+# */
/* Updated: 2023/09/22 19:21:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned int 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);
}

30
lib/libft/ft_memcpy.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 21:19:41 by aortigos #+# #+# */
/* Updated: 2023/10/18 20:18:28 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t num)
{
size_t i;
i = 0;
if (num == 0 || dst == src)
{
return (dst);
}
while (i < num)
{
((char *)dst)[i] = ((char *)src)[i];
i++;
}
return (dst);
}

42
lib/libft/ft_memmove.c Normal file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 21:18:07 by aortigos #+# #+# */
/* Updated: 2023/09/20 21:18:58 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t num)
{
unsigned char *x;
const unsigned char *y;
x = ((unsigned char *)dst);
y = ((const unsigned char *)src);
if (x != y)
{
if (x > y)
{
y = y + num - 1;
x = x + num - 1;
while (num > 0)
{
*x = *y;
x--;
y--;
num--;
}
}
else
{
dst = ft_memcpy(x, y, num);
}
}
return (dst);
}

26
lib/libft/ft_memset.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 18:12:08 by aortigos #+# #+# */
/* Updated: 2023/10/08 16:36:16 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *ptr, int value, size_t num)
{
size_t i;
i = 0;
while (i < num)
{
((char *)ptr)[i] = (unsigned char)value;
i++;
}
return (ptr);
}

18
lib/libft/ft_putchar_fd.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 19:14:55 by aortigos #+# #+# */
/* Updated: 2023/10/22 19:44:11 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

26
lib/libft/ft_putendl_fd.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 19:26:52 by aortigos #+# #+# */
/* Updated: 2023/10/22 19:41:22 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
int i;
i = 0;
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
write(fd, "\n", 1);
}

29
lib/libft/ft_putnbr_fd.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 19:29:35 by aortigos #+# #+# */
/* Updated: 2023/10/22 19:35:33 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
unsigned int nbr;
if (n < 0)
{
ft_putchar_fd('-', fd);
nbr = (unsigned int)(n * -1);
}
else
nbr = (unsigned int)n;
if (nbr >= 10)
ft_putnbr_fd(nbr / 10, fd);
ft_putchar_fd((char)(nbr % 10 + 48), fd);
}

25
lib/libft/ft_putstr_fd.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 19:17:32 by aortigos #+# #+# */
/* Updated: 2023/10/22 19:23:17 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
int i;
i = 0;
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
}

99
lib/libft/ft_split.c Normal file
View File

@@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 10:43:43 by aortigos #+# #+# */
/* Updated: 2023/10/22 10:56:39 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_str(char const *s1, char c)
{
int i;
int j;
i = 0;
j = 0;
if (*s1 == '\0')
return (0);
while (*s1 != '\0')
{
if (*s1 == c)
j = 0;
else if (j == 0)
{
j = 1;
i++;
}
s1++;
}
return (i);
}
static int count_char(char const *s2, char c, int i)
{
int len;
len = 0;
while (s2[i] != c && s2[i] != '\0')
{
len++;
i++;
}
return (len);
}
static char **free_mem(char const **dest, int j)
{
while (j > 0)
{
j--;
free((void *)dest[j]);
}
free(dest);
return (NULL);
}
static char **split(char const *s, char **dest, char c, int l)
{
int i;
int j;
int k;
i = 0;
j = 0;
while (s[i] != '\0' && j < l)
{
k = 0;
while (s[i] == c)
i++;
dest[j] = (char *)malloc(sizeof(char) * count_char(s, c, i) + 1);
if (dest[j] == NULL)
return (free_mem((char const **)dest, j));
while (s[i] != '\0' && s[i] != c)
dest[j][k++] = s[i++];
dest[j][k] = '\0';
j++;
}
dest[j] = 0;
return (dest);
}
char **ft_split(char const *s, char c)
{
char **dest;
int i;
if (s == NULL)
return (NULL);
i = count_str(s, c);
dest = (char **)malloc(sizeof(char *) * (i + 1));
if (dest == NULL)
return (NULL);
return (split(s, dest, c, i));
}

33
lib/libft/ft_strchr.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/21 17:16:15 by aortigos #+# #+# */
/* Updated: 2023/10/12 15:15:10 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *str, int c)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
if (str[i] == (char)c)
{
return ((char *)&str[i]);
}
i++;
}
if ((char)c == '\0')
{
return ((char *)&str[i]);
}
return (NULL);
}

29
lib/libft/ft_strdup.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/27 21:04:50 by aortigos #+# #+# */
/* Updated: 2023/10/22 11:15:45 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s1)
{
char *s2;
size_t i;
i = ft_strlen(s1);
s2 = (char *)malloc(i + 1);
if (!(s2))
{
return (NULL);
}
ft_memcpy(s2, s1, i);
s2[i] = '\0';
return (s2);
}

27
lib/libft/ft_striteri.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 19:10:23 by aortigos #+# #+# */
/* Updated: 2023/10/22 19:13:30 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
unsigned int i;
if (!s || !(*s) || !f)
return ;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

40
lib/libft/ft_strjoin.c Normal file
View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 19:39:00 by aortigos #+# #+# */
/* Updated: 2023/10/03 19:47:34 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *x;
size_t c1;
size_t c2;
size_t i;
i = 0;
c1 = 0;
c2 = 0;
if (s1 == NULL || s2 == NULL)
return (NULL);
x = (char *)malloc(sizeof(char) * ft_strlen(s1) + ft_strlen(s2) + 1);
if (!(x))
return (NULL);
while (c1 != ft_strlen(s1))
{
x[i++] = s1[c1++];
}
while (c2 != ft_strlen(s2))
{
x[i++] = s2[c2++];
}
x[i] = '\0';
return (x);
}

37
lib/libft/ft_strlcat.c Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 21:30:13 by aortigos #+# #+# */
/* Updated: 2023/09/22 18:35:30 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t i;
size_t len;
len = 0;
i = 0;
while (dest[i] != '\0' && i < size)
{
i++;
}
len = i;
while (src[i - len] && i + 1 < size)
{
dest[i] = src[i - len];
i++;
}
if (i < size)
{
dest[i] = '\0';
}
return (len + ft_strlen(src));
}

34
lib/libft/ft_strlcpy.c Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 21:20:19 by aortigos #+# #+# */
/* Updated: 2023/10/22 15:42:47 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
i = 0;
if (size > 0)
{
while (src[i] && i < (size - 1))
{
dest[i] = src[i];
i++;
}
dest[i] = 0;
}
while (src[i])
{
i++;
}
return (i);
}

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

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 17:58:58 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:38:05 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}

33
lib/libft/ft_strmapi.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 16:12:53 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:37:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f) (unsigned int, char))
{
char *x;
unsigned int i;
i = 0;
if (!s)
return (NULL);
x = malloc(sizeof(char) * ft_strlen(s) + 1);
if (!(x))
return (NULL);
while (i != ft_strlen(s))
{
x[i] = (*f)(i, s[i]);
i++;
}
x[i] = '\0';
return (x);
}

34
lib/libft/ft_strncmp.c Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 18:50:00 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:36:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
size_t re;
i = 0;
re = 0;
if (n == 0)
return (0);
while (i < n && (s1[i] != '\0' && s1[i] == s2[i]))
{
i++;
}
if (i == n)
{
i--;
}
re = (unsigned char)s1[i] - (unsigned char)s2[i];
return (re);
}

34
lib/libft/ft_strnstr.c Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 19:22:45 by aortigos #+# #+# */
/* Updated: 2023/09/22 20:44:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t n)
{
size_t c;
if (*needle == '\0')
{
return ((char *)haystack);
}
c = ft_strlen(needle);
while (*haystack && c <= n)
{
if (ft_strncmp(haystack, needle, c) == 0)
{
return ((char *)haystack);
}
haystack++;
n--;
}
return (NULL);
}

42
lib/libft/ft_strrchr.c Normal file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 18:34:08 by aortigos #+# #+# */
/* Updated: 2023/10/19 16:15:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *str, int c)
{
char x;
size_t i;
i = 0;
x = ((char)c);
while (str[i] != '\0')
{
i++;
}
while (i > 0)
{
if (str[i] == x)
{
return (&((char *)str)[i]);
}
i--;
}
if (i == 0)
{
if (str[i] == x)
{
return (&((char *)str)[i]);
}
}
return (0);
}

37
lib/libft/ft_strtrim.c Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 20:21:56 by aortigos #+# #+# */
/* Updated: 2023/10/19 16:58:30 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
size_t len;
char *x;
if (!s1 || !set)
return (NULL);
while (ft_strchr(set, *s1) && *s1 != '\0')
{
s1++;
}
if (*s1 == '\0')
{
return (ft_strdup(""));
}
len = ft_strlen(s1);
while (ft_strchr(set, s1[len]))
{
len--;
}
x = ft_substr(s1, 0, len + 1);
return (x);
}

41
lib/libft/ft_substr.c Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 15:15:06 by aortigos #+# #+# */
/* Updated: 2023/10/23 18:29:42 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *x;
size_t i;
if (!s)
return (NULL);
if (ft_strlen(s) < start)
{
x = ft_calloc(1, sizeof(char));
if (!x)
return (NULL);
}
else
{
i = ft_strlen(s + start);
if (!(i < len))
i = len;
x = (char *)malloc((i + 1) * sizeof(char));
if (!x)
return (NULL);
x[i] = 0;
while (i-- > 0)
x[i] = s[start + i];
}
return (x);
}

22
lib/libft/ft_tolower.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/21 17:13:25 by aortigos #+# #+# */
/* Updated: 2023/09/22 18:45:54 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
{
return (c + 32);
}
return (c);
}

22
lib/libft/ft_toupper.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/21 16:40:59 by aortigos #+# #+# */
/* Updated: 2023/10/04 20:20:28 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
{
return (c - 32);
}
return (c);
}

80
lib/libft/libft.h Normal file
View File

@@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 20:56:18 by aortigos #+# #+# */
/* Updated: 2023/10/26 07:37:33 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.h>
# include <stdlib.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
// String
size_t ft_strlen(const char *str);
size_t ft_strlcpy(char *dest, const char *src, size_t size);
size_t ft_strlcat(char *dest, const char *src, size_t size);
char *ft_strchr(const char *str, int c);
char *ft_strrchr(const char *str, int c);
char *ft_strnstr(const char *haystack, const char *needle, size_t n);
char *ft_strdup(const char *s1);
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));
// Memoria
void *ft_memset(void *ptr, int value, size_t num);
void *ft_memcpy(void *dst, const void *src, size_t num);
void *ft_memmove(void *dst, const void *src, size_t num);
void *ft_memchr(const void *s, int c, size_t n);
void *ft_calloc(size_t count, size_t size);
void ft_bzero(void *ptr, size_t num);
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);
int ft_memcmp(const void *s1, const void *s2, size_t n);
// Caracteres
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_tolower(int c);
int ft_toupper(int c);
// Otras funciones
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_atoi(const char *str);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void*));
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstiter(t_list *lst, void (*f)(void*));
t_list *ft_lstmap(t_list *lst, void *(*f)(void*), void (*del)(void*));
#endif