skeletton untested project

This commit is contained in:
thrieg 2025-12-11 06:18:16 +01:00
commit 6fc620e8f4
187 changed files with 6584 additions and 0 deletions

View file

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arglist_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/27 17:04:47 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:39 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
static void ft_set_default_flag_values(t_arglist *arglist)
{
arglist->width_mini = 0;
arglist->precision = -1;
arglist->flag_hashtag = 0;
arglist->flag_zero = 0;
arglist->flag_justified_left = 0;
arglist->flag_space = 0;
arglist->flag_plus = 0;
}
static void ft_handle_overriding_arguments(t_arglist *arglist)
{
if ((arglist->precision >= 0) || arglist->flag_justified_left)
arglist->flag_zero = 0;
if (arglist->flag_plus)
arglist->flag_space = 0;
}
static void ft_handle_width_precision(
t_arglist *arglist,
int *i,
const char *str,
va_list *args)
{
if (ft_isdigit(str[*i]))
{
arglist->width_mini = ft_atoi(&str[*i], NULL);
while (ft_isdigit(str[*i]))
(*i)++;
}
else if (str[*i] == '*')
{
arglist->width_mini = va_arg(*args, int);
(*i)++;
}
if (str[*i] == '.')
{
(*i)++;
if (str[*i] == '*')
{
arglist->precision = va_arg(*args, int);
(*i)++;
}
else
arglist->precision = ft_atoi(&str[*i], NULL);
while (ft_isdigit(str[*i]))
(*i)++;
}
}
static void ft_handle_flags(t_arglist *arglist, int *i, const char *str)
{
while (str[*i] == '#' || str[*i] == '0' || str[*i] == '-'
|| str[*i] == ' ' || str[*i] == '+')
{
if (str[*i] == '#')
arglist->flag_hashtag = 1;
if (str[*i] == '0')
arglist->flag_zero = 1;
if (str[*i] == '-')
arglist->flag_justified_left = 1;
if (str[*i] == ' ')
arglist->flag_space = 1;
if (str[*i] == '+')
arglist->flag_plus = 1;
(*i)++;
}
}
t_arglist *ft_create_arglist(const char *str, va_list *args)
{
t_arglist *arglist;
int i;
arglist = malloc(sizeof(t_arglist));
if (!arglist)
return (NULL);
ft_set_default_flag_values(arglist);
i = 0;
if (str[i] == '%')
i++;
ft_handle_flags(arglist, &i, str);
ft_handle_width_precision(arglist, &i, str, args);
arglist->size_of_argument_string = i;
ft_handle_overriding_arguments(arglist);
return (arglist);
}

Binary file not shown.

View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_d_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 17:12:38 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:44 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
char *init_str_to_cat(t_arglist *arglist, unsigned int nbr);
void convert_precision_if_flag_zero(t_arglist *arglist, int nbr);
int ft_handle_if_precision(
t_arglist *arglist,
int nbr,
t_vector *vec,
int *size);
int ft_handle_if_not_precision(
t_arglist *arglist,
int nbr,
t_vector *vec,
int *size);
int ft_handle_concat_and_padding(
t_arglist *arglist,
char *str_to_cat,
t_vector *vec,
int size);
//flags to handle: zero, space, plus, precision, justified left... hard mode
//flag zero padding without precision behaves just like a precision
//(zeroes after the sign) and when it's spaces they're before the sign
int ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
char *str_to_cat;
int nbr;
int size;
nbr = va_arg(*args, int);
str_to_cat = init_str_to_cat(arglist, nbr);
if (!str_to_cat)
return (0);
convert_precision_if_flag_zero(arglist, nbr);
size = (int)ft_strlen(str_to_cat);
if (nbr < 0)
size--;
if ((size < arglist->precision) && (arglist->precision >= 0))
{
if (!ft_handle_if_precision(arglist, nbr, vec, &size))
return (free(str_to_cat), 0);
}
else
{
if (!ft_handle_if_not_precision(arglist, nbr, vec, &size))
return (free(str_to_cat), 0);
}
if (!ft_handle_concat_and_padding(arglist, str_to_cat, vec, size))
return (free(str_to_cat), 0);
return (free(str_to_cat), 1);
}

