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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/*-------------------------------------------------------------------------
vfprintf.c - source file for reduced version of printf
Copyright (C) 1999, Sandeep Dutta <sandeep.dutta AT ieee.org>
Modified for pic16 port, by Vangelis Rokas, 2005 <vrokas AT otenet.gr>
Bug-fixed and feature-enhanced by Mauro Giachero, 2008 <mauro.giachero AT gmail.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
As a special exception, if you link this library with other files,
some of which are compiled with SDCC, to produce an executable,
this library does not by itself cause the resulting executable to
be covered by the GNU General Public License. This exception does
not however invalidate any other reasons why the executable file
might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/
/* following formats are supported :-
format output type argument-type
%% - -
%u unsigned int
%u* unsigned *
%b binary int
%lb binary long
%hb binary char
%d decimal int
%lu unsigned long
%hu unsigned char
%l[di] decimal long
%lu[di] unsigned long
%h[di] decimal char
%hu[di] unsigned char
%[xX] hexadecimal int
%l[xX] hexadecimal long
%h[xX] hexadecimal char
%o octal int
%lo octal long
%ho octal char
%c character char
%s character generic pointer
Also supported are:
- the '0', '-' and ' ' alignment modifiers
- the '+' and ' ' modifiers
- the width field for integral types
- the precision field for strings
*/
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
/***********************************************************
* The following switches enable some "advanced" features. *
* With all the switches enabled: *
* ; Statistics: *
* ; code size: 2062 (0x080e) bytes ( 1.57%) *
* ; 1031 (0x0407) words *
* ; udata size: 16 (0x0010) bytes ( 1.25%) *
* ; access size: 31 (0x001f) bytes *
* With all the switches disabled: *
* ; Statistics: *
* ; code size: 1278 (0x04fe) bytes ( 0.98%) *
* ; 639 (0x027f) words *
* ; udata size: 16 (0x0010) bytes ( 1.25%) *
* ; access size: 25 (0x0019) bytes *
***********************************************************/
/*
* Define this to enable support of the field width, which
* allows to specify the minimum number of characters an
* integer must use.
* Costs ~200 code words and 3 bytes in access RAM.
*/
#define FIELD_WIDTH
/*
* Define this to enable support of the precision, which
* allows to specify the maximum number of characters a
* string can use. Note that this implementation doesn't
* use this field for integers (as it should).
* Costs ~85 code words and 1 byte in access RAM.
*/
#define PRECISION
/*
* Define this to enable support of the '+' and ' ' modifiers,
* which specify that a positive signed number must be
* preceded respectively with a '+' or a ' ' character.
* Costs ~70 code words and 2 words of access RAM
*/
#define SIGN_MODIFIERS
/*
* With this macro defined, trying to print a float number
* will generate the "<NO FLOAT>" string.
* Costs ~25 code words
*/
#define FLOAT_PLACEHOLDER
/*
* With this macro defined, printing floats will work.
* This also enables PRECISION and disables FLOAT_PLACEHOLDER.
*/
#if defined(USE_FLOATS)
/* The configure script always defines USE_FLOATS to 0 or 1. */
# if USE_FLOATS < 1
# undef USE_FLOATS
# endif
#else
/* # define USE_FLOATS */
#endif
#if defined(USE_FLOATS)
#define PRECISION
#undef FLOAT_PLACEHOLDER
#endif
/*
* This macro enables the use of the 'b' binary specifier and
* the use of "%b", "%hb" and "%lb"
*/
/* #define BINARY_SPECIFIER */
/*
* This macro enables the use of the 'i' integer specifier and
* the use of "%u", "%lu", ... in place of "%ud", "%lud", ... .
* costs ~10 code words
*/
#define EXTRA_INTEGER
#if defined(USE_FLOATS)
/* x_ftoa requires up to 8 digits (integral part) + '.' + 24 digits
* (fractional part). Adding a sign and a NUL byte yields 35 byte. */
# define BUF_SIZE 36
#elif defined(BINARY_SPECIFIER)
/* "%lb" = "0" - "11111111111111111111111111111111" */
# define BUF_SIZE 33
#else
/* "%lo" = "0" - "37777777777" or "-20000000000" - "17777777777" */
# define BUF_SIZE 13
#endif
#if _DEBUG
extern void io_long (unsigned long);
extern void io_str (char *);
extern void io_int (unsigned int);
#endif
int
vfprintf (FILE * stream, const char *fmt, va_list ap)
{
unsigned char radix;
unsigned char flong;
unsigned char fstr;
unsigned char fchar;
#if defined(FLOAT_PLACEHOLDER) || defined(USE_FLOATS)
unsigned char ffloat;
#endif
unsigned char nosign;
unsigned char upcase;
#ifdef FIELD_WIDTH
unsigned char fieldwidth;
unsigned char lalign;
char padchar;
const char *str1;
#endif
#ifdef PRECISION
unsigned char precision;
#endif
#ifdef SIGN_MODIFIERS
unsigned char printsign;
char positivechar;
#endif
int count = 0;
const char *ch;
char *str;
long val;
char buffer[BUF_SIZE];
char *stringbuffer;
if (0x80 == (unsigned char)(((unsigned long)stream) >> 16)) {
/* strmputchar will modify *(char **)stream, thus confusing the user */
stringbuffer = (char *) stream;
stream = (FILE *) &stringbuffer;
}
#if _DEBUG
io_str ("vfprintf: ");
io_long ((unsigned long) stream);
io_long ((unsigned long) fmt);
#endif
// va_start(ap,fmt);
ch = fmt;
while (*ch) //for (; *fmt ; fmt++ )
{
if (*ch == '%')
{
flong = 0;
fstr = 0;
fchar = 0;
#if defined(FLOAT_PLACEHOLDER) || defined(USE_FLOATS)
ffloat = 0;
#endif
nosign = 0;
radix = 10;
upcase = 0;
#ifdef FIELD_WIDTH
fieldwidth = 0;
lalign = 0;
padchar = ' ';
#endif
#ifdef PRECISION
// precision == -1 is used as an "unlimited" precision marker
precision = (unsigned char)-1;
#endif
#ifdef SIGN_MODIFIERS
printsign = 0;
positivechar = '+';
#endif
++ch;
if (*ch == '%')
{
__stream_putchar (stream, *ch);
++count;
++ch;
continue;
}
#ifdef FIELD_WIDTH
if (*ch == '0')
{
padchar = '0';
++ch;
}
if (*ch == '-')
{
lalign = 1;
++ch;
}
#endif
#ifdef SIGN_MODIFIERS
if (*ch == ' ')
{
printsign = 1;
positivechar = ' ';
++ch;
}
if (*ch == '+')
{
printsign = 1;
++ch;
}
#endif
#ifdef FIELD_WIDTH
if ((*ch >= '1') && (*ch <= '9'))
{
while ((*ch >= '0') && (*ch <= '9'))
{
fieldwidth = 10 * fieldwidth + (*ch) - '0';
++ch;
}
}
#endif
#ifdef PRECISION
if (*ch == '.')
{
++ch;
precision = 0;
while ((*ch >= '0') && (*ch <= '9'))
{
precision = 10 * precision + (*ch) - '0';
++ch;
}
}
#endif
if (*ch == 'l')
{
flong = 1;
++ch;
}
else if (*ch == 'h')
{
fchar = 1;
++ch;
}
if (*ch == 'u')
{
nosign = 1;
++ch;
}
if (*ch == 's')
{
fstr = 1;
#ifdef FIELD_WIDTH
padchar = ' '; /* Strings are always space-padded */
#endif
}
else if (*ch == 'x')
radix = 16;
else if (*ch == 'X')
{
radix = 16;
upcase = 1;
}
else if (*ch == 'c')
radix = 0;
else if (*ch == 'o')
radix = 8;
#ifdef BINARX_SPECIFIER
else if (*ch == 'b')
radix = 2;
#endif
#if defined(FLOAT_PLACEHOLDER) || defined(USE_FLOATS)
else if (*ch == 'f')
{
ffloat = 1;
}
#endif
#ifdef EXTRA_INTEGER
else if ((*ch == 'd') || (*ch == 'i')) /* This is the default */
;
else if (nosign) /* %u alone is the same as %ud */
--ch;
#else
else if (*ch == 'd')
;
#endif
else
{
__stream_putchar (stream, *ch);
++count;
++ch;
continue;
}
if (fstr)
{
str = va_arg (ap, char *);
#if defined(USE_FLOATS)
}
else if (ffloat)
{
float f = va_arg(ap, float);
str = buffer;
x_ftoa (f, buffer, BUF_SIZE, precision);
precision = -1;
#elif defined(FLOAT_PLACEHOLDER)
}
else if (ffloat)
{
str = (char*)"<NO FLOAT>";
va_arg (ap, float);
#ifdef PRECISION
precision = (unsigned char)-1;
#endif /* PRECISION */
#endif /* FLOAT_PLACEHOLDER */
}
else
{
#ifdef PRECISION
precision = (unsigned char)-1; //FIXME: No support for the precision field on numerals
#endif
val = 0;
if (flong)
{
val = va_arg (ap, long);
#if _DEBUG
io_long (val);
#endif
}
else if (fchar)
{
val = (char) va_arg (ap, int); // FIXME: SDCC passes 1-byte char varargs as 2-byte ints...
if ((radix != 10) || nosign)
val = (unsigned char) val; //Avoid unwanted sign extension
#if _DEBUG
io_long (val);
#endif
}
else
{
val = va_arg (ap, int);
if ((radix != 10) || nosign)
val = (unsigned int) val; //Avoid unwanted sign extension
#if _DEBUG
io_long (val);
#endif
}
str = buffer + 1; //Reserve space for a forced '+'
if (radix)
{
if (nosign)
ultoa (val, buffer + 1, radix);
else
ltoa (val, buffer + 1, radix);
#ifdef SIGN_MODIFIERS
if (printsign && (*str != '-'))
{
--str;
*str = positivechar;
}
#endif
}
else
{
*str = (unsigned char) val;
*(str + 1) = '\0';
}
}
#ifdef FIELD_WIDTH
//Count how many pad chars are required in fieldwidth
str1 = str;
while (fieldwidth && *str1)
{
++str1;
--fieldwidth;
}
//Left padding
if (!lalign)
{
while (fieldwidth)
{
__stream_putchar (stream, padchar);
++count;
--fieldwidth;
}
}
#endif
while (*str
#ifdef PRECISION
&& (!~precision || precision--)
#endif
)
{
radix = *str;
if (upcase)
{
radix = toupper (radix);
}
__stream_putchar (stream, radix);
++str;
++count;
if (fieldwidth)
{
fieldwidth--;
}
}
#ifdef FIELD_WIDTH
//Right padding (with spaces)
if (lalign)
{
while (fieldwidth)
{
__stream_putchar (stream, ' ');
++count;
--fieldwidth;
}
}
#endif
}
else
{
__stream_putchar (stream, *ch);
++count;
}
++ch;
}
return count;
}
|