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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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