-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.h
70 lines (61 loc) · 1.17 KB
/
console.h
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
// requires windows.h
#define console_print_const(c, msg) \
console_write(c, msg, sizeof(msg))
typedef struct {
HANDLE stdout;
} Console;
static void console_init(Console *c)
{
AllocConsole();
c->stdout = GetStdHandle(STD_OUTPUT_HANDLE);
}
static void console_deinit(Console *c)
{
FreeConsole();
}
static unsigned long console_write(Console *c, void *msg, size_t len)
{
unsigned long n;
WriteConsoleA(c->stdout, msg, len, &n, NULL);
return n;
}
static unsigned long console_print(Console *c, void *msg)
{
return console_write(c, msg, lstrlen(msg));
}
static unsigned long console_put(Console *c, char b)
{
char msg[1] = {b};
return console_write(c, msg, sizeof(msg));
}
static unsigned long console_println(Console *c, void *msg)
{
unsigned long n;
n = console_write(c, msg, lstrlen(msg));
return n + console_put(c, '\n');
}
static unsigned long console_printu(Console *c, unsigned v)
{
char *tp, *rtp;
char t[11];
size_t n;
char x;
if(v < 10) {
return console_put(c, v+'0');
}
tp = rtp = t;
while(v) {
*tp++ = (v%10)+'0';
v = v/10;
}
n = tp-t;
tp--;
while(tp > rtp) {
x = *tp;
*tp = *rtp;
*rtp = x;
tp--;
rtp++;
}
return console_write(c, t, n);
}