-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfmt.c
267 lines (235 loc) · 5.81 KB
/
fmt.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "cc.h"
#define MAX_FMT 64
static int badconv(struct fmt *);
struct convfmt {
int c;
int (*fmt)(struct fmt *);
};
static struct {
int nfmt;
struct convfmt fmt[MAX_FMT];
} fmtalloc;
int fmtinstall(int c, int (*fp)(struct fmt *))
{
struct convfmt *p, *q;
if (c <= 0 || c >= 256)
return -1;
if (!fp)
fp = badconv;
q = &fmtalloc.fmt[fmtalloc.nfmt];
for (p = fmtalloc.fmt; p < q; p++)
if (p->c == c)
break;
if (p == &fmtalloc.fmt[MAX_FMT])
return -1;
p->fmt = fp;
if (p == q) {
fmtalloc.nfmt++;
p->c = c;
}
return 0;
}
static int fmtflush(struct fmt *fp)
{
if (fp->stream) {
for (char *p = fp->start; p < fp->to; p++)
fputc(*p, fp->stream);
fp->nfmt += fp->to - fp->start;
fp->to = fp->start;
}
return 0;
}
static int fmtputc(struct fmt *fp, int c)
{
if (fp->to < fp->stop) {
*fp->to = c;
++fp->to;
}
if (fp->to == fp->stop)
return fmtflush(fp);
return 0;
}
static int fmtputs(struct fmt *fp, const char *s)
{
if (!s)
return -1;
for (int c; (c = *s); s++) {
if (fp->to < fp->stop) {
*fp->to = c;
++fp->to;
}
if (fp->to == fp->stop)
fmtflush(fp);
}
return 0;
}
static int badconv(struct fmt *fp)
{
return fmtputc(fp, fp->c);
}
static int dofmt(struct fmt *fp, const char *fmt)
{
struct convfmt *p, *q;
char buf[256], *str;
int nfmt = fp->nfmt;
q = &fmtalloc.fmt[fmtalloc.nfmt];
for (; *fmt; fmt++) {
if (*fmt != '%') {
fmtputc(fp, *fmt);
continue;
}
int c = *++fmt;
for (p = fmtalloc.fmt; p < q; p++)
if (p->c == c)
break;
if (p < q) {
fp->c = c;
p->fmt(fp);
continue;
}
switch (c) {
case 'c':
snprintf(buf, sizeof(buf), "%c", va_arg(fp->args, int));
break;
case 'd':
case 'i':
snprintf(buf, sizeof(buf), "%d", va_arg(fp->args, int));
break;
case 'u':
snprintf(buf, sizeof(buf), "%u", va_arg(fp->args, unsigned int));
break;
case 'x':
snprintf(buf, sizeof(buf), "%x", va_arg(fp->args, int));
break;
case 'X':
snprintf(buf, sizeof(buf), "%X", va_arg(fp->args, int));
break;
case 'o':
snprintf(buf, sizeof(buf), "%o", va_arg(fp->args, int));
break;
case 's':
str = va_arg(fp->args, char *);
fmtputs(fp, str);
buf[0] = '\0';
break;
case 'p':
snprintf(buf, sizeof(buf), "%p", va_arg(fp->args, void *));
break;
case 'f':
snprintf(buf, sizeof(buf), "%f", va_arg(fp->args, double));
break;
case 'l':
if (fmt[1] == 'd') {
fmt++;
snprintf(buf, sizeof(buf), "%ld", va_arg(fp->args, long));
} else if (fmt[1] == 'u') {
fmt++;
snprintf(buf, sizeof(buf), "%lu", va_arg(fp->args, unsigned long));
} else if (fmt[1] == 'x') {
fmt++;
snprintf(buf, sizeof(buf), "%lx", va_arg(fp->args, long));
} else if (fmt[1] == 'X') {
fmt++;
snprintf(buf, sizeof(buf), "%lX", va_arg(fp->args, long));
} else if (fmt[1] == 'l' && fmt[2] == 'd') {
fmt += 2;
snprintf(buf, sizeof(buf), "%lld", va_arg(fp->args, long long));
} else if (fmt[1] == 'l' && fmt[2] == 'u') {
fmt += 2;
snprintf(buf, sizeof(buf), "%llu", va_arg(fp->args, unsigned long long));
} else {
snprintf(buf, sizeof(buf), "%c", *fmt);
}
break;
case 'L':
if (fmt[1] == 'f') {
fmt++;
snprintf(buf, sizeof(buf), "%Lf", va_arg(fp->args, long double));
} else {
snprintf(buf, sizeof(buf), "%c", *fmt);
}
break;
default:
snprintf(buf, sizeof(buf), "%c", *fmt);
break;
}
fmtputs(fp, buf);
}
fmtflush(fp);
return fp->nfmt - nfmt;
}
int print(const char *fmt, ...)
{
va_list ap;
int n;
va_start(ap, fmt);
n = vfprint(stdout, fmt, ap);
va_end(ap);
return n;
}
int vfprint(FILE *stream, const char *fmt, va_list ap)
{
struct fmt fp;
char buf[256];
va_copy(fp.args, ap);
fp.start = buf;
fp.stop = buf + sizeof(buf);
fp.to = buf;
fp.stream = stream;
fp.nfmt = 0;
return dofmt(&fp, fmt);
}
int fprint(FILE *stream, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vfprint(stream, fmt, ap);
va_end(ap);
return n;
}
int vsnprint(char *buf, size_t size, const char *fmt, va_list ap)
{
struct fmt fp;
if (size <= 0)
return -1;
va_copy(fp.args, ap);
fp.start = buf;
fp.stop = buf + size - 1;
fp.to = buf;
fp.stream = NULL;
fp.nfmt = 0;
dofmt(&fp, fmt);
*fp.to = '\0';
return fp.to - buf;
}
int snprint(char *s, size_t size, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vsnprint(s, size, fmt, ap);
va_end(ap);
return n;
}
int fmtstrcpy(struct fmt *fp, const char *s)
{
int n;
n = fmtputs(fp, s);
if (n >= 0)
return 0;
return n;
}
int fmtprint(struct fmt *fp, const char *fmt, ...)
{
int n;
va_list ap;
va_copy(ap, fp->args);
va_start(fp->args, fmt);
n = dofmt(fp, fmt);
va_end(fp->args);
va_copy(fp->args, ap);
if (n >= 0)
return 0;
return n;
}