-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.c
More file actions
80 lines (68 loc) · 1.56 KB
/
stream.c
File metadata and controls
80 lines (68 loc) · 1.56 KB
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
#include <aria/stream.h>
#include <aria/string.h>
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
static void stream_write_number(struct stream_info *stream, uint64_t nunmber,
int base);
int stream_print(struct stream_info *stream, const char *str, va_list arg)
{
if (stream == NULL || stream->write == NULL)
return -1;
for (size_t i = 0; i < strlen(str); i++) {
if (str[i] != '%') {
stream->write(stream, str[i]);
continue;
}
switch (str[++i]) {
case 'd': {
uint64_t number = va_arg(arg, uint64_t);
stream_write_number(stream, number, 10);
break;
}
case 's': {
const char *s = va_arg(arg, const char *);
if (!stream) {
stream->write(stream, 'N');
stream->write(stream, 'U');
stream->write(stream, 'L');
stream->write(stream, 'L');
break;
}
for (size_t i = 0; i < strlen(s); i++) {
stream->write(stream, s[i]);
}
break;
}
case 'c': {
char c = va_arg(arg, int);
stream->write(stream, c);
break;
}
case 'x': {
uint64_t number = va_arg(arg, uint64_t);
stream_write_number(stream, number, 16);
break;
}
case 'b': {
uint64_t number = va_arg(arg, uint64_t);
stream_write_number(stream, number, 2);
break;
}
}
}
return 0;
}
static void stream_write_number(struct stream_info *stream, uint64_t number,
int base)
{
static char characters[] = "0123456789ABCDEF";
int arr[50], cnt = 0;
do {
arr[cnt++] = number % base;
number /= base;
} while (number);
for (int i = cnt - 1; i > -1; i--) {
stream->write(stream, characters[arr[i]]);
}
}