-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdp_printf.c
107 lines (96 loc) · 2.23 KB
/
vdp_printf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "vdp.h"
#include "string.h"
#include <stdarg.h>
/* a quick hacky printf workaround */
/* not written to be efficient, only to be fast to write */
/* always returns 0, and is nowhere near a full implementation */
int vdpprintf(char *str, ...) {
char *p;
char *s;
char *orig;
unsigned int u;
char ch;
int w,x;
va_list ap;
va_start(ap, str);
p = str;
orig = p;
while (*p) {
w = 0;
if (*p != '%') {
vdpputchar(*(p++));
} else {
++p;
while ((*p >= '0') && (*p <= '9')) {
// width field
w *= 10;
w += (*p) - '0';
++p;
}
switch (*p) {
case '.':
case 'f':
// float and/or precision
// no
vdpputchar('#');
++p;
break;
case 's':
// string
s = va_arg(ap, char*);
if (w > 0) {
x=strlen(s);
w-=x;
while (w-- > 0) vdpputchar(' ');
}
while (*s) vdpputchar(*(s++));
++p;
break;
case 'u':
// unsigned int
u = va_arg(ap, unsigned int);
s = uint2str(u);
if (w > 0) {
x = strlen(s);
w-=x;
while (w-- > 0) vdpputchar(' ');
}
while (*s) vdpputchar(*(s++));
++p;
break;
case 'i':
case 'd':
// signed int
u = va_arg(ap, int);
s = int2str(u);
if (w > 0) {
x = strlen(s);
w-=x;
while (w-- > 0) vdpputchar(' ');
}
while (*s) vdpputchar(*(s++));
++p;
break;
case 'c':
// char
ch = va_arg(ap, int); // char promotes to int for varargs
vdpputchar(ch);
++p;
break;
case 'X':
// uppercase hex - only expects a char for debug here anyway
// no precision or double bytes...
u = va_arg(ap, int);
hexprint(u&0xff);
++p;
break;
default:
vdpputchar(*p);
++p;
break;
}
} // else
} // while
va_end(ap);
return 0;
} // printf