]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_AT91SAM3U256_IAR/printf-stdarg.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_AT91SAM3U256_IAR / printf-stdarg.c
1 /*\r
2         Copyright 2001, 2002 Georges Menie (www.menie.org)\r
3         stdarg version contributed by Christian Ettinger\r
4 \r
5     This program is free software; you can redistribute it and/or modify\r
6     it under the terms of the GNU Lesser General Public License as published by\r
7     the Free Software Foundation; either version 2 of the License, or\r
8     (at your option) any later version.\r
9 \r
10     This program is distributed in the hope that it will be useful,\r
11     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13     GNU Lesser General Public License for more details.\r
14 \r
15     You should have received a copy of the GNU Lesser General Public License\r
16     along with this program; if not, write to the Free Software\r
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
18 */\r
19 \r
20 /*\r
21         putchar is the only external dependency for this file,\r
22         if you have a working putchar, leave it commented out.\r
23         If not, uncomment the define below and\r
24         replace outbyte(c) by your own function call.\r
25 \r
26 */\r
27 \r
28 #define putchar(c) c\r
29 \r
30 #include <stdarg.h>\r
31 \r
32 static void printchar(char **str, int c)\r
33 {\r
34         //extern int putchar(int c);\r
35         \r
36         if (str) {\r
37                 **str = (char)c;\r
38                 ++(*str);\r
39         }\r
40         else\r
41         { \r
42                 (void)putchar(c);\r
43         }\r
44 }\r
45 \r
46 #define PAD_RIGHT 1\r
47 #define PAD_ZERO 2\r
48 \r
49 static int prints(char **out, const char *string, int width, int pad)\r
50 {\r
51         register int pc = 0, padchar = ' ';\r
52 \r
53         if (width > 0) {\r
54                 register int len = 0;\r
55                 register const char *ptr;\r
56                 for (ptr = string; *ptr; ++ptr) ++len;\r
57                 if (len >= width) width = 0;\r
58                 else width -= len;\r
59                 if (pad & PAD_ZERO) padchar = '0';\r
60         }\r
61         if (!(pad & PAD_RIGHT)) {\r
62                 for ( ; width > 0; --width) {\r
63                         printchar (out, padchar);\r
64                         ++pc;\r
65                 }\r
66         }\r
67         for ( ; *string ; ++string) {\r
68                 printchar (out, *string);\r
69                 ++pc;\r
70         }\r
71         for ( ; width > 0; --width) {\r
72                 printchar (out, padchar);\r
73                 ++pc;\r
74         }\r
75 \r
76         return pc;\r
77 }\r
78 \r
79 /* the following should be enough for 32 bit int */\r
80 #define PRINT_BUF_LEN 12\r
81 \r
82 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)\r
83 {\r
84         char print_buf[PRINT_BUF_LEN];\r
85         register char *s;\r
86         register int t, neg = 0, pc = 0;\r
87         register unsigned int u = (unsigned int)i;\r
88 \r
89         if (i == 0) {\r
90                 print_buf[0] = '0';\r
91                 print_buf[1] = '\0';\r
92                 return prints (out, print_buf, width, pad);\r
93         }\r
94 \r
95         if (sg && b == 10 && i < 0) {\r
96                 neg = 1;\r
97                 u = (unsigned int)-i;\r
98         }\r
99 \r
100         s = print_buf + PRINT_BUF_LEN-1;\r
101         *s = '\0';\r
102 \r
103         while (u) {\r
104                 t = (unsigned int)u % b;\r
105                 if( t >= 10 )\r
106                         t += letbase - '0' - 10;\r
107                 *--s = (char)(t + '0');\r
108                 u /= b;\r
109         }\r
110 \r
111         if (neg) {\r
112                 if( width && (pad & PAD_ZERO) ) {\r
113                         printchar (out, '-');\r
114                         ++pc;\r
115                         --width;\r
116                 }\r
117                 else {\r
118                         *--s = '-';\r
119                 }\r
120         }\r
121 \r
122         return pc + prints (out, s, width, pad);\r
123 }\r
124 \r
125 static int print( char **out, const char *format, va_list args )\r
126 {\r
127         register int width, pad;\r
128         register int pc = 0;\r
129         char scr[2];\r
130 \r
131         for (; *format != 0; ++format) {\r
132                 if (*format == '%') {\r
133                         ++format;\r
134                         width = pad = 0;\r
135                         if (*format == '\0') break;\r
136                         if (*format == '%') goto out;\r
137                         if (*format == '-') {\r
138                                 ++format;\r
139                                 pad = PAD_RIGHT;\r
140                         }\r
141                         while (*format == '0') {\r
142                                 ++format;\r
143                                 pad |= PAD_ZERO;\r
144                         }\r
145                         for ( ; *format >= '0' && *format <= '9'; ++format) {\r
146                                 width *= 10;\r
147                                 width += *format - '0';\r
148                         }\r
149                         if( *format == 's' ) {\r
150                                 register char *s = (char *)va_arg( args, int );\r
151                                 pc += prints (out, s?s:"(null)", width, pad);\r
152                                 continue;\r
153                         }\r
154                         if( *format == 'd' ) {\r
155                                 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');\r
156                                 continue;\r
157                         }\r
158                         if( *format == 'x' ) {\r
159                                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');\r
160                                 continue;\r
161                         }\r
162                         if( *format == 'X' ) {\r
163                                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');\r
164                                 continue;\r
165                         }\r
166                         if( *format == 'u' ) {\r
167                                 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');\r
168                                 continue;\r
169                         }\r
170                         if( *format == 'c' ) {\r
171                                 /* char are converted to int then pushed on the stack */\r
172                                 scr[0] = (char)va_arg( args, int );\r
173                                 scr[1] = '\0';\r
174                                 pc += prints (out, scr, width, pad);\r
175                                 continue;\r
176                         }\r
177                 }\r
178                 else {\r
179                 out:\r
180                         printchar (out, *format);\r
181                         ++pc;\r
182                 }\r
183         }\r
184         if (out) **out = '\0';\r
185         va_end( args );\r
186         return pc;\r
187 }\r
188 \r
189 int printf(const char *format, ...)\r
190 {\r
191         va_list args;\r
192         \r
193         va_start( args, format );\r
194         return print( 0, format, args );\r
195 }\r
196 \r
197 int sprintf(char *out, const char *format, ...)\r
198 {\r
199         va_list args;\r
200         \r
201         va_start( args, format );\r
202         return print( &out, format, args );\r
203 }\r
204 \r
205 \r
206 int snprintf( char *buf, unsigned int count, const char *format, ... )\r
207 {\r
208         va_list args;\r
209         \r
210         ( void ) count;\r
211         \r
212         va_start( args, format );\r
213         return print( &buf, format, args );\r
214 }\r
215 \r
216 \r
217 #ifdef TEST_PRINTF\r
218 int main(void)\r
219 {\r
220         char *ptr = "Hello world!";\r
221         char *np = 0;\r
222         int i = 5;\r
223         unsigned int bs = sizeof(int)*8;\r
224         int mi;\r
225         char buf[80];\r
226 \r
227         mi = (1 << (bs-1)) + 1;\r
228         printf("%s\n", ptr);\r
229         printf("printf test\n");\r
230         printf("%s is null pointer\n", np);\r
231         printf("%d = 5\n", i);\r
232         printf("%d = - max int\n", mi);\r
233         printf("char %c = 'a'\n", 'a');\r
234         printf("hex %x = ff\n", 0xff);\r
235         printf("hex %02x = 00\n", 0);\r
236         printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);\r
237         printf("%d %s(s)%", 0, "message");\r
238         printf("\n");\r
239         printf("%d %s(s) with %%\n", 0, "message");\r
240         sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);\r
241         sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);\r
242         sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);\r
243         sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);\r
244         sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);\r
245         sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);\r
246         sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);\r
247         sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);\r
248 \r
249         return 0;\r
250 }\r
251 \r
252 /*\r
253  * if you compile this file with\r
254  *   gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c\r
255  * you will get a normal warning:\r
256  *   printf.c:214: warning: spurious trailing `%' in format\r
257  * this line is testing an invalid % at the end of the format string.\r
258  *\r
259  * this should display (on 32bit int machine) :\r
260  *\r
261  * Hello world!\r
262  * printf test\r
263  * (null) is null pointer\r
264  * 5 = 5\r
265  * -2147483647 = - max int\r
266  * char a = 'a'\r
267  * hex ff = ff\r
268  * hex 00 = 00\r
269  * signed -3 = unsigned 4294967293 = hex fffffffd\r
270  * 0 message(s)\r
271  * 0 message(s) with %\r
272  * justif: "left      "\r
273  * justif: "     right"\r
274  *  3: 0003 zero padded\r
275  *  3: 3    left justif.\r
276  *  3:    3 right justif.\r
277  * -3: -003 zero padded\r
278  * -3: -3   left justif.\r
279  * -3:   -3 right justif.\r
280  */\r
281 \r
282 #endif\r
283 \r
284 \r
285 /* To keep linker happy. */\r
286 int     write( int i, char* c, int n)\r
287 {\r
288         (void)i;\r
289         (void)n;\r
290         (void)c;\r
291         return 0;\r
292 }\r
293 \r