toaruos/libc/stdio/printf.c

486 lines
10 KiB
C
Raw Normal View History

2018-02-25 08:13:54 +03:00
#include <stdio.h>
#include <string.h>
#include <va_list.h>
#define OUT(c) do { callback(userData, (c)); written++; } while (0)
static size_t print_dec(unsigned long long value, unsigned int width, int (*callback)(void*,char), void * userData, int fill_zero, int align_right, int precision) {
size_t written = 0;
2021-04-06 15:18:44 +03:00
unsigned long long n_width = 1;
unsigned long long i = 9;
2020-04-30 13:02:28 +03:00
if (precision == -1) precision = 1;
2018-04-29 10:32:35 +03:00
if (value == 0) {
n_width = 0;
2018-04-29 10:32:35 +03:00
} else {
2021-04-06 15:18:44 +03:00
unsigned long long val = value;
while (val >= 10UL) {
val /= 10UL;
n_width++;
}
2018-02-25 08:13:54 +03:00
}
2021-04-06 15:18:44 +03:00
if (n_width < (unsigned long long)precision) n_width = precision;
2020-04-30 13:02:28 +03:00
2018-02-25 08:13:54 +03:00
int printed = 0;
2018-03-03 14:23:13 +03:00
if (align_right) {
while (n_width + printed < width) {
OUT(fill_zero ? '0' : ' ');
2018-03-03 14:23:13 +03:00
printed += 1;
}
2018-02-25 08:13:54 +03:00
2018-03-03 14:23:13 +03:00
i = n_width;
char tmp[100];
2018-03-03 14:23:13 +03:00
while (i > 0) {
2021-04-06 15:18:44 +03:00
unsigned long long n = value / 10;
long long r = value % 10;
tmp[i - 1] = r + '0';
2018-03-03 14:23:13 +03:00
i--;
value = n;
}
while (i < n_width) {
OUT(tmp[i]);
i++;
}
2018-03-03 14:23:13 +03:00
} else {
i = n_width;
char tmp[100];
2018-03-03 14:23:13 +03:00
while (i > 0) {
2021-04-06 15:18:44 +03:00
unsigned long long n = value / 10;
long long r = value % 10;
tmp[i - 1] = r + '0';
2018-03-03 14:23:13 +03:00
i--;
value = n;
printed++;
}
while (i < n_width) {
OUT(tmp[i]);
i++;
}
2021-04-06 15:18:44 +03:00
while (printed < (long long)width) {
OUT(fill_zero ? '0' : ' ');
2018-03-03 14:23:13 +03:00
printed += 1;
}
2018-02-25 08:13:54 +03:00
}
return written;
2018-02-25 08:13:54 +03:00
}
/*
* Hexadecimal to string
*/
static size_t print_hex(unsigned long long value, unsigned int width, int (*callback)(void*,char), void* userData, int fill_zero, int alt, int caps, int align) {
size_t written = 0;
2018-02-25 08:13:54 +03:00
int i = width;
2021-04-06 15:18:44 +03:00
unsigned long long n_width = 1;
unsigned long long j = 0x0F;
while (value > j && j < UINT64_MAX) {
2018-02-25 08:13:54 +03:00
n_width += 1;
j *= 0x10;
j += 0x0F;
}
if (!fill_zero && align == 1) {
while (i > (long long)n_width + 2*!!alt) {
OUT(' ');
i--;
}
}
if (alt) {
OUT('0');
OUT(caps ? 'X' : 'x');
}
if (fill_zero && align == 1) {
while (i > (long long)n_width + 2*!!alt) {
OUT('0');
i--;
}
2018-02-25 08:13:54 +03:00
}
2021-04-06 15:18:44 +03:00
i = (long long)n_width;
2018-02-25 08:13:54 +03:00
while (i-- > 0) {
char c = (caps ? "0123456789ABCDEF" : "0123456789abcdef")[(value>>(i*4))&0xF];
OUT(c);
2018-02-25 08:13:54 +03:00
}
if (align == 0) {
i = width;
while (i > (long long)n_width + 2*!!alt) {
OUT(' ');
i--;
}
}
return written;
2018-02-25 08:13:54 +03:00
}
/*
* vasprintf()
*/
size_t xvasprintf(int (*callback)(void *, char), void * userData, const char * fmt, va_list args) {
2018-02-25 08:13:54 +03:00
char * s;
size_t written = 0;
2018-02-25 08:13:54 +03:00
for (const char *f = fmt; *f; f++) {
if (*f != '%') {
OUT(*f);
2018-02-25 08:13:54 +03:00
continue;
}
++f;
unsigned int arg_width = 0;
int align = 1; /* right */
2018-03-01 03:07:43 +03:00
int fill_zero = 0;
2018-06-25 12:15:02 +03:00
int big = 0;
2018-10-10 15:46:58 +03:00
int alt = 0;
int always_sign = 0;
2021-05-31 04:47:02 +03:00
int precision = -1;
while (1) {
if (*f == '-') {
align = 0;
++f;
} else if (*f == '#') {
alt = 1;
++f;
} else if (*f == '*') {
arg_width = (char)va_arg(args, int);
++f;
} else if (*f == '0') {
fill_zero = 1;
++f;
} else if (*f == '+') {
always_sign = 1;
++f;
} else if (*f == ' ') {
always_sign = 2;
++f;
} else {
break;
}
2018-03-01 03:07:43 +03:00
}
2018-02-25 08:13:54 +03:00
while (*f >= '0' && *f <= '9') {
arg_width *= 10;
arg_width += *f - '0';
++f;
}
2018-06-25 13:42:07 +03:00
if (*f == '.') {
++f;
precision = 0;
if (*f == '*') {
precision = (int)va_arg(args, int);
2018-06-25 13:42:07 +03:00
++f;
} else {
while (*f >= '0' && *f <= '9') {
precision *= 10;
precision += *f - '0';
++f;
}
2018-06-25 13:42:07 +03:00
}
}
2018-06-25 12:15:02 +03:00
if (*f == 'l') {
big = 1;
++f;
2018-10-10 08:27:03 +03:00
if (*f == 'l') {
big = 2;
++f;
}
2018-06-25 12:15:02 +03:00
}
if (*f == 'j') {
big = (sizeof(uintmax_t) == sizeof(unsigned long long) ? 2 :
sizeof(uintmax_t) == sizeof(unsigned long) ? 1 : 0);
++f;
}
2018-09-25 12:44:22 +03:00
if (*f == 'z') {
big = (sizeof(size_t) == sizeof(unsigned long long) ? 2 :
sizeof(size_t) == sizeof(unsigned long) ? 1 : 0);
++f;
}
if (*f == 't') {
big = (sizeof(ptrdiff_t) == sizeof(unsigned long long) ? 2 :
sizeof(ptrdiff_t) == sizeof(unsigned long) ? 1 : 0);
2018-09-25 12:44:22 +03:00
++f;
}
2018-02-25 08:13:54 +03:00
/* fmt[i] == '%' */
switch (*f) {
case 's': /* String pointer -> String */
{
size_t count = 0;
2018-06-25 12:15:02 +03:00
if (big) {
return written;
2018-06-25 12:15:02 +03:00
} else {
s = (char *)va_arg(args, char *);
if (s == NULL) {
s = "(null)";
}
if (precision >= 0) {
2018-06-25 13:42:07 +03:00
while (*s && precision > 0) {
OUT(*s++);
2018-06-25 13:42:07 +03:00
count++;
precision--;
if (arg_width && count == arg_width) break;
}
} else {
while (*s) {
OUT(*s++);
2018-06-25 13:42:07 +03:00
count++;
if (arg_width && count == arg_width) break;
}
2018-06-25 12:15:02 +03:00
}
2018-02-25 08:13:54 +03:00
}
while (count < arg_width) {
OUT(' ');
2018-02-25 08:13:54 +03:00
count++;
}
}
break;
case 'c': /* Single character */
OUT((char)va_arg(args,int));
2018-02-25 08:13:54 +03:00
break;
2018-06-25 13:42:07 +03:00
case 'p':
alt = 1;
2021-05-31 04:47:02 +03:00
if (sizeof(void*) == sizeof(long long)) big = 2; /* fallthrough */
case 'X':
2018-02-25 08:13:54 +03:00
case 'x': /* Hexadecimal number */
2021-04-06 15:18:44 +03:00
{
unsigned long long val;
if (big == 2) {
val = (unsigned long long)va_arg(args, unsigned long long);
} else if (big == 1) {
val = (unsigned long)va_arg(args, unsigned long);
} else {
val = (unsigned int)va_arg(args, unsigned int);
2018-10-10 08:27:03 +03:00
}
written += print_hex(val, arg_width, callback, userData, fill_zero, alt, !(*f & 32), align);
2018-10-10 08:27:03 +03:00
}
2018-02-25 08:13:54 +03:00
break;
2018-06-25 14:22:01 +03:00
case 'i':
2018-02-25 08:13:54 +03:00
case 'd': /* Decimal number */
2018-02-25 12:29:25 +03:00
{
2018-10-10 08:27:03 +03:00
long long val;
if (big == 2) {
val = (long long)va_arg(args, long long);
2021-04-06 15:18:44 +03:00
} else if (big == 1) {
2018-10-10 08:27:03 +03:00
val = (long)va_arg(args, long);
2021-04-06 15:18:44 +03:00
} else {
val = (int)va_arg(args, int);
2018-10-10 08:27:03 +03:00
}
2018-02-25 12:29:25 +03:00
if (val < 0) {
OUT('-');
2018-02-25 12:29:25 +03:00
val = -val;
} else if (always_sign) {
OUT(always_sign == 2 ? ' ' : '+');
2018-02-25 12:29:25 +03:00
}
written += print_dec(val, arg_width, callback, userData, fill_zero, align, precision);
2018-02-25 12:29:25 +03:00
}
2018-02-25 08:13:54 +03:00
break;
2018-08-08 09:03:47 +03:00
case 'u': /* Unsigned ecimal number */
{
2018-10-10 08:27:03 +03:00
unsigned long long val;
if (big == 2) {
val = (unsigned long long)va_arg(args, unsigned long long);
2021-04-06 15:18:44 +03:00
} else if (big == 1) {
2018-10-10 08:27:03 +03:00
val = (unsigned long)va_arg(args, unsigned long);
2021-04-06 15:18:44 +03:00
} else {
val = (unsigned int)va_arg(args, unsigned int);
2018-10-10 08:27:03 +03:00
}
written += print_dec(val, arg_width, callback, userData, fill_zero, align, precision);
2018-08-08 09:03:47 +03:00
}
break;
case 'G':
case 'F':
2018-06-26 14:53:48 +03:00
case 'g': /* supposed to also support e */
2018-06-10 13:06:48 +03:00
case 'f':
{
2021-04-06 15:18:44 +03:00
if (precision == -1) precision = 8;
2018-06-10 13:06:48 +03:00
double val = (double)va_arg(args, double);
2021-04-06 15:18:44 +03:00
uint64_t asBits;
memcpy(&asBits,&val,sizeof(double));
#define SIGNBIT(d) (d & 0x8000000000000000UL)
/* Extract exponent */
int64_t exponent = (asBits & 0x7ff0000000000000UL) >> 52;
/* Fraction part */
uint64_t fraction = (asBits & 0x000fffffffffffffUL);
if (exponent == 0x7ff) {
if (!fraction) {
if (SIGNBIT(asBits)) {
OUT('-');
2021-04-06 15:18:44 +03:00
}
OUT('i');
OUT('n');
OUT('f');
2021-04-06 15:18:44 +03:00
} else {
OUT('n');
OUT('a');
OUT('n');
2021-04-06 15:18:44 +03:00
}
break;
} else if ((*f == 'g' || *f == 'G') && exponent == 0 && fraction == 0) {
2021-04-06 15:18:44 +03:00
if (SIGNBIT(asBits)) {
OUT('-');
2021-04-06 15:18:44 +03:00
}
OUT('0');
2021-04-06 15:18:44 +03:00
break;
}
/* Okay, now we can do some real work... */
int isNegative = !!SIGNBIT(asBits);
if (isNegative) {
OUT('-');
2018-06-10 13:06:48 +03:00
val = -val;
}
2021-04-06 15:18:44 +03:00
written += print_dec((unsigned long long)val, arg_width, callback, userData, fill_zero, align, 1);
OUT('.');
2021-04-06 15:18:44 +03:00
for (int j = 0; j < ((precision > -1 && precision < 16) ? precision : 16); ++j) {
if ((unsigned long long)(val * 100000.0) % 100000 == 0 && j != 0) break;
val = val - (unsigned long long)val;
2018-06-10 13:06:48 +03:00
val *= 10.0;
2021-04-06 15:18:44 +03:00
double roundy = ((double)(val - (unsigned long long)val) - 0.99999);
if (roundy < 0.00001 && roundy > -0.00001 && ((unsigned long long)(val) % 10) != 9) {
written += print_dec((unsigned long long)(val) % 10 + 1, 0, callback, userData, 0, 0, 1);
2021-04-06 15:18:44 +03:00
break;
}
written += print_dec((unsigned long long)(val) % 10, 0, callback, userData, 0, 0, 1);
2018-06-10 13:06:48 +03:00
}
}
break;
2018-02-25 08:13:54 +03:00
case '%': /* Escape */
OUT('%');
2018-02-25 08:13:54 +03:00
break;
default: /* Nothing at all, just dump it */
OUT(*f);
2018-02-25 08:13:54 +03:00
break;
}
}
return written;
2018-05-15 06:39:38 +03:00
}
2018-02-25 08:13:54 +03:00
/* Strings */
struct CBData {
char * str;
size_t size;
size_t written;
};
2018-02-25 08:13:54 +03:00
static int cb_sprintf(void * user, char c) {
struct CBData * data = user;
if (data->size > data->written + 1) {
data->str[data->written] = c;
data->written++;
if (data->written < data->size) {
data->str[data->written] = '\0';
}
}
return 0;
2018-06-25 09:45:32 +03:00
}
int vsnprintf(char *str, size_t size, const char *format, va_list ap) {
struct CBData data = {str,size,0};
int out = xvasprintf(cb_sprintf, &data, format, ap);
cb_sprintf(&data, '\0');
return out;
2018-06-25 14:15:45 +03:00
}
int snprintf(char * str, size_t size, const char * format, ...) {
struct CBData data = {str,size,0};
va_list args;
va_start(args, format);
int out = xvasprintf(cb_sprintf, &data, format, args);
va_end(args);
cb_sprintf(&data, '\0');
2018-07-19 13:12:49 +03:00
return out;
}
/* Unlimited strings */
static int cb_sxprintf(void * user, char c) {
struct CBData * data = user;
data->str[data->written] = c;
data->written++;
return 0;
2018-08-21 15:50:52 +03:00
}
2018-06-25 14:15:45 +03:00
int vsprintf(char *str, const char *format, va_list ap) {
struct CBData data = {str,0,0};
int out = xvasprintf(cb_sxprintf, &data, format, ap);
cb_sxprintf(&data, '\0');
2018-05-15 06:39:38 +03:00
return out;
2018-02-25 08:13:54 +03:00
}
int sprintf(char * str, const char * format, ...) {
struct CBData data = {str,0,0};
2018-02-25 08:13:54 +03:00
va_list args;
va_start(args, format);
int out = xvasprintf(cb_sxprintf, &data, format, args);
2018-02-25 08:13:54 +03:00
va_end(args);
cb_sxprintf(&data, '\0');
2018-05-15 06:39:38 +03:00
return out;
2018-02-25 08:13:54 +03:00
}
/**
* String that needs to reallocate as it goes
*/
static int cb_asprintf(void * user, char c) {
struct CBData * data = user;
if (data->written + 1 > data->size) {
data->size = data->size < 8 ? 8 : data->size * 2;
data->str = realloc(data->str, data->size);
}
data->str[data->written] = c;
data->written++;
return 0;
}
int vasprintf(char ** buf, const char * fmt, va_list args) {
struct CBData data = {NULL,0,0};
xvasprintf(cb_asprintf, &data, fmt, args);
cb_asprintf(&data, '\0');
*buf = data.str;
return 0;
}
int asprintf(char ** ret, const char * fmt, ...) {
va_list args;
va_start(args, fmt);
int out = vasprintf(ret,fmt,args);
va_end(args);
return out;
}
/* Streams */
static int cb_fprintf(void * user, char c) {
fputc(c,(FILE*)user);
return 0;
}
int fprintf(FILE *stream, const char * fmt, ...) {
2018-02-25 08:13:54 +03:00
va_list args;
va_start(args, fmt);
int out = xvasprintf(cb_fprintf, stream, fmt, args);
2018-02-25 08:13:54 +03:00
va_end(args);
return out;
}
int printf(const char * fmt, ...) {
2018-06-10 13:31:18 +03:00
va_list args;
va_start(args, fmt);
int out = xvasprintf(cb_fprintf, stdout, fmt, args);
2018-06-10 13:31:18 +03:00
va_end(args);
return out;
}
int vfprintf(FILE * stream, const char *fmt, va_list args) {
return xvasprintf(cb_fprintf, stream, fmt, args);
}
int vprintf(const char *fmt, va_list args) {
return xvasprintf(cb_fprintf, stdout, fmt, args);
}
2018-02-25 08:13:54 +03:00