alcyone/SDK/CRT/STDIO/wprintf.c
Quinn Stephens 5c52adf492 [SDK] Big update
Signed-off-by: Quinn Stephens <quinn@osmora.org>
2025-06-11 20:00:34 -04:00

93 lines
1.4 KiB
C

/*++
Copyright (c) 2024-2025, Quinn Stephens.
Provided under the BSD 3-Clause license.
Module Name:
wprintf.c
Abstract:
Provides wide formatted string printing routines.
--*/
#include <wchar.h>
static
size_t
print_hex (
wchar_t *wcs,
size_t maxlen,
unsigned int num
)
{
wchar_t *dest;
size_t n;
int shift;
unsigned int x;
dest = wcs;
n = 0;
shift = 28;
while (n < maxlen && shift >= 0) {
x = (num >> shift) & 0xF;
if (x >= 0xa) {
*dest = 'a' + (x - 0xa);
} else {
*dest = '0' + x;
}
dest++;
n++;
shift -= 4;
}
return n;
}
int
vswprintf (
wchar_t *wcs,
size_t maxlen,
const wchar_t *format,
va_list args
)
{
wchar_t *dest;
size_t n, size;
dest = wcs;
n = 0;
while (n < maxlen && *format != '\0') {
if (*format != '%') {
*dest++ = *format++;
n++;
continue;
}
format++;
switch (*format) {
case 'x':
size = print_hex(dest, maxlen - n, va_arg(args, unsigned int));
n += size;
dest += size;
format++;
break;
case '\0':
break;
case '%':
default:
*dest++ = *format++;
n++;
break;
}
}
wcs[n] = '\0';
return (int)n;
}