2 * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright notice, this list
10 * of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice, this
13 * list of conditions and the following disclaimer in the documentation and/or other
14 * materials provided with the distribution.
16 * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
17 * contributors may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
39 #include "build/build_config.h"
43 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
44 #include "typeconversion.h"
48 #ifdef REQUIRE_CC_ARM_PRINTF_SUPPORT
53 // print bf, padded from left to at least n characters.
54 // padding is zero ('0') if z!=0, space (' ') otherwise
55 static int putchw(void *putp
, putcf putf
, int n
, char z
, char *bf
)
58 char fc
= z
? '0' : ' ';
64 putf(putp
, fc
); written
++;
66 while ((ch
= *bf
++)) {
67 putf(putp
, ch
); written
++;
72 // retrun number of bytes written
73 int tfp_format(void *putp
, putcf putf
, const char *fmt
, va_list va
)
79 while ((ch
= *(fmt
++))) {
81 putf(putp
, ch
); written
++;
84 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
93 if (ch
>= '0' && ch
<= '9') {
94 ch
= a2i(ch
, &fmt
, 10, &w
);
96 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
106 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
108 uli2a(va_arg(va
, unsigned long int), 10, 0, bf
);
111 ui2a(va_arg(va
, unsigned int), 10, 0, bf
);
112 written
+= putchw(putp
, putf
, w
, lz
, bf
);
116 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
118 li2a(va_arg(va
, unsigned long int), bf
);
121 i2a(va_arg(va
, int), bf
);
122 written
+= putchw(putp
, putf
, w
, lz
, bf
);
127 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
129 uli2a(va_arg(va
, unsigned long int), 16, (ch
== 'X'), bf
);
132 ui2a(va_arg(va
, unsigned int), 16, (ch
== 'X'), bf
);
133 written
+= putchw(putp
, putf
, w
, lz
, bf
);
136 putf(putp
, (char) (va_arg(va
, int))); written
++;
139 written
+= putchw(putp
, putf
, w
, 0, va_arg(va
, char *));
142 putf(putp
, ch
); written
++;
145 *va_arg(va
, int*) = written
;
156 void init_printf(void *putp
, void (*putf
) (void *, char))
162 static void putcp(void *p
, char c
)
164 *(*((char **) p
))++ = c
;
167 int tfp_sprintf(char *s
, const char *fmt
, ...)
172 int written
= tfp_format(&s
, putcp
, fmt
, va
);
178 #endif // REQUIRE_CC_ARM_PRINTF_SUPPORT