-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
169 lines (154 loc) · 2.27 KB
/
ft_printf.c
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
# include <stdarg.h>
# include <unistd.h>
# include <stdlib.h>
int p;
int w;
int pf_strlen(char *s)
{
if (!s)
return 6;
char *start = s;
while (*s)
++s;
return (s - start);
}
int ft_min(int x, int y)
{
return (x < y) ? x : y;
}
int print_pad(char sym, int pad)
{
if (pad <= 0)
return 0;
int i = pad + 1;
while (--i)
write(1, &sym, 1);
return (pad);
}
int count_digits(size_t n, size_t bs)
{
int i = 1;
while (n / bs)
{
n /= bs;
++i;
}
return i;
}
int print_num(size_t n, size_t bs, int len)
{
char res[len];
char digits[] = "0123456789abcdef";
int i = len - 1;
while (i >= 0)
{
res[i] = digits[n % bs];
n /= bs;
--i;
}
return (write(1, res, len));
}
int print_str(char *s, int len)
{
if (!s)
return (write(1,"(null)",len));
else
return (write(1, s, len));
}
void atoi_wp(const char **fmt, int *value)
{
int n = 0;
while (**fmt >= '0' && **fmt <= '9')
{
n = n * 10 + (**fmt - '0');
++*fmt;
}
*value = n;
}
void print_fmt(const char **fmt, va_list arg, int *ret)
{
int arglen;
int sign = 0;
size_t n;
char *s;
char c = **fmt;
if (!c)
return;
else
++*fmt;
if (c == 's')
{
s = va_arg(arg, char *);
arglen = pf_strlen(s);
if (p > -1)
arglen = ft_min(p, arglen);
p = 0;
w = w - arglen;
}
else
{
if (c == 'x')
{
n = (size_t)va_arg(arg, unsigned);
arglen = count_digits(n, 16);
}
if (c == 'd')
{
long d = (long)va_arg(arg, int);
if (d < 0)
{
n = (size_t)(-d);
sign = 1;
}
else
n = (size_t)d;
arglen = count_digits(n, 10);
}
if (n == 0 && p == 0)
arglen = 0;
if (p > 0)
p = p - arglen;
if (p < 0)
p = 0;
w = w - p - arglen - sign;
}
*ret += print_pad(' ', w);
if (sign)
*ret += write(1, "-", 1);
*ret += print_pad('0', p);
if (c == 's')
*ret += print_str(s, arglen);
if (c == 'd')
*ret += print_num(n, 10, arglen);
if (c == 'x')
*ret += print_num(n, 16, arglen);
}
int ft_printf(const char *fmt, ... )
{
va_list arg;
int ret;
if (!fmt)
return (-1);
ret = 0;
va_start(arg, fmt);
while (*fmt)
{
if (*fmt != '%')
ret += write(1, fmt++, 1);
if (*fmt == '%')
{
w = 0;
p = -1;
++fmt;
atoi_wp(&fmt, &w);
if (*fmt == '.')
{
++fmt;
atoi_wp(&fmt, &p);
}
print_fmt(&fmt, arg, &ret);
}
}
va_end(arg);
return ret;
}