Libft extra functions

This commit is contained in:
2025-11-16 20:26:45 +01:00
parent 625a922cc9
commit cbbade912d
13 changed files with 459 additions and 13 deletions

View File

@@ -1,13 +1,26 @@
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 = 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 \
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 \
ft_array.c ft_atof.c ft_itoa_base.c ft_memfree_all.c \
ft_memfree.c ft_issign.c ft_isspace.c ft_iabs.c \
ft_strcmp.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
@@ -18,9 +31,6 @@ $(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 $@
@@ -31,6 +41,6 @@ clean:
fclean: clean
rm -f $(NAME)
re: fclean bonus
re: fclean
.PHONY: bonus all clean fclean re
.PHONY: all clean fclean re

81
lib/libft/ft_array.c Normal file
View File

@@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
char **ft_arrayadd(char *str, char **array)
{
char **new_array;
int i;
if (!array)
{
array = ft_calloc(sizeof(char *), 2);
array[0] = str;
return (array);
} else {
new_array = ft_calloc(sizeof(char *), ft_arraylen(array) + 2);
i = 0;
while (array[i++])
new_array[i] = ft_strdup(array[i]);
new_array[i] = ft_strdup(str);
free2d(array);
free(str);
return (new_array);
}
}
char **ft_arraydelete(int pos, char **array)
{
int len;
int i;
len = ft_arraylen(array);
if (!array || pos < 0 || pos > len)
return (array);
i = pos;
while (array[i])
{
array[i] = array[i + 1];
i++;
}
return (array);
}
char **ft_arraydup(char **array)
{
char **new_array;
int i;
if (!array)
return (NULL);
new_array = ft_calloc(sizeof(char *), ft_arraylen(array) + 1);
i = 0;
while (array[i])
{
new_array[i] = ft_strdup(array[i]);
i++;
}
return (new_array);
}
int ft_arraylen(char **array)
{
int i;
i = 0;
if (!array)
return (0);
while (array[i])
i++;
return (i);
}

38
lib/libft/ft_atof.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
double ft_atof(char *str)
{
int num;
double rest;
char *tmp;
int i;
num = ft_atoi(str);
tmp = ft_strchr(str, '.');
if (!tmp)
tmp = ft_strchr(str, ',');
if (!tmp)
return (num);
rest = ft_atoi(&tmp[1]);
i = 1;
while (tmp[i])
{
rest /= 10;
i++;
}
if (num < 0 || (str[0] == '-' && str[1] == '0'))
return (num - rest);
return (num + rest);
}

20
lib/libft/ft_iabs.c Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_iabs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/16 20:22:27 by aortigos #+# #+# */
/* Updated: 2025/11/16 20:22:46 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_iabs(int nb)
{
if (nb < 0)
return (nb * -1);
return (nb);
}

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

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_issign.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/16 19:05:57 by aortigos #+# #+# */
/* Updated: 2025/11/16 19:06:27 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_issign(int c)
{
return (c == 43 || c == 45);
}

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

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/16 19:06:46 by aortigos #+# #+# */
/* Updated: 2025/11/16 19:07:08 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(char c)
{
return (c == 32 || (c >= 9 && c <= 13));
}

82
lib/libft/ft_itoa_base.c Normal file
View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 20:46:43 by aortigos #+# #+# */
/* Updated: 2025/11/16 19:04:48 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int mem_str(int base)
{
if (base == 2)
return (sizeof(int) * 8);
else if (base == 8)
return (22);
else if (base == 16)
return (16);
return (0);
}
static void test(int base, char *str, int i, unsigned int digit)
{
if (base == 2 || base == 8)
str[i] = '0' + digit;
else if (base == 16)
{
if (digit < 10)
str[i] = '0' + digit;
else
str[i] = 'a' + digit - 10;
}
}
static char *str_invert(char *str, int index)
{
int i;
int j;
char temp;
i = 0;
j = index - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
return (str);
}
char *ft_itoa_base(unsigned long long ul, int base)
{
char *str;
int i;
unsigned int digit;
if (base != 2 && base != 8 && base != 16)
return (NULL);
str = (char *)malloc(sizeof(char) * (mem_str(base) + 1));
if (!str)
return (NULL);
i = 0;
while (ul > 0)
{
digit = ul % base;
test(base, str, i, digit);
ul /= base;
i++;
}
if (i == 0)
str[i++] = '0';
str[i] = 0;
str_invert(str, i);
return (str);
}

88
lib/libft/ft_ltoa.c Normal file
View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 20:46:43 by aortigos #+# #+# */
/* Updated: 2025/11/16 20:25:47 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t len_sl(long long nl)
{
size_t len;
len = 0;
if (nl <= 0)
len++;
while (nl != 0)
{
len++;
nl /= 10;
}
return (len);
}
char *ft_sltoa(long long nl)
{
char *str;
size_t len;
len = len_sl(nl);
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
if (nl < 0)
{
str[0] = '-';
nl *= -1;
}
str[len] = 0;
str[--len] = (nl % 10) + '0';
nl /= 10;
while (nl != 0 && len--)
{
str[len] = (nl % 10) + '0';
nl /= 10
}
return (str);
}
static size_t len_ul(unsigned long long un)
{
size_t len;
len = 0;
if (un == 0)
len++;
while (un != 0)
{
len++;
un /= 10;
}
return (len);
}
char *ft_ultoa(unsigned long long un)
{
char *str;
size_t len;
len = len_ul(un);
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
str[len] = 0;
str[--len] = (un % 10) + '0';
un /= 10;
while (un != 0 && len--)
{
str[len] = (un % 10) + '0';
un /= 10;
}
return (str);
}

21
lib/libft/ft_memfree.c Normal file
View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memfree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/16 19:04:46 by aortigos #+# #+# */
/* Updated: 2025/11/16 19:05:35 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_memfree(void *ptr)
{
if (!ptr)
return ;
free(ptr);
ptr = NULL;
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memfree_all.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/22 20:46:43 by aortigos #+# #+# */
/* Updated: 2025/11/16 19:04:45 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void free2d(char **ptr)
{
int i;
if (!ptr)
return ;
i = 0;
while (ptr[i])
ft_memfree(ptr[i++]);
free(ptr);
ptr = NULL;
}

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* 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 */
/* Updated: 2025/11/16 19:04:57 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */

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

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/16 20:23:46 by aortigos #+# #+# */
/* Updated: 2025/11/16 20:24:27 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == '\0')
return (0);
s1++;
s2++;
}
return (*s1 - *s2);
}

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* 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 */
/* Updated: 2025/11/16 20:25:28 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -55,6 +55,11 @@ 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);
void free2d(char **ptr);
void ft_memfree(void *ptr);
int ft_strcmp(const char *s1, const char *s2);
// Caracteres
int ft_isalpha(int c);
int ft_isdigit(int c);
@@ -63,10 +68,24 @@ int ft_isascii(int c);
int ft_isprint(int c);
int ft_tolower(int c);
int ft_toupper(int c);
int ft_issign(int c);
int ft_isspace(char c);
// Array
char **ft_arrayadd(char *str, char **array);
char **ft_arraydelete(int pos, char **array);
char **ft_arraydup(char **array);
int ft_arraylen(char **array);
// Otras funciones
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_atoi(const char *str);
double ft_atof(char *str);
char *ft_sltoa(long long nl);
char *ft_ultoa(unsigned long long un);
char *ft_itoa_base(unsigned long long ul, int base);
int ft_iabs(int nb);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);