Binary file not shown.

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_d_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/27 15:59:04 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:45 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
char *init_str_to_cat(t_arglist *arglist, unsigned int nbr)
{
if (nbr == 0 && arglist->precision == 0)
return (ft_strdup(""));
return (ft_itoa(nbr));
}
void convert_precision_if_flag_zero(t_arglist *arglist, int nbr)
{
if ((arglist->precision < 0) && !(arglist->flag_justified_left)
&& arglist->flag_zero)
{
arglist->precision = arglist->width_mini;
arglist->width_mini = 0;
if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
arglist->precision--;
}
}
int ft_handle_if_precision(
t_arglist *arglist,
int nbr,
t_vector *vec,
int *size)
{
if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
{
if (padding_if_needed_before(arglist, 'd',
arglist->precision + 1, vec) < 0)
return (0);
}
else
if (padding_if_needed_before(arglist, 'd', arglist->precision, vec) < 0)
return (0);
if (arglist->flag_plus && (nbr >= 0) && !ft_vector_pushback(vec, '+'))
return (0);
if (arglist->flag_space && (nbr >= 0) && !(arglist->flag_plus) && nbr >= 0)
if (!ft_vector_pushback(vec, ' '))
return (0);
if (nbr < 0 && !ft_vector_pushback(vec, '-'))
return (0);
while ((*size) < arglist->precision)
{
if (!ft_vector_pushback(vec, '0'))
return (0);
(*size)++;
}
if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
(*size)++;
return (1);
}
int ft_handle_if_not_precision(
t_arglist *arglist,
int nbr,
t_vector *vec,
int *size)
{
if (nbr < 0 || arglist->flag_space || arglist->flag_plus)
(*size)++;
if (padding_if_needed_before(arglist, 'd', (*size), vec) < 0)
return (0);
if (arglist->flag_plus && (nbr >= 0))
if (!ft_vector_pushback(vec, '+'))
return (0);
if (arglist->flag_space && (nbr >= 0) && !(arglist->flag_plus))
if (nbr >= 0)
if (!ft_vector_pushback(vec, ' '))
return (0);
if (nbr < 0)
if (!ft_vector_pushback(vec, '-'))
return (0);
return (1);
}
int ft_handle_concat_and_padding(
t_arglist *arglist,
char *str_to_cat,
t_vector *vec,
int size)
{
if (!ft_vector_concat(vec, str_to_cat + ft_strnonchr(str_to_cat, '-'),
ft_strlen(str_to_cat) - ft_strnonchr(str_to_cat, '-')))
return (0);
if (padding_if_needed_after(arglist, 'd', size, vec) < 0)
return (0);
return (1);
}

