struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / pic16 / libc / stdio / printf_small.c
blob175530599a3b10cd64a06e6baf15cdc48ed1de9e
1 /*-------------------------------------------------------------------------
2 printf_small.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 %d decimal int
41 %ld decimal long
42 %hd decimal char
43 %x hexadecimal int
44 %lx hexadecimal long
45 %hx hexadecimal char
46 %o octal int
47 %lo octal long
48 %ho octal char
49 %c character char
50 %s character generic pointer
51 %f float float
54 #include <stdarg.h>
55 #include <stdio.h>
56 #include <stdlib.h>
58 void
59 printf_small (const char *fmt, ...)
60 __reentrant
62 const char *ch;
63 char radix;
64 char flong;
65 char fstr;
66 char fchar;
67 char ffloat;
68 float flt;
69 char *str;
70 __data char *str1;
71 long val;
72 static char buffer[16];
73 va_list ap;
75 ch = fmt;
76 va_start (ap, fmt);
78 while (*ch) //for (; *fmt ; fmt++ )
80 if (*ch == '%')
82 flong = fstr = fchar = ffloat = 0;
83 radix = 0;
84 ++ch;
86 if (*ch == 'l')
88 flong = 1;
89 ++ch;
91 else if (*ch == 'h')
93 fchar = 1;
94 ++ch;
97 if (*ch == 's')
98 fstr = 1;
99 else if (*ch == 'f')
100 ffloat = 1;
101 else if (*ch == 'd')
102 radix = 10;
103 else if (*ch == 'x')
104 radix = 16;
105 else if (*ch == 'c')
106 radix = 0;
107 else if (*ch == 'o')
108 radix = 8;
110 if (fstr)
112 str = va_arg (ap, char *);
113 while (*str)
114 putchar (*str++);
116 else if (ffloat)
118 flt = va_arg (ap, float);
119 x_ftoa (flt, buffer, 32, 6);
120 str1 = buffer;
121 while (*str1)
122 ++str1;
123 --str1;
124 while (*str1 == '0')
125 --str1;
126 ++str1;
127 *str1 = 0;
128 str1 = buffer;
129 while (*str1)
130 putchar (*str1++);
132 else
134 if (flong)
135 val = va_arg (ap, long);
136 else if (fchar)
137 val = (char) va_arg (ap, int); // FIXME: SDCC casts char arguments into ints
138 else
140 val = va_arg (ap, int);
143 if (radix)
145 ltoa (val, buffer, radix);
147 str1 = buffer;
148 while (*str1)
150 putchar (*str1++);
153 else
154 putchar ((char) val);
157 else
158 putchar (*ch);
160 ++ch;