libs: added libft

This commit is contained in:
Jérôme Guélen 2025-02-12 14:51:05 +01:00 committed by Khaïs COLIN
parent 6e1552a35d
commit ddb2306630
63 changed files with 2989 additions and 4 deletions

51
libft/Makefile Normal file
View file

@ -0,0 +1,51 @@
LIBDIR = ./libft/
CC = cc
AR = ar
ARFLAGS = rcs
CFLAGS = -Wall -Wextra -Werror -g
NAME = libft.a
SOURCES = get_next_line.c \
ft_printf_atoi.c\
ft_printf_decimal.c\
ft_printf_parsing.c\
ft_printf_utils2.c\
ft_printf.c\
ft_printf_pointer.c\
ft_printf_utils.c\
ft_printf_char.c\
ft_printf_hexa.c\
ft_printf_str.c
BONUSSOURCES =
BONUSOBJ = $(BONUSSOURCES:.c=.o)
OBJECTS = $(SOURCES:.c=.o)
LIBFT = $(LIBDIR)libft.a
all: $(NAME)
$(NAME): $(LIBFT) $(OBJECTS)
cp $(LIBFT) $(NAME)
$(AR) $(ARFLAGS) $@ $(OBJECTS)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $< -I$(LIBDIR)
$(LIBFT):
$(MAKE) -C $(LIBDIR)
bonus: $(NAME)
clean:
$(MAKE) -C $(LIBDIR) clean
rm -f $(OBJECTS) $(BONUSOBJ)
@rm -f test
fclean: clean
$(MAKE) -C $(LIBDIR) fclean
rm -f $(NAME)
re: fclean all
#bonus: $(OBJECTS) $(BONUSOBJ)
# $(AR) $(ARFLAGS) $(NAME) $^
.PHONY: clean fclean re bonus

83
libft/ft_printf.c Normal file
View file

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/06 09:32:35 by jguelen #+# #+# */
/* Updated: 2025/02/04 10:16:41 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int check_minwidth_and_precision(t_printf_format *format)
{
if (format->minwidth == LONG_MAX || format->minwidth == LONG_MIN
|| format->precision == LONG_MAX || format->precision == LONG_MIN
|| format->minwidth == INT_MIN)
return (1);
if (format->minwidth < 0)
{
format->flags |= PRINTF_MINUSFLAG;
format->minwidth *= -1;
}
if (format->precision < 0)
format->flags &= ~PRINTF_DOTFLAG;
return (0);
}
int ft_print_printf_format(int fd, t_printf_format *format)
{
static int (*const printspec[])(int fd, t_printf_format *format) = {
[PRINTF_NOSPEC] = &ft_printf_badspec,
[PRINTF_CHARSPEC] = &ft_printf_char,
[PRINTF_STRINGSPEC] = &ft_printf_string,
[PRINTF_POINTERSPEC] = &ft_printf_ptr,
[PRINTF_INTSPEC] = &ft_printf_int,
[PRINTF_UINTSPEC] = &ft_printf_uint,
[PRINTF_HEXA_LOWSPEC] = &ft_printf_hexa,
[PRINTF_HEXA_UPSPEC] = &ft_printf_hexa,
[PRINTF_PERCENTSPEC] = &ft_printf_percent,
};
if (check_minwidth_and_precision(format))
return (-1);
return (printspec[format->specifier](fd, format));
}
static int ft_printf_fd(int fd, const char *s,
va_list *arg_list)
{
int len;
t_printf_format format;
if (!s)
return (-1);
len = ft_printf_parsing(fd, (char *)s, &format, arg_list);
return (len);
}
__attribute__((format(printf, 1, 2))) int ft_printf(const char *s, ...)
{
va_list arg_list;
int len;
va_start(arg_list, s);
len = ft_printf_fd(1, s, &arg_list);
va_end(arg_list);
return (len);
}
__attribute__((format(printf, 2, 3))) int ft_dprintf(int fd,
const char *s, ...)
{
va_list arg_list;
int len;
va_start(arg_list, s);
len = ft_printf_fd(fd, s, &arg_list);
va_end(arg_list);
return (len);
}

105
libft/ft_printf.h Normal file
View file

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/06 09:22:02 by jguelen #+# #+# */
/* Updated: 2025/02/04 09:48:18 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include "libft/libft.h"
# include <stdarg.h>
# include <limits.h>
# define PRINTF_SPECLIST "cspdiuxX%"
# define PRINTF_FLAGS "#+ -0"
# define HEXA_UP "0123456789ABCDEF"
# define HEXA_LOW "0123456789abcdef"
# define HEXA_RADIX_LOW "0x"
# define HEXA_RADIX_UP "0X"
# define HEX_LEN 16
typedef enum e_printf_spec
{
PRINTF_NOSPEC = 0,
PRINTF_CHARSPEC = 1,
PRINTF_STRINGSPEC = 2,
PRINTF_POINTERSPEC = 3,
PRINTF_INTSPEC = 4,
PRINTF_UINTSPEC = 5,
PRINTF_HEXA_LOWSPEC = 6,
PRINTF_HEXA_UPSPEC = 7,
PRINTF_PERCENTSPEC = 8
} t_printf_spec;
typedef enum e_printf_flags
{
PRINTF_NOFLAG = 0,
PRINTF_PLUSFLAG = 1,
PRINTF_MINUSFLAG = 2,
PRINTF_SPACEFLAG = 4,
PRINTF_ZEROFLAG = 8,
PRINTF_HASHTAGFLAG = 16,
PRINTF_DOTFLAG = 32,
} t_printf_flags;
typedef union u_printf_var
{
char c;
char *s;
void *p;
int i;
unsigned int u;
} t_printf_var;
/*precision and minwith are to store integers but use long
to deal with errors.*/
typedef struct s_printf_format
{
t_printf_flags flags;
t_printf_spec specifier;
t_printf_var var;
long minwidth;
long precision;
char prefix[3];
int prefix_len;
int total_len;
} t_printf_format;
int ft_dprintf(int fd, const char *s, ...);
int ft_printf(const char *s, ...);
int ft_print_printf_format(int fd, t_printf_format *format);
/*ft_printf_parsing*/
int ft_printf_parsing(int fd, char *s, t_printf_format *format,
va_list *arg_list);
/*ft_printf_atoi*/
long ft_printf_atoi(char *str, char **current);
/*ft_printf_utils*/
int ft_int_len(int n, int base_len);
int ft_uint_len(unsigned int n, int base_len);
int ft_ull_len(unsigned long long n, int base_len);
/*Takes precision into account when relevant.*/
int var_len(t_printf_format *format);
/*ft_printf_utils2*/
void get_prefix_format(t_printf_format *format);
/**/
int ft_printf_str(int fd, const char *str, int maxchar);
int ft_pad_char(int fd, t_printf_format *format, char *c);
int ft_printf_badspec(int fd, t_printf_format *format);
int ft_printf_char(int fd, t_printf_format *format);
int ft_printf_string(int fd, t_printf_format *format);
int ft_printf_ptr(int fd, t_printf_format *format);
int ft_printf_int(int fd, t_printf_format *format);
int ft_printf_uint(int fd, t_printf_format *format);
int ft_printf_hexa(int fd, t_printf_format *format);
int ft_printf_percent(int fd, t_printf_format *format);
int ft_put_ull_base_fd(unsigned long long n, char *base,
int base_size, int fd);
int ft_isnumeric_conv(t_printf_format *format);
#endif