Binary file not shown.

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_lowx_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 17:09:37 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:46 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
static int handle_hashtag_prefix(
t_vector *vec,
t_arglist *arglist,
unsigned int nbr,
int flag_zero)
{
if (arglist->flag_hashtag && !flag_zero && nbr != 0)
{
if (!ft_vector_concat(vec, "0x", 2))
return (0);
}
return (1);
}
static int apply_precision_padding(t_vector *vec, size_t *size, int precision)
{
while ((int)(*size)++ < precision)
{
if (!ft_vector_pushback(vec, '0'))
return (0);
}
(*size)--;
return (1);
}
static char *init_str_to_cat(t_arglist *arglist, unsigned int nbr)
{
if (nbr == 0 && arglist->precision == 0)
return (ft_strdup(""));
return (ft_utoa_base(nbr, "0123456789abcdef"));
}
static int handle_padding_and_prefix(
t_vector *vec,
t_arglist *arglist,
size_t size,
unsigned int nbr)
{
if (arglist->flag_hashtag && arglist->flag_zero && nbr != 0)
{
if (!ft_vector_concat(vec, "0x", 2))
return (0);
}
if (((int)size < arglist->precision) && (arglist->precision >= 0))
{
if (padding_if_needed_before(arglist, 'x', arglist->precision
+ (arglist->flag_hashtag * 2 * (nbr != 0)), vec) < 0)
return (0);
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
return (0);
}
else
{
if (arglist->flag_hashtag && nbr != 0)
size += 2;
if (padding_if_needed_before(arglist, 'x', size, vec) < 0)
return (0);
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
return (0);
}
return (1);
}
int ft_case_x_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
char *str_to_cat;
size_t size;
unsigned int nbr;
nbr = va_arg(*args, unsigned int);
str_to_cat = init_str_to_cat(arglist, nbr);
if (!str_to_cat)
return (0);
size = ft_strlen(str_to_cat);
if (!handle_padding_and_prefix(vec, arglist, size, nbr))
return (free(str_to_cat), 0);
if (((int)size < arglist->precision) && (arglist->precision >= 0))
{
if (!apply_precision_padding(vec, &size, arglist->precision))
return (free(str_to_cat), 0);
}
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
if (padding_if_needed_after(arglist, 'x', size, vec) < 0)
return (0);
return (1);
}

Binary file not shown.

View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_p_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 16:22:40 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:47 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
//zero and precision flags have undefined behavior here
int ft_case_p_bonus_utils(t_vector *vec, char *str_to_cat, t_arglist *arglist)
{
size_t index;
size_t size;
index = ft_strnonchr(str_to_cat, '0');
size = ft_strlen(str_to_cat + index);
if (padding_if_needed_before(arglist, 'p', (size + 2), vec) < 0)
return (0);
if (!ft_vector_concat(vec, "0x", 2))
return (0);
if (!ft_vector_concat(vec, (str_to_cat + index), size))
return (0);
if (padding_if_needed_after(arglist, 'p', (size + 2), vec) < 0)
return (0);
return (1);
}
//zero and precision flags have undefined behavior here
int ft_case_p_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
char *str_to_cat;
void *arg;
int ret;
arg = va_arg(*args, void *);
if (!arg)
{
if (padding_if_needed_before(arglist, 'p', 5, vec) < 0)
return (0);
if (!ft_vector_concat(vec, "(nil)", 5))
return (0);
if (padding_if_needed_after(arglist, 'p', 5, vec) < 0)
return (0);
return (1);
}
str_to_cat = ft_addr_to_strhex(arg);
if (!str_to_cat)
return (0);
ret = ft_case_p_bonus_utils(vec, str_to_cat, arglist);
return (free(str_to_cat), ret);
}

Binary file not shown.

View file

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_s_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 17:03:40 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:48 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
static int ft_s_sub_bonus(t_vector *vec, char *str_to_cat, t_arglist *arglist)
{
size_t size;
if (!str_to_cat)
{
size = 6;
if ((int)size > arglist->precision && (arglist->precision >= 0))
size = 0;
if (padding_if_needed_before(arglist, 's', size, vec) < 0)
return (0);
if (!ft_vector_concat(vec, "(null)", size))
return (0);
}
else
{
size = ft_strlen(str_to_cat);
if ((int)size > arglist->precision && (arglist->precision >= 0))
size = arglist->precision;
if (padding_if_needed_before(arglist, 's', size, vec) < 0)
return (0);
if (!ft_vector_concat(vec, str_to_cat, size))
return (0);
}
if (padding_if_needed_after(arglist, 's', size, vec) < 0)
return (0);
return (1);
}
//flag 0 has undefined behavior with c so the minimum width has to be spaces
int ft_case_s_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
char *str_to_cat;
str_to_cat = va_arg(*args, char *);
return (ft_s_sub_bonus(vec, str_to_cat, arglist));
}

