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
|
#include <stdarg.h>
#include <stdint.h>
enum TYPE {
TYPE_INT = 1,
TYPE_UINT = 2,
TYPE_HEX = 3,
TYPE_STRING = 4,
TYPE_PERCENT = 5
};
enum FLAGS {
FL_ZERO = 1, /* zero padded */
FL_ALT = 2, /* alternate form (effect is type dependent) */
FL_UPPER = 4 /* uppercase */
};
static void hex(char **str, char flags, int fwidth, int size, int x) {
int i, y, len;
char c;
char *prefl = "0x";
char *prefu = "0X";
/* print the prefix, if the alt flag is set */
if(flags & FL_ALT) {
if(flags & FL_UPPER) {
while(*prefu)
*(*str)++ = *prefu++;
} else {
while(*prefl)
*(*str)++ = *prefl++;
}
}
/* calculate the number of digits */
len = 0;
y = x;
for(i = 0; i < sizeof(y) << 1; i++) {
c = ((unsigned int) y >> 28);
if(c)
len++;
y = y << 4;
}
/* print the padding characters (if any) */
c = ' ';
if(flags & FL_ZERO)
c = '0';
for(i = 0; i < fwidth - len; i++)
*(*str)++ = c;
/* actually print the digits */
for(i = 0; i < sizeof(x) << 1; i++) {
c = ((unsigned int) x >> 28) + '0';
if(c > '9') {
c += 0x27;
if(flags & FL_UPPER)
c -= 0x20;
}
if(c != '0')
*(*str)++ = c;
x = x << 4;
}
}
int vsprintf(char *str, char *fmt, va_list ap) {
int x, fwidth;
char flags;
char *s;
char *start = str;
uint8_t type;
while(*fmt) {
if(*fmt != '%') {
*str++ = *fmt++;
continue;
}
type = 0;
flags = 0;
fwidth = 0;
fmt++;
while(1) {
switch(*fmt) {
case '#':
flags |= FL_ALT;
break;
case '%':
type = TYPE_PERCENT;
goto done;
case 's':
type = TYPE_STRING;
goto done;
case 'x':
type = TYPE_HEX;
goto done;
case 'X':
type = TYPE_HEX;
flags |= FL_UPPER;
goto done;
default:
/* the first zero enables zero-padding */
if(*fmt == '0' && !fwidth) {
flags |= FL_ZERO;
goto next;
}
/* subsequent numbers indicate field width */
if(*fmt >= '0' && *fmt <= '9') {
fwidth = (fwidth * 10) + (*fmt - '0');
goto next;
}
goto done;
}
next:
fmt++;
}
done:
fmt++;
switch(type) {
case TYPE_STRING:
s = va_arg(ap, char*);
while(*s)
*str++ = *s++;
break;
case TYPE_HEX:
x = va_arg(ap, int);
hex(&str, flags, fwidth, 4, x);
break;
case TYPE_PERCENT:
*str++ = '%';
break;
}
}
*str = 0;
return (str - start);
}
|