2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
*
|
2013-03-17 15:53:57 +01:00
|
|
|
* @file printf-stdarg.c
|
2010-09-27 09:28:45 +02:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2013-05-19 16:37:30 +02:00
|
|
|
* Parts by Thorsten Klose (tk@midibox.org)
|
2010-09-27 09:28:45 +02:00
|
|
|
* @brief Formatted print functions
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2013-03-17 15:53:57 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-09-27 09:28:45 +02:00
|
|
|
* (at your option) any later version.
|
2013-03-17 15:53:57 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2010-09-27 09:28:45 +02:00
|
|
|
* for more details.
|
2013-03-17 15:53:57 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-09-27 09:28:45 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
putchar is the only external dependency for this file,
|
|
|
|
if you have a working putchar, leave it commented out.
|
|
|
|
If not, uncomment the define below and
|
|
|
|
replace outbyte(c) by your own function call.
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
#define putchar(c) outbyte(c)
|
|
|
|
*/
|
|
|
|
// #define putchar(c) COMSendChar(c)
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
#include <pios.h>
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void printchar(char * *str, int c)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (str) {
|
|
|
|
**str = c;
|
|
|
|
++(*str);
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
#ifdef PIOS_COM_DEBUG
|
2013-05-19 16:37:30 +02:00
|
|
|
else {
|
|
|
|
PIOS_COM_SendChar(PIOS_COM_DEBUG, c);
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PAD_RIGHT 1
|
2013-05-19 16:37:30 +02:00
|
|
|
#define PAD_ZERO 2
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
static int prints(char * *out, const char *string, int width, int pad, int limit)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-11-17 02:47:58 +01:00
|
|
|
register int pc = limit, padchar = ' ';
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
if (width > 0) {
|
|
|
|
register int len = 0;
|
|
|
|
register const char *ptr;
|
|
|
|
for (ptr = string; *ptr; ++ptr) {
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
if (len >= width) {
|
|
|
|
width = 0;
|
|
|
|
} else {
|
|
|
|
width -= len;
|
|
|
|
}
|
|
|
|
if (pad & PAD_ZERO) {
|
|
|
|
padchar = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(pad & PAD_RIGHT)) {
|
2013-11-17 02:47:58 +01:00
|
|
|
for (; width > 0 && pc; --width) {
|
2013-05-19 16:37:30 +02:00
|
|
|
printchar(out, padchar);
|
2013-11-17 02:47:58 +01:00
|
|
|
--pc;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-17 02:47:58 +01:00
|
|
|
for (; *string && pc; ++string) {
|
2013-05-19 16:37:30 +02:00
|
|
|
printchar(out, *string);
|
2013-11-17 02:47:58 +01:00
|
|
|
--pc;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-11-17 02:47:58 +01:00
|
|
|
for (; width > 0 && pc; --width) {
|
2013-05-19 16:37:30 +02:00
|
|
|
printchar(out, padchar);
|
2013-11-17 02:47:58 +01:00
|
|
|
--pc;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
return limit - pc;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the following should be enough for 32 bit int */
|
|
|
|
#define PRINT_BUF_LEN 12
|
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
static int printi(char * *out, int i, int b, int sg, int width, int pad, int letbase, int limit)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
char print_buf[PRINT_BUF_LEN];
|
|
|
|
register char *s;
|
2013-11-17 02:47:58 +01:00
|
|
|
register int t, neg = 0, pc = limit;
|
2013-05-19 16:37:30 +02:00
|
|
|
register unsigned int u = i;
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
print_buf[0] = '0';
|
|
|
|
print_buf[1] = '\0';
|
2013-11-17 02:47:58 +01:00
|
|
|
return prints(out, print_buf, width, pad, limit);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sg && b == 10 && i < 0) {
|
|
|
|
neg = 1;
|
|
|
|
u = -i;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = print_buf + PRINT_BUF_LEN - 1;
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
while (u) {
|
|
|
|
t = u % b;
|
|
|
|
if (t >= 10) {
|
|
|
|
t += letbase - '0' - 10;
|
|
|
|
}
|
|
|
|
*--s = t + '0';
|
|
|
|
u /= b;
|
|
|
|
}
|
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
if (neg && pc) {
|
2013-05-19 16:37:30 +02:00
|
|
|
if (width && (pad & PAD_ZERO)) {
|
|
|
|
printchar(out, '-');
|
2013-11-17 02:47:58 +01:00
|
|
|
--pc;
|
2013-05-19 16:37:30 +02:00
|
|
|
--width;
|
|
|
|
} else {
|
|
|
|
*--s = '-';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
return (limit - pc) + prints(out, s, width, pad, pc);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
static int print(int limit, char * *out, const char *format, va_list args)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
register int width, pad;
|
2013-11-17 02:47:58 +01:00
|
|
|
register int pc = limit;
|
2013-05-19 16:37:30 +02:00
|
|
|
char scr[2];
|
|
|
|
|
2013-11-17 02:47:58 +01:00
|
|
|
for (; *format != 0 && pc; ++format) {
|
2013-05-19 16:37:30 +02:00
|
|
|
if (*format == '%') {
|
|
|
|
++format;
|
|
|
|
width = pad = 0;
|
|
|
|
if (*format == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*format == '%') {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (*format == '-') {
|
|
|
|
++format;
|
|
|
|
pad = PAD_RIGHT;
|
|
|
|
}
|
|
|
|
while (*format == '0') {
|
|
|
|
++format;
|
|
|
|
pad |= PAD_ZERO;
|
|
|
|
}
|
|
|
|
for (; *format >= '0' && *format <= '9'; ++format) {
|
|
|
|
width *= 10;
|
|
|
|
width += *format - '0';
|
|
|
|
}
|
|
|
|
if (*format == 's') {
|
|
|
|
register char *s = (char *)va_arg(args, int);
|
2013-11-17 02:47:58 +01:00
|
|
|
pc -= prints(out, s ? s : "(null)", width, pad, pc);
|
2013-05-19 16:37:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*format == 'd') {
|
2013-11-17 02:47:58 +01:00
|
|
|
pc -= printi(out, va_arg(args, int), 10, 1, width, pad, 'a', pc);
|
2013-05-19 16:37:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*format == 'x') {
|
2013-11-17 02:47:58 +01:00
|
|
|
pc -= printi(out, va_arg(args, int), 16, 0, width, pad, 'a', pc);
|
2013-05-19 16:37:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*format == 'X') {
|
2013-11-17 02:47:58 +01:00
|
|
|
pc -= printi(out, va_arg(args, int), 16, 0, width, pad, 'A', pc);
|
2013-05-19 16:37:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*format == 'u') {
|
2013-11-17 02:47:58 +01:00
|
|
|
pc -= printi(out, va_arg(args, int), 10, 0, width, pad, 'a', pc);
|
2013-05-19 16:37:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*format == 'c') {
|
|
|
|
/* char are converted to int then pushed on the stack */
|
|
|
|
scr[0] = (char)va_arg(args, int);
|
|
|
|
scr[1] = '\0';
|
2013-11-17 02:47:58 +01:00
|
|
|
pc -= prints(out, scr, width, pad, pc);
|
2013-05-19 16:37:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
2010-09-27 09:28:45 +02:00
|
|
|
out:
|
2013-05-19 16:37:30 +02:00
|
|
|
printchar(out, *format);
|
2013-11-17 02:47:58 +01:00
|
|
|
--pc;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out) {
|
|
|
|
**out = '\0';
|
|
|
|
}
|
|
|
|
va_end(args);
|
2013-11-17 02:47:58 +01:00
|
|
|
return limit - pc;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int printf(const char *format, ...)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
va_list args;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
va_start(args, format);
|
2013-11-17 02:47:58 +01:00
|
|
|
return print(-1, 0, format, args);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TK: added for alternative parameter passing
|
|
|
|
int vprintf(const char *format, va_list args)
|
|
|
|
{
|
2013-11-17 02:47:58 +01:00
|
|
|
return print(-1, 0, format, args);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sprintf(char *out, const char *format, ...)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
va_list args;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
va_start(args, format);
|
2013-11-17 02:47:58 +01:00
|
|
|
return print(-1, &out, format, args);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TK: added for alternative parameter passing
|
|
|
|
int vsprintf(char *out, const char *format, va_list args)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
char *_out;
|
|
|
|
|
|
|
|
_out = out;
|
2013-11-17 02:47:58 +01:00
|
|
|
return print(-1, &_out, format, args);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int snprintf(char *buf, size_t count, const char *format, ...)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
va_list args;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
va_start(args, format);
|
2013-11-17 02:47:58 +01:00
|
|
|
return print(count, &buf, format, args);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2013-11-17 18:49:50 +01:00
|
|
|
// TK: added for alternative parameter passing
|
|
|
|
int vsnprintf(char *buf, size_t count, const char *format, va_list args)
|
|
|
|
{
|
|
|
|
return print(count, &buf, format, args);
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @}
|
|
|
|
*/
|