2017-05-07 17:58:44 +02:00
|
|
|
/* File : barebones/ee_printf.c
|
|
|
|
This file contains an implementation of ee_printf that only requires a method to output a char to a UART without pulling in library code.
|
|
|
|
|
|
|
|
This code is based on a file that contains the following:
|
|
|
|
Copyright (C) 2002 Michael Ringgaard. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of the project nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software
|
|
|
|
without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
/* TuxSH's changes: add support for 64-bit numbers, remove floating-point code
|
|
|
|
TODO! fix left-padding handling with > 10 characters. */
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
#include "fmt.h"
|
|
|
|
#include "strings.h"
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
#define ZEROPAD (1<<0) //Pad with zero
|
|
|
|
#define SIGN (1<<1) //Unsigned/signed long
|
|
|
|
#define PLUS (1<<2) //Show plus
|
|
|
|
#define SPACE (1<<3) //Spacer
|
|
|
|
#define LEFT (1<<4) //Left justified
|
|
|
|
#define HEX_PREP (1<<5) //0x
|
|
|
|
#define UPPERCASE (1<<6) //'ABCDEF'
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
static u32 skipAtoi(const char **s)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
u32 i = 0;
|
|
|
|
while(IS_DIGIT(**s)) i = i * 10 + *((*s)++) - '0';
|
|
|
|
return i;
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
static char *processNumber(char *str, s64 num, bool isHex, s32 size, s32 precision, u32 type)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
if(type & LEFT) type &= ~ZEROPAD;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
char sign = 0;
|
|
|
|
|
|
|
|
if(type & SIGN)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
if(num < 0)
|
|
|
|
{
|
|
|
|
sign = '-';
|
|
|
|
num = -num;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
else if(type & PLUS)
|
|
|
|
{
|
|
|
|
sign = '+';
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
else if(type & SPACE)
|
|
|
|
{
|
|
|
|
sign = ' ';
|
|
|
|
size--;
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(type & HEX_PREP && isHex) size -= 2;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
static const char *lowerDigits = "0123456789abcdef",
|
|
|
|
*upperDigits = "0123456789ABCDEF";
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
s32 i = 0;
|
|
|
|
char tmp[66],
|
|
|
|
c = (type & ZEROPAD) ? '0' : ' ';
|
|
|
|
const char *dig = (type & UPPERCASE) ? upperDigits : lowerDigits;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(num == 0) tmp[i++] = '0';
|
2017-05-07 17:58:44 +02:00
|
|
|
else
|
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
while (num != 0)
|
|
|
|
{
|
|
|
|
u64 base = isHex ? 16ULL : 10ULL;
|
|
|
|
tmp[i++] = dig[(u64)num % base];
|
|
|
|
num = (s64)((u64)num / base);
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(i > precision) precision = i;
|
|
|
|
size -= precision;
|
|
|
|
if(!(type & (ZEROPAD | LEFT))) while(size-- > 0) *str++ = ' ';
|
|
|
|
if(sign) *str++ = sign;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(type & HEX_PREP && isHex)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
*str++ = '0';
|
|
|
|
*str++ = 'x';
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(!(type & LEFT)) while(size-- > 0) *str++ = c;
|
|
|
|
while(i < precision--) *str++ = '0';
|
|
|
|
while(i-- > 0) *str++ = tmp[i];
|
|
|
|
while(size-- > 0) *str++ = ' ';
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
return str;
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
u32 vsprintf(char *buf, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
char *str;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
for(str = buf; *fmt; fmt++)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
if(*fmt != '%')
|
|
|
|
{
|
|
|
|
*str++ = *fmt;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
//Process flags
|
|
|
|
u32 flags = 0; //Flags to number()
|
|
|
|
bool loop = false;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
while(!loop)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
2017-05-07 23:18:26 +02:00
|
|
|
switch(*++fmt)
|
|
|
|
{
|
|
|
|
case '-': flags |= LEFT; break;
|
|
|
|
case '+': flags |= PLUS; break;
|
|
|
|
case ' ': flags |= SPACE; break;
|
|
|
|
case '#': flags |= HEX_PREP; break;
|
|
|
|
case '0': flags |= ZEROPAD; break;
|
|
|
|
default: loop = true; break;
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
//Get field width
|
|
|
|
s32 fieldWidth = -1; //Width of output field
|
|
|
|
if(IS_DIGIT(*fmt)) fieldWidth = skipAtoi(&fmt);
|
|
|
|
else if(*fmt == '*')
|
|
|
|
{
|
|
|
|
fmt++;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
fieldWidth = va_arg(args, s32);
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(fieldWidth < 0)
|
|
|
|
{
|
|
|
|
fieldWidth = -fieldWidth;
|
|
|
|
flags |= LEFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the precision
|
|
|
|
s32 precision = -1; //Min. # of digits for integers; max number of chars for from string
|
|
|
|
if(*fmt == '.')
|
|
|
|
{
|
|
|
|
fmt++;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(IS_DIGIT(*fmt)) precision = skipAtoi(&fmt);
|
|
|
|
else if(*fmt == '*')
|
|
|
|
{
|
|
|
|
fmt++;
|
|
|
|
precision = va_arg(args, s32);
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(precision < 0) precision = 0;
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
//Get the conversion qualifier
|
|
|
|
bool isLongLong = false;
|
|
|
|
if(*fmt == 'l')
|
|
|
|
{
|
|
|
|
if(*++fmt == 'l')
|
|
|
|
{
|
|
|
|
fmt++;
|
|
|
|
isLongLong = true;
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
//Default base
|
|
|
|
bool isHex = false;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
switch(*fmt)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
if(!(flags & LEFT)) while(--fieldWidth > 0) *str++ = ' ';
|
|
|
|
*str++ = (u8)va_arg(args, s32);
|
|
|
|
while(--fieldWidth > 0) *str++ = ' ';
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
char *s = va_arg(args, char *);
|
|
|
|
if(!s) s = "<NULL>";
|
|
|
|
u32 len = (precision != -1) ? strnlen(s, precision) : strlen(s);
|
|
|
|
if(!(flags & LEFT)) while((s32)len < fieldWidth--) *str++ = ' ';
|
|
|
|
for(u32 i = 0; i < len; i++) *str++ = *s++;
|
|
|
|
while((s32)len < fieldWidth--) *str++ = ' ';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
if(fieldWidth == -1)
|
|
|
|
{
|
|
|
|
fieldWidth = 8;
|
|
|
|
flags |= ZEROPAD;
|
|
|
|
}
|
|
|
|
str = processNumber(str, (s64)va_arg(args, u32), true, fieldWidth, precision, flags);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//Integer number formats - set up the flags and "break"
|
|
|
|
case 'X':
|
|
|
|
flags |= UPPERCASE;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
isHex = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
flags |= SIGN;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if(*fmt != '%') *str++ = '%';
|
|
|
|
if(*fmt) *str++ = *fmt;
|
|
|
|
else fmt--;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
s64 num;
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
if(flags & SIGN)
|
|
|
|
{
|
|
|
|
if(isLongLong) num = va_arg(args, s64);
|
|
|
|
else num = va_arg(args, s32);
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
else
|
2017-05-07 23:18:26 +02:00
|
|
|
{
|
|
|
|
if(isLongLong) num = va_arg(args, u64);
|
|
|
|
else num = va_arg(args, u32);
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
str = processNumber(str, num, isHex, fieldWidth, precision, flags);
|
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
*str = 0;
|
|
|
|
return str - buf;
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:18:26 +02:00
|
|
|
u32 sprintf(char *buf, const char *fmt, ...)
|
2017-05-07 17:58:44 +02:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2017-05-07 23:18:26 +02:00
|
|
|
u32 res = vsprintf(buf, fmt, args);
|
2017-05-07 17:58:44 +02:00
|
|
|
va_end(args);
|
2017-05-07 23:18:26 +02:00
|
|
|
return res;
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|