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
126 extern void io_long (unsigned long);
127 extern void io_str (char *);
128 extern void io_int (unsigned int);
132 vfprintf (FILE * stream, const char *fmt, va_list ap)
138 #ifdef FLOAT_PLACEHOLDER
139 unsigned char ffloat;
141 unsigned char nosign;
142 unsigned char upcase;
144 unsigned char fieldwidth;
145 unsigned char lalign;
150 unsigned char precision;
152 #ifdef SIGN_MODIFIERS
153 unsigned char printsign;
159 // static char buffer[16];
163 io_str ("vfprintf: ");
164 io_long ((unsigned long) stream);
165 io_long ((unsigned long) fmt);
171 while (*ch) //for (; *fmt ; fmt++ )
178 #ifdef FLOAT_PLACEHOLDER
190 // precision == -1 is used as an "unlimited" precision marker
193 #ifdef SIGN_MODIFIERS
201 __stream_putchar (stream, *ch);
220 #ifdef SIGN_MODIFIERS
236 if ((*ch >= '1') && (*ch <= '9'))
238 while ((*ch >= '0') && (*ch <= '9'))
240 fieldwidth = 10 * fieldwidth + (*ch) - '0';
251 while ((*ch >= '0') && (*ch <= '9'))
253 precision = 10 * precision + (*ch) - '0';
280 padchar = ' '; /* Strings are always space-padded */
294 #ifdef BINARX_SPECIFIER
298 #ifdef FLOAT_PLACEHOLDER
308 else if ((*ch == 'd') || (*ch == 'i')) /* This is the default */
310 else if (nosign) /* %u alone is the same as %ud */
318 __stream_putchar (stream, *ch);
326 str = va_arg (ap, char *);
327 #ifdef FLOAT_PLACEHOLDER
338 precision = -1; //FIXME: No support for the precision field on numerals
343 val = va_arg (ap, long);
350 val = (char) va_arg (ap, int); // FIXME: SDCC passes 1-byte char varargs as 2-byte ints...
351 if ((radix != 10) || nosign)
352 val = (unsigned char) val; //Avoid unwanted sign extension
359 val = va_arg (ap, int);
360 if ((radix != 10) || nosign)
361 val = (unsigned int) val; //Avoid unwanted sign extension
367 str = buffer + 1; //Reserve space for a forced '+'
371 ultoa (val, buffer + 1, radix);
373 ltoa (val, buffer + 1, radix);
374 #ifdef SIGN_MODIFIERS
375 if (printsign && (*str != '-'))
384 *str = (unsigned char) val;
390 //Count how many pad chars are required in fieldwidth
392 while (fieldwidth && *str1)
402 __stream_putchar (stream, padchar);
410 && (!~precision || precision--)
417 radix = toupper (radix);
419 __stream_putchar (stream, radix);
424 //Right padding (with spaces)
429 __stream_putchar (stream, ' ');
438 __stream_putchar (stream, *ch);