1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/crtlib/sprintf.c

246 lines
7.1 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: *s*printf() family //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K 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 //
// any later version. //
// //
// OS/K 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 OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
//
// TODO integer modifiers
//
#include <kalbase.h>
//
// Format str according to fmt using ellipsed arguments
//
// Standard is dumb so we won't follow it:
// 1) we return a size_t instead of int
// 2) for (v)snprintf we return the number of characters written
// instead of the number of characters that would have been written
// given large enough n
//
size_t sprintf(char *str, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(str, SIZE_T_MAX, fmt, ap);
va_end(ap);
return ret;
}
size_t vsprintf(char *str, const char *fmt, va_list ap)
{
return vsnprintf(str, SIZE_T_MAX, fmt, ap);
}
//
// (v)sprintf() but with a size limit: no more than n bytes are written in str
// Always null-terminate str
//
size_t snprintf(char *str, size_t n, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(str, n, fmt, ap);
va_end(ap);
return ret;
}
// Size of the buffer is for convertions
#define CONVBUF 4096
size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{
size_t ret = 0;
bool lflag = 0, hflag = 0, hhflag = 0, altflag = 0;
int base;
char mod;
char convbuf[CONVBUF] = { 0 };
char c;
int d;
uint u;
char *s;
// For aesthetic reasons...
n--;
// Go through the format string
while (*fmt && ret < n) {
// Regular character
if (*fmt != '%') {
*str++ = *fmt++;
ret++;
continue;
}
// Found a '%'
while (1) {
mod = *++fmt;
if (mod == '%') {
*str++ = '%';
ret++;
break;
}
if (mod == '#') {
altflag = 1;
continue;
}
if (mod == 'l' || mod == 'z' || mod == 't') {
// 'll'/'z'/'t' aliased to 'l'
lflag = 1;
continue;
}
if (mod == 'h') {
if (hflag) hhflag = 1;
else hflag = 1;
continue;
}
if (mod == 'c') {
c = (char)va_arg(ap, int);
*str++ = c;
ret++;
break;
}
if (mod == 's') {
s = va_arg(ap, char *);
while (*s && ret < n) {
*str++ = *s++;
ret++;
}
break;
}
if (mod == 'i') {
mod = 'd';
goto numeric;
}
if (mod == 'p') {
mod = 'x';
lflag = 1;
altflag = 1;
goto numeric;
}
if (mod == 'd' || mod == 'u' || mod == 'b' ||
mod == 'o' || mod == 'x' || mod == 'X') {
// Label to avoid some useless tests
numeric:
if (altflag && mod != 'd' && mod != 'u') {
// #d / #u is undefined behaviour
*str++ = '0';
if (++ret >= n) break;
if (mod != 'o') {
*str++ = mod;
ret++;
}
}
// "%d" is a special snowflake, deal with it separately
if (mod == 'd') {
if (lflag) {
ltoa(va_arg(ap, long), convbuf, 10);
}
else {
d = va_arg(ap, int);
if (hhflag) d &= 0xff; // char-ify
else if (hflag) d &= 0xffff; // short-ify
itoa(d, convbuf, 10);
}
}
// All unsigned mods
else {
base = (mod == 'u' ? 10 : (mod == 'b' ? 2 : (mod == 'o' ? 8 : 16)));
// Every other mod here is unsigned
if (lflag) {
ultoa(va_arg(ap, ulong), convbuf, base);
}
else {
u = va_arg(ap, uint);
if (hhflag) u &= 0xff; // char-ify
else if (hflag) u &= 0xffff; // short-ify
utoa(u, convbuf, base);
}
// "abcdef" => "ABCDEF"
if (mod == 'X') {
// Re-use s as an iterator for convbuf
s = convbuf;
while (*s) {
if (islower(*s))
*s = toupper(*s);
s++;
}
}
}
s = convbuf;
// Convertions happened, now we write into str
while (*s && ret < n) {
*str++ = *s++;
ret++;
}
// We're done dealing with this modifier
break;
}
// Unknown/unsupported modifier :|
*str++ = mod;
break;
}
// We fallthrough here from the "while (1)"
fmt++;
}
// Null-terminate no matter what
*str = 0;
return ret + 1;
}