34
libft/ft_printf_atoi.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 13:45:04 by jguelen #+# #+# */
/* Updated: 2024/12/01 17:16:53 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*Does not deal with the isspace(3) characters.
* @RETURN a long castable into an int or LONG_MAX if overflow has
* occurred in the positive.
*/
long ft_printf_atoi(char *str, char **current)
{
long calc;
calc = 0;
while (*str && ft_isdigit(*str))
{
if ((calc > (INT_MAX / 10))
|| ((calc == (INT_MAX / 10)) && ((INT_MAX % 10) < (*str - '0'))))
return (LONG_MAX);
calc = calc * 10 + (*str - '0');
str++;
}
*current = str;
return (calc);
}

28
libft/ft_printf_char.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 14:54:42 by jguelen #+# #+# */
/* Updated: 2025/02/04 09:59:58 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf_percent(int fd, t_printf_format *format)
{
format->total_len = write(fd, "%", 1);
return (format->total_len);
}
int ft_printf_char(int fd, t_printf_format *format)
{
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
ft_pad_char(fd, format, " ");
if (write(fd, &format->var.c, 1) == -1)
return (-1);
return (ft_pad_char(fd, format, " "));
}

131
libft/ft_printf_decimal.c Normal file
View file

@ -0,0 +1,131 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_decimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 10:52:39 by jguelen #+# #+# */
/* Updated: 2025/02/04 09:56:04 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_display_absint(int fd, int n)
{
int i;
int calc;
char buf[20];
int j;
i = 0;
calc = n;
j = 19;
if (n == 0)
return (write(fd, "0", 1));
while (calc != 0)
{
buf[j] = (calc % 10) * (1 - 2 * (n < 0)) + '0';
calc /= 10;
j--;
i++;
}
return (write(fd, buf + j + 1, i));
}
static int ft_printf_putint(int fd, t_printf_format *format)
{
int i;
int calc;
calc = format->var.i;
i = ft_int_len(calc, 10);
if (format->var.i < 0 && write(fd, "-", 1) == -1)
return (-1);
while ((format->flags & PRINTF_DOTFLAG) && (i++ < format->precision))
{
if (write(fd, "0", 1) == -1)
return (-1);
}
if (((format->flags & PRINTF_DOTFLAG) && (format->precision == 0))
&& (format->var.i == 0))
return (0);
return (ft_display_absint(fd, calc) + (format->var.i < 0));
}
int ft_printf_int(int fd, t_printf_format *format)
{
if (format->var.i < 0)
format->total_len++;
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
&& (format->var.i == 0))
format->total_len = 0;
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
{
if ((format->flags & ~PRINTF_ZEROFLAG) == format->flags
&& ft_pad_char(fd, format, " ") == -1)
return (-1);
else if (format->flags & PRINTF_ZEROFLAG)
{
format->precision = max_int(max_int((int)format->minwidth,
format->precision), 1) - (format->var.i < 0);
format->total_len = max_int((int)format->minwidth,
format->total_len);
format->flags |= PRINTF_DOTFLAG;
}
}
if (ft_printf_str(fd, format->prefix, INT_MAX) == -1
|| ft_printf_putint(fd, format) == -1)
return (-1);
return (ft_pad_char(fd, format, " "));
}
/*base is presumed to be a valid base and base_len to be its number
of "digits"*/
int ft_printf_putuint_base(int fd, t_printf_format *format, char *base,
int base_len)
{
unsigned int calc;
char buf[20];
int i;
calc = format->var.u;
i = ft_uint_len(calc, 10);
while (i++ < format->precision)
{
if (write(fd, "0", 1) == -1)
return (-1);
}
i = 19;
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
&& (format->var.u == 0))
return (0);
if (calc == 0)
return (write(fd, "0", 1));
while (calc)
{
buf[i] = base[calc % base_len];
calc /= base_len;
i--;
}
return (write(fd, buf + i + 1, 19 - i));
}
int ft_printf_uint(int fd, t_printf_format *format)
{
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
&& (format->var.u == 0))
format->total_len = 0;
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
{
if ((format->flags & ~PRINTF_ZEROFLAG) == format->flags
&& ft_pad_char(fd, format, " ") == -1)
return (-1);
else if (ft_pad_char(fd, format, "0") == -1)
return (-1);
}
if (ft_printf_putuint_base(fd, format, "0123456789", 10) == -1)
return (-1);
return (ft_pad_char(fd, format, " "));
}

73
libft/ft_printf_hexa.c Normal file
View file

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 10:39:10 by jguelen #+# #+# */
/* Updated: 2025/02/04 09:52:40 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*Presumes base 10 or higher*/
int ft_put_ull_base_fd(unsigned long long n, char *base, int base_size, int fd)
{
unsigned long long calc;
char buf[20];
int i;
if (n == 0)
return (write(fd, "0", 1));
calc = n;
i = 19;
while (calc != 0)
{
buf[i--] = base[calc % base_size];
calc /= base_size;
}
return (write(fd, buf + i + 1, 19 - i));
}
static int ft_printf_hexa2(int fd, t_printf_format *format)
{
if (ft_printf_str(fd, format->prefix, INT_MAX) == -1
|| (format->specifier == PRINTF_HEXA_UPSPEC
&& ft_put_ull_base_fd((unsigned long long)format->var.u,
HEXA_UP, HEX_LEN, fd) == -1))
return (-1);
else if (format->specifier == PRINTF_HEXA_LOWSPEC
&& ft_put_ull_base_fd((unsigned long long)format->var.u,
HEXA_LOW, HEX_LEN, fd) == -1)
return (-1);
return (ft_pad_char(fd, format, " "));
}
int ft_printf_hexa(int fd, t_printf_format *format)
{
int i;
unsigned long long calc;
calc = (unsigned long long)format->var.u;
i = ft_uint_len(calc, HEX_LEN);
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
&& (format->var.u == 0))
format->total_len = 0;
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
{
if ((format->flags & ~PRINTF_ZEROFLAG) == format->flags
&& ft_pad_char(fd, format, " ") == -1)
return (-1);
else if (ft_pad_char(fd, format, "0") == -1)
return (-1);
}
while (i++ < format->precision)
if (write(fd, "0", 1) == -1)
return (-1);
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
&& (format->var.u == 0))
return (ft_pad_char(fd, format, " "));
return (ft_printf_hexa2(fd, format));
}

132
libft/ft_printf_parsing.c Normal file
View file

