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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "printf.h"
#include "string.h"
#include "init.h"
#include "hal/obj.h"
#include <stdint.h>
int kprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int r = kvprintf(fmt, ap);
va_end(ap);
return r;
}
#define SPRINTF_PUTC(str, size, i, c) \
if(str && i < size) str[i] = (c); \
i++
static int
convert_as_int(
char *str,
size_t size,
int stri,
uintmax_t value,
int radix,
int unsign)
{
unsigned digits = 0;
int ostri = stri;
if((intmax_t)value < 0 && !unsign) {
SPRINTF_PUTC(str, size, stri, '-');
value = 0 - (intmax_t)value;
}
for(uintmax_t i = value; i != 0; i /= radix) digits++;
if(value == 0) digits++;
for(uintmax_t i = value, digiti = 0; digiti < digits; i /= radix) {
char i_mod = (char)(i % radix);
int stri_digiti = stri + (digits - digiti) - 1;
SPRINTF_PUTC(str, size, stri_digiti,
(i_mod > 9 ? (i_mod + 'a' - 10) : (i_mod + '0')));
digiti++;
}
return digits;
}
enum printf_state
{
FSTATE_DIRECT = 0,
FSTATE_CONVERT
};
static int
s_convert_string(char *str, size_t size, va_list ap, size_t stri)
{
const char *strarg = va_arg(ap, const char*);
for(const char *strargc = strarg; *strargc; strargc++) {
SPRINTF_PUTC(str, size, stri, *strargc);
}
return stri;
}
static int
s_convert_int(char *str, size_t size, va_list ap, size_t stri,
int fw, char fp, int typelen, int radix, int usign, int caps)
{
char num_buf[64];
uintmax_t value = 0;
switch(typelen) {
case 1: value = (uintmax_t)(int8_t)va_arg(ap, int); break;
case 2: value = (uintmax_t)(int16_t)va_arg(ap, int); break;
default:
case 4: value = (uintmax_t)va_arg(ap, unsigned int); break;
case 8: value = (uintmax_t)va_arg(ap, uintmax_t); break;
}
size_t pad_stri = stri;
int advance = convert_as_int(num_buf, 64, 0, value, radix, usign);
if(caps) {
for(int i = 0; i < advance; i++) {
num_buf[i] = c_toupper(num_buf[i]);
}
}
int pad_diff = fw - advance;
for(int i = 0; i < pad_diff; i++) {
SPRINTF_PUTC(str, size, stri, fp);
}
for(int i = 0; i < advance; i++) {
SPRINTF_PUTC(str, size, stri, num_buf[i]);
}
return stri;
}
int
s_convert_path(char *str, size_t size, va_list ap, size_t stri)
{
uint16_t *obj_path = va_arg(ap, uint16_t*);
size_t obj_pathlen = va_arg(ap, size_t);
for(size_t i = 0; i < obj_pathlen; i++) {
uint16_t subdir = obj_path[i];
int advance = convert_as_int(str, size, stri, subdir, 10, 1);
stri += advance;
SPRINTF_PUTC(str, size, stri, '/');
}
return stri;
}
int
vsnprintf(
char *str, size_t size,
const char *fmt, va_list ap)
{
int stri = 0;
int state = FSTATE_DIRECT;
for(const char *fmtc = fmt; *fmtc; fmtc++) {
if(state == FSTATE_DIRECT) {
if(*fmtc == '%') {
state = FSTATE_CONVERT;
continue;
}
SPRINTF_PUTC(str, size, stri, *fmtc);
}else {
int field_width = 0;
char field_pad = ' ';
int lenspec = 4;
int radix = 10;
int unsign = 0;
int caps = 0;
if(state == FSTATE_CONVERT) {
switch(*fmtc) {
case 's':
stri = s_convert_string(str, size, ap, stri);
break;
case 'b':
stri = s_convert_int(str, size, ap, stri, field_width, field_pad, lenspec, 2, 1, 0);
break;
case 'o':
stri = s_convert_int(str, size, ap, stri, field_width, field_pad, lenspec, 8, 0, 0);
break;
case 'p':
lenspec = sizeof(uintptr_t);
field_width = lenspec * 2;
field_pad = '0';
case 'X':
caps = 1;
case 'x':
unsign = 1;
stri = s_convert_int(str, size, ap, stri, field_width, field_pad, lenspec, 16, unsign, caps);
break;
case 'u':
unsign = 1;
case 'd':
case 'i':
stri = s_convert_int(str, size, ap, stri, field_width, field_pad, lenspec, 10, unsign, caps);
break;
/*custom formatters for jove.*/
case 'P':
stri = s_convert_path(str, size, ap, stri);
break;
}
}
state = FSTATE_DIRECT;
}
}
if(str && stri < size) str[stri] = 0;
return stri;
}
|