2019-06-30 01:03:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 naehrwert
|
2024-01-07 10:38:10 +00:00
|
|
|
* Copyright (c) 2019-2024 CTCaer
|
2019-06-30 01:03:00 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-06-14 13:45:45 +00:00
|
|
|
#include <utils/types.h>
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
char **sout_buf;
|
|
|
|
|
|
|
|
static void _s_putc(char c)
|
|
|
|
{
|
|
|
|
**sout_buf = c;
|
|
|
|
*sout_buf += 1;
|
|
|
|
}
|
|
|
|
|
2020-03-03 02:11:13 +00:00
|
|
|
static void _s_puts(char *s)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
for (; *s; s++)
|
|
|
|
_s_putc(*s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _s_putn(u32 v, int base, char fill, int fcnt)
|
|
|
|
{
|
2022-05-19 12:01:10 +00:00
|
|
|
static const char digits[] = "0123456789ABCDEF";
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
char *p;
|
2022-05-19 12:01:10 +00:00
|
|
|
char buf[65];
|
2019-06-30 01:03:00 +00:00
|
|
|
int c = fcnt;
|
2022-05-08 02:26:01 +00:00
|
|
|
bool negative = false;
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2022-05-19 12:01:10 +00:00
|
|
|
if (base != 10 && base != 16)
|
2019-06-30 01:03:00 +00:00
|
|
|
return;
|
|
|
|
|
2022-05-08 02:26:01 +00:00
|
|
|
// Account for negative numbers.
|
|
|
|
if (base == 10 && v & 0x80000000)
|
|
|
|
{
|
|
|
|
negative = true;
|
|
|
|
v = (int)v * -1;
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
p = buf + 64;
|
|
|
|
*p = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c--;
|
|
|
|
*--p = digits[v % base];
|
|
|
|
v /= base;
|
|
|
|
} while (v);
|
|
|
|
|
2022-05-08 02:26:01 +00:00
|
|
|
if (negative)
|
|
|
|
*--p = '-';
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
if (fill != 0)
|
|
|
|
{
|
2022-05-08 02:26:01 +00:00
|
|
|
while (c > 0 && p > buf)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
*--p = fill;
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_s_puts(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void s_printf(char *out_buf, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int fill, fcnt;
|
|
|
|
|
|
|
|
sout_buf = &out_buf;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2022-05-19 12:01:10 +00:00
|
|
|
while (*fmt)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
2022-05-19 12:01:10 +00:00
|
|
|
if (*fmt == '%')
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
fmt++;
|
|
|
|
fill = 0;
|
|
|
|
fcnt = 0;
|
|
|
|
if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ')
|
|
|
|
{
|
|
|
|
fcnt = *fmt;
|
|
|
|
fmt++;
|
|
|
|
if (*fmt >= '0' && *fmt <= '9')
|
|
|
|
{
|
|
|
|
fill = fcnt;
|
|
|
|
fcnt = *fmt - '0';
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fill = ' ';
|
|
|
|
fcnt -= '0';
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 12:01:10 +00:00
|
|
|
switch (*fmt)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
case 'c':
|
2024-01-06 19:52:48 +00:00
|
|
|
char c = va_arg(ap, u32);
|
|
|
|
if (c != '\0')
|
|
|
|
_s_putc(c);
|
2019-06-30 01:03:00 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
_s_puts(va_arg(ap, char *));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
_s_putn(va_arg(ap, u32), 10, fill, fcnt);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
_s_putn(va_arg(ap, u32), 16, fill, fcnt);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
_s_putc('%');
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
goto out;
|
|
|
|
default:
|
|
|
|
_s_putc('%');
|
|
|
|
_s_putc(*fmt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_s_putc(*fmt);
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
**sout_buf = '\0';
|
|
|
|
va_end(ap);
|
2022-01-20 10:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void s_vprintf(char *out_buf, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int fill, fcnt;
|
|
|
|
|
|
|
|
sout_buf = &out_buf;
|
|
|
|
|
2022-05-19 12:01:10 +00:00
|
|
|
while (*fmt)
|
2022-01-20 10:37:41 +00:00
|
|
|
{
|
2022-05-19 12:01:10 +00:00
|
|
|
if (*fmt == '%')
|
2022-01-20 10:37:41 +00:00
|
|
|
{
|
|
|
|
fmt++;
|
|
|
|
fill = 0;
|
|
|
|
fcnt = 0;
|
|
|
|
if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ')
|
|
|
|
{
|
|
|
|
fcnt = *fmt;
|
|
|
|
fmt++;
|
|
|
|
if (*fmt >= '0' && *fmt <= '9')
|
|
|
|
{
|
|
|
|
fill = fcnt;
|
|
|
|
fcnt = *fmt - '0';
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fill = ' ';
|
|
|
|
fcnt -= '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(*fmt)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
_s_putc(va_arg(ap, u32));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
_s_puts(va_arg(ap, char *));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
_s_putn(va_arg(ap, u32), 10, fill, fcnt);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
_s_putn(va_arg(ap, u32), 16, fill, fcnt);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
_s_putc('%');
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
goto out;
|
|
|
|
default:
|
|
|
|
_s_putc('%');
|
|
|
|
_s_putc(*fmt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_s_putc(*fmt);
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
**sout_buf = '\0';
|
|
|
|
}
|