OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_AVR32_UC3/] [printf-stdarg.c] - Blame information for rev 818

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/*This file has been prepared for Doxygen automatic documentation generation.*/
2
/*! \file *********************************************************************
3
 *
4
 * \brief sprintf functions to replace newlib for AVR32 UC3.
5
 *
6
 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
7
 * - Supported devices:  All AVR32 devices can be used.
8
 * - AppNote:
9
 *
10
 * \author               Atmel Corporation: http://www.atmel.com \n
11
 *                       Support and FAQ: http://support.atmel.no/
12
 *
13
 *****************************************************************************/
14
 
15
/* Copyright (c) 2007, Atmel Corporation All rights reserved.
16
 *
17
 * Redistribution and use in source and binary forms, with or without
18
 * modification, are permitted provided that the following conditions are met:
19
 *
20
 * 1. Redistributions of source code must retain the above copyright notice,
21
 * this list of conditions and the following disclaimer.
22
 *
23
 * 2. Redistributions in binary form must reproduce the above copyright notice,
24
 * this list of conditions and the following disclaimer in the documentation
25
 * and/or other materials provided with the distribution.
26
 *
27
 * 3. The name of ATMEL may not be used to endorse or promote products derived
28
 * from this software without specific prior written permission.
29
 *
30
 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
31
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
33
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
34
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
 */
41
 
42
/*
43
        Copyright 2001, 2002 Georges Menie (www.menie.org)
44
        stdarg version contributed by Christian Ettinger
45
 
46
    This program is free software; you can redistribute it and/or modify
47
    it under the terms of the GNU Lesser General Public License as published by
48
    the Free Software Foundation; either version 2 of the License, or
49
    (at your option) any later version.
50
 
51
    This program is distributed in the hope that it will be useful,
52
    but WITHOUT ANY WARRANTY; without even the implied warranty of
53
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54
    GNU Lesser General Public License for more details.
55
 
56
    You should have received a copy of the GNU Lesser General Public License
57
    along with this program; if not, write to the Free Software
58
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
59
*/
60
 
61
/*
62
        putchar is the only external dependency for this file,
63
        if you have a working putchar, leave it commented out.
64
        If not, uncomment the define below and
65
        replace outbyte(c) by your own function call.
66
 
67
#define putchar(c) outbyte(c)
68
*/
69
 
70
#include <sys/reent.h>
71
#include <stdarg.h>
72
 
73
 
74
static void printchar(char **str, int c)
75
{
76
        extern int putchar(int c);
77
 
78
        if (str) {
79
                **str = c;
80
                ++(*str);
81
        }
82
        else (void)putchar(c);
83
}
84
 
85
#define PAD_RIGHT 1
86
#define PAD_ZERO 2
87
 
88
static int prints(char **out, const char *string, int width, int pad)
89
{
90
        register int pc = 0, padchar = ' ';
91
 
92
        if (width > 0) {
93
                register int len = 0;
94
                register const char *ptr;
95
                for (ptr = string; *ptr; ++ptr) ++len;
96
                if (len >= width) width = 0;
97
                else width -= len;
98
                if (pad & PAD_ZERO) padchar = '0';
99
        }
100
        if (!(pad & PAD_RIGHT)) {
101
                for ( ; width > 0; --width) {
102
                        printchar (out, padchar);
103
                        ++pc;
104
                }
105
        }
106
        for ( ; *string ; ++string) {
107
                printchar (out, *string);
108
                ++pc;
109
        }
110
        for ( ; width > 0; --width) {
111
                printchar (out, padchar);
112
                ++pc;
113
        }
114
 
115
        return pc;
116
}
117
 
118
/* the following should be enough for 32 bit int */
119
#define PRINT_BUF_LEN 12
120
 
121
static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
122
{
123
        char print_buf[PRINT_BUF_LEN];
124
        register char *s;
125
        register int t, neg = 0, pc = 0;
126
        register unsigned int u = i;
127
 
128
        if (i == 0) {
129
                print_buf[0] = '0';
130
                print_buf[1] = '\0';
131
                return prints (out, print_buf, width, pad);
132
        }
133
 
134
        if (sg && b == 10 && i < 0) {
135
                neg = 1;
136
                u = -i;
137
        }
138
 
139
        s = print_buf + PRINT_BUF_LEN-1;
140
        *s = '\0';
141
 
142
        while (u) {
143
                t = u % b;
144
                if( t >= 10 )
145
                        t += letbase - '0' - 10;
146
                *--s = t + '0';
147
                u /= b;
148
        }
149
 
150
        if (neg) {
151
                if( width && (pad & PAD_ZERO) ) {
152
                        printchar (out, '-');
153
                        ++pc;
154
                        --width;
155
                }
156
                else {
157
                        *--s = '-';
158
                }
159
        }
160
 
161
        return pc + prints (out, s, width, pad);
162
}
163
 