@ -0,0 +1,132 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 10:38:17 by jguelen #+# #+# */
/* Updated: 2025/02/04 10:04:05 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static t_printf_flags ft_get_flag(char c)
{
if (c == '0')
return (PRINTF_ZEROFLAG);
if (c == '-')
return (PRINTF_MINUSFLAG);
if (c == '#')
return (PRINTF_HASHTAGFLAG);
if (c == '+')
return (PRINTF_PLUSFLAG);
if (c == ' ')
return (PRINTF_SPACEFLAG);
return (PRINTF_NOFLAG);
}
static t_printf_spec ft_get_spec(char c)
{
if (c == 'c')
return (PRINTF_CHARSPEC);
if (c == 's')
return (PRINTF_STRINGSPEC);
if (c == 'p')
return (PRINTF_POINTERSPEC);
if (c == 'd' || c == 'i')
return (PRINTF_INTSPEC);
if (c == 'u')
return (PRINTF_UINTSPEC);
if (c == 'x')
return (PRINTF_HEXA_LOWSPEC);
if (c == 'X')
return (PRINTF_HEXA_UPSPEC);
if (c == '%')
return (PRINTF_PERCENTSPEC);
return (PRINTF_NOSPEC);
}
static t_printf_var ft_get_var(t_printf_spec specifier,
va_list *arg_list, char c)
{
t_printf_var var;
ft_bzero(&var, sizeof(t_printf_var));
if (specifier == PRINTF_CHARSPEC)
var.c = (char)va_arg(*arg_list, int);
else if (specifier == PRINTF_STRINGSPEC)
var.s = va_arg(*arg_list, char *);
else if (specifier == PRINTF_POINTERSPEC)
var.p = va_arg(*arg_list, void *);
else if (specifier == PRINTF_INTSPEC)
var.i = va_arg(*arg_list, int);
else if (specifier == PRINTF_UINTSPEC || specifier == PRINTF_HEXA_LOWSPEC
|| specifier == PRINTF_HEXA_UPSPEC)
var.u = va_arg(*arg_list, unsigned int);
else
var.c = c;
return (var);
}
/*PRINTF_ZEROFLAG only for numerical conv*/
static void ft_getprintf_format(char **s, t_printf_format *format,
va_list *arg_list)
{
char *tmp;
tmp = (char *)*s;
while (*tmp && ft_strchr(PRINTF_FLAGS, *tmp))
format->flags |= ft_get_flag(*tmp++);
if (*tmp == '*' && tmp++)
format->minwidth = (long)va_arg(*arg_list, int);
else if (ft_isdigit(*tmp))
format->minwidth = ft_printf_atoi(tmp, &tmp);
if (*tmp == '.' && tmp++)
{
format->flags = (format->flags | PRINTF_DOTFLAG) & ~PRINTF_ZEROFLAG;
if (*tmp == '*' && tmp++)
format->precision = (long)va_arg(*arg_list, int);
else if (ft_isdigit(*tmp))
format->precision = ft_printf_atoi(tmp, &tmp);
}
format->specifier = ft_get_spec(*tmp);
if (*tmp)
{
format->var = ft_get_var(format->specifier, arg_list, *tmp);
*s = tmp + 1;
}
format->total_len = var_len(format);
get_prefix_format(format);
}
int ft_printf_parsing(int fd, char *s, t_printf_format *format,
va_list *arg_list)
{
int len;
int i;
len = 0;
while (*s)
{
i = 0;
while (s[i] && s[i] != '%')
i++;
i = write(fd, s, i);
if (i == -1)
return (-1);
s += i;
len += i;
if (*s == '%' && s++)
{
ft_bzero(format, sizeof(t_printf_format));
ft_getprintf_format(&s, format, arg_list);
i = ft_print_printf_format(fd, format);
if (((INT_MAX - len) < format->total_len) || (i == -1))
return (-1);
len += format->total_len;
}
}
return (len);
}

34
libft/ft_printf_pointer.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 10:46:34 by jguelen #+# #+# */
/* Updated: 2025/02/04 09:50:47 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf_ptr(int fd, t_printf_format *format)
{
unsigned long long ptr_hexa;
ptr_hexa = (unsigned long long)format->var.p;
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags
&& ft_pad_char(fd, format, " ") == -1)
return (-1);
if (ptr_hexa == 0)
{
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
ft_pad_char(fd, format, " ");
return (ft_printf_str(fd, "(nil)", INT_MAX),
ft_pad_char(fd, format, " "));
}
if (ft_printf_str(fd, format->prefix, INT_MAX) == -1
|| ft_put_ull_base_fd(ptr_hexa, HEXA_LOW, HEX_LEN, fd) == -1)
return (-1);
return (ft_pad_char(fd, format, " "));
}

93
libft/ft_printf_str.c Normal file
View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 11:40:22 by jguelen #+# #+# */
/* Updated: 2025/02/04 10:03:36 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/* c points to the character used to pad*/
int ft_pad_char(int fd, t_printf_format *format, char *c)
{
while (format->total_len < format->minwidth)
{
if (write(fd, c, 1) == -1)
return (-1);
format->total_len++;
}
return (format->total_len);
}
int ft_printf_str(int fd, const char *str, int maxchar)
{
int len;
char *s;
len = 0;
s = (char *)str;
if (s == NULL)
{
if (maxchar >= 6)
return (ft_printf_str(fd, "(null)", INT_MAX));
return (0);
}
while (s[len] && len < maxchar)
len++;
return ((int)write(fd, s, len));
}
int ft_printf_string(int fd, t_printf_format *format)
{
int i;
i = INT_MAX;
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
{
if (ft_pad_char(fd, format, " ") == -1)
return (-1);
}
if (format->flags & PRINTF_DOTFLAG)
i = (int)format->precision;
if (ft_printf_str(fd, format->var.s, i) == -1)
return (-1);
if (ft_pad_char(fd, format, " ") == -1)
return (-1);
return (format->total_len);
}
int ft_printf_badspec(int fd, t_printf_format *format)
{
(void)fd;
return (format->total_len);
}
/*
//TODO write field width and precision
int ft_printf_badspec(int fd, t_printf_format *format)
{
int i;
i = 0;
if ((format->flags & PRINTF_HASHTAGFLAG) && write(fd, "#", 1) == -1)
return (-1);
if ((format->flags & PRINTF_PLUSFLAG) && write(fd, "+", 1) == -1)
return (-1);
if ((format->flags & PRINTF_SPACEFLAG)
&& (format->flags & ~PRINTF_PLUSFLAG) == format->flags
&& write(fd, " ", 1) == -1)
return (-1);
if ((format->flags & PRINTF_MINUSFLAG) && write(fd, "-", 1) == -1)
return (-1);
if ((format->flags & PRINTF_ZEROFLAG)
&& write(fd, "0", 1) == -1)
return (-1);
// WRITE FIELD WIDTH AND '.'-PRECEDED PRECISION
if (write(fd, &format->var.c, 1) == -1 && i++)
return (-1);
return (format->total_len);
}*/

