commit c70b29dfd1ace8adcf560d5682f9b526cdeae6ca Author: Etienne Rey-bethbeder Date: Thu Nov 10 15:25:19 2022 +0100 printf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..48c320e --- /dev/null +++ b/Makefile @@ -0,0 +1,139 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: erey-bet +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# # +# Updated: 2022/10/15 15:39:56 by erey-bet ### ########.fr # +# # +# **************************************************************************** # + +SRCS = ft_get_size.c ft_itoa.c ft_power.c ft_printf.c ft_putchar.c ft_putnbr.c \ +ft_putnbrhex.c ft_putnbrhex_upper.c ft_putstr.c ft_putunbr.c ft_putvd.c +OBJS = ${SRCS:.c=.o} +CC = gcc +CFLAGS = -Wall -Wextra -Werror +NAME = libftprintf.a + + +all: ${NAME} + +${NAME}: ${OBJS} + ar -rc ${NAME} ${OBJS} + +%.o:%.c + ${CC} ${CFLAGS} -c -o $@ $< + +clean: + rm -f ${OBJS} + +fclean: clean + rm -f ${NAME} + +re: fclean all + +coffee: + @clear + @echo "" + @echo " (" + @echo " ) (" + @echo " ___...(-------)-....___" + @echo ' .-"" ) ( ""-.' + @echo " .-''''|-._ ) _.-|" + @echo ' / .--.| `""---...........---""` |' + @echo " / / | |" + @echo " | | | |" + @echo " \ \ | |" + @echo " '\ '\ | |" + @echo " '\ '| |" + @echo " _/ /\ /" + @echo " (__/ \ /" + @echo ' _..---""` \ /`""---.._' + @echo " .-' \ / '-." + @echo ": '-.__ __.-' :" + @echo ': ) ""---...---"" ( :' + @echo "\'._ '"--...___...--"' _.'" + @echo ' \""--..__ __..--""/' + @echo " '._ """----.....______.....----""" _.'" + @echo ' ""--..,,_____ _____,,..--"""''' + @echo ' """------"""' + @sleep 0.3 + @clear + @echo "" + @echo " (" + @echo " ) (" + @echo " ___..(.------)--....___" + @echo ' .-"" ) ( ""-.' + @echo " .-''''|-._ ( ) _.-|" + @echo ' / .--.| `""---...........---""` |' + @echo " / / | |" + @echo " | | | |" + @echo " \ \ | |" + @echo " '\ '\ | |" + @echo " '\ '| |" + @echo " _/ /\ /" + @echo " (__/ \ /" + @echo ' _..---""` \ /`""---.._' + @echo " .-' \ / '-." + @echo ": '-.__ __.-' :" + @echo ': ) ""---...---"" ( :' + @echo "\'._ '"--...___...--"' _.'" + @echo ' \""--..__ __..--""/' + @echo " '._ """----.....______.....----""" _.'" + @echo ' ""--..,,_____ _____,,..--"""''' + @echo ' """------"""' + @sleep 0.3 + @clear + @echo "" + @echo " (" + @echo " ) (" + @echo " ___..(.------)--....___" + @echo ' .-"" ) ( ""-.' + @echo " .-''''|-._ ( ) _.-|" + @echo ' / .--.| `""---...........---""` |' + @echo " / / | |" + @echo " | | | |" + @echo " \ \ | |" + @echo " '\ '\ | |" + @echo " '\ '| |" + @echo " _/ /\ /" + @echo " (__/ \ /" + @echo ' _..---""` \ /`""---.._' + @echo " .-' \ / '-." + @echo ": '-.__ __.-' :" + @echo ': ) ""---...---"" ( :' + @echo "\'._ '"--...___...--"' _.'" + @echo ' \""--..__ __..--""/' + @echo " '._ """----.....______.....----""" _.'" + @echo ' ""--..,,_____ _____,,..--"""''' + @echo ' """------"""' + @sleep 0.3 + @clear + @echo "" + @echo " ( ) " + @echo " ) (" + @echo " ___)...----)----....___" + @echo ' .-"" ) ( ""-.' + @echo " .-''''|-._ ( ) _.-|" + @echo ' / .--.| `""---...........---""` |' + @echo " / / | |" + @echo " | | | |" + @echo " \ \ | |" + @echo " '\ '\ | |" + @echo " '\ '| |" + @echo " _/ /\ /" + @echo " (__/ \ /" + @echo ' _..---""` \ /`""---.._' + @echo " .-' \ / '-." + @echo ": '-.__ __.-' :" + @echo ': ) ""---...---"" ( :' + @echo "\'._ '"--...___...--"' _.'" + @echo ' \""--..__ __..--""/' + @echo " '._ """----.....______.....----""" _.'" + @echo ' ""--..,,_____ _____,,..--"""''' + @echo ' """------"""' + ${MAKE} coffee + +.PHONY: all clean fclean re coffee diff --git a/ft_get_size.c b/ft_get_size.c new file mode 100644 index 0000000..4916c4f --- /dev/null +++ b/ft_get_size.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 22:50:35 by erey-bet #+# #+# */ +/* Updated: 2022/10/11 22:14:29 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_get_size(long n) +{ + long i; + + if (n == 0) + return (1); + i = 0; + n *= (n > 0) - (n < 0); + while (n > 0) + { + i++; + n = n / 10; + } + return (i); +} diff --git a/ft_itoa.c b/ft_itoa.c new file mode 100644 index 0000000..70ef546 --- /dev/null +++ b/ft_itoa.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */ +/* Updated: 2022/10/10 22:15:30 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_itoa_bis(char *str, int len, int i, long nl) +{ + while (len > 1) + { + len--; + str[i] = (nl / ft_power(10, len) % 10) + 48; + i++; + } + str[i] = nl % 10 + 48; + i++; + str[i] = '\0'; +} + +char *ft_itoa(int n) +{ + char *str; + int len; + int i; + long nl; + + nl = n; + nl *= (n > 0) - (n < 0); + len = ft_get_size(nl); + i = 0; + if (n < 0) + { + str = malloc(len + 2); + if (str == NULL) + return (NULL); + str[i] = '-'; + i++; + } + else + str = malloc(len + 1); + if (str == NULL) + return (NULL); + ft_itoa_bis(str, len, i, nl); + return (str); +} diff --git a/ft_power.c b/ft_power.c new file mode 100644 index 0000000..90f6466 --- /dev/null +++ b/ft_power.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 22:50:22 by erey-bet #+# #+# */ +/* Updated: 2022/10/10 22:50:31 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_power(int nb, int power) +{ + int i; + int new_nb; + + if (power < 0) + return (0); + i = 0; + new_nb = 1; + while (i < power) + { + i++; + new_nb *= nb; + } + return (new_nb); +} diff --git a/ft_printf.c b/ft_printf.c new file mode 100644 index 0000000..8cffd3b --- /dev/null +++ b/ft_printf.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/12 16:27:59 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 23:19:38 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include + +static int ft_is_format(const char c) +{ + return (c == 'c' || c == 's' || c == 'p' || c == 'i' || c == 'd' + || c == 'u' || c == 'x' || c == 'X' || c == '%'); +} + +static int ft_post_character_bis(char format, va_list args) +{ + int count; + + count = 1; + if (format == 'c') + ft_putchar(va_arg(args, int)); + else if (format == 's') + count = ft_putstr(va_arg(args, char *)); + else if (format == 'd' || format == 'i') + count = ft_putnbr(va_arg(args, int)); + else if (format == 'u') + count = ft_putunbr(va_arg(args, unsigned int)); + else if (format == 'x') + count = ft_putnbrhex(va_arg(args, int)); + else if (format == 'X') + count = ft_putnbrhex_upper(va_arg(args, int)); + else if (format == '%') + ft_putchar('%'); + else if (format == 'p') + count = ft_putvd(va_arg(args, void *)); + return (count); +} + +static int ft_post_character(char format, va_list args) +{ + int count; + + count = ft_post_character_bis(format, args); + if (count == -1) + { + ft_putstr("(null)"); + count = 6; + } + else if (count == -2) + { + ft_putstr("(nil)"); + count = 5; + } + return (count); +} + +int ft_printf(const char *str, ...) +{ + va_list args; + int i; + int y; + int count; + + va_start(args, str); + i = -1; + y = 0; + count = 0; + while (str[++i]) + { + if (str[i] == '%' && ft_is_format(str[i + 1])) + { + count += ft_post_character(str[i + 1], args); + i++; + y++; + } + else + { + ft_putchar(str[i]); + count++; + } + } + va_end(args); + return (count); +} diff --git a/ft_printf.h b/ft_printf.h new file mode 100644 index 0000000..77adc70 --- /dev/null +++ b/ft_printf.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 23:16:11 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include +# include +# include + +void ft_putchar(char c); +int ft_putstr(char *s); +char *ft_itoa(int n); +int ft_power(int nb, int power); +int ft_get_size(long n); +int ft_putnbr(int n); +int ft_putunbr(unsigned int n); +int ft_putnbrhex(int v); +int ft_putnbrhex_upper(int v); +int ft_putvd(void *v); + +#endif diff --git a/ft_putchar.c b/ft_putchar.c new file mode 100644 index 0000000..7e6fb3e --- /dev/null +++ b/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/05 00:21:56 by erey-bet #+# #+# */ +/* Updated: 2022/10/05 00:31:51 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/ft_putnbr.c b/ft_putnbr.c new file mode 100644 index 0000000..437d90f --- /dev/null +++ b/ft_putnbr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/05 10:22:16 by erey-bet #+# #+# */ +/* Updated: 2022/10/10 22:49:10 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putnbr(int n) +{ + int len; + int count; + long nl; + + count = 0; + nl = n; + nl *= (n > 0) - (n < 0); + len = ft_get_size(nl); + if (n < 0) + { + ft_putchar('-'); + count++; + } + while (len > 1) + { + --len; + ft_putchar(nl / ft_power(10, len) % 10 + 48); + count++; + } + ft_putchar(nl % 10 + 48); + count++; + return (count); +} diff --git a/ft_putnbrhex.c b/ft_putnbrhex.c new file mode 100644 index 0000000..f057cd6 --- /dev/null +++ b/ft_putnbrhex.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrhex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 23:24:36 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 22:20:19 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putnbrhex(int v) +{ + int tmp; + int count; + + if (v == 0) + ft_putchar('0'); + count = 0; + if (v < 0) + { + write(1, "-", 1); + v *= -1; + count++; + } + tmp = 0; + while (v > 0) + { + tmp = v % 16; + if (tmp < 10) + ft_putchar(tmp + 48); + else + ft_putchar(tmp + 87); + v = v / 16; + count++; + } + return (count); +} diff --git a/ft_putnbrhex_upper.c b/ft_putnbrhex_upper.c new file mode 100644 index 0000000..b53f9f9 --- /dev/null +++ b/ft_putnbrhex_upper.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrhex_upper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 23:24:36 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 22:20:49 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putnbrhex_upper(int v) +{ + int tmp; + int count; + + if (v == 0) + ft_putchar('0'); + count = 0; + if (v < 0) + { + write(1, "-", 1); + v *= -1; + count++; + } + tmp = 0; + while (v > 0) + { + tmp = v % 16; + if (tmp < 10) + ft_putchar(tmp + 48); + else + ft_putchar(tmp + 55); + v = v / 16; + count++; + } + return (count); +} diff --git a/ft_putstr.c b/ft_putstr.c new file mode 100644 index 0000000..de781fd --- /dev/null +++ b/ft_putstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/05 00:32:38 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 23:15:57 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putstr(char *s) +{ + int count; + + count = 0; + if (s == NULL) + return (-1); + while (*s) + { + ft_putchar(*s); + s++; + count++; + } + return (count); +} diff --git a/ft_putunbr.c b/ft_putunbr.c new file mode 100644 index 0000000..c0b2689 --- /dev/null +++ b/ft_putunbr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putunbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 23:11:19 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 00:02:36 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putunbr(unsigned int n) +{ + unsigned int len; + unsigned long nl; + + nl = n; + len = ft_get_size(nl); + while (len > 1) + { + --len; + ft_putchar(nl / ft_power(10, len) % 10 + 48); + } + ft_putchar(nl % 10 + 48); + return (ft_get_size(nl)); +} diff --git a/ft_putvd.c b/ft_putvd.c new file mode 100644 index 0000000..30be7cb --- /dev/null +++ b/ft_putvd.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putvd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/14 23:01:21 by erey-bet #+# #+# */ +/* Updated: 2022/10/15 22:15:15 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_putnbrhexull(unsigned long v) +{ + int tmp; + int count; + + count = 0; + tmp = 0; + while (v > 0) + { + tmp = v % 16; + if (tmp < 10) + ft_putchar(tmp + 48); + else + ft_putchar(tmp + 87); + v = v / 16; + count++; + } + return (count); +} + +int ft_putvd(void *v) +{ + int count; + unsigned long u; + + if (v == NULL) + return (-2); + u = (unsigned long)v; + count = 2; + ft_putstr("0x"); + count += ft_putnbrhexull(u); + return (count); +}