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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
typedef struct
{
int precision;
int width;
char specifier;
} t_format_spec;
void ft_putchar(char c)
{
write(STDOUT_FILENO, &c, 1);
}
int ft_strlen(char *s)
{
int c;
c = 0;
while (s[c])
c++;
return (c);
}
void ft_putstr(char *s)
{
write(STDOUT_FILENO, s, ft_strlen(s));
}
int ft_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
int ft_atoi_skip(char **s)
{
int nb;
int is_neg;
is_neg = **s == '-';
nb = 0;
while (ft_isdigit(**s))
{
nb *= 10;
nb += **s - '0';
(*s)++;
}
return (is_neg ? -nb : nb);
}
char *parse(char *format, t_format_spec *spec)
{
int tmp;
if (ft_isdigit(*format))
tmp = ft_atoi_skip(&format);
if (*format == '.')
{
spec->precision = tmp;
if (ft_isdigit(*++format))
spec->width = ft_atoi_skip(&format);
}
else
spec->width = tmp;
if (*format != 's' && *format != 'd' && *format != 'x')
return (NULL);
spec->specifier = *format++;
return (format);
}
int count_digits(int nb, int radix)
{
int counter;
counter = nb < 0 ? 1 : 0;
while (nb > 0)
{
nb /= radix;
counter++;
}
return (counter);
}
char *ft_itoa_base(int nb, char *base)
{
int len;
int radix;
char *s;
radix = ft_strlen(base);
len = count_digits(nb, radix);
if ((s = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
return (NULL);
s[len] = 0;
if (nb < 0)
s[0] = '-';
while (--len >= (nb < 0 ? 1 : 0))
{
s[len] = base[nb % radix];
nb /= radix;
}
return (s);
}
int handle_s(va_list ap, t_format_spec *spec)
{
char *tmp;
tmp = va_arg(ap, char*);
ft_putstr(tmp);
return (ft_strlen(tmp));
}
int handle_d(va_list ap, t_format_spec *spec)
{
int tmp;
char *s;
tmp = va_arg(ap, int);
s = ft_itoa_base(tmp, "0123456789");
ft_putstr(s);
return ft_strlen(s);
}
int handle_x(va_list ap, t_format_spec *spec)
{
int tmp;
char *s;
tmp = va_arg(ap, int);
s = ft_itoa_base(tmp, "0123456789abcdef");
ft_putstr(s);
return ft_strlen(s);
}
int print_spec(va_list ap, t_format_spec *spec)
{
if (spec->specifier == 's')
return (handle_s(ap, spec));
else if (spec->specifier == 'd')
return (handle_d(ap, spec));
else if (spec->specifier == 'x')
return (handle_x(ap, spec));
else
return (-1);
}
int ft_printf(char *format, ...)
{
va_list ap;
int counter;
t_format_spec spec;
va_start(ap, format);
counter = 0;
while (*format)
{
if (*format == '%')
{
if ((format = parse(format + 1, &spec)) == NULL)
return (-1);
counter += print_spec(ap, &spec);
}
else
{
ft_putchar(*format++);
counter++;
}
}
va_end(ap);
return (counter);
}
|