-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
194 lines (167 loc) · 2.87 KB
/
file.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "cedit.h"
/*
* creates a new empty file as current file - it will be located after the last
* file - otherwise it will set itself as first file
*/
void file_new()
{
File *file;
file = newFile();
if(CF == 0) {
CF = file;
} else {
while(CF->next != 0) CF = CF->next;
CF->next = file;
file->prev = CF;
CF = CF->next;
}
draw_all();
}
/*
* loads a file into memory by calling file_new() and populating it with content
*/
void file_open(char *path)
{
char ch;
FILE *fp;
File *file;
Line *line;
size_t len;
if(path == 0) {
dialog_openfile();
return;
}
if(access(path, R_OK) != -1){
fp = fopen(path, "r");
} else {
fp = fopen(path, "w");
}
if(fp == NULL){
dialog_openfile_failure();
return;
}
file_new();
CF->path = malloc(strlen(path));
strncpy(CF->path, path, strlen(path));
CF->type = getFiletype();
line = CF->first;
ch = fgetc(fp);
while(ch != EOF){
if (ch != 10) {
len = tb_utf8_char_length(ch);
line->blen += len;
core_ensure_cap(line, line->blen);
while(len > 0 && ch != EOF) {
strncat(line->c, &ch, 1);
ch = fgetc(fp);
len--;
}
line->clen += 1;
} else if((ch = fgetc(fp)) != EOF){
line->next = newLine();
line->next->prev = line;
line = line->next;
}
}
fclose(fp);
draw_all();
dialog_openfile_success();
}
/*
* close the current file, freeing all of its contents and relinking remaining
* files if neccessary
*/
void file_close()
{
Line *line;
File *f;
f = CF;
line = f->first;
while(line != 0) {
free(line->c);
if(line->next != 0) {
line = line->next;
free(line->prev);
} else {
free(line);
break;
}
}
free(f->cur);
free(f->anc);
free(f->path);
free(f->type);
if(f->prev != 0) f->prev->next = f->next;
if(f->next != 0) f->next->prev = f->prev;
if(f->prev != 0){
CF = f->prev;
} else if(f->next != 0) {
CF = f->next;
} else {
CF = 0;
}
free(f);
}
/*
* saves current file to disc
*/
void file_save(char *path)
{
FILE *fp;
Line *line;
size_t i;
if(path != 0 && strlen(path) > 0){
CF->path = malloc(strlen(path));
strncpy(CF->path, path, strlen(path));
CF->type = getFiletype();
}
if(CF->path == 0) {
dialog_savefile();
return;
}
line = CF->first;
fp = fopen(CF->path, "w+");
if(fp == NULL){
dialog_savefile_failure();
return;
}
while(line != 0) {
for(i = 0; i < line->blen; i++){
fputc(line->c[i], fp);
}
fputc('\n', fp);
line = line->next;
}
draw_all();
fclose(fp);
dialog_savefile_success();
}
/*
* switch files to the right, left or to the n-th file
*/
void file_switch_right()
{
if(CF->next != 0) {
CF = CF->next;
draw_all();
}
}
void file_switch_left()
{
if(CF->prev != 0) {
CF = CF->prev;
draw_all();
}
}
void file_switch(size_t n){
File *f;
size_t c;
f = CF;
while(f->prev != 0) f = f->prev;
for(c = 1; c < n ; c++){
if(f->next == 0) break;
f = f->next;
}
CF = f;
draw_all();
}