1 /*-----------------------------------------------------------------
2 vfprintf.c - source file for reduced version of printf
4 Modified for pic16 port, by Vangelis Rokas, 2005 (vrokas@otenet.gr)
5 Bug-fixed and feature-enhanced by Mauro Giachero, 2008 (mauro.giachero@gmail.com)
7 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
9 This library is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding!
27 As a special exception, if you link this library with other files,
28 some of which are compiled with SDCC, to produce an executable,
29 this library does not by itself cause the resulting executable
30 to be covered by the GNU General Public License.
31 This exception does not however invalidate any other reasons why
32 the executable file might be covered by the GNU General Public License.
33 -------------------------------------------------------------------------*/
35 /* following formats are supported :-
36 format output type argument-type
51 %l[xX] hexadecimal long
52 %h[xX] hexadecimal char
57 %s character generic pointer
59 - the '0', '-' and ' ' alignment modifiers
60 - the '+' and ' ' modifiers
61 - the width field for integral types
62 - the precision field for strings
70 /***********************************************************
71 * The following switches enable some "advanced" features. *
72 * With all the switches enabled: *
74 * ; code size: 2062 (0x080e) bytes ( 1.57%) *
75 * ; 1031 (0x0407) words *
76 * ; udata size: 16 (0x0010) bytes ( 1.25%) *
77 * ; access size: 31 (0x001f) bytes *
78 * With all the switches disabled: *
80 * ; code size: 1278 (0x04fe) bytes ( 0.98%) *
81 * ; 639 (0x027f) words *
82 * ; udata size: 16 (0x0010) bytes ( 1.25%) *
83 * ; access size: 25 (0x0019) bytes *
84 ***********************************************************/
86 * Define this to enable support of the field width, which
87 * allows to specify the minimum number of characters an
89 * Costs ~200 code words and 3 bytes in access RAM.
93 * Define this to enable support of the precision, which
94 * allows to specify the maximum number of characters a
95 * string can use. Note that this implementation doesn't
96 * use this field for integers (as it should).
97 * Costs ~85 code words and 1 byte in access RAM.
101 * Define this to enable support of the '+' and ' ' modifiers,
102 * which specify that a positive signed number must be
103 * preceded respectively with a '+' or a ' ' character.
104 * Costs ~70 code words and 2 words of access RAM
106 #define SIGN_MODIFIERS
108 * With this macro defined, trying to print a float number
109 * will generate the "<NO FLOAT>" string.
110 * Costs ~25 code words
112 #define FLOAT_PLACEHOLDER
114 * This macro enables the use of the 'b' binary specifier and
115 * the use of "%b", "%hb" and "%lb"
117 /* #define BINARY_SPECIFIER */
119 * This macro enables the use of the 'i' integer specifier and
120 * the use of "%u", "%lu", ... in place of "%ud", "%lud", ... .
121 * costs ~10 code words
123 #define EXTRA_INTEGER
125 #ifdef BINARY_SPECIFIER
126 /* "%lb" = "0" - "11111111111111111111111111111111" */
129 /* "%lo" = "0" - "37777777777" or "-20000000000" - "17777777777" */
134 extern void io_long (unsigned long);
135 extern void io_str (char *);
136 extern void io_int (unsigned int);
140 vfprintf (FILE * stream, const char *fmt, va_list ap)
146 #ifdef FLOAT_PLACEHOLDER
147 unsigned char ffloat;
149 unsigned char nosign;
150 unsigned char upcase;
152 unsigned char fieldwidth;
153 unsigned char lalign;
158 unsigned char precision;
160 #ifdef SIGN_MODIFIERS
161 unsigned char printsign;
167 char buffer[BUF_SIZE];
170 io_str ("vfprintf: ");
171 io_long ((unsigned long) stream);
172 io_long ((unsigned long) fmt);
178 while (*ch) //for (; *fmt ; fmt++ )
185 #ifdef FLOAT_PLACEHOLDER
197 // precision == -1 is used as an "unlimited" precision marker
200 #ifdef SIGN_MODIFIERS
208 __stream_putchar (stream, *ch);
227 #ifdef SIGN_MODIFIERS
243 if ((*ch >= '1') && (*ch <= '9'))
245 while ((*ch >= '0') && (*ch <= '9'))
247 fieldwidth = 10 * fieldwidth + (*ch) - '0';
258 while ((*ch >= '0') && (*ch <= '9'))
260 precision = 10 * precision + (*ch) - '0';
287 padchar = ' '; /* Strings are always space-padded */
301 #ifdef BINARX_SPECIFIER
305 #ifdef FLOAT_PLACEHOLDER
315 else if ((*ch == 'd') || (*ch == 'i')) /* This is the default */
317 else if (nosign) /* %u alone is the same as %ud */
325 __stream_putchar (stream, *ch);
333 str = va_arg (ap, char *);
334 #ifdef FLOAT_PLACEHOLDER
345 precision = -1; //FIXME: No support for the precision field on numerals
350 val = va_arg (ap, long);
357 val = (char) va_arg (ap, int); // FIXME: SDCC passes 1-byte char varargs as 2-byte ints...
358 if ((radix != 10) || nosign)
359 val = (unsigned char) val; //Avoid unwanted sign extension
366 val = va_arg (ap, int);
367 if ((radix != 10) || nosign)
368 val = (unsigned int) val; //Avoid unwanted sign extension
374 str = buffer + 1; //Reserve space for a forced '+'
378 ultoa (val, buffer + 1, radix);
380 ltoa (val, buffer + 1, radix);
381 #ifdef SIGN_MODIFIERS
382 if (printsign && (*str != '-'))
391 *str = (unsigned char) val;
397 //Count how many pad chars are required in fieldwidth
399 while (fieldwidth && *str1)
409 __stream_putchar (stream, padchar);
417 && (!~precision || precision--)
424 radix = toupper (radix);
426 __stream_putchar (stream, radix);
431 //Right padding (with spaces)
436 __stream_putchar (stream, ' ');
445 __stream_putchar (stream, *ch);