aboutsummaryrefslogtreecommitdiff
path: root/lib/io/io.c
blob: e8384f3ed5edc0e1cdaf3ece18eea37ccd2ef7e6 (plain)
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
#include "io.h"

#include "../cstr/cstr.h"
#include "../aec/aec.h"

void __wstdn(const char *buf, u64 n)
{
	write(STDOUT_FD, buf, n);
}


void wstd(const char *buf)
{
	__wstdn(buf, cstr_length(buf));
}


void rstd(char *buf, unsigned long count)
{
	read(STDIN_FD, buf, count);
}

void wstdf__(const char *buf, const void **args)
{
	const char *start = buf;
	char stoi_buf[32] = "";
	int i;
	for (; *buf; ++buf) {
		if (*buf == '%') {
			if (buf - start > 0)
				__wstdn(start, buf - start);
			
			++buf;

			switch (*buf) {
				case '%': __wstdn("%", 1); break;
				case 's':
					__wstdn(((const char **)args)[0], cstr_length(((const char**)args)[0]));
					++args;
					break;
				case 'i':
					for (i = 0; i < 32; ++i)
						stoi_buf[i] = 0;
					i64_to_cstr(((u64 *)args)[0], stoi_buf, 32);
					__wstdn(stoi_buf, cstr_length(stoi_buf));
					++args;
					break;
				case 'u':
					for (i = 0; i < 32; ++i)
						stoi_buf[i] = 0;
					u64_to_cstr(((u64 *)args)[0], stoi_buf, 32);
					__wstdn(stoi_buf, cstr_length(stoi_buf));
					++args;
					break;
				case 'S':
					sgr(((const char **)args)[0]);
					++args;
					break;
				case 'F':
					foreground(((const char **)args)[0]);
					++args;
					break;
				case 'B':
					background(((const char **)args)[0]);
					++args;
					break;
			}

			start = buf + 1;
		}
	}

	if (buf - start > 0)
		__wstdn(start, buf - start);
}

#ifdef IO_LIB_UNIT_TEST
int main() {
	wstdf("%s %S%F%s%S\n", "hallo", SGR_UNDERLINE, COLOR_RED, "welt", SGR_RESET);
}
#endif