112
libft/ft_printf_utils.c Normal file
View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/04 10:39:30 by jguelen #+# #+# */
/* Updated: 2024/12/11 14:25:40 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*To handle special printf behaviors regarding strings and precision.*/
static int ft_printf_strlen(char *s, int maxchar)
{
int len;
len = 0;
if (!s)
{
if (maxchar > 5)
return (6);
return (0);
}
while (s[len] && len < maxchar)
len++;
return (len);
}
int ft_int_len(int n, int base_len)
{
int len;
int calc;
len = 0;
calc = n;
if (n == 0)
return (1);
while (calc != 0)
{
calc /= base_len;
len++;
}
return (len);
}
int ft_uint_len(unsigned int n, int base_len)
{
int len;
unsigned int calc;
calc = n;
len = 0;
if (n == 0)
return (1);
while (calc != 0)
{
calc /= base_len;
len++;
}
return (len);
}
/*Naming is a bit off since it can only be used for pointers*/
int ft_ull_len(unsigned long long n, int base_len)
{
unsigned long long calc;
int len;
len = 0;
calc = n;
if (n == 0)
return (ft_strlen("(nil)"));
while (calc != 0)
{
calc /= base_len;
len++;
}
return (len);
}
/*Takes precision into account when relevant.*/
int var_len(t_printf_format *format)
{
t_printf_spec spec;
spec = format->specifier;
if (spec == PRINTF_CHARSPEC || spec == PRINTF_NOSPEC
|| spec == PRINTF_PERCENTSPEC)
return (1);
if (spec == PRINTF_STRINGSPEC)
{
if (format->flags & PRINTF_DOTFLAG)
return (ft_printf_strlen(format->var.s, (int)format->precision));
else
return ((int)ft_printf_strlen(format->var.s, INT_MAX));
}
if (spec == PRINTF_INTSPEC)
return (max_int(ft_int_len(format->var.i, 10), format->precision));
if (spec == PRINTF_UINTSPEC)
return (max_int(ft_uint_len(format->var.u, 10), format->precision));
if (spec == PRINTF_HEXA_LOWSPEC || spec == PRINTF_HEXA_UPSPEC)
return (max_int(ft_uint_len(format->var.u, HEX_LEN),
format->precision));
if (spec == PRINTF_POINTERSPEC)
return (ft_ull_len((unsigned long long)format->var.p, HEX_LEN));
if (ft_isnumeric_conv(format) && !format->var.i && !format->var.u)
return (0);
return (0);
}

70
libft/ft_printf_utils2.c Normal file
View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/05 17:10:15 by jguelen #+# #+# */
/* Updated: 2024/12/11 14:23:21 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_isnumeric_conv(t_printf_format *format)
{
t_printf_spec spec;
spec = format->specifier;
if (spec == PRINTF_HEXA_LOWSPEC || spec == PRINTF_HEXA_UPSPEC
|| spec == PRINTF_INTSPEC || spec == PRINTF_UINTSPEC)
return (1);
return (0);
}
static void get_prefix_format_util(t_printf_format *format)
{
t_printf_spec spec;
spec = format->specifier;
if (ft_isnumeric_conv(format) && (format->flags & PRINTF_DOTFLAG))
format->flags &= ~PRINTF_ZEROFLAG;
if (spec == PRINTF_POINTERSPEC)
{
ft_strlcpy(format->prefix, HEXA_RADIX_LOW, 3);
format->prefix_len = 2;
}
if (spec == PRINTF_INTSPEC
|| (spec == PRINTF_POINTERSPEC && format->var.p)
|| ((spec == PRINTF_HEXA_LOWSPEC || spec == PRINTF_HEXA_UPSPEC)
&& format->var.u))
format->total_len += format->prefix_len;
}
void get_prefix_format(t_printf_format *format)
{
if ((format->flags & PRINTF_PLUSFLAG)
&& format->specifier == PRINTF_INTSPEC && format->var.i >= 0)
{
format->flags &= ~PRINTF_SPACEFLAG;
format->prefix[0] = '+';
format->prefix_len = 1;
}
else if ((format->flags & PRINTF_HASHTAGFLAG)
&& (format->specifier == PRINTF_HEXA_LOWSPEC
|| format->specifier == PRINTF_HEXA_UPSPEC) && format->var.u != 0)
{
ft_strlcpy(format->prefix, HEXA_RADIX_LOW, 3);
if (format->specifier == PRINTF_HEXA_UPSPEC)
ft_strlcpy(format->prefix, HEXA_RADIX_UP, 3);
format->prefix_len = 2;
}
else if ((format->flags & PRINTF_SPACEFLAG)
&& format->specifier == PRINTF_INTSPEC && format->var.i >= 0)
{
format->prefix[0] = ' ';
format->prefix_len = 1;
}
get_prefix_format_util(format);
}

128
libft/get_next_line.c Normal file
View file

@ -0,0 +1,128 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/01 15:08:34 by jguelen #+# #+# */
/* Updated: 2025/01/06 14:37:50 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "get_next_line.h"
/*Finds the next \n in the buffer and sets *copy_til to it and returns 1 if
found. If no \n is found, returns 0 and sets copy_til to buffer->end if
the buffer is not empty*/
static int find_eol(t_buffer *buffer, char **copy_til)
{
size_t i;
i = 0;
if (buffer->start == NULL)
return (0);
while (buffer->start + i <= buffer->end)
{
if (buffer->start[i] == '\n')
{
*copy_til = buffer->start + i;
return (1);
}
i++;
}
*copy_til = buffer->end;
return (0);
}
static int line_aggregation(t_buffer *buffer, char **copy_til, char **line,
size_t *len)
{
char *new;
size_t old_len;
old_len = *len;
if (!buffer->start)
return (1);
*len += *copy_til - buffer->start + 1;
new = malloc((*len + 1) * sizeof(char));
if (!new)
{
free(*line);
line = NULL;
return (0);
}
if (*line)
ft_memcpy(new, *line, old_len);
ft_memcpy(new + old_len, buffer->start, *len - old_len);
new[*len] = '\0';
buffer->start = *copy_til + 1;
if (buffer->start > buffer->end)
buffer->start = NULL;
free(*line);
*line = new;
return (1);
}
static char *ft_read_line(int fd, t_buffer *buffer, char **line)
{
char *copy_til;
size_t line_length;
ssize_t bytes_read;
line_length = 0;
while (!find_eol(buffer, &copy_til))
{
if (!line_aggregation(buffer, &copy_til, line, &line_length))
return (NULL);
bytes_read = read(fd, buffer->buffer, BUFFER_SIZE);
if (bytes_read == -1)
return (buffer->start = NULL, free(*line), NULL);
buffer->start = buffer->buffer;
buffer->end = buffer->start + bytes_read - 1;
if (buffer->end < buffer->start)
buffer->start = NULL;
if (bytes_read < BUFFER_SIZE)
break ;
}
find_eol(buffer, &copy_til);
if (!line_aggregation(buffer, &copy_til, line, &line_length))
return (NULL);
return (*line);
}
char *get_next_line(int fd)
{
static t_buffer buffer = {{0}, NULL, NULL};
char *line;
if (fd < 0 || fd >= FD_MAX || BUFFER_SIZE <= 0)
return (NULL);
line = NULL;
return (ft_read_line(fd, &buffer, &line));
}
/**
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd;
char *line;
fd = open("../TestGNL/giant_line_nl.txt", O_RDONLY);
line = "";
while (line != NULL)
{
printf("%s", line);
line = get_next_line(fd);
}
line = get_next_line(fd);
printf("%s", line);
line = get_next_line(fd);
printf("%s", line);
close(fd);
return (0);
}**/

