]> Untitled Git - axy/ft/ft_printf.git/commitdiff
String printing
author= <=>
Thu, 30 Oct 2025 16:33:29 +0000 (17:33 +0100)
committer= <=>
Thu, 30 Oct 2025 16:33:29 +0000 (17:33 +0100)
a.out [new file with mode: 0755]
ft_printf.c

diff --git a/a.out b/a.out
new file mode 100755 (executable)
index 0000000..5fa0ae8
Binary files /dev/null and b/a.out differ
index a1dcda58863122cd8c2c5a1de3a90575d63e7996..ac6b3951c4f87bbc107267e172ad6ec37a07c43f 100644 (file)
@@ -6,12 +6,11 @@
 /*   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>
 
@@ -81,13 +80,69 @@ typedef struct s_format
        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)
 {
@@ -104,9 +159,9 @@ 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')
@@ -182,7 +237,9 @@ t_format    ft_parse_format(const char **s)
 
        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);
 }
@@ -191,18 +248,16 @@ int       ft_print_step(const char **s, t_format *format, t_step_arg arg)
 {
        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);
 }
@@ -223,24 +278,30 @@ int       ft_printf(const char *s, ...)
        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);
+}