From 95b7ca347f77f1e92e148e5039fba774999bfd4a Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Mon, 21 Nov 2022 19:33:11 +0100 Subject: [PATCH] Fini printf --- ft_itoa.c | 53 -------------------------------------------- ft_printf.c | 6 ++--- ft_printf.h | 9 ++++---- ft_putnbrhex.c | 26 +++++++++++----------- ft_putnbrhex_upper.c | 26 +++++++++++----------- ft_putvd.c | 6 ++--- 6 files changed, 36 insertions(+), 90 deletions(-) delete mode 100644 ft_itoa.c diff --git a/ft_itoa.c b/ft_itoa.c deleted file mode 100644 index 70ef546..0000000 --- a/ft_itoa.c +++ /dev/null @@ -1,53 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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_printf.c b/ft_printf.c index 8cffd3b..386b4d2 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 16:27:59 by erey-bet #+# #+# */ -/* Updated: 2022/10/15 23:19:38 by erey-bet ### ########.fr */ +/* Updated: 2022/11/21 19:14:36 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,9 +33,9 @@ static int ft_post_character_bis(char format, va_list args) else if (format == 'u') count = ft_putunbr(va_arg(args, unsigned int)); else if (format == 'x') - count = ft_putnbrhex(va_arg(args, int)); + count = ft_putnbrhex(va_arg(args, unsigned int)); else if (format == 'X') - count = ft_putnbrhex_upper(va_arg(args, int)); + count = ft_putnbrhex_upper(va_arg(args, unsigned int)); else if (format == '%') ft_putchar('%'); else if (format == 'p') diff --git a/ft_printf.h b/ft_printf.h index ff32694..f118253 100644 --- a/ft_printf.h +++ b/ft_printf.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */ -/* Updated: 2022/10/15 23:16:11 by erey-bet ### ########.fr */ +/* Updated: 2022/11/21 19:31:17 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,15 @@ # include # include -int ft_printf(const char *str, ...); +int ft_printf(const char *str, ...); 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 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_putnbrhex(unsigned int v); +int ft_putnbrhex_upper(unsigned int v); int ft_putvd(void *v); #endif diff --git a/ft_putnbrhex.c b/ft_putnbrhex.c index f057cd6..fcc3757 100644 --- a/ft_putnbrhex.c +++ b/ft_putnbrhex.c @@ -6,36 +6,36 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/14 23:24:36 by erey-bet #+# #+# */ -/* Updated: 2022/10/15 22:20:19 by erey-bet ### ########.fr */ +/* Updated: 2022/11/21 19:25:08 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" -int ft_putnbrhex(int v) +int ft_putnbrhex(unsigned int v) { - int tmp; - int count; + int tmp; + int count; + char hex[100]; if (v == 0) - ft_putchar('0'); - count = 0; - if (v < 0) { - write(1, "-", 1); - v *= -1; - count++; + ft_putchar('0'); + return (1); } + count = 0; tmp = 0; while (v > 0) { tmp = v % 16; if (tmp < 10) - ft_putchar(tmp + 48); + hex[count++] = (tmp + 48); else - ft_putchar(tmp + 87); + hex[count++] = (tmp + 87); v = v / 16; - count++; } + tmp = count; + while (--tmp >= 0) + ft_putchar(hex[tmp]); return (count); } diff --git a/ft_putnbrhex_upper.c b/ft_putnbrhex_upper.c index b53f9f9..1de2606 100644 --- a/ft_putnbrhex_upper.c +++ b/ft_putnbrhex_upper.c @@ -6,36 +6,36 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/14 23:24:36 by erey-bet #+# #+# */ -/* Updated: 2022/10/15 22:20:49 by erey-bet ### ########.fr */ +/* Updated: 2022/11/21 19:16:57 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" -int ft_putnbrhex_upper(int v) +int ft_putnbrhex_upper(unsigned int v) { - int tmp; - int count; + int tmp; + int count; + char hex[100]; if (v == 0) - ft_putchar('0'); - count = 0; - if (v < 0) { - write(1, "-", 1); - v *= -1; - count++; + ft_putchar('0'); + return (1); } + count = 0; tmp = 0; while (v > 0) { tmp = v % 16; if (tmp < 10) - ft_putchar(tmp + 48); + hex[count++] = (tmp + 48); else - ft_putchar(tmp + 55); + hex[count++] = (tmp + 55); v = v / 16; - count++; } + tmp = count; + while (--tmp >= 0) + ft_putchar(hex[tmp]); return (count); } diff --git a/ft_putvd.c b/ft_putvd.c index f795732..c0075f9 100644 --- a/ft_putvd.c +++ b/ft_putvd.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/14 23:01:21 by erey-bet #+# #+# */ -/* Updated: 2022/10/15 22:15:15 by erey-bet ### ########.fr */ +/* Updated: 2022/11/21 19:26:59 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,8 @@ static int ft_putnbrhexull(unsigned long v) { - int tmp; - int count; + int tmp; + int count; char hex[100]; count = 0;