-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
101 lines (92 loc) · 2.82 KB
/
ft_printf.c
File metadata and controls
101 lines (92 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 19:26:03 by ldulling #+# #+# */
/* Updated: 2023/10/22 20:29:29 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void reset_format(t_struct *f);
static int parseandprint(const char *format, int *i, t_struct *f, va_list *ap);
static int print_argument(t_struct *f, va_list *ap);
int ft_printf(const char *format, ...)
{
int i;
int printed;
int temp;
t_struct f;
va_list ap;
if (format == NULL)
return (-1);
va_start(ap, format);
f.unresolved = 0;
printed = 0;
i = 0;
while (format[i])
{
reset_format(&f);
temp = parseandprint(format, &i, &f, &ap);
if (temp == -1)
{
printed = temp;
break ;
}
printed += temp;
}
va_end(ap);
return (printed);
}
static void reset_format(t_struct *f)
{
f->hash = 0;
f->plus = 0;
f->space = 0;
f->minus = 0;
f->zero = 0;
f->width = 0;
f->precision = NO_PRECISION_SET;
f->specifier = '\0';
return ;
}
static int parseandprint(const char *format, int *i, t_struct *f, va_list *ap)
{
int printed;
int parsed;
printed = 0;
parsed = 1;
if (format[(*i)++] == '%')
{
parsed += set_format(format, i, f, ap);
if (format[*i] == '\0' && !f->specifier && !f->unresolved)
return (-1);
if (f->specifier)
printed += print_argument(f, ap);
}
if (!f->specifier)
printed += print_parsed(&format[*i - parsed], parsed, f);
return (printed);
}
static int print_argument(t_struct *f, va_list *ap)
{
int printed;
printed = 0;
if (f->specifier == 'c')
printed = print_char((unsigned char) va_arg(*ap, int), f);
else if (f->specifier == 's')
printed = print_str(va_arg(*ap, const char *), f);
else if (f->specifier == 'p')
printed = print_ptr((size_t) va_arg(*ap, void *), f);
else if (f->specifier == 'd' || f->specifier == 'i')
printed = print_nbr((long) va_arg(*ap, int), f);
else if (f->specifier == 'u')
printed = print_nbr((long) va_arg(*ap, unsigned int), f);
else if (f->specifier == 'x' || f->specifier == 'X')
printed = print_nbr((long) va_arg(*ap, unsigned int), f);
else if (f->specifier == '%')
printed = ft_putnchar_fd('%', 1, FD);
return (printed);
}