2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
6 * @file printf-stdarg.c
7 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
8 * Parts by Thorsten Klose (tk@midibox.org)
9 * @brief Formatted print functions
10 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 putchar is the only external dependency for this file,
31 if you have a working putchar, leave it commented out.
32 If not, uncomment the define below and
33 replace outbyte(c) by your own function call.
35 #define putchar(c) outbyte(c)
37 // #define putchar(c) COMSendChar(c)
41 static void printchar(char * *str
, int c
)
49 PIOS_COM_SendChar(PIOS_COM_DEBUG
, c
);
57 static int prints(char * *out
, const char *string
, int width
, int pad
, int limit
)
59 register int pc
= limit
, padchar
= ' ';
63 register const char *ptr
;
64 for (ptr
= string
; *ptr
; ++ptr
) {
76 if (!(pad
& PAD_RIGHT
)) {
77 for (; width
> 0 && pc
; --width
) {
78 printchar(out
, padchar
);
82 for (; *string
&& pc
; ++string
) {
83 printchar(out
, *string
);
86 for (; width
> 0 && pc
; --width
) {
87 printchar(out
, padchar
);
94 /* the following should be enough for 32 bit int */
95 #define PRINT_BUF_LEN 12
97 static int printi(char * *out
, int i
, int b
, int sg
, int width
, int pad
, int letbase
, int limit
)
99 char print_buf
[PRINT_BUF_LEN
];
101 register int t
, neg
= 0, pc
= limit
;
102 register unsigned int u
= i
;
107 return prints(out
, print_buf
, width
, pad
, limit
);
110 if (sg
&& b
== 10 && i
< 0) {
115 s
= print_buf
+ PRINT_BUF_LEN
- 1;
121 t
+= letbase
- '0' - 10;
128 if (width
&& (pad
& PAD_ZERO
)) {
137 return (limit
- pc
) + prints(out
, s
, width
, pad
, pc
);
140 static int print(int limit
, char * *out
, const char *format
, va_list args
)
142 register int width
, pad
;
143 register int pc
= limit
;
146 for (; *format
!= 0 && pc
; ++format
) {
147 if (*format
== '%') {
150 if (*format
== '\0') {
153 if (*format
== '%') {
156 if (*format
== '-') {
160 while (*format
== '0') {
164 for (; *format
>= '0' && *format
<= '9'; ++format
) {
166 width
+= *format
- '0';
168 if (*format
== 's') {
169 register char *s
= (char *)va_arg(args
, int);
170 pc
-= prints(out
, s
? s
: "(null)", width
, pad
, pc
);
173 if (*format
== 'd') {
174 pc
-= printi(out
, va_arg(args
, int), 10, 1, width
, pad
, 'a', pc
);
177 if (*format
== 'x') {
178 pc
-= printi(out
, va_arg(args
, int), 16, 0, width
, pad
, 'a', pc
);
181 if (*format
== 'X') {
182 pc
-= printi(out
, va_arg(args
, int), 16, 0, width
, pad
, 'A', pc
);
185 if (*format
== 'u') {
186 pc
-= printi(out
, va_arg(args
, int), 10, 0, width
, pad
, 'a', pc
);
189 if (*format
== 'c') {
190 /* char are converted to int then pushed on the stack */
191 scr
[0] = (char)va_arg(args
, int);
193 pc
-= prints(out
, scr
, width
, pad
, pc
);
198 printchar(out
, *format
);
209 int printf(const char *format
, ...)
213 va_start(args
, format
);
214 return print(-1, 0, format
, args
);
217 // TK: added for alternative parameter passing
218 int vprintf(const char *format
, va_list args
)
220 return print(-1, 0, format
, args
);
223 int sprintf(char *out
, const char *format
, ...)
227 va_start(args
, format
);
228 return print(-1, &out
, format
, args
);
231 // TK: added for alternative parameter passing
232 int vsprintf(char *out
, const char *format
, va_list args
)
237 return print(-1, &_out
, format
, args
);
240 int snprintf(char *buf
, size_t count
, const char *format
, ...)
245 va_start(args
, format
);
246 return print(count
, &buf
, format
, args
);
249 // TK: added for alternative parameter passing
250 int vsnprintf(char *buf
, size_t count
, const char *format
, va_list args
)
252 return print(count
, &buf
, format
, args
);