2 * found at: http://www.sparetimelabs.com/tinyprintf/tinyprintf.php
3 * and: http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
9 Copyright (C) 2004 Kustaa Nyholm
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 typedef void (*putcf) (void*,char);
30 static putcf stdout_putf;
31 static void* stdout_putp;
33 // this adds cca 400 bytes
34 #define PRINTF_LONG_SUPPORT
36 #ifdef PRINTF_LONG_SUPPORT
38 static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf)
48 if (n || dgt>0|| d==0) {
49 *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
56 static void li2a (long num, char * bf)
67 static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
77 if (n || dgt>0 || d==0) {
78 *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
85 static void i2a (int num, char * bf)
94 static int a2d(char ch)
96 if (ch>='0' && ch<='9')
98 else if (ch>='a' && ch<='f')
100 else if (ch>='A' && ch<='F')
105 static char a2i(char ch, char** src,int base,int* nump)
110 while ((digit=a2d(ch))>=0) {
111 if (digit>base) break;
120 static void putchw(void* putp,putcf putf,int n, char z, char* bf)
122 char fc=z? '0' : ' ';
125 while (*p++ && n > 0)
133 void tfp_format(void* putp,putcf putf,char *fmt, va_list va)
140 while ((ch=*(fmt++))) {
145 #ifdef PRINTF_LONG_SUPPORT
154 if (ch>='0' && ch<='9') {
155 ch=a2i(ch,&fmt,10,&w);
157 #ifdef PRINTF_LONG_SUPPORT
167 #ifdef PRINTF_LONG_SUPPORT
169 uli2a(va_arg(va, unsigned long int),10,0,bf);
172 ui2a(va_arg(va, unsigned int),10,0,bf);
173 putchw(putp,putf,w,lz,bf);
177 #ifdef PRINTF_LONG_SUPPORT
179 li2a(va_arg(va, unsigned long int),bf);
182 i2a(va_arg(va, int),bf);
183 putchw(putp,putf,w,lz,bf);
187 #ifdef PRINTF_LONG_SUPPORT
189 uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
192 ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
193 putchw(putp,putf,w,lz,bf);
196 putf(putp,(char)(va_arg(va, int)));
199 putchw(putp,putf,w,0,va_arg(va, char*));
212 void init_printf(void* putp,void (*putf) (void*,char))
218 void tfp_printf(char *fmt, ...)
222 tfp_format(stdout_putp,stdout_putf,fmt,va);
226 static void putcp(void* p,char c)
228 *(*((char**)p))++ = c;
233 void tfp_sprintf(char* s,char *fmt, ...)
237 tfp_format(&s,putcp,fmt,va);