164
int fprintf(__FILE *stream, const char *format, ...)
165
{
166
return 0;
167
}
168
static int print(char **out, const char *format, va_list args )
169
{
170
        register int width, pad;
171
        register int pc = 0;
172
        char scr[2];
173
 
174
        for (; *format != 0; ++format) {
175
                if (*format == '%') {
176
                        ++format;
177
                        width = pad = 0;
178
                        if (*format == '\0') break;
179
                        if (*format == '%') goto out;
180
                        if (*format == '-') {
181
                                ++format;
182
                                pad = PAD_RIGHT;
183
                        }
184
                        while (*format == '0') {
185
                                ++format;
186
                                pad |= PAD_ZERO;
187
                        }
188
                        for ( ; *format >= '0' && *format <= '9'; ++format) {
189
                                width *= 10;
190
                                width += *format - '0';
191
                        }
192
                        if( *format == 's' ) {
193
                                register char *s = (char *)va_arg( args, int );
194
                                pc += prints (out, s?s:"(null)", width, pad);
195
                                continue;
196
                        }
197
                        if( *format == 'd' ) {
198
                                pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
199
                                continue;
200
                        }
201
                        if( *format == 'x' ) {
202
                                pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
203
                                continue;
204
                        }
205
                        if( *format == 'X' ) {
206
                                pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
207
                                continue;
208
                        }
209
                        if( *format == 'u' ) {
210
                                pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
211
                                continue;
212
                        }
213
                        if( *format == 'c' ) {
214
                                /* char are converted to int then pushed on the stack */
215
                                scr[0] = (char)va_arg( args, int );
216
                                scr[1] = '\0';
217
                                pc += prints (out, scr, width, pad);
218
                                continue;
219
                        }
220
                }
221
                else {
222
                out:
223
                        printchar (out, *format);
224
                        ++pc;
225
                }
226
        }
227
        if (out) **out = '\0';
228
        va_end( args );
229
        return pc;
230
}
231
 
232
int printk(const char *format, ...)
233
{
234
        va_list args;
235
 
236
        va_start( args, format );
237
        return print( 0, format, args );
238
}
239
 
240
int sprintf(char *out, const char *format, ...)
241
{
242
        va_list args;
243
 
244
        va_start( args, format );
245
        return print( &out, format, args );
246
}
247
 
248
#ifdef TEST_PRINTF
249
int main(void)
250
{
251
        char *ptr = "Hello world!";
252
        char *np = 0;
253
        int i = 5;
254
        unsigned int bs = sizeof(int)*8;
255
        int mi;
256
        char buf[80];
257
 
258
        mi = (1 << (bs-1)) + 1;
259
        printf("%s\n", ptr);
260
        printf("printf test\n");
261
        printf("%s is null pointer\n", np);
262
        printf("%d = 5\n", i);
263
        printf("%d = - max int\n", mi);
264
        printf("char %c = 'a'\n", 'a');
265
        printf("hex %x = ff\n", 0xff);
266
        printf("hex %02x = 00\n", 0);
267
        printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
268
        printf("%d %s(s)%", 0, "message");
269
        printf("\n");
270
        printf("%d %s(s) with %%\n", 0, "message");
271
        sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
272
        sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
273
        sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
274
        sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
275
        sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
276
        sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
277
        sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
278
        sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
279
 
280
        return 0;
281
}
282
 
283
/*
284
 * if you compile this file with
285
 *   gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
286
 * you will get a normal warning:
287
 *   printf.c:214: warning: spurious trailing `%' in format
288
 * this line is testing an invalid % at the end of the format string.
289
 *
290
 * this should display (on 32bit int machine) :
291
 *
292
 * Hello world!
293
 * printf test
294
 * (null) is null pointer
295
 * 5 = 5
296
 * -2147483647 = - max int
297
 * char a = 'a'
298
 * hex ff = ff
299
 * hex 00 = 00
300
 * signed -3 = unsigned 4294967293 = hex fffffffd
301
 * 0 message(s)
302
 * 0 message(s) with %
303
 * justif: "left      "
304
 * justif: "     right"
305
 *  3: 0003 zero padded
306
 *  3: 3    left justif.
307
 *  3:    3 right justif.
308
 * -3: -003 zero padded
309
 * -3: -3   left justif.
310
 * -3:   -3 right justif.
311
 */
312
 
313
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.