-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
124 lines (103 loc) · 2.88 KB
/
utility.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kex.h>
#include <utility.h>
static char brain_commands[BRAINCOMMANDS] = {'<', '>', '+', '-', '.', ',', '[', ']'};
char file_name[BUFFERSIZE] = "";
void new_file() {
KeX_clear_lines(1);
}
void open_file() {
KeX_input("Enter yout testing file:", file_name, WIDTH/2, HEIGHT/2);
load_file();
}
void load_file() {
FILE *file = fopen(file_name, "r");
if(!file) {
KeX_message("Can't open the file.", WIDTH/2, HEIGHT/2);
return;
}
KeX_clear_lines(0);
LineListT *list = KeX_get_lines();
LineT *line = list->first;
for(int i = 0; !feof(file); ++i) {
fgets(line->text, WIDTH-ROWSW, file);
line->len = strlen(line->text);
if(line->text[line->len-1] == 10) {
line->text[--line->len] = 0;
line->enter = 1;
}
line = KeX_newline(list, line, NULL);
}
list->curline = 1;
KeX_update_cursor(0, 0);
KeX_redraw_numbers(1);
KeX_redraw();
fclose(file);
}
void save_file() {
FILE *file = fopen(file_name, "w");
if(!file) {
KeX_message("Can't save the file.", WIDTH/2, HEIGHT/2);
return;
}
LineT *line = KeX_get_lines()->first;
for(int i = 0; line; line = line->next, ++i) {
fprintf(file, "%s", line->text);
if(line->enter)
putc('\n', file);
}
fclose(file);
KeX_message("Saved!", WIDTH/2, HEIGHT/2);
}
void build_file() {
int c_len = 0,
buf = 0,
st = 0,
stack[STACKSIZE];
char bf[BRAINSIZE],
buffer[BUFFERSIZE],
commands[BRAINSIZE];
KeX_message("Press any key to continue...", WIDTH/2, HEIGHT/2);
for(LineT *line = KeX_get_lines()->first; line; line = line->next)
for(int i = 0; line->text[i] != 0 && i < line->len; ++i)
for(int c = 0; c < BRAINCOMMANDS; ++c)
if(line->text[i] == brain_commands[c])
commands[c_len++] = line->text[i];
commands[c_len] = 0;
memset(bf, 0, BRAINSIZE);
memset(buffer, 0, sizeof(buffer));
memset(stack, 0, sizeof(stack));
int stop = 0, i = 0, c = 0;
while(!stop) {
switch(commands[c++]) {
case 0: stop = 1; break;
case '<': i = (i - 1 + BRAINSIZE) % BRAINSIZE; break;
case '>': i = (i + 1) % BRAINSIZE; break;
case '+': ++bf[i]; break;
case '-': --bf[i]; break;
case ',': bf[i] = 0; break;
case '.': (buf < BUFFERSIZE) ? (buffer[buf++] = bf[i]) : (stop = 2); break;
case '[':
if(st >= STACKSIZE) { stop = 3; break; }
if(bf[i] != 0) stack[st++] = c;
else while(c < c_len && commands[c] != ']') c++;
break;
case ']':
if(bf[i] != 0) c = stack[st-1];
else st--;
if(st < 0) stop = 4;
break;
}
}
if(st > 0) stop = 4;
else buffer[buf] = 0;
switch(stop) {
case 1: KeX_message(buffer, WIDTH/2, HEIGHT/2); break;
case 2: KeX_message("ERROR: Buffer Overflow", WIDTH/2, HEIGHT/2); break;
case 3: KeX_message("ERROR: Stack Overflow", WIDTH/2, HEIGHT/2); break;
case 4: KeX_message("ERROR: Unbalanced '['-']'", WIDTH/2, HEIGHT/2); break;
default: KeX_message("ERROR: Unhandled", WIDTH/2, HEIGHT/2);
}
}