From 5cecf487b3a3f04e17d7a61eb1874a3aa94b18a0 Mon Sep 17 00:00:00 2001 From: Marc Agen Date: Wed, 30 Oct 2024 14:27:33 +0100 Subject: [PATCH 1/2] truc --- include/my.h | 2 +- src/handler/put_hex.c | 12 +++++------- src/handler/put_pointer.c | 6 +++--- tests/main.c | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/include/my.h b/include/my.h index f21248c..b3bceca 100644 --- a/include/my.h +++ b/include/my.h @@ -57,7 +57,7 @@ typedef enum { char *baby_revstr(char *); int baby_getnbr(char const *); int baby_put_nbr(int, flags_t *, int); -int baby_put_hex(size_t nb, flags_t *); +int baby_put_hex(size_t nb, flags_t *, int); int baby_putchar(char); int baby_putstr(char *); int baby_stridx(char const *, char); diff --git a/src/handler/put_hex.c b/src/handler/put_hex.c index bcd03eb..cd822d9 100644 --- a/src/handler/put_hex.c +++ b/src/handler/put_hex.c @@ -7,11 +7,10 @@ #include "my.h" -int baby_put_hex(size_t nb, flags_t *flags) +int baby_put_hex(size_t nb, flags_t *flags, int i) { size_t result = (size_t)nb; char str[64]; - int i = 0; for (; nb != 0; i++) { result = nb % 16; @@ -25,15 +24,14 @@ int baby_put_hex(size_t nb, flags_t *flags) baby_revstr(str); flags->spec_buff.str = str; flags->spec_buff.count = i; - return 0; + return i; } static -int baby_put_hex_upc(size_t nb, flags_t *flags) +int baby_put_hex_upc(size_t nb, flags_t *flags, int i) { size_t result = (size_t)nb; char str[64]; - int i = 0; for (; nb != 0; i++) { result = nb % 16; @@ -62,7 +60,7 @@ void printf_put_hex(flags_t *flags) return; } if (isupper(flags->spec)) - baby_put_hex_upc(nb, flags); + baby_put_hex_upc(nb, flags, 0); else - baby_put_hex(nb, flags); + baby_put_hex(nb, flags, 0); } diff --git a/src/handler/put_pointer.c b/src/handler/put_pointer.c index 9eb4525..770dabd 100644 --- a/src/handler/put_pointer.c +++ b/src/handler/put_pointer.c @@ -16,8 +16,8 @@ void printf_put_pointer(flags_t *flags) char *old_spec_buff_str; old_spec_buff_str = flags->spec_buff.str; - flags->spec_buff.str = "0x"; - flags->spec_buff.str += 2; - baby_put_hex(src, flags); + flags->spec_buff.str[0]= '0'; + flags->spec_buff.str[1]= 'x'; + baby_put_hex(src, flags, 3); flags->spec_buff.str = old_spec_buff_str; } diff --git a/tests/main.c b/tests/main.c index d34b47b..87e3052 100644 --- a/tests/main.c +++ b/tests/main.c @@ -6,6 +6,6 @@ int main(void) { int a = 42; - my_printf("%5.3s pad 3: %5.3s %d\n", "hello", "hello", 42); - printf("%5.3s pad 3: %5.3s %d\n", "hello", "helloo", 42); + my_printf("%p\n", &a); + printf("%p\n", &a); } From 237175ee3f791bc80af22f66fab46398c35e6f5e Mon Sep 17 00:00:00 2001 From: Marc Agen Date: Wed, 30 Oct 2024 20:29:39 +0100 Subject: [PATCH 2/2] com --- src/handler/put_float.c | 12 +++++++++++- src/handler/put_hex.c | 11 +++++++++-- src/handler/put_pointer.c | 4 ++-- tests/main.c | 4 ++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/handler/put_float.c b/src/handler/put_float.c index 205066a..945ee8f 100644 --- a/src/handler/put_float.c +++ b/src/handler/put_float.c @@ -57,6 +57,16 @@ int put_point(flags_t *flags, int i) return 0; } +void round_float(flags_t *flags, int i) +{ + int a = i; + for (; flags->spec_buff.str[a] == 9; a--) { + flags->spec_buff.str[a] = '0'; + } + flags->spec_buff.str[a]++; + flags->spec_buff.count = i; +} + void printf_put_float(flags_t *flags) { double nbr = va_arg(flags->args, double); @@ -74,5 +84,5 @@ void printf_put_float(flags_t *flags) nbr = (nbr - (int)nbr) * 10; i = baby_put_nbr((int)(nbr < 0 ? - nbr : nbr), flags, i); } - flags->spec_buff.count = i; + round_float(flags, i); }; diff --git a/src/handler/put_hex.c b/src/handler/put_hex.c index 0a9c040..9516b0f 100644 --- a/src/handler/put_hex.c +++ b/src/handler/put_hex.c @@ -50,6 +50,7 @@ int baby_put_hex_upc(size_t nb, flags_t *flags, int i) void printf_put_hex(flags_t *flags) { + int i = 0; size_t nb = (size_t)va_arg(flags->args, void *); if (flags->precision == 0 && nb == 0) @@ -59,8 +60,14 @@ void printf_put_hex(flags_t *flags) flags->spec_buff.count = 1; return; } + if (flags->precision > 0) { + if (flags->flags & FLAGS_PAD_RIGHT) + flags->flags &= ~FLAGS_PAD_RIGHT; + flags->flags |= FLAGS_PAD_ZERO; + flags->width = flags->precision; + } if (isupper(flags->spec)) - baby_put_hex_upc(nb, flags, 0); + baby_put_hex_upc(nb, flags, i); else - baby_put_hex(nb, flags, 0); + baby_put_hex(nb, flags, i); } diff --git a/src/handler/put_pointer.c b/src/handler/put_pointer.c index 770dabd..368c328 100644 --- a/src/handler/put_pointer.c +++ b/src/handler/put_pointer.c @@ -16,8 +16,8 @@ void printf_put_pointer(flags_t *flags) char *old_spec_buff_str; old_spec_buff_str = flags->spec_buff.str; - flags->spec_buff.str[0]= '0'; - flags->spec_buff.str[1]= 'x'; + flags->spec_buff.str[0] = '0'; + flags->spec_buff.str[1] = 'x'; baby_put_hex(src, flags, 3); flags->spec_buff.str = old_spec_buff_str; } diff --git a/tests/main.c b/tests/main.c index 87e3052..abd1223 100644 --- a/tests/main.c +++ b/tests/main.c @@ -6,6 +6,6 @@ int main(void) { int a = 42; - my_printf("%p\n", &a); - printf("%p\n", &a); + my_printf("%.5f\n", 33.01); + printf("%.5f\n", 33.01); }