/* 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 */
/* */
/* ************************************************************************** */
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;
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 == '-')
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')
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);
}
return (STEP_INT);
}
-int ft_printf(const char *format, ...)
+int ft_printf(const char *s, ...)
{
va_list args;
int count;
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;