From 358e42540b75c1035f9041b066581b69966348a2 Mon Sep 17 00:00:00 2001 From: Axy Date: Thu, 30 Oct 2025 12:41:05 +0100 Subject: [PATCH] Finished up arg parsing --- ft_printf.c | 64 +++++++++++++++++++++++++++++++++++---------------- libftprintf.h | 4 ++-- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/ft_printf.c b/ft_printf.c index 48b7309..a1dcda5 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/10/29 14:24:47 by agilliar #+# #+# */ -/* Updated: 2025/10/30 12:12:46 by agilliar ### ########.fr */ +/* Updated: 2025/10/30 12:37:41 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,8 +64,8 @@ typedef enum e_specifier SPECIFIER_VOID_PTR, SPECIFIER_INT, SPECIFIER_UINT, - SPECIFIER_INT_HEX_LOWER, - SPECIFIER_INT_HEX_UPPER, + SPECIFIER_UINT_HEX_LOWER, + SPECIFIER_UINT_HEX_UPPER, SPECIFIER_PERCENT, } t_specifier; @@ -114,6 +114,10 @@ size_t ft_parse_num(const char **s) return (res); } +/* + norminette doesn't want switch and case? + fine then, poor man's switch +*/ bool ft_parse_flag(char c, t_format *format) { if (c == '-') @@ -131,6 +135,26 @@ bool ft_parse_flag(char c, t_format *format) return (true); } +void ft_parse_width(const char **s, t_format *format) +{ + while (**s >= '0' && **s <= '9') + format->width = format->width * 10 + *((*s)++) - '0'; +} + +void ft_parse_precision(const char **s, t_format *format) +{ + if (**s != '.') + return ; + (*s)++; + format->enable_precision = true; + while (**s >= '0' && **s <= '9') + format->precision = format->precision * 10 + *((*s)++) - '0'; +} + +/* + really wish there was a language feature to allow doing that in a neater way, + but of course no such thing! +*/ t_specifier ft_parse_specifier(char c) { if (c == 'c') @@ -144,42 +168,42 @@ t_specifier ft_parse_specifier(char c) if (c == 'u') return (SPECIFIER_UINT); if (c == 'x') - return (SPECIFIER_INT_HEX_LOWER); + return (SPECIFIER_UINT_HEX_LOWER); if (c == 'X') - return (SPECIFIER_INT_HEX_UPPER); + return (SPECIFIER_UINT_HEX_UPPER); if (c == '%') return (SPECIFIER_PERCENT); return (SPECIFIER_NONE); } -t_format ft_parse_format(const char **format) +t_format ft_parse_format(const char **s) { t_format res; res = ft_format_default(); - while (ft_parse_flag(**format, &res)) - format++; - res.specifier = ft_parse_specifier(*((*format)++)); + while (ft_parse_flag(**s, &res)) + s++; + res.specifier = ft_parse_specifier(*((*s)++)); return (res); } -int ft_print_step(const char **format, t_format *step, t_step_arg arg) +int ft_print_step(const char **s, t_format *format, t_step_arg arg) { int written; - if (step->specifier == SPECIFIER_NONE && **format != '%') + if (format->specifier == SPECIFIER_NONE && **s != '%') { - if (write(STDOUT_FILENO, (*format)++, 1) != 1) + if (write(STDOUT_FILENO, (*s)++, 1) != 1) return (-1); return (1); } - if (step->specifier != SPECIFIER_NONE) + if (format->specifier != SPECIFIER_NONE) { - written = ft_putformat(step, arg); - *step = ft_format_default(); + written = ft_putformat(format, arg); + *format = ft_format_default(); return (written); } - *step = ft_parse_format(format); + *format = ft_parse_format(s); return (0); } @@ -194,7 +218,7 @@ t_step specifier_to_step(t_specifier spec) return (STEP_INT); } -int ft_printf(const char *format, ...) +int ft_printf(const char *s, ...) { va_list args; int count; @@ -202,12 +226,12 @@ int ft_printf(const char *format, ...) t_format step; t_step_arg arg; - va_start(args, format); + va_start(args, s); count = 0; step.specifier = SPECIFIER_NONE; - while (*format) + while (*s) { - written = ft_print_step(&format, &step, arg); + written = ft_print_step(&s, &step, arg); if (written < 0) return (written); count += written; diff --git a/libftprintf.h b/libftprintf.h index a51f240..d93cb02 100644 --- a/libftprintf.h +++ b/libftprintf.h @@ -6,13 +6,13 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/10/29 14:21:01 by agilliar #+# #+# */ -/* Updated: 2025/10/29 14:22:40 by agilliar ### ########.fr */ +/* Updated: 2025/10/30 12:19:53 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBFTPRINTF_H # define LIBFTPRINTF_H -int ft_printf(const char *format, ...); +int ft_printf(const char *s, ...); #endif -- 2.51.0