39
libft/get_next_line.h Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/01 15:08:34 by jguelen #+# #+# */
/* Updated: 2024/11/23 16:29:54 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1024
# endif
# ifndef FD_MAX
# define FD_MAX 1024
# endif
# include <stddef.h>
# include <stdlib.h>
# include <unistd.h>
/*start is set to NULL when there is nothing left in buffer to be dealt with*/
typedef struct s_buffer
{
char buffer[BUFFER_SIZE];
char *start;
char *end;
} t_buffer;
/*get_next_line.c*/
char *get_next_line(int fd);
/*get_next_line_utils.c*/
void *ft_memcpy(void *dest, const void *src, size_t n);
#endif

167
libft/libft.h Normal file
View file

@ -0,0 +1,167 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
/* Updated: 2025/01/07 18:10:15 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <unistd.h>
# include <ctype.h>
# include <string.h>
# include <strings.h>
# include <stdlib.h>
# include <stdarg.h>
# include <limits.h>
# define PRINTF_SPECLIST "cspdiuxX%"
# define PRINTF_FLAGS "#+ -0"
# define HEXA_UP "0123456789ABCDEF"
# define HEXA_LOW "0123456789abcdef"
# define HEXA_RADIX_LOW "0x"
# define HEXA_RADIX_UP "0X"
# define HEX_LEN 16
# define SIZE_T_MAX 0xFFFFFFFFFFFFFFFF
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
typedef enum e_printf_spec
{
PRINTF_NOSPEC = 0,
PRINTF_CHARSPEC = 1,
PRINTF_STRINGSPEC = 2,
PRINTF_POINTERSPEC = 3,
PRINTF_INTSPEC = 4,
PRINTF_UINTSPEC = 5,
PRINTF_HEXA_LOWSPEC = 6,
PRINTF_HEXA_UPSPEC = 7,
PRINTF_PERCENTSPEC = 8
} t_printf_spec;
typedef enum e_printf_flags
{
PRINTF_NOFLAG = 0,
PRINTF_PLUSFLAG = 1,
PRINTF_MINUSFLAG = 2,
PRINTF_SPACEFLAG = 4,
PRINTF_ZEROFLAG = 8,
PRINTF_HASHTAGFLAG = 16,
PRINTF_DOTFLAG = 32,
} t_printf_flags;
typedef union u_printf_var
{
char c;
char *s;
void *p;
int i;
unsigned int u;
} t_printf_var;
/*precision and minwith are to store integers but use long
to deal with errors.*/
typedef struct s_printf_format
{
t_printf_flags flags;
t_printf_spec specifier;
t_printf_var var;
long minwidth;
long precision;
char prefix[3];
int prefix_len;
int total_len;
} t_printf_format;
int ft_dprintf(int fd, const char *s, ...);
int ft_printf(const char *s, ...);
int ft_print_printf_format(t_printf_format *format);
/*ft_printf_parsing*/
int ft_printf_parsing(char *s, t_printf_format *format,
va_list *arg_list);
/*ft_printf_atoi*/
long ft_printf_atoi(char *str, char **current);
/*ft_printf_utils*/
int ft_int_len(int n, int base_len);
int ft_uint_len(unsigned int n, int base_len);
int ft_ull_len(unsigned long long n, int base_len);
/*Takes precision into account when relevant.*/
int var_len(t_printf_format *format);
/*ft_printf_utils2*/
void get_prefix_format(t_printf_format *format);
/**/
int ft_printf_str(const char *str, int maxchar);
int ft_pad_char(t_printf_format *format, char *c);
int ft_printf_badspec(t_printf_format *format);
int ft_printf_char(t_printf_format *format);
int ft_printf_string(t_printf_format *format);
int ft_printf_ptr(t_printf_format *format);
int ft_printf_int(t_printf_format *format);
int ft_printf_uint(t_printf_format *format);
int ft_printf_hexa(t_printf_format *format);
int ft_printf_percent(t_printf_format *format);
int ft_put_ull_base_fd(unsigned long long n, char *base,
int base_size, int fd);
int ft_isnumeric_conv(t_printf_format *format);
/* GET_NEXT_LINE*/
char *get_next_line(int fd);
/*ORIGINAL LIBFT*/
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);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, 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);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
int ft_toupper(int c);
int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
void *ft_calloc(size_t nmemb, size_t size);
char *ft_strdup(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
int max_int(int a, int b);
int min_int(int a, int b);
long max_long(long a, long b);
long min_long(long a, long b);
/*BONUSLIBFT*/
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

40
libft/libft/Makefile Normal file
View file

@ -0,0 +1,40 @@
CC = cc
AR = ar
ARFLAGS = rcs
CFLAGS = -Wall -Wextra -Werror
NAME = libft.a
SOURCES = ft_atoi.c ft_itoa.c ft_putendl_fd.c ft_strlcat.c ft_substr.c \
ft_bzero.c ft_putnbr_fd.c ft_strlcpy.c ft_tolower.c ft_calloc.c \
ft_memchr.c ft_putstr_fd.c ft_strlen.c ft_toupper.c ft_isalnum.c \
ft_memcmp.c ft_split.c ft_strmapi.c ft_isalpha.c ft_memcpy.c ft_strchr.c \
ft_strncmp.c ft_isascii.c ft_memmove.c ft_strdup.c ft_strnstr.c \
ft_isdigit.c ft_memset.c ft_striteri.c ft_strrchr.c ft_isprint.c \
ft_putchar_fd.c ft_strjoin.c ft_strtrim.c ft_min_max.c
BONUSSOURCES = ft_lstnew_bonus.c ft_lstadd_back_bonus.c ft_lstsize_bonus.c \
ft_lstadd_front_bonus.c ft_lstclear_bonus.c ft_lstdelone_bonus.c \
ft_lstiter_bonus.c ft_lstlast_bonus.c ft_lstmap_bonus.c
BONUSOBJ = $(BONUSSOURCES:.c=.o)
OBJECTS = $(SOURCES:.c=.o)
all: $(NAME)
$(NAME): bonus
#$(NAME): $(OBJECTS)
# $(AR) $(ARFLAGS) $@ $^
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -f $(OBJECTS) $(BONUSOBJ)
fclean: clean
rm -f $(NAME)
re: fclean all
bonus: $(OBJECTS) $(BONUSOBJ)
$(AR) $(ARFLAGS) $(NAME) $^
.PHONY: all clean fclean re bonus

63
libft/libft/ft_atoi.c Normal file
View file

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/22 13:39:57 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int ft_isspace(char c)
{
return ((c == ' ' || c == '\f' || c == '\n'
|| c == '\r' || c == '\t' || c == '\v'));
}
int ft_atoi(const char *nptr)
{
int sign;
int calc;
sign = 1;
calc = 0;
while (*nptr && ft_isspace(*nptr))
nptr++;
if (*nptr == '+' || *nptr == '-')
{
sign = 1 - 2 * (*nptr == '-');
nptr++;
}
while (*nptr && ft_isdigit(*nptr))
{
calc = calc * 10 + (*nptr - '0');
nptr++;
}
return (sign * calc);
}
/*
#include <stdio.h>
int main()
{
printf("%d\n", atoi("-2147483648"));
printf("%d\n", ft_atoi("-2147483648"));
printf("%d\n", atoi("-2147483649"));
printf("%d\n", ft_atoi("-2147483649"));
printf("%d\n", atoi("+2147483647"));
printf("%d\n", ft_atoi("+2147483647"));
printf("%d\n", atoi("2147483648"));
printf("%d\n", ft_atoi("2147483648"));
printf("%d\n", atoi("2147483649"));
printf("%d\n", ft_atoi("2147483649"));
printf("%d\n", atoi("0"));
printf("%d\n", ft_atoi("0"));
printf("%d\n", atoi("++2147483649"));
printf("%d\n", ft_atoi("++2147483649"));
return (0);
}
*/

25
libft/libft/ft_bzero.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:22:18 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:14:10 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <strings.h>
void ft_bzero(void *s, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
*((char *) s + i) = '\0';
i++;
}
}

