-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
73 lines (66 loc) · 1.48 KB
/
io.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
#include "io.h"
// #define DEBUGIO
char add_buffer_to_tape(char *string, TDLListT *last) {
char nlflag = 0;
TDLListT prev = *last;
TDLListT temp = 0;
int i = 0;
for (; string[i] ; i++) {
if (string[i] != '\n') {
temp = aloc_tape_cell(string[i]);
if (prev) {
prev->next = temp;
temp->prev = prev;
}
prev = temp;
} else {
nlflag = 1;
break;
}
}
*last = prev;
return nlflag;
}
TLine readfile(const char *filename) {
char buffer[1000];
FILE* file = fopen(filename, "r");
if (!file) {
return 0;
}
TLine temp = init_line();
TDLListT last = temp->finger->value->sentry;
while (fgets(buffer, 1000, file)) {
#ifdef DEBUGIO
printf("%s", buffer);
printf("%p\n", temp->finger);
#endif
char nl = add_buffer_to_tape(buffer, &last);
if (nl) {
create_down(temp);
move_down(temp, 1);
last = temp->finger->value->sentry;
}
}
delete_line_on_finger(temp);
temp->finger = temp->sentry->next;
temp->cursor = temp->finger->value->sentry;
temp->posx = 1;
temp->posy = 1;
temp->posline = 1;
return temp;
fclose(file);
}
void writefile(const char *filename, TLine line) {
// go past the line sentry
TDLListL templine = line->sentry->next;
TDLListT temptape = 0;
FILE* filep = fopen(filename, "w");
for(; templine; templine = templine->next) {
temptape = templine->value->sentry->next;
for (; temptape; temptape = temptape->next) {
fprintf(filep, "%c", temptape->value);
}
fprintf(filep, "\n");
}
fclose(filep);
}