remove inputs

This commit is contained in:
2024-12-02 12:32:14 +01:00
commit 60acac0231
247 changed files with 9071 additions and 0 deletions

52
2022/2/libft/ft_printf/Makefile Executable file
View File

@ -0,0 +1,52 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
# Updated: 2023/11/15 14:42:45 by tomoron ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
CC = gcc
SRCS = ft_protected_atoi.c\
ft_convert.c\
ft_isdigit.c\
ft_parse_arg.c\
ft_print_hex_ptr.c\
ft_print_int.c\
ft_print_unsigned_int.c\
ft_printf.c\
ft_putchar.c\
ft_putstr.c\
ft_strlen.c\
ft_write_str_part.c
OBJS = $(SRCS:.c=.o)
FLAGS = -Wall -Wextra -Werror
$(NAME): $(OBJS)
ar rcs $(NAME) $(OBJS)
.c.o:
$(CC) $(FLAGS) -c $< -o $@
all: $(NAME)
bonus: all
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: bonus clean all re fclean

View File

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 23:24:31 by tomoron #+# #+# */
/* Updated: 2023/11/07 23:55:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_fill_width(char c, int n)
{
int res;
res = n;
if (res < 0)
res = 0;
while (n > 0)
{
ft_putchar(c);
n--;
}
return (res);
}
size_t ft_print_char(int c, t_flags flags)
{
int res;
res = 0;
if (flags.min_width && !flags.left_justify)
res += ft_fill_width(' ', flags.min_width - 1);
res += ft_putchar((char)c);
if (flags.min_width && flags.left_justify)
res += ft_fill_width(' ', flags.min_width - 1);
return (res);
}
size_t ft_print_str(char *s, t_flags flags)
{
int nb_to_print;
int res;
res = 0;
if (!s && (flags.precision >= 6 || flags.precision == -1))
s = "(null)";
else if (!s)
s = "";
nb_to_print = ft_strlen(s);
if (flags.precision != -1 && nb_to_print > flags.precision)
nb_to_print = flags.precision;
if (flags.min_width && !flags.left_justify)
res += ft_fill_width(' ', flags.min_width - nb_to_print);
write(1, s, nb_to_print);
if (flags.min_width && flags.left_justify)
res += ft_fill_width(' ', flags.min_width - nb_to_print);
return (res + nb_to_print);
}
size_t ft_convert(char *s, t_flags flags, va_list args, int err)
{
if (*s == 'c')
return (ft_print_char(va_arg(args, int), flags));
else if (*s == 's')
return (ft_print_str(va_arg(args, char *), flags));
else if (*s == 'p')
return (ft_print_ptr(va_arg(args, void *), flags));
else if (*s == 'd' || *s == 'i')
return (ft_print_signed_int(va_arg(args, int), flags));
else if (*s == 'u')
return (ft_print_unsigned_int(va_arg(args, unsigned int), flags));
else if (*s == 'x')
return (ft_print_hex(va_arg(args, unsigned int), flags, 'L'));
else if (*s == 'X')
return (ft_print_hex(va_arg(args, unsigned int), flags, 'U'));
else if (*s == '%')
return (ft_putstr("%"));
else if (err != 2)
return (ft_putstr("%"));
else
return (0);
}

Binary file not shown.

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/13 18:10:01 by tomoron #+# #+# */
/* Updated: 2023/10/30 12:13:53 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (!(c >= '0' && c <= '9'))
return (0);
return (1);
}

Binary file not shown.

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 11:43:10 by tomoron #+# #+# */
/* Updated: 2023/11/15 14:32:59 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_is_char_in_flags(char *s, char c)
{
int i;
i = 0;
while (s[i] == '-' || s[i] == '0' || s[i] == '#'
|| s[i] == ' ' || s[i] == '+')
{
if (s[i] == c)
return (1);
i++;
}
return (0);
}
void ft_get_width_precision(char **s, va_list args, t_flags *flags, int *err)
{
if (**s == '*')
flags->min_width = va_arg(args, int);
else
flags->min_width = ft_protected_atoi(*s);
while (ft_isdigit(**s) || **s == '*')
(*s)++;
flags->precision = -1;
if (**s == '.' && *(*s + 1) == '*')
flags->precision = va_arg(args, int);
else if (**s == '.')
{
if (ft_isdigit(*(*s + 1)))
flags->precision = ft_protected_atoi(*s + 1);
else
flags->precision = 0;
(*s)++;
}
while (ft_isdigit(**s) || **s == '.' || **s == '*')
(*s)++;
if (flags->min_width == -2 || flags->precision == -2)
*err = 3;
}
void ft_get_flags(char **s, va_list args, t_flags *flags, int *err)
{
flags->left_justify = ft_is_char_in_flags(*s, '-');
flags->zero_padding = ft_is_char_in_flags(*s, '0');
flags->always_sign_number = ft_is_char_in_flags(*s, '+');
flags->blank_positive = ft_is_char_in_flags(*s, ' ');
flags->zero_x_prefix = ft_is_char_in_flags(*s, '#');
while (**s == '-' || **s == '0' || **s == '#'
|| **s == ' ' || **s == '+')
{
(*s)++;
}
return (ft_get_width_precision(s, args, flags, err));
}
size_t ft_parse_arg(char **s, va_list args, int *err)
{
t_flags flags;
size_t res;
res = 0;
if (**s == '%')
{
(*s)++;
ft_get_flags(s, args, &flags, err);
if (*err == 3)
return (0);
res += ft_convert(*s, flags, args, *err);
if (**s == 'c' || **s == 's' || **s == 'p' || **s == 'd'
|| **s == 'i' || **s == 'u' || **s == 'x'
|| **s == 'X' || **s == '%')
(*s)++;
}
return (res);
}

Binary file not shown.

View File

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_hex_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 15:58:57 by tomoron #+# #+# */
/* Updated: 2023/11/05 15:58:58 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_calc_hex_len(long unsigned int n, t_flags flags)
{
int res;
res = 1;
if (n == 0 && flags.precision == 0)
return (0);
while (n > 15)
{
res++;
n /= 16;
}
return (res);
}
void ft_put_hex(unsigned long int n, char mode)
{
if (n > 15)
ft_put_hex(n / 16, mode);
if (mode == 'L')
write(1, &"0123456789abcdef"[n % 16], 1);
if (mode == 'U')
write(1, &"0123456789ABCDEF"[n % 16], 1);
}
size_t ft_print_ptr(void *ptr, t_flags flags)
{
int len;
int res;
res = 0;
if (!ptr)
return (ft_print_str("(nil)", flags));
len = ft_calc_hex_len((long unsigned int)ptr, flags) + 2;
if (flags.min_width && !flags.left_justify)
res += ft_fill_width(' ', flags.min_width - len);
ft_putstr("0x");
ft_put_hex((unsigned long int)ptr, 'L');
if (flags.min_width && flags.left_justify)
res += ft_fill_width(' ', flags.min_width - len);
return (res + len);
}
size_t ft_write_blank(int blank_len, t_flags flags, char mode)
{
size_t res;
res = 0;
if (blank_len > 0 && ((flags.left_justify && mode == 'A')
|| (!flags.left_justify && mode == 'B')))
{
if (flags.zero_padding && flags.precision == -1)
res += ft_fill_width('0', blank_len);
else
res += ft_fill_width(' ', blank_len);
}
return (res);
}
size_t ft_print_hex(unsigned int nb, t_flags flags, char mode)
{
int blank_len;
int zero_len;
int number_len;
size_t res;
number_len = ft_calc_hex_len(nb, flags);
res = number_len;
zero_len = 0;
if (flags.precision > number_len)
zero_len = flags.precision - number_len;
blank_len = flags.min_width - zero_len - number_len;
blank_len -= flags.zero_x_prefix * 2;
res += ft_write_blank(blank_len, flags, 'B');
if (flags.zero_x_prefix && nb && mode == 'L')
res += ft_putstr("0x");
if (flags.zero_x_prefix && nb && mode == 'U')
res += ft_putstr("0X");
res += ft_fill_width('0', zero_len);
if (flags.precision != 0 || nb != 0)
ft_put_hex(nb, mode);
res += ft_write_blank(blank_len, flags, 'A');
return (res);
}

Binary file not shown.

View File

@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 15:52:46 by tomoron #+# #+# */
/* Updated: 2023/11/05 15:53:56 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_calc_signed_int_len(int nb, t_flags flags)
{
int res;
long int n;
res = 1;
n = (long int) nb;
if (flags.precision == 0 && nb == 0)
return (0);
if (n < 0)
{
n = n * -1;
}
while (n > 9)
{
res++;
n /= 10;
}
return (res);
}
size_t ft_putsign(int nb, t_flags flags, int sign)
{
if (nb < 0)
return (ft_putchar('-'));
if (sign && flags.blank_positive)
return (ft_putchar(' '));
if (sign)
return (ft_putchar('+'));
return (0);
}
int ft_print_signed_int_blank_sign(int nb, t_flags flags, size_t *res)
{
int blank_len;
int zero_len;
int number_len;
int sign;
number_len = ft_calc_signed_int_len(nb, flags);
sign = (nb < 0) || flags.always_sign_number || flags.blank_positive;
zero_len = 0;
if (flags.precision > number_len)
zero_len = flags.precision - number_len;
blank_len = flags.min_width - zero_len - number_len - sign;
if (blank_len > 0 && !flags.left_justify && ((!flags.zero_padding
&& nb != 0) || (flags.zero_padding && flags.precision != -1)))
*res += ft_fill_width(' ', blank_len);
*res += ft_putsign(nb, flags, sign);
if (blank_len > 0 && !flags.left_justify && flags.zero_padding
&& flags.precision == -1)
*res += ft_fill_width('0', blank_len);
*res += ft_fill_width('0', zero_len);
return (blank_len);
}
void ft_put_lu_nbr(long unsigned int nb)
{
if (nb > 9)
ft_put_lu_nbr(nb / 10);
ft_putchar("0123456789"[nb % 10]);
}
size_t ft_print_signed_int(int nb, t_flags flags)
{
int blank_len;
long int n;
size_t res;
res = ft_calc_signed_int_len(nb, flags);
n = (long int)nb;
if (n < 0)
n *= -1;
blank_len = ft_print_signed_int_blank_sign(nb, flags, &res);
if (flags.precision != 0 || nb != 0)
ft_put_lu_nbr((long unsigned int)n);
if (blank_len > 0 && flags.left_justify)
{
if (flags.zero_padding)
res += ft_fill_width('0', blank_len);
else
res += ft_fill_width(' ', blank_len);
}
return (res);
}

Binary file not shown.

View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_unsigned_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 16:06:48 by tomoron #+# #+# */
/* Updated: 2023/11/05 16:06:49 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_calc_unsigned_int_len(unsigned int nb, t_flags flags)
{
int res;
res = 1;
if (nb == 0 && flags.precision == 0)
return (0);
while (nb > 9)
{
res++;
nb /= 10;
}
return (res);
}
size_t ft_print_unsigned_int(unsigned int nb, t_flags flags)
{
int blank_len;
int zero_len;
int number_len;
size_t res;
number_len = ft_calc_unsigned_int_len(nb, flags);
res = number_len;
zero_len = 0;
if (flags.precision > number_len)
zero_len = flags.precision - number_len;
blank_len = flags.min_width - zero_len - number_len;
if (blank_len > 0 && !flags.left_justify && flags.zero_padding
&& flags.precision == -1)
res += ft_fill_width('0', blank_len);
else if (blank_len > 0 && !flags.left_justify)
res += ft_fill_width(' ', blank_len);
res += ft_fill_width('0', zero_len);
if (flags.precision != 0 || nb != 0)
ft_put_lu_nbr(nb);
if (blank_len > 0 && flags.left_justify && flags.zero_padding)
res += ft_fill_width('0', blank_len);
else if (blank_len > 0 && flags.left_justify)
res += ft_fill_width(' ', blank_len);
return (res);
}

Binary file not shown.

View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/02 23:41:38 by tomoron #+# #+# */
/* Updated: 2023/11/08 14:51:19 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_check_placeholders(const char *str)
{
int err;
err = 0;
while (*str)
{
while (*str && *str != '%')
str++;
if (*str == '%')
{
str++;
while (*str == '-' || *str == '0' || *str == '#'
|| *str == ' ' || *str == '+' || ft_isdigit(*str))
str++;
if (*str != 'c' && *str != 's' && *str != 'p' && *str != 'd'
&& *str && *str != 'i' && *str != 'u' && *str != 'x'
&& *str != 'X' && *str != '%')
err = 1;
if (!*str && err == 0)
err = 2;
if (*str == '%')
str++;
}
}
return (err);
}
int ft_printf(const char *str, ...)
{
va_list args;
size_t res;
char *s;
int err;
if (!str)
return (-1);
va_start(args, str);
res = 0;
s = (char *)str;
err = ft_check_placeholders(s);
res += ft_write_str_part(&s);
while (*s)
{
res += ft_parse_arg(&s, args, &err);
if (err == 3)
break ;
res += ft_write_str_part(&s);
}
va_end(args);
if (err >= 2)
return (-1);
return (res);
}

View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */
/* Updated: 2023/11/15 14:31:34 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <unistd.h>
typedef struct s_flags
{
int left_justify;
int zero_padding;
int always_sign_number;
int blank_positive;
int zero_x_prefix;
int min_width;
int precision;
} t_flags;
int ft_printf(const char *str, ...);
size_t ft_write_str_part(char **s);
size_t ft_parse_arg(char **s, va_list args, int *err);
size_t ft_convert(char *s, t_flags flags, va_list args, int err);
size_t ft_putchar(char c);
size_t ft_putstr(char *s);
size_t ft_strlen(char const *s);
int ft_protected_atoi(const char *s);
int ft_isdigit(int c);
size_t ft_print_unsigned_int(unsigned int nb, t_flags flags);
size_t ft_print_signed_int(int nb, t_flags flags);
size_t ft_fill_width(char c, int n);
size_t ft_print_hex(unsigned int nb, t_flags flags, char mode);
size_t ft_print_ptr(void *ptr, t_flags flags);
size_t ft_print_str(char *s, t_flags flags);
void ft_put_lu_nbr(long unsigned int nb);
#endif

Binary file not shown.

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/17 13:41:15 by tomoron #+# #+# */
/* Updated: 2023/11/15 14:31:19 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
int ft_protected_atoi(const char *str)
{
long long res;
int inv;
res = 0;
inv = 1;
while (*str == ' ' || (*str >= '\t' && *str <= '\r'))
str++;
if (*str == '+' || *str == '-')
{
if (*str == '-')
inv *= -1;
str++;
}
while (*str >= '0' && *str <= '9')
{
res *= 10;
res += *str - '0';
str++;
}
if (res > 2147483647)
return (-2);
return ((int)res * inv);
}

Binary file not shown.

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 04:10:53 by tomoron #+# #+# */
/* Updated: 2023/11/05 15:51:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}

Binary file not shown.

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 04:13:29 by tomoron #+# #+# */
/* Updated: 2023/11/05 14:42:32 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_putstr(char *s)
{
if (s)
write(1, s, ft_strlen(s));
return (ft_strlen(s));
}

Binary file not shown.

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 12:34:41 by tomoron #+# #+# */
/* Updated: 2023/11/04 08:19:20 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen(const char *str)
{
unsigned int n;
n = 0;
while (str[n])
n++;
return (n);
}

Binary file not shown.

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_write_str_part.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 11:16:38 by tomoron #+# #+# */
/* Updated: 2023/11/05 15:50:41 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_write_str_part(char **s)
{
size_t i;
i = 0;
while ((*s)[i] != '%' && (*s)[i])
i++;
if (i)
write(1, *s, i);
*s += i;
return (i);
}

Binary file not shown.

Binary file not shown.