3 * Copyright 2007 Free Software Foundation, Inc.
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.
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.
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/>.
20 * Based on code from the SDCC z80 library ;)
27 _printn(unsigned u, unsigned base, char issigned,
28 void (*emitter)(char, void *), void *pData)
30 const char *_hex = "0123456789ABCDEF";
31 if (issigned && ((int)u < 0)) {
32 (*emitter)('-', pData);
33 u = (unsigned)-((int)u);
36 _printn(u/base, base, 0, emitter, pData);
37 (*emitter)(_hex[u%base], pData);
41 _printf(const char *format, void (*emitter)(char, void *),
42 void *pData, va_list va)
46 (*emitter)(*format, pData);
49 case 0: /* hit end of format string */
53 char c = (char)va_arg(va, int);
59 unsigned u = va_arg(va, unsigned);
60 _printn(u, 10, 0, emitter, pData);
65 unsigned u = va_arg(va, unsigned);
66 _printn(u, 10, 1, emitter, pData);
72 unsigned u = va_arg(va, unsigned);
73 _printn(u, 16, 0, emitter, pData);
78 char *s = va_arg(va, char *);
80 (*emitter)(*s, pData);
92 _char_emitter(char c, void *pData __attribute__((unused)))
98 printf(const char *format, ...)
101 va_start(va, format);
103 _printf(format, _char_emitter, NULL, va);
107 // wrong return value...
114 // Totally dangerous. Don't use
116 _buf_emitter(char c, void *pData)
118 *((*((char **)pData)))++ = c;
121 int sprintf(char *pInto, const char *format, ...)
124 va_start(va, format);
126 _printf(format, _buf_emitter, &pInto, va);
131 // FIXME wrong return value