34
libft/libft/ft_calloc.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/25 10:29:48 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
char *ptr;
size_t i;
size_t block;
i = 0;
if (nmemb != 0 && (SIZE_T_MAX / nmemb) < size)
return (NULL);
block = nmemb * size;
ptr = (char *)malloc(block * sizeof(char));
if (block == 0 || ptr == NULL)
return (ptr);
while (i < block)
ptr[i++] = 0;
return (ptr);
}

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

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:37:30 by jguelen #+# #+# */
/* Updated: 2024/10/22 13:50:45 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

17
libft/libft/ft_isalpha.c Normal file
View file

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 09:41:00 by jguelen #+# #+# */
/* Updated: 2024/10/23 15:57:05 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
return ((c <= 'z' && c >= 'a')
|| (c <= 'Z' && c >= 'A'));
}

16
libft/libft/ft_isascii.c Normal file
View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:38:17 by jguelen #+# #+# */
/* Updated: 2024/10/22 13:54:55 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

16
libft/libft/ft_isdigit.c Normal file
View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:11:36 by jguelen #+# #+# */
/* Updated: 2024/10/23 15:57:52 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

16
libft/libft/ft_isprint.c Normal file
View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 16:52:39 by jguelen #+# #+# */
/* Updated: 2024/10/23 15:58:31 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
return (c >= 32 && c <= 126);
}

90
libft/libft/ft_itoa.c Normal file
View file

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/23 09:15:57 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
/*Returns the size of the string needed to represent n in base 10 as a string*/
/* DOES NOT INCLUDE THE NULL BYTE*/
static size_t ft_itolen(int n)
{
size_t i;
int calc;
i = 0;
calc = n;
if (n == 0)
return (1);
while (calc != 0)
{
calc /= 10;
i++;
}
if (n > 0)
return (i);
return (i + 1);
}
char *ft_itoa(int n)
{
char *arg;
int calc;
size_t len;
calc = n;
len = ft_itolen(n);
arg = (char *)malloc((len + 1) * sizeof(char));
if (arg == NULL)
return (NULL);
arg[len--] = '\0';
if (calc == 0)
arg[0] = '0';
if (calc < 0)
{
arg[0] = '-';
arg[len--] = (-1 * (calc % 10)) + '0';
calc /= -10;
}
while (calc > 0)
{
arg[len--] = calc % 10 + '0';
calc /= 10;
}
return (arg);
}
/*
int main(void)
{
int i;
char *s;
i = -2147483648;
i = (-1 * (i % 10));
printf("Test op : %d\n", i);
i = -2147483648;
s = ft_itoa(i);
printf("%s\n", s);
free(s);
i = -2;
s = ft_itoa(i);
printf("%s\n", s);
free(s);
i = 2147483647;
s = ft_itoa(i);
printf("%s\n", s);
free(s);
i = 0;
s = ft_itoa(i);
printf("%s\n", s);
free(s);
return (0);
}*/

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 14:19:14 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Presumes lst not to be NULL and *lst not to be circular*/
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tmp;
tmp = *lst;
if (tmp == NULL)
{
*lst = new;
return ;
}
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 13:14:18 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Can create circular chain list -> be careful of the dangling part then
and new not to be NULL*/
void ft_lstadd_front(t_list **lst, t_list *new)
{
new->next = *lst;
*lst = new;
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 14:58:01 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *tmp;
if (lst == NULL)
return ;
while (*lst)
{
tmp = (*lst)->next;
del((*lst)->content);
free(*lst);
*lst = tmp;
}
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 14:44:36 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
/*Presumes del != NULL
Allows one to delete one single element of the generic chain.
The user is responsible of the dealing with any other element.*/
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (lst == NULL)
return ;
del(lst->content);
free(lst);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 15:08:39 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (lst == NULL)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 14:15:10 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Presumes the list not to be circular*/
t_list *ft_lstlast(t_list *lst)
{
if (lst == NULL)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 15:23:04 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new;
t_list *buf;
void *tmpcontent;
if (lst == NULL)
return (NULL);
tmpcontent = f(lst->content);
new = ft_lstnew(tmpcontent);
if (new == NULL)
return (del(tmpcontent), NULL);
buf = new;
while (lst->next)
{
tmpcontent = f(lst->next->content);
buf->next = ft_lstnew(tmpcontent);
if (buf->next == NULL)
{
del(tmpcontent);
return (ft_lstclear(&new, del), NULL);
}
buf = buf->next;
lst = lst->next;
}
return (new);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 13:07:05 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
t_list *ft_lstnew(void *content)
{
t_list *new;
new = (t_list *)malloc(sizeof(t_list));
if (new == NULL)
return (NULL);
new->content = content;
new->next = NULL;
return (new);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 13:54:34 by jguelen #+# #+# */
/* Updated: 2024/10/23 16:33:32 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Presumes the linked list not to be circular*/
int ft_lstsize(t_list *lst)
{
int len;
len = 1;
if (lst == NULL)
return (0);
while (lst->next)
{
len++;
lst = lst->next;
}
return (len);
}

27
libft/libft/ft_memchr.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/22 11:20:45 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *) s)[i] == (unsigned char) c)
return (&(((unsigned char *)s)[i]));
i++;
}
return (NULL);
}

25
libft/libft/ft_memcmp.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/17 15:25:56 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
return (0);
while (i < n - 1 && ((unsigned char *)s1)[i] == ((unsigned char *)s2)[i])
i++;
return (((unsigned char *)s1)[i] - ((unsigned char *) s2)[i]);
}

