/* By: agilliar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/29 14:24:47 by agilliar #+# #+# */
-/* Updated: 2025/10/30 12:37:41 by agilliar ### ########.fr */
+/* Updated: 2025/10/30 17:33:02 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h>
-#include <stddef.h>
#include <stdbool.h>
#include <unistd.h>
t_sign_flag sign_flag;
t_justify justify;
t_num_prefix num_prefix;
- size_t width;
+ int width;
t_padding_type padding_type;
- size_t precision;
+ int precision;
bool enable_precision;
} t_format;
-int ft_putformat(const t_format *format, t_step_arg arg);
+int ft_putchar_sim(char c, bool simulated)
+{
+ if (!simulated && write(STDOUT_FILENO, &c, 1) != 1)
+ return (-1);
+ return (1);
+}
+
+int ft_putnchar(char c, int n)
+{
+ int i;
+
+ i = 0;
+ while (i++ < n)
+ if (ft_putchar_sim(c, false) == -1)
+ return (-1);
+ return (n);
+}
+
+
+int ft_putformat_char(const t_format *format, t_step_arg arg, bool simulated)
+{
+ return (ft_putchar_sim(arg.v_int, simulated));
+}
+
+int ft_putformat_str(const t_format *format, t_step_arg arg, bool simulated)
+{
+ int i;
+ const char *s;
+
+ i = 0;
+ s = arg.v_ptr;
+ if (!s)
+ s = "(null)";
+ while ((!format->enable_precision || format->precision > i) && s[i])
+ if (ft_putchar_sim(s[i++], simulated) == -1)
+ return (-1);
+ return (i);
+}
+
+int ft_putformat(const t_format *format, t_step_arg arg)
+{
+ int (*f)(const t_format *, t_step_arg, bool);
+ int len;
+
+ if (format->specifier == SPECIFIER_CHAR)
+ f = ft_putformat_char;
+ else if (format->specifier == SPECIFIER_STRING)
+ f = ft_putformat_str;
+ else
+ return (-1);
+ len = f(format, arg, true);
+ if ((format->justify == JUSTIFY_LEFT && f(format, arg, false) == -1)
+ || (format->width > len && ft_putnchar(' ', format->width - len) == -1)
+ || (format->justify == JUSTIFY_RIGHT && f(format, arg, false) == -1))
+ return (-1);
+ return (len);
+}
t_format ft_format_default(void)
{
return (res);
}
-size_t ft_parse_num(const char **s)
+int ft_parse_num(const char **s)
{
- size_t res;
+ int res;
res = 0;
while (**s >= '0' && **s <= '9')
res = ft_format_default();
while (ft_parse_flag(**s, &res))
- s++;
+ (*s)++;
+ ft_parse_width(s, &res);
+ ft_parse_precision(s, &res);
res.specifier = ft_parse_specifier(*((*s)++));
return (res);
}
{
int written;
+
if (format->specifier == SPECIFIER_NONE && **s != '%')
- {
- if (write(STDOUT_FILENO, (*s)++, 1) != 1)
- return (-1);
- return (1);
- }
+ return (ft_putchar_sim(*((*s)++), false));
if (format->specifier != SPECIFIER_NONE)
{
written = ft_putformat(format, arg);
*format = ft_format_default();
return (written);
}
+ (*s)++;
*format = ft_parse_format(s);
return (0);
}
va_list args;
int count;
int written;
- t_format step;
+ t_format format;
t_step_arg arg;
va_start(args, s);
count = 0;
- step.specifier = SPECIFIER_NONE;
- while (*s)
+ format = ft_format_default();
+ while (*s || format.specifier != SPECIFIER_NONE)
{
- written = ft_print_step(&s, &step, arg);
- if (written < 0)
- return (written);
+ written = ft_print_step(&s, &format, arg);
+ if (written == -1)
+ return (-1);
count += written;
- if (specifier_to_step(step.specifier) == STEP_INT)
+ if (specifier_to_step(format.specifier) == STEP_INT)
arg.v_int = va_arg(args, int);
- if (specifier_to_step(step.specifier) == STEP_UINT)
+ if (specifier_to_step(format.specifier) == STEP_UINT)
arg.v_uint = va_arg(args, unsigned);
- if (specifier_to_step(step.specifier) == STEP_PTR)
+ if (specifier_to_step(format.specifier) == STEP_PTR)
arg.v_ptr = va_arg(args, void *);
}
+ va_end(args);
return (count);
}
+
+int main(void)
+{
+ ft_printf("c%-10s", NULL);
+}