This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/arm9/source/fmt.c

320 lines
9.1 KiB
C
Raw Permalink Normal View History

/*
* This file is part of Luma3DS
2020-04-25 14:26:21 +02:00
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
*
* 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
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
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-08 15:10:08 +02:00
//TuxSH's changes: add support for 64-bit numbers, remove floating-point code
2017-05-07 17:58:44 +02:00
#include "memory.h"
2017-05-08 15:10:08 +02:00
#include "fmt.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-08 15:10:08 +02:00
static s32 skipAtoi(const char **s)
2017-05-07 17:58:44 +02:00
{
2017-05-08 15:10:08 +02:00
s32 i = 0;
2017-05-07 23:18:26 +02:00
while(IS_DIGIT(**s)) i = i * 10 + *((*s)++) - '0';
2017-05-08 15:10:08 +02:00
2017-05-07 23:18:26 +02:00
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
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
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;
2017-05-08 04:49:20 +02:00
char tmp[20];
2017-05-07 23:18:26 +02:00
const char *dig = (type & UPPERCASE) ? upperDigits : lowerDigits;
2017-05-07 17:58:44 +02:00
2017-05-08 04:49:20 +02:00
if(num == 0)
{
if(precision != 0) tmp[i++] = '0';
type &= ~HEX_PREP;
}
2017-05-07 17:58:44 +02:00
else
{
2017-05-08 04:49:20 +02:00
while(num != 0)
2017-05-07 23:18:26 +02:00
{
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-08 04:49:20 +02:00
if(type & LEFT || precision != -1) type &= ~ZEROPAD;
if(type & HEX_PREP && isHex) size -= 2;
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-08 04:49:20 +02:00
if(type & ZEROPAD) while(size-- > 0) *str++ = '0';
2017-05-07 23:18:26 +02:00
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
int vsprintf(char *buf, const char *fmt, va_list args)
2017-05-07 23:18:26 +02:00
{
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()
2017-05-08 15:10:08 +02:00
bool loop = true;
2017-05-07 17:58:44 +02:00
2017-05-08 15:10:08 +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;
2017-05-08 15:10:08 +02:00
default: loop = false; break;
2017-05-07 23:18:26 +02:00
}
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
2017-05-08 04:49:20 +02:00
u32 integerType = 0;
2017-05-07 23:18:26 +02:00
if(*fmt == 'l')
{
if(*++fmt == 'l')
{
fmt++;
2017-05-08 04:49:20 +02:00
integerType = 1;
2017-05-07 23:18:26 +02:00
}
2018-06-14 18:13:57 +02:00
2017-05-08 04:49:20 +02:00
}
else if(*fmt == 'h')
{
if(*++fmt == 'h')
{
fmt++;
integerType = 3;
}
else integerType = 2;
2017-05-07 23:18:26 +02:00
}
2017-05-07 17:58:44 +02:00
2017-05-08 04:49:20 +02:00
bool isHex;
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;
}
2017-05-08 15:10:08 +02:00
str = processNumber(str, va_arg(args, u32), true, fieldWidth, precision, flags);
2017-05-07 23:18:26 +02:00
continue;
//Integer number formats - set up the flags and "break"
case 'X':
flags |= UPPERCASE;
//Falls through
2017-05-07 23:18:26 +02:00
case 'x':
isHex = true;
break;
case 'd':
case 'i':
flags |= SIGN;
//Falls through
2017-05-07 23:18:26 +02:00
case 'u':
2017-05-08 04:49:20 +02:00
isHex = false;
2017-05-07 23:18:26 +02:00
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)
{
2017-05-08 05:33:01 +02:00
if(integerType == 1) num = va_arg(args, s64);
2017-05-07 23:18:26 +02:00
else num = va_arg(args, s32);
2017-05-08 04:49:20 +02:00
if(integerType == 2) num = (s16)num;
else if(integerType == 3) num = (s8)num;
2017-05-07 23:18:26 +02:00
}
2017-05-07 17:58:44 +02:00
else
2017-05-07 23:18:26 +02:00
{
2017-05-08 05:33:01 +02:00
if(integerType == 1) num = va_arg(args, u64);
2017-05-07 23:18:26 +02:00
else num = va_arg(args, u32);
2017-05-08 04:49:20 +02:00
if(integerType == 2) num = (u16)num;
else if(integerType == 3) num = (u8)num;
2017-05-07 23:18:26 +02:00
}
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 (int)(str - buf);
2017-05-07 17:58:44 +02:00
}
int sprintf(char *buf, const char *fmt, ...)
2017-05-07 17:58:44 +02:00
{
va_list args;
va_start(args, fmt);
int 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
}