29
libft/libft/ft_memcpy.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:22:18 by jguelen #+# #+# */
/* Updated: 2024/10/22 13:51:17 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
/*Memory areas src and dest must not overlap*/
void *ft_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
i = 0;
if (src == dest)
return (dest);
while (i < n)
{
((char *) dest)[i] = ((char *) src)[i];
i++;
}
return (dest);
}

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

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:22:18 by jguelen #+# #+# */
/* Updated: 2024/10/22 16:08:59 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
/*Presumes dest and src not to be NULL*/
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
i = 0;
if (dest == src || n == 0)
return (dest);
if (dest < src)
{
while (i < n)
{
((char *) dest)[i] = ((char *) src)[i];
i++;
}
}
else
{
i = n - 1;
while (i != 0)
{
((char *) dest)[i] = ((char *) src)[i];
i--;
}
((char *) dest)[i] = ((char *) src)[i];
}
return (dest);
}

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

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 17:24:13 by jguelen #+# #+# */
/* Updated: 2024/10/22 10:49:13 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
void *ft_memset(void *s, int c, size_t n)
{
unsigned int i;
i = 0;
while (i < n)
{
*((char *)s + i) = (char) c;
i++;
}
return (s);
}

41
libft/libft/ft_min_max.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_min_max.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/04 11:01:52 by jguelen #+# #+# */
/* Updated: 2024/12/04 11:02:08 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int max_int(int a, int b)
{
if (a > b)
return (a);
return (b);
}
int min_int(int a, int b)
{
if (a < b)
return (a);
return (b);
}
long max_long(long a, long b)
{
if (a > b)
return (a);
return (b);
}
long min_long(long a, long b)
{
if (a < b)
return (a);
return (b);
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/20 16:54:01 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
/*
int main(void)
{
ft_putchar_fd('\t', 1);
ft_putchar_fd('1', 1);
ft_putchar_fd('\n', 1);
}*/

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/20 17:03:26 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
void ft_putendl_fd(char *s, int fd)
{
ft_putstr_fd(s, fd);
write(fd, "\n", 1);
}

View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/21 12:37:18 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
#include <stdio.h>
static int ft_getnbdigits(int n)
{
int len;
len = 0;
if (n == 0)
return (1);
while (n != 0)
{
len++;
n /= 10;
}
return (len);
}
/*Doesn't handle negative powers and doesn't need to*/
static int ft_pow(int n, int power)
{
int calc;
int i_pow;
calc = 1;
i_pow = 1;
if (power < 0)
return (0);
while (i_pow++ <= power)
calc *= n;
return (calc);
}
/*Does not handle INT_MIN and doesn't need to*/
static int ft_abs(int n)
{
if (n < 0)
n *= -1;
return (n);
}
void ft_putnbr_fd(int n, int fd)
{
int len;
char c;
len = ft_getnbdigits(n);
if (n < 0)
write(fd, "-", 1);
while (len > 0)
{
c = ft_abs((n / ft_pow(10, --len)) % 10) + '0';
write(fd, &c, 1);
}
}
/*
int main(void)
{
int i;
i = 8;
ft_putnbr_fd(i, 1);
write(1, "\n", 1);
i = -2147483648;
ft_putnbr_fd(i, 1);
write(1, "\n", 1);
i = 2147483647;
ft_putnbr_fd(i, 1);
write(1, "\n", 1);
i = 0;
ft_putnbr_fd(i, 1);
write(1, "\n", 1);
return (0);
}*/

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/20 16:59:56 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
write(fd, s, ft_strlen(s));
}
/*
int main(void)
{
ft_putstr_fd("", 1);
return (0);
}*/

109
libft/libft/ft_split.c Normal file
View file

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/22 16:44:25 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
/* Presumes s not to be NULL
Returns the number of non-empty c separated words in s*/
static size_t ft_word_count(const char *s, char c)
{
size_t count;
int flag;
count = 0;
flag = 1;
while (*s)
{
if (flag && *s != c)
{
flag = 0;
count++;
}
if (!flag && *s == c)
flag = 1;
s++;
}
return (count);
}
static size_t ft_firstwordlen(const char *s, char c)
{
size_t len;
len = 0;
while (*s == c)
s++;
while (*s && *s != c)
{
len++;
s++;
}
return (len);
}
/*Frees the first i cells of split array then the array itself*/
static void ft_freesplit(char **split_array, size_t i)
{
size_t j;
j = 0;
while (j < i)
free(split_array[j++]);
free(split_array);
}
static char *ft_strdupandmove(char **s, char c)
{
char *copy;
size_t len;
size_t i;
while (**s && **s == c)
(*s)++;
len = ft_firstwordlen(*s, c);
copy = (char *) malloc((len + 1) * sizeof(char));
if (copy == NULL)
return (NULL);
i = 0;
while (i < len)
{
copy[i++] = **s;
(*s)++;
}
copy[i] = '\0';
return (copy);
}
char **ft_split(char const *s, char c)
{
char **split_array;
size_t wc;
size_t i;
wc = ft_word_count(s, c);
split_array = (char **) malloc((wc + 1) * sizeof(char *));
if (split_array == NULL)
return (NULL);
while (*s == c)
s++;
i = 0;
while (i < wc)
{
split_array[i] = ft_strdupandmove((char **)&s, c);
if (split_array[i] == NULL)
return (ft_freesplit(split_array, i), NULL);
i++;
}
split_array[i] = NULL;
return (split_array);
}

27
libft/libft/ft_strchr.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:16:33 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
/*Presumes s not to be NULL*/
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*s == (char) c)
return ((char *)s);
s++;
}
if ((char) c == *s)
return ((char *)s);
return (NULL);
}

43
libft/libft/ft_strdup.c Normal file
View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/24 17:07:33 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *ft_strdup(const char *s)
{
char *copy;
size_t len;
copy = NULL;
len = 0;
while (s[len])
len ++;
copy = (char *) malloc((len + 1) * sizeof(char));
if (copy == NULL)
return (NULL);
len = 0;
while (s[len])
{
copy[len] = s[len];
len++;
}
copy[len] = '\0';
return (copy);
}
/*
int main(void)
{
printf("%s\n", strdup("No problem Sir."));
return (0);
}*/

26
libft/libft/ft_striteri.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:02:26 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Presumes s != NULL*/
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
i = 0;
while (s[i])
{
(*f)(i, &s[i]);
i++;
}
}

