forked from wkoszek/mini_printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_printf.c
132 lines (120 loc) · 2.38 KB
/
mini_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
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
#include "sys.h"
extern void io_putc(int c);
typedef unsigned int u32;
typedef unsigned long u64;
/*
* String to number conversion
*/
int
vpf_str_to_num(const char *fmtstr, int *resnum)
{
const char *cptr;
int res, digit, is_digit;
res = 0;
for (cptr = fmtstr; *fmtstr != '\0'; cptr++) {
is_digit = (*cptr >= '0' && *cptr <= '9');
if (!is_digit) {
break;
}
digit = *cptr - '0';
res *= 10;
res += digit;
}
*resnum = res;
return ((int)(cptr - fmtstr));
}
/*
* Number to string conversion
*/
void
vpf_num_to_str(u32 a, int is_hex, int pad_len, int pad_char)
{
char buf[32];
u32 base;
int bufidx, i;
for (i = 0; i < (int)sizeof(buf); i++) {
buf[i] = pad_char;
}
base = 10;
if (is_hex) {
base = 16;
}
dprintf(stderr, "a=%08x, base=%d\n", a, base);
bufidx = 0;
do {
dprintf(stderr, "a %% base = %d\n", a % base);
buf[bufidx] = "0123456789abcdef" [ a % base ];
dprintf(stderr, "buf=%c, bufidx=%d\n", buf[bufidx], bufidx);
a /= base;
bufidx++;
} while (a > 0);
if (pad_len > 0) {
if (pad_len >= (int)sizeof(buf)) {
pad_len = sizeof(buf) - 1;
}
if (bufidx < pad_len) {
bufidx = pad_len;
}
}
buf[bufidx] = '\0';
for (i = bufidx - 1; i >= 0; i--) {
io_putc(buf[i]);
}
}
/*
* Main body of vpf() routine. Main parsing of the print format happens here
*/
int
vpf(const char *fmtstr, va_list va)
{
const char *cptr, *prefmtptr;
int fmt, c, va_int, pad_len, pad_char, i;
pad_char = ' ';
for (cptr = fmtstr; *cptr != '\0'; cptr++) {
fmt = 0;
pad_len = 0;
c = *cptr;
prefmtptr = cptr;
if (*cptr == '%') {
prefmtptr = cptr;
cptr++;
if (*cptr >= '0' && *cptr <= '9') {
dprintf(stderr, "PAD *cptr = %c\n", *cptr);
cptr += vpf_str_to_num(cptr, &pad_len);
}
fmt = *cptr;
dprintf(stderr, "fmt=%c\n", fmt);
}
if ((fmt == 'd') || (fmt == 'x')) {
va_int = va_arg(va, int);
dprintf(stderr, "va_int=%08x, pad_len=%d\n", va_int,
pad_len);
vpf_num_to_str(va_int, fmt == 'x', pad_len, pad_char);
} else {
#ifndef MACOSX_PRINTF
for (i = 0; i < (cptr - prefmtptr); i++) {
dprintf(stderr, "C=%c\n", prefmtptr[i]);
io_putc(prefmtptr[i]);
}
#else
for (i = 0; i < pad_len - 1; i++) {
io_putc(pad_char);
}
#endif
io_putc(fmt != 0 ? fmt : c);
}
}
return 0;
}
/*
* Mini printf called "pf"
*/
int
pf(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
vpf(fmt, va);
va_end(va);
return 0;
}