-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.c
69 lines (64 loc) · 1.51 KB
/
text.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
#include "text.h"
#include <stdlib.h>
#include <stdio.h>
#include "slow.h"
#include "strtable.h"
#include "symtable.h"
int get_sym_var(const char** str) {
char delim = *++*str;
if (delim == '\'' || delim == '\"') {
char sym[SYM_LEN], *s = sym;
int* var;
size_t n = 0;
++*str;
while (**str && **str != delim && n < SYM_LEN - 1) {
*s++ = *(*str)++;
n++;
}
*s = '\0';
if (**str == delim)
++*str;
var = get_var(get_sym(sym));
if (var)
return *var;
}
return -1;
}
int get_num_arg(const char** str) {
char buf[8] = "0", *num = buf;
size_t n = 0;
while (**str >= '0' && **str <= '9' && n < sizeof(buf) - 1) {
*num++ = *(*str)++;
n++;
}
*num = '\0';
return atoi(buf);
}
void print_text(const char* text) {
while (*text) {
if (*text == '%') {
switch (*++text) {
case 'O':
case 'o':
printf("%d", get_sym_var(&text));
break;
case 'T':
case 't':
print_text(get_string(get_sym_var(&text)));
break;
case 'W':
case 'w':
text++;
wait(get_num_arg(&text));
break;
default:
putchar('%');
break;
}
} else {
putchar(*text++);
fflush(stdout);
slow_char(1);
}
}
}