]> Untitled Git - axy/ft/ft_printf.git/commitdiff
Finished up arg parsing
authorAxy <gilliardmarthey.axel@gmail.com>
Thu, 30 Oct 2025 11:41:05 +0000 (12:41 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Thu, 30 Oct 2025 11:41:05 +0000 (12:41 +0100)
ft_printf.c
libftprintf.h

index 48b730950e37c80b06a3b81588d32b7e1d14dd1c..a1dcda58863122cd8c2c5a1de3a90575d63e7996 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
index a51f2405ff68387126e2c3b0f73b09ae922447b0..d93cb02eb2c99a37b195fb38c383bf4d47967950 100644 (file)
@@ -6,13 +6,13 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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