4d858648daa41478aa2f7c67bc5a2488efce0211
[debian/gnuradio] / usrp2 / firmware / lib / printf.c.smaller
1 /* -*- c -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /*
20  * Based on code from the SDCC z80 library ;)
21  */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <hal_io.h>             /* FIXME refactor into stdio */
26
27 #define PUTCHAR(x) hal_putc(x)
28
29
30 static void 
31 _printn(unsigned u, unsigned base, char issigned)
32 {
33   const char *_hex = "0123456789ABCDEF";
34   if (issigned && ((int)u < 0)) {
35     PUTCHAR('-');
36     u = (unsigned)-((int)u);
37   }
38   if (u >= base)
39     _printn(u/base, base, 0);
40   PUTCHAR(_hex[u%base]);
41 }
42
43 static void 
44 _printf(const char *format, va_list va)
45 {
46   while (*format) {
47     if (*format != '%')
48       PUTCHAR(*format);
49     else {
50       switch (*++format) {
51       case 0:                   /* hit end of format string */
52         return;
53       case 'c':
54         {
55           char c = (char)va_arg(va, int);
56           PUTCHAR(c);
57           break;
58         }
59       case 'u':
60         {
61           unsigned u = va_arg(va, unsigned);
62           _printn(u, 10, 0);
63           break;
64         }
65       case 'd':
66         {
67           unsigned u = va_arg(va, unsigned);
68           _printn(u, 10, 1);
69           break;
70         }
71       case 'x':
72       case 'p':
73         {
74           unsigned u = va_arg(va, unsigned);
75           _printn(u, 16, 0);
76           break;
77         }
78       case 's':
79         {
80           char *s = va_arg(va, char *);
81           while (*s) {
82             PUTCHAR(*s);
83             s++;
84           }
85           break;
86         }
87       }
88     }
89     format++;
90   }
91 }
92
93 #if 0
94 static void 
95 _char_emitter(char c, void *pData __attribute__((unused)))
96 {
97   hal_putc(c);
98 }
99 #endif
100
101 int 
102 printf(const char *format, ...)
103 {
104   va_list va;
105   va_start(va, format);
106
107   _printf(format, va);
108
109   // wrong return value...
110   return 0;
111 }
112
113
114 #if 0
115
116 // Totally dangerous.  Don't use
117 static void 
118 _buf_emitter(char c, void *pData)
119 {
120   *((*((char **)pData)))++ = c;
121 }
122
123 int sprintf(char *pInto, const char *format, ...)
124 {
125   va_list va;
126   va_start(va, format);
127
128   _printf(format, _buf_emitter, &pInto, va);
129   *pInto++ = '\0';
130
131   // FIXME wrong return value
132   return 0;
133 }
134 #endif