42
libft/libft/ft_strjoin.c Normal file
View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:07:41 by jguelen #+# #+# */
/* Updated: 2024/10/22 11:47:38 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *join;
size_t ljoin;
size_t i;
size_t j;
ljoin = ft_strlen(s1) + ft_strlen(s2) + 1;
join = (char *) malloc(ljoin * sizeof(char));
i = 0;
j = 0;
if (join == NULL)
return (NULL);
while (s1[i])
{
join[i] = s1[i];
i++;
}
while (s2[j])
{
join[i + j] = s2[j];
j++;
}
join[i + j] = '\0';
return (join);
}

46
libft/libft/ft_strlcat.c Normal file
View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:22:18 by jguelen #+# #+# */
/* Updated: 2024/10/24 14:00:34 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <string.h>
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t i;
size_t dlen;
size_t slen;
dlen = ft_strlen(dst);
slen = ft_strlen(src);
if (dlen >= size)
return (size + slen);
i = dlen;
while (src[i - dlen] && i < size - 1)
{
dst[i] = src[i - dlen];
i++;
}
dst[i] = '\0';
return (dlen + slen);
}
/*
#include <stdio.h>
#include <bsd/string.h>
int main (void)
{
char buff1[123] = "abcd\0";
char buff2[123] = "abcd\0";
printf("\n~%zu~\n", strlcat(buff1, buff1+1, 5));
printf("\n~%zu~\n", ft_strlcat(buff2, buff2+1, 5));
return (0);
}*/

47
libft/libft/ft_strlcpy.c Normal file
View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:22:18 by jguelen #+# #+# */
/* Updated: 2024/10/24 13:51:46 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <string.h>
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
size_t ls;
i = 0;
ls = ft_strlen(src);
if (size == 0)
return (ls);
if (dst != src)
{
while (src[i] && i < size - 1)
{
dst[i] = src[i];
i++;
}
}
dst[i] = '\0';
return (ls);
}
/*
#include <stdio.h>
#include <bsd/string.h>
int main (void)
{
char buff1[] = "abcd\0";
char buff2[] = "abcd\0";
printf("\n~%zu~\n", strlcpy(buff1, buff1+1, 3));
printf("\n~%zu~\n", ft_strlcpy(buff2, buff2+1, 3));
return (0);
}*/

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

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 17:07:40 by jguelen #+# #+# */
/* Updated: 2025/02/04 10:23:44 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
if (!s)
return (len);
while (s[len])
len++;
return (len);
}

48
libft/libft/ft_strmapi.c Normal file
View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/23 10:06:54 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
#include <stdio.h>
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *new;
unsigned int i;
size_t len;
len = ft_strlen(s);
new = (char *)malloc((len + 1) * sizeof(char));
if (new == NULL)
return (NULL);
i = 0;
while (i < len)
{
new[i] = (*f)(i, s[i]);
i++;
}
new[i] = '\0';
return (new);
}
/*
char ft_rot13(unsigned int i, char c)
{
if (ft_isprint(c))
return (((i - i + 13 + c - 32) % (127 - 32) + 32));
return (c);
}
int main(void)
{
printf("%s\n", ft_strmapi("Hello World! 248", ft_rot));
return (0);
}*/

25
libft/libft/ft_strncmp.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:18:01 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
return (0);
while (s1[i] && s1[i] == s2[i] && i < n - 1)
i++;
return ((unsigned char) s1[i] - (unsigned char) s2[i]);
}

49
libft/libft/ft_strnstr.c Normal file
View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/23 09:47:48 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
/*little is presumed not to be NULL here. Returns 1 if big starts with little.*/
static int ft_strncheck(const char *big, const char *little, size_t len)
{
size_t i;
i = 0;
while (little[i] && big[i] && i < len)
{
if (big[i] != little[i])
return (0);
i++;
}
if (!little[i])
return (1);
return (0);
}
/*Removed NULL entry protection because it needs to crash apparently*/
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
i = 0;
if (*little == '\0')
return ((char *)big);
if (len == 0)
return (NULL);
while (big[i] && i < len)
{
if (ft_strncheck((big + i), little, len - i))
return ((char *)big + i);
i++;
}
return (NULL);
}

30
libft/libft/ft_strrchr.c Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:34:03 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:17:14 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
/*Presumes s not to be NULL*/
char *ft_strrchr(const char *s, int c)
{
char *s2;
s2 = NULL;
while (*s)
{
if (*s == (char) c)
s2 = (char *)s;
s++;
}
if (*s == (char) c)
s2 = (char *)s;
return (s2);
}

84
libft/libft/ft_strtrim.c Normal file
View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 16:29:21 by jguelen #+# #+# */
/* Updated: 2024/10/23 13:15:24 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
#include <stdio.h>
static int ft_ischarinset(char c, const char *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
/*til_trim is never used with NULL*/
static char *til_trim(const char *s1, const char *set)
{
char *valid_end;
valid_end = (char *)s1;
while (*s1)
{
if (!ft_ischarinset(*s1, set))
valid_end = (char *)s1;
s1++;
}
return (valid_end);
}
/* '\0' is presupposed not to be taken into account in set
Check if NULL protection of s1 needed or wanted*/
char *ft_strtrim(char const *s1, char const *set)
{
char *trim;
size_t trim_len;
size_t i;
i = 0;
if (s1 == NULL)
return (NULL);
while (*s1 && ft_ischarinset(*s1, set))
s1++;
trim = til_trim(s1, set);
trim_len = (size_t)(trim - s1) + 1;
if (*s1 == '\0')
trim_len = 0;
trim = (char *) malloc((trim_len + 1) * sizeof(char));
if (trim == NULL)
return (NULL);
while (i < trim_len)
{
trim[i] = s1[i];
i++;
}
trim[i] = '\0';
return (trim);
}
/*
int main(void)
{
char *s;
char *s1;
char *set;
s = "cabcdccc";
s1 = "";
set = "c";
printf("%s\n", ft_strtrim(s, set));
printf("%s\n", ft_strtrim(s1, set));
return (0);
}*/

48
libft/libft/ft_substr.c Normal file
View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 15:33:32 by jguelen #+# #+# */
/* Updated: 2024/10/23 12:11:02 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static size_t ft_min(size_t a, size_t b)
{
if (a < b)
return (a);
return (b);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t sublen;
size_t i;
if (start > ft_strlen(s))
{
sub = (char *)malloc(sizeof(char));
if (sub)
sub[0] = '\0';
return (sub);
}
sublen = ft_min(ft_strlen(s + start), len);
sub = (char *)malloc((sublen + 1) * sizeof(char));
if (sub == NULL)
return (NULL);
i = 0;
while (i < sublen)
{
sub[i] = s[start + i];
i++;
}
sub[i] = '\0';
return (sub);
}

20
libft/libft/ft_tolower.c Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 12:04:22 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:15:35 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <ctype.h>
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A';
return (c);
}

20
libft/libft/ft_toupper.c Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:22:18 by jguelen #+# #+# */
/* Updated: 2024/10/22 14:14:55 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <ctype.h>
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
return (c);
}

77
libft/libft/libft.h Normal file
View file

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
/* Updated: 2024/12/11 14:27:45 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <unistd.h>
# include <ctype.h>
# include <string.h>
# include <strings.h>
# include <stdlib.h>
# define SIZE_T_MAX 0xFFFFFFFFFFFFFFFF
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
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);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, 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);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
int ft_toupper(int c);
int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
void *ft_calloc(size_t nmemb, size_t size);
char *ft_strdup(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
int max_int(int a, int b);
int min_int(int a, int b);
long max_long(long a, long b);
long min_long(long a, long b);
/*BONUS*/
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