struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / pic16 / libc / stdio / printf_tiny.c
blob1d3eb787c02f1fbbac36bdafa1edc7adb5fcc100
1 /*-------------------------------------------------------------------------
2 printf_tiny.c - source file for reduced version of printf
4 Copyright (C) 1999, Sandeep Dutta <sandeep.dutta AT ieee.org>
5 Modified for pic16 port, by Vangelis Rokas, 2004 <vrokas AT otenet.gr>
7 This library is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA.
22 As a special exception, if you link this library with other files,
23 some of which are compiled with SDCC, to produce an executable,
24 this library does not by itself cause the resulting executable to
25 be covered by the GNU General Public License. This exception does
26 not however invalidate any other reasons why the executable file
27 might be covered by the GNU General Public License.
28 -------------------------------------------------------------------------*/
30 /* This function uses function putchar() to dump a character
31 * to standard output. putchar() is defined in libc18f.lib
32 * as dummy function, which will be linked if no putchar()
33 * function is provided by the user.
34 * The user can write his own putchar() function and link it
35 * with the source *BEFORE* the libc18f.lib library. This way
36 * the linker will link the first function (i.e. the user's function) */
38 /* following formats are supported :-
39 format output type argument-type
40 %u* unsigned *
42 %b binary
43 %d decimal int
44 %ld decimal long
45 %hd decimal char
46 %x hexadecimal int
47 %lxX hexadecimal long
48 %hxX hexadecimal char
49 %o octal int
50 %lo octal long
51 %ho octal char
52 %c character char
53 %s character generic pointer
57 * This macro enables the use of the 'b' binary specifier and
58 * the use of "%b", "%hb" and "%lb"
60 /* #define BINARY_SPECIFIER */
62 #include <ctype.h>
63 #include <stdarg.h>
64 #include <stdio.h>
65 #include <stdlib.h>
67 #if 0
68 #define DPUT(c) putchar(c)
69 #else
70 #define DPUT(c)
71 #endif
73 #define ISLONG (flong)
74 #define ISSTR (fstr)
75 #define ISCHAR (fchar)
76 #define HAVESIGN (nosign)
78 #ifdef BINARY_SPECIFIER
79 /* "%lb" = "0" - "11111111111111111111111111111111" */
80 # define BUF_SIZE 33
81 #else
82 /* "%lo" = "0" - "37777777777" or "-21777777777" - "17777777777" */
83 # define BUF_SIZE 13
84 #endif
86 void
87 printf_tiny (const char *fmt, ...)
89 char radix;
90 char flong, fstr;
91 char fchar, nosign;
92 char upcase;
94 const char *str, *ch;
95 __data char *str1;
96 long val;
97 char buffer[BUF_SIZE];
98 va_list ap;
100 va_start (ap, fmt);
101 ch = fmt;
103 while (*ch) //for (; *fmt ; fmt++ )
105 if (*ch == '%')
107 ISLONG = 0;
108 ISSTR = 0;
109 ISCHAR = 0;
110 HAVESIGN = 0;
111 radix = 0;
112 upcase = 0;
113 ch++;
115 if (*ch == 'u')
117 HAVESIGN = 1;
118 ++ch;
121 if (*ch == 'l')
123 ISLONG = 1;
124 ++ch;
126 else if (*ch == 'h')
128 ISCHAR = 1;
129 ++ch;
132 if (*ch == 's')
133 ISSTR = 1;
134 else if (*ch == 'd')
135 radix = 10;
136 else if (*ch == 'x')
138 radix = 16;
139 upcase = 0;
141 else if (*ch == 'X')
143 radix = 16;
144 upcase = 1;
146 else if (*ch == 'c')
147 radix = 0;
148 else if (*ch == 'o')
149 radix = 8;
150 #ifdef BINARX_SPECIFIER
151 else if (*ch == 'b')
152 radix = 2;
153 #endif
155 if (ISSTR)
157 str = va_arg (ap, const char *);
158 while (*str)
160 putchar (*str);
161 ++str;
164 else
166 if (ISLONG)
167 val = va_arg (ap, long);
168 else if (ISCHAR)
170 val = (unsigned char) va_arg (ap, int); // FIXME: SDCC casts char arguments into ints
171 if (!HAVESIGN)
172 val = (char) val; // FIXME cont'd: sign-extend if required
174 else
175 val = va_arg (ap, int);
177 if (radix)
179 if (HAVESIGN)
180 ultoa (val, buffer, radix);
181 else
182 ltoa (val, buffer, radix);
184 str1 = buffer;
185 while ((*str1))
187 radix = *str1;
188 if (upcase)
189 radix = toupper (radix);
190 putchar (radix);
191 ++str1;
194 else
195 putchar ((char) val);
198 else
199 putchar (*ch);
201 ++ch;