Binary file not shown.

View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_u_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 17:07:14 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:49 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
static int ft_handle_padding_precision_bonus(
t_vector *vec,
size_t size,
t_arglist *arglist,
int padding_length)
{
if (padding_if_needed_before(arglist, 'u', padding_length, vec) < 0)
return (0);
while ((int)size++ < arglist->precision)
{
if (!ft_vector_pushback(vec, '0'))
return (0);
}
size--;
return (1);
}
// Plus and space flags have undefined behavior here
int ft_case_u_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
char *str_to_cat;
int size;
unsigned int nbr;
int padding_length;
nbr = va_arg(*args, unsigned int);
if (nbr == 0 && arglist->precision == 0)
str_to_cat = ft_strdup("");
else
str_to_cat = ft_utoa(nbr);
if (!str_to_cat)
return (0);
size = (int)ft_strlen(str_to_cat);
if (size < arglist->precision)
padding_length = arglist->precision;
else
padding_length = size;
if (!ft_handle_padding_precision_bonus(vec, size, arglist, padding_length))
return (free(str_to_cat), 0);
if (!ft_vector_concat(vec, str_to_cat, size))
return (free(str_to_cat), 0);
if (padding_if_needed_after(arglist, 'u', padding_length, vec) < 0)
return (free(str_to_cat), 0);
return (free(str_to_cat), 1);
}

Binary file not shown.

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_case_uppx_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 17:11:07 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:50 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
static int handle_hashtag_prefix(
t_vector *vec,
t_arglist *arglist,
unsigned int nbr,
int flag_zero)
{
if (arglist->flag_hashtag && !flag_zero && nbr != 0)
{
if (!ft_vector_concat(vec, "0X", 2))
return (0);
}
return (1);
}
static int apply_precision_padding(t_vector *vec, size_t *size, int precision)
{
while ((int)(*size)++ < precision)
{
if (!ft_vector_pushback(vec, '0'))
return (0);
}
(*size)--;
return (1);
}
static char *init_str_to_cat(t_arglist *arglist, unsigned int nbr)
{
if (nbr == 0 && arglist->precision == 0)
return (ft_strdup(""));
return (ft_utoa_base(nbr, "0123456789ABCDEF"));
}
static int handle_padding_and_prefix(
t_vector *vec,
t_arglist *arglist,
size_t size,
unsigned int nbr)
{
if (arglist->flag_hashtag && arglist->flag_zero && nbr != 0)
{
if (!ft_vector_concat(vec, "0X", 2))
return (0);
}
if (((int)size < arglist->precision) && (arglist->precision >= 0))
{
if (padding_if_needed_before(arglist, 'X', arglist->precision
+ (arglist->flag_hashtag * 2 * (nbr != 0)), vec) < 0)
return (0);
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
return (0);
}
else
{
if (arglist->flag_hashtag && nbr != 0)
size += 2;
if (padding_if_needed_before(arglist, 'X', size, vec) < 0)
return (0);
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
return (0);
}
return (1);
}
int ft_case_upperx_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
char *str_to_cat;
size_t size;
unsigned int nbr;
nbr = va_arg(*args, unsigned int);
str_to_cat = init_str_to_cat(arglist, nbr);
if (!str_to_cat)
return (0);
size = ft_strlen(str_to_cat);
if (!handle_padding_and_prefix(vec, arglist, size, nbr))
return (free(str_to_cat), 0);
if (((int)size < arglist->precision) && (arglist->precision >= 0))
{
if (!apply_precision_padding(vec, &size, arglist->precision))
return (free(str_to_cat), 0);
}
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
if (padding_if_needed_after(arglist, 'X', size, vec) < 0)
return (0);
return (1);
}

