From d9fc240b0edd5ceb00059be9ec482382da207288 Mon Sep 17 00:00:00 2001 From: gbaconni Date: Tue, 26 Apr 2022 17:53:39 +0200 Subject: [PATCH] Bonus first try --- Makefile | 16 +++++++----- libftprintf/Makefile | 5 +++- libftprintf/ft_eoflags.c | 2 +- libftprintf/ft_vprintf.c | 20 +++++++------- libftprintf/ft_vprintf_decimal.c | 39 +++++++++++++++++++++++++--- libftprintf/ft_vprintf_flags_bonus.c | 24 ++++++++++------- libftprintf/ft_vprintf_hexadecimal.c | 14 +++++++--- libftprintf/ft_vprintf_percent.c | 8 +++--- libftprintf/ft_vprintf_pointer.c | 2 +- libftprintf/ft_vprintf_string.c | 30 ++++++++++++++++++--- 10 files changed, 115 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index 201c0fd..cacfa18 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/26 12:43:41 by gbaconni ### ########.fr # +# Updated: 2022/04/26 17:47:55 by gbaconni ### ########.fr # # # # **************************************************************************** # # @@ -74,6 +74,12 @@ fclean: clean re: fclean all @$(MAKE) -C $(LIBFTPRINTF) re +bonus: + @$(MAKE) -C $(LIBFTPRINTF) bonus + +rebonus: + @$(MAKE) -C $(LIBFTPRINTF) rebonus + check: @$(MAKE) -C $(LIBFTPRINTF) check @@ -117,16 +123,14 @@ test3: fast test42: clean $(NAME) @$(PREFIX) ./$(NAME) "Hello World %% %44s %d %c %x %p" "Lausanne" 42 C 66 "C" @$(PREFIX) ./$(NAME) "Characters: %c %c \n" 'a' 65 - @$(PREFIX) ./$(NAME) "Decimals: %d %ld\n" 1977 650000L + @$(PREFIX) ./$(NAME) "Decimals: %d\n" 1977 @$(PREFIX) ./$(NAME) "Preceding with blanks: %10d \n" 1977 @$(PREFIX) ./$(NAME) "Preceding with zeros: %010d \n" 1977 - @$(PREFIX) ./$(NAME) "Some different radices: %d %x %o %#x %#o \n" 100 100 100 100 100 - @$(PREFIX) ./$(NAME) "floats: %4.2f %+.0e %E \n" 3.1416 3.1416 3.1416 + @$(PREFIX) ./$(NAME) "Some different radices: %d %x %#x\n" 100 100 100 @$(PREFIX) ./$(NAME) "Width trick: %*d \n" 5 10 - @$(PREFIX) ./$(NAME) "%s \n" "A string" watch: - @read -p "cmd: " cmd; while :; do clear; date "+%F %T"; echo; $${cmd} 2>&1 | tail -n 20; sleep 2; done + @read -p "cmd: " cmd; while :; do clear; date "+%F %T"; echo; $${cmd} 2>&1 | tail -n 20 || true; sleep 2; done doc: @curl -s -L -z fr.subject.pdf -o fr.subject.pdf $(PDF_FR) diff --git a/libftprintf/Makefile b/libftprintf/Makefile index a7d7ca9..025a35f 100644 --- a/libftprintf/Makefile +++ b/libftprintf/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/26 12:41:34 by gbaconni ### ########.fr # +# Updated: 2022/04/26 17:38:06 by gbaconni ### ########.fr # # # # **************************************************************************** # # @@ -144,6 +144,9 @@ ft: @echo "functions:" @nm $(NAME) | grep T | grep -o 'ft_.*' | sort -u +watch: + @read -p "cmd: " cmd; while :; do clear; date "+%F %T"; echo; $${cmd} 2>&1 | tail -n 20 || true; sleep 2; done + update: sync sync: online pull diff --git a/libftprintf/ft_eoflags.c b/libftprintf/ft_eoflags.c index 8809ac8..0bb580e 100644 --- a/libftprintf/ft_eoflags.c +++ b/libftprintf/ft_eoflags.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/26 10:13:59 by gbaconni #+# #+# */ -/* Updated: 2022/04/26 12:00:16 by gbaconni ### ########.fr */ +/* Updated: 2022/04/26 13:24:34 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftprintf/ft_vprintf.c b/libftprintf/ft_vprintf.c index 3ff920f..2f9580e 100644 --- a/libftprintf/ft_vprintf.c +++ b/libftprintf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:43:52 by gbaconni #+# #+# */ -/* Updated: 2022/04/26 12:04:14 by gbaconni ### ########.fr */ +/* Updated: 2022/04/26 15:20:34 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,21 +15,19 @@ int ft_vprintf(char const *format, va_list ap) { int ret; - const char *fmt; - + ret = 0; - fmt = format; - while (*fmt != 0) + while (*format != 0) { - if (*fmt == '%') + if (*format == '%') { - fmt++; - fmt += ft_eoflags(fmt); - ret += ft_vprintf_percent(fmt, ap); + format++; + ret += ft_vprintf_percent(format, ap); + format += ft_eoflags(format); } else - ret += ft_vprintf_other(fmt, ap); - fmt++; + ret += ft_vprintf_other(format, ap); + format++; } return (ret); } diff --git a/libftprintf/ft_vprintf_decimal.c b/libftprintf/ft_vprintf_decimal.c index 4612022..8d3758f 100644 --- a/libftprintf/ft_vprintf_decimal.c +++ b/libftprintf/ft_vprintf_decimal.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:55:02 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 01:19:09 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/26 17:48:37 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,11 @@ int ft_vprintf_decimal(const char *format, va_list ap) unsigned int u; int d; char *s; + char c; - (void) format; - if (*format == 'u') + ret = 0; + c = *(format + ft_eoflags(format)); + if (c == 'u') { u = va_arg(ap, unsigned int); s = ft_ltoa_base((long) u, "0123456789"); @@ -30,7 +32,36 @@ int ft_vprintf_decimal(const char *format, va_list ap) d = va_arg(ap, int); s = ft_itoa(d); } - ret = ft_puts(s); + + const char *f; + int flags; + int precision; + int len; + precision = 0; + flags = ft_vprintf_flags_bonus(format, ap); + f = format; + while (*f != '\0') + { + if (*f >= '0' && *f <= '9') + precision = precision * 10 + *f - '0'; + if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%') + break ; + f++; + } + len = 0; + if (precision > 0) + { + len = precision - ft_strlen(s); + while (len --> 0) + { + if (flags & F_ZERO) + ret += ft_putchar('0'); + else + ret += ft_putchar(' '); + } + } + + ret += ft_puts(s); free(s); return (ret); } diff --git a/libftprintf/ft_vprintf_flags_bonus.c b/libftprintf/ft_vprintf_flags_bonus.c index 97feca3..cd60b02 100644 --- a/libftprintf/ft_vprintf_flags_bonus.c +++ b/libftprintf/ft_vprintf_flags_bonus.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/25 13:15:32 by gbaconni #+# #+# */ -/* Updated: 2022/04/25 13:17:47 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/26 17:18:20 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,29 @@ int ft_vprintf_flags_bonus(const char *format, va_list ap) { - int ret; + int ret; + const char *f; ret = 0; + f = format; (void) ap; - while (*format != '\0') + while (*f != '\0') { - if (*format == '#') + if (*f == '#') ret |= F_HASH; - else if (*format == '0') + else if (*f == '0') ret |= F_ZERO; - else if (*format == '-') + else if (*f == '-') ret |= F_MINUS; - else if (*format == ' ') + else if (*f == ' ') ret |= F_SPACE; - else if (*format == '+') + else if (*f == '+') ret |= F_PLUS; - format++; + else if (*f >= '0' && *f <= '9') + break ; + if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%') + break ; + f++; } return (ret); } diff --git a/libftprintf/ft_vprintf_hexadecimal.c b/libftprintf/ft_vprintf_hexadecimal.c index c6f05d1..d222566 100644 --- a/libftprintf/ft_vprintf_hexadecimal.c +++ b/libftprintf/ft_vprintf_hexadecimal.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:55:33 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 00:30:39 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/26 17:33:12 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,20 @@ int ft_vprintf_hexadecimal(const char *format, va_list ap) int ret; long l; char *s; + char c; + int flags; - (void) format; + ret = 0; l = va_arg(ap, long); - if (*format == 'x') + c = *format; + if (c == 'x') s = ft_ltoa_base(l, "0123456789abcdef"); else s = ft_ltoa_base(l, "0123456789ABCDEF"); - ret = ft_puts(s); + flags = ft_vprintf_flags_bonus(format, ap); + if (flags & F_HASH) + ret += ft_puts("0x"); + ret += ft_puts(s); free(s); return (ret); } diff --git a/libftprintf/ft_vprintf_percent.c b/libftprintf/ft_vprintf_percent.c index 91ac9a9..9908b62 100644 --- a/libftprintf/ft_vprintf_percent.c +++ b/libftprintf/ft_vprintf_percent.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:55:58 by gbaconni #+# #+# */ -/* Updated: 2022/04/24 01:06:15 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/26 15:40:34 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,11 @@ int ft_vprintf_percent(const char *format, va_list ap) { - int ret; - char c; + int ret; + char c; ret = 0; - c = *format; + c = *(format + ft_eoflags(format)); if (c == 'c') ret = ft_vprintf_char(format, ap); else if (c == 's') diff --git a/libftprintf/ft_vprintf_pointer.c b/libftprintf/ft_vprintf_pointer.c index 3515f93..e840526 100644 --- a/libftprintf/ft_vprintf_pointer.c +++ b/libftprintf/ft_vprintf_pointer.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:56:16 by gbaconni #+# #+# */ -/* Updated: 2022/04/16 00:34:18 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/26 13:53:06 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftprintf/ft_vprintf_string.c b/libftprintf/ft_vprintf_string.c index 9d8503f..14da4ae 100644 --- a/libftprintf/ft_vprintf_string.c +++ b/libftprintf/ft_vprintf_string.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:56:37 by gbaconni #+# #+# */ -/* Updated: 2022/04/25 07:12:44 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/26 17:43:42 by gbaconni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,8 @@ int ft_vprintf_string(const char *format, va_list ap) { - int ret; - char *s; + int ret; + char *s; (void) format; ret = 0; @@ -25,6 +25,28 @@ int ft_vprintf_string(const char *format, va_list ap) ret = ft_puts("(null)"); return (ret); } - ret = ft_puts(s); + + const char *f; + int flags; + int precision; + int len; + precision = 0; + flags = 0; + f = format; + while (*f != '\0') + { + if (*f >= '0' && *f <= '9') + precision = precision * 10 + *f - '0'; + if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%') + break ; + f++; + } + if (precision > 0) + { + len = precision - ft_strlen(s); + while (len --> 0) + ret += ft_putchar(' '); + } + ret += ft_puts(s); return (ret); }