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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [MB91460_Softune/] [SRC/] [utility/] [printf-stdarg.c] - Blame information for rev 584

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 584 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
#define putchar(c) outbyte(c)
27
*/
28
 
29
#include <stdarg.h>
30
 
31
static void printchar(char **str, int c)
32
{
33
        extern int putchar(int c);
34
 
35
        if (str) {
36
                **str = c;
37
                ++(*str);
38
        }
39
        else (void)putchar(c);
40
}
41
 
42
#define PAD_RIGHT 1
43
#define PAD_ZERO 2
44
 
45
static int prints(char **out, const char *string, int width, int pad)
46
{
47
        register int pc = 0, padchar = ' ';
48
 
49
        if (width > 0) {
50
                register int len = 0;
51
                register const char *ptr;
52
                for (ptr = string; *ptr; ++ptr) ++len;
53
                if (len >= width) width = 0;
54
                else width -= len;
55
                if (pad & PAD_ZERO) padchar = '0';
56
        }
57
        if (!(pad & PAD_RIGHT)) {
58
                for ( ; width > 0; --width) {
59
                        printchar (out, padchar);
60
                        ++pc;
61
                }
62
        }
63
        for ( ; *string ; ++string) {
64
                printchar (out, *string);
65
                ++pc;
66
        }
67
        for ( ; width > 0; --width) {
68
                printchar (out, padchar);
69
                ++pc;
70
        }
71
 
72
        return pc;
73
}
74
 
75
/* the following should be enough for 32 bit int */
76
#define PRINT_BUF_LEN 12
77
 
78
static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
79
{
80
        char print_buf[PRINT_BUF_LEN];
81
        register char *s;
82
        register int t, neg = 0, pc = 0;
83
        register unsigned int u = i;
84
 
85
        if (i == 0) {
86
                print_buf[0] = '0';
87
                print_buf[1] = '\0';
88
                return prints (out, print_buf, width, pad);
89
        }
90
 
91
        if (sg && b == 10 && i < 0) {
92
                neg = 1;
93
                u = -i;
94
        }
95
 
96
        s = print_buf + PRINT_BUF_LEN-1;
97
        *s = '\0';
98
 
99
        while (u) {
100
                t = u % b;
101
                if( t >= 10 )
102
                        t += letbase - '0' - 10;
103
                *--s = t + '0';
104
                u /= b;
105
        }
106
 
107
        if (neg) {
108
                if( width && (pad & PAD_ZERO) ) {
109
                        printchar (out, '-');
110
                        ++pc;
111
                        --width;
112
                }
113
                else {
114
                        *--s = '-';
115
                }
116
        }
117
 
118
        return pc + prints (out, s, width, pad);
119
}
120
 
121
static int print( char **out, const char *format, va_list args )
122
{
123
        register int width, pad;
124
        register int pc = 0;
125
        char scr[2];
126
 
127
        for (; *format != 0; ++format) {
128
                if (*format == '%') {
129
                        ++format;
130
                        width = pad = 0;
131
                        if (*format == '\0') break;
132
                        if (*format == '%') goto out;
133
                        if (*format == '-') {
134
                                ++format;
135
                                pad = PAD_RIGHT;
136
                        }
137
                        while (*format == '0') {
138
                                ++format;
139
                                pad |= PAD_ZERO;
140
                        }
141
                        for ( ; *format >= '0' && *format <= '9'; ++format) {
142
                                width *= 10;
143
                                width += *format - '0';
144
                        }
145
                        if( *format == 's' ) {
146
                                register char *s = (char *)va_arg( args, int );
147
                                pc += prints (out, s?s:"(null)", width, pad);
148
                                continue;
149
                        }
150
                        if( *format == 'd' ) {
151
                                pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
152
                                continue;
153
                        }
154
                        if( *format == 'x' ) {
155
                                pc += printi (out, va_arg( args, int ), 16, 0, 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 == 'u' ) {
163
                                pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
164
                                continue;
165
                        }
166
                        if( *format == 'c' ) {
167
                                /* char are converted to int then pushed on the stack */
168
                                scr[0] = (char)va_arg( args, int );
169
                                scr[1] = '\0';
170
                                pc += prints (out, scr, width, pad);
171
                                continue;
172
                        }
173
                }
174
                else {
175
                out:
176
                        printchar (out, *format);
177
                        ++pc;
178
                }
179
        }
180
        if (out) **out = '\0';
181
        va_end( args );
182
        return pc;
183
}
184
 
185
int printf(const char *format, ...)
186
{
187
        va_list args;
188
 
189
        va_start( args, format );
190
        return print( 0, format, args );
191
}
192
 
193
int sprintf(char *out, const char *format, ...)
194
{
195
        va_list args;
196
 
197
        va_start( args, format );
198
        return print( &out, format, args );
199
}
200
 
201
 
202
int snprintf( char *buf, unsigned int count, const char *format, ... )
203
{
204
        va_list args;
205
 
206
        ( void ) count;
207
 
208
        va_start( args, format );
209
        return print( &buf, format, args );
210
}
211
 
212
 
213
#ifdef TEST_PRINTF
214
int main(void)
215
{
216
        char *ptr = "Hello world!";
217
        char *np = 0;
218
        int i = 5;
219
        unsigned int bs = sizeof(int)*8;
220
        int mi;
221
        char buf[80];
222
 
223
        mi = (1 << (bs-1)) + 1;
224
        printf("%s\n", ptr);
225
        printf("printf test\n");
226
        printf("%s is null pointer\n", np);
227
        printf("%d = 5\n", i);
228
        printf("%d = - max int\n", mi);
229
        printf("char %c = 'a'\n", 'a');
230
        printf("hex %x = ff\n", 0xff);
231
        printf("hex %02x = 00\n", 0);
232
        printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
233
        printf("%d %s(s)%", 0, "message");
234
        printf("\n");
235
        printf("%d %s(s) with %%\n", 0, "message");
236
        sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
237
        sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
238
        sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
239
        sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
240
        sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
241
        sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
242
        sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
243
        sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
244
 
245
        return 0;
246
}
247
 
248
/*
249
 * if you compile this file with
250
 *   gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
251
 * you will get a normal warning:
252
 *   printf.c:214: warning: spurious trailing `%' in format
253
 * this line is testing an invalid % at the end of the format string.
254
 *
255
 * this should display (on 32bit int machine) :
256
 *
257
 * Hello world!
258
 * printf test
259
 * (null) is null pointer
260
 * 5 = 5
261
 * -2147483647 = - max int
262
 * char a = 'a'
263
 * hex ff = ff
264
 * hex 00 = 00
265
 * signed -3 = unsigned 4294967293 = hex fffffffd
266
 * 0 message(s)
267
 * 0 message(s) with %
268
 * justif: "left      "
269
 * justif: "     right"
270
 *  3: 0003 zero padded
271
 *  3: 3    left justif.
272
 *  3:    3 right justif.
273
 * -3: -003 zero padded
274
 * -3: -3   left justif.
275
 * -3:   -3 right justif.
276
 */
277
 
278
#endif
279
 
280
 
281
/* To keep linker happy. */
282
int     write( int i, char* c, int n)
283
{
284
        return 0;
285
}
286
 

powered by: WebSVN 2.1.0

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