Binary file not shown.

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cases_easy_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 13:03:05 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:51 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int padding_if_needed_before(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
int padding_if_needed_after(
t_arglist *arglist,
char caller,
size_t len,
t_vector *vec);
//flag 0 has undefined behavior with c so the minimum width has to be spaces
int ft_case_c_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
if (padding_if_needed_before(arglist, 'c', 1, vec) < 0)
return (0);
if (!ft_vector_pushback(vec, (char)va_arg(*args, int)))
return (0);
if (padding_if_needed_after(arglist, 'c', 1, vec) < 0)
return (0);
return (1);
}
int ft_case_percent_bonus(t_vector *vec)
{
if (!ft_vector_pushback(vec, '%'))
return (0);
return (1);
}
int ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_i_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
return (ft_case_d_bonus(vec, args, arglist));
}

Binary file not shown.

View file

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cases_mandatory_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 18:52:37 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:52 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
int ft_case_c(t_vector *vec, va_list *args)
{
if (!ft_vector_pushback(vec, (char)va_arg(*args, int)))
return (0);
return (1);
}
int ft_case_percent(t_vector *vec)
{
if (!ft_vector_pushback(vec, '%'))
return (0);
return (1);
}
int ft_case_s(t_vector *vec, va_list *args)
{
char *str_to_cat;
str_to_cat = va_arg(*args, char *);
if (!str_to_cat)
{
if (!ft_vector_concat(vec, "(null)", 6))
return (free(str_to_cat), 0);
}
else
{
str_to_cat = ft_strdup(str_to_cat);
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
}
return (1);
}
int ft_case_p(t_vector *vec, va_list *args)
{
char *str_to_cat;
void *arg;
size_t index;
arg = va_arg(*args, void *);
if (!arg)
{
if (!ft_vector_concat(vec, "(nil)", 5))
return (0);
return (1);
}
if (!ft_vector_concat(vec, "0x", 2))
return (0);
str_to_cat = ft_addr_to_strhex(arg);
if (!str_to_cat)
return (0);
index = ft_strnonchr(str_to_cat, '0');
if (!ft_vector_concat(vec, str_to_cat + index,
ft_strlen(str_to_cat) - index))
return (free(str_to_cat), 0);
free(str_to_cat);
return (1);
}
int ft_case_u(t_vector *vec, va_list *args)
{
char *str_to_cat;
str_to_cat = ft_utoa(va_arg(*args, unsigned int));
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
return (1);
}

Binary file not shown.

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cases_mandatory_two.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 18:52:33 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:58 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
int ft_case_x(t_vector *vec, va_list *args)
{
char *str_to_cat;
str_to_cat = ft_utoa_base(va_arg(*args, unsigned int), "0123456789abcdef");
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
return (1);
}
int ft_case_upperx(t_vector *vec, va_list *args)
{
char *str_to_cat;
str_to_cat = ft_utoa_base(va_arg(*args, unsigned int), "0123456789ABCDEF");
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
return (1);
}
int ft_case_d(t_vector *vec, va_list *args)
{
char *str_to_cat;
str_to_cat = ft_itoa(va_arg(*args, int));
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
return (1);
}
int ft_case_i(t_vector *vec, va_list *args)
{
char *str_to_cat;
str_to_cat = ft_itoa(va_arg(*args, int));
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
return (free(str_to_cat), 0);
free(str_to_cat);
return (1);
}

Binary file not shown.

View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_padding_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/27 16:54:14 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:07:59 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
//len is the len of what's already printed
//returns the number of characters writen or -1 if an error occurs.
int padding_if_needed_before(t_arglist *arglist,
char caller,
size_t len,
t_vector *vec)
{
char padding_char;
int i;
int padding_len;
padding_len = arglist->width_mini - (int)len;
if (padding_len <= 0)
return (0);
if (arglist->flag_justified_left)
return (0);
if ((arglist->flag_zero == 1) && caller != 'c' && caller != 's'
&& caller != 'p' && (arglist->precision < 0))
padding_char = '0';
else
padding_char = ' ';
i = 0;
while (i++ < padding_len)
if (!ft_vector_pushback(vec, padding_char))
return (-1);
return (padding_len);
}
//len is the len of what's already printed
//returns the number of characters writen or -1 if an error occurs.
int padding_if_needed_after(t_arglist *arglist,
char caller,
size_t len,
t_vector *vec)
{
int i;
int padding_len;
(void)caller;
padding_len = arglist->width_mini - (int)len;
if (padding_len <= 0)
return (0);
if (!(arglist->flag_justified_left))
return (0);
i = 0;
while (i++ < padding_len)
if (!ft_vector_pushback(vec, ' '))
return (-1);
return (padding_len);
}

Binary file not shown.

126
libft/ft_printf/ft_printf.c Executable file
View file

@ -0,0 +1,126 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 18:24:04 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:08:03 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
int ft_case_c(t_vector *vec, va_list *args);
int ft_case_percent(t_vector *vec);
int ft_case_s(t_vector *vec, va_list *args);
int ft_case_p(t_vector *vec, va_list *args);
int ft_case_u(t_vector *vec, va_list *args);
int ft_case_x(t_vector *vec, va_list *args);
int ft_case_upperx(t_vector *vec, va_list *args);
int ft_case_d(t_vector *vec, va_list *args);
int ft_case_i(t_vector *vec, va_list *args);
int handle_percent_bonus(const char *str, t_vector *vec, va_list *args);
//properly frees the vector and return string
void ft_cleanup(t_vector **vec, va_list *args)
{
ft_free_vector(vec);
va_end(*args);
}
//returns the number of characters to skip after the % sign
//or 0 if something goes wrong
int handle_percent(const char *str, t_vector *vec, va_list *args)
{
if (str[0] == '%' && str[1] == 'c')
return (ft_case_c(vec, args));
if (str[0] == '%' && str[1] == '%')
return (ft_case_percent(vec));
if (str[0] == '%' && str[1] == 's')
return (ft_case_s(vec, args));
if (str[0] == '%' && str[1] == 'p')
return (ft_case_p(vec, args));
if (str[0] == '%' && str[1] == 'u')
return (ft_case_u(vec, args));
if (str[0] == '%' && str[1] == 'x')
return (ft_case_x(vec, args));
if (str[0] == '%' && str[1] == 'X')
return (ft_case_upperx(vec, args));
if (str[0] == '%' && (str[1] == 'd'))
return (ft_case_d(vec, args));
if (str[0] == '%' && (str[1] == 'i'))
return (ft_case_i(vec, args));
return (handle_percent_bonus(str, vec, args));
}
static int ft_parsing_loop(
t_vector *return_vector,
const char *str,
va_list *args
)
{
char *next_percent;
int nb_character;
while (*str)
{
next_percent = ft_strchr(str, '%');
if (!next_percent)
{
next_percent = ft_strchr(str, '\0');
if (!ft_vector_concat(return_vector, str, (next_percent - str)))
return (0);
return (1);
}
if (!ft_vector_concat(return_vector, str, (next_percent - str)))
return (0);
str = next_percent;
nb_character = handle_percent(str, return_vector, args);
if (!nb_character)
return (0);
str += nb_character;
str++;
}
return (1);
}
//returns the number of characters written or -1 on errors
int ft_printf(const char *str, ...)
{
int nb_character;
t_vector *return_vector;
va_list args;
if (!str)
return (-1);
va_start(args, str);
return_vector = ft_create_vector(2);
if (!return_vector)
return (-1);
if (!ft_parsing_loop(return_vector, str, &args))
return (ft_cleanup(&return_vector, &args), -1);
nb_character = write(1, return_vector->buffer, return_vector->index);
return (ft_cleanup(&return_vector, &args), nb_character);
}
//returns the number of characters written or -1 on errors
int ft_dprintf(int fd, const char *str, ...)
{
int nb_character;
t_vector *return_vector;
va_list args;
if (!str)
return (-1);
va_start(args, str);
return_vector = ft_create_vector(2);
if (!return_vector)
return (-1);
if (!ft_parsing_loop(return_vector, str, &args))
return (ft_cleanup(&return_vector, &args), -1);
nb_character = write(fd, return_vector->buffer, return_vector->index);
return (ft_cleanup(&return_vector, &args), nb_character);
}

22
libft/ft_printf/ft_printf.h Executable file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 12:38:59 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:08:20 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include "../ft_vector.h"
int ft_printf(const char *format, ...);
int ft_dprintf(int fd, const char *format, ...);
#endif

BIN
libft/ft_printf/ft_printf.o Normal file

Binary file not shown.

139
libft/ft_printf/ft_printf_bonus.c Executable file
View file

@ -0,0 +1,139 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 12:55:41 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:08:00 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"
int ft_case_c_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_percent_bonus(t_vector *vec);
int ft_case_s_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_p_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_u_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_x_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_upperx_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int ft_case_i_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
static int ft_handle_percent_bonus_i(
const char *str,
t_vector *vec,
va_list *args,
t_arglist *arglist)
{
int offset;
offset = arglist->size_of_argument_string;
if (str[0] == 'i')
{
if (ft_case_i_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
return (free(arglist), 0);
}
static int ft_handle_percent_bonus_xd(
const char *str,
t_vector *vec,
va_list *args,
t_arglist *arglist)
{
int offset;
offset = arglist->size_of_argument_string;
if (str[0] == 'x')
{
if (ft_case_x_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
if (str[0] == 'X')
{
if (ft_case_upperx_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
if (str[0] == 'd')
{
if (ft_case_d_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
return (ft_handle_percent_bonus_i(str, vec, args, arglist));
}
static int ft_handle_percent_bonus_spu(
const char *str,
t_vector *vec,
va_list *args,
t_arglist *arglist)
{
int offset;
offset = arglist->size_of_argument_string;
if (str[0] == 's')
{
if (ft_case_s_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
if (str[0] == 'p')
{
if (ft_case_p_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
if (str[0] == 'u')
{
if (ft_case_u_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
return (ft_handle_percent_bonus_xd(str, vec, args, arglist));
}
//returns the number of characters to skip after the % sign
//or 0 if something goes wrong
int handle_percent_bonus(const char *str, t_vector *vec, va_list *args)
{
t_arglist *arglist;
int offset;
arglist = ft_create_arglist(str, args);
if (!arglist)
return (0);
offset = arglist->size_of_argument_string;
str += offset;
if (str[0] == 'c')
{
if (ft_case_c_bonus(vec, args, arglist))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
if (str[0] == '%')
{
if (ft_case_percent_bonus(vec))
return (free(arglist), offset);
else
return (free(arglist), 0);
}
return (ft_handle_percent_bonus_spu(str, vec, args, arglist));
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 13:04:14 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:08:01 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_BONUS_H
# define FT_PRINTF_BONUS_H
typedef struct s_arglist
{
int width_mini;
int precision;
int flag_hashtag;
int flag_zero;
int flag_justified_left;
int flag_space;
int flag_plus;
int size_of_argument_string;
} t_arglist;
t_arglist *ft_create_arglist(const char *str, va_list *args);
#endif

Binary file not shown.

49
libft/ft_printf/libft.h Executable file
View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 12:38:59 by thrieg #+# #+# */
/* Updated: 2025/02/16 19:08:22 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <unistd.h>
# include <limits.h>
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);
char *ft_strdup(const char *s);
char *ft_itoa(int n);
char *ft_itoa_base(int nbr, char *base);
char *ft_addr_to_strhex(void *addr);
char *ft_utoa(unsigned int n);
char *ft_utoa_base(unsigned int nbr, char *base);
size_t ft_strnonchr(char *str, char c);
#endif