-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cs
More file actions
379 lines (328 loc) · 13.2 KB
/
FileSystem.cs
File metadata and controls
379 lines (328 loc) · 13.2 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.IO;
using Tree;
using Gtk;
using Log;
using System.Collections.Generic;
namespace FileSystem
{
class FileSystem {
public String path;
private FileTree tree;
private Logger log;
private Editor editor;
private bool active = true;
public FileSystem(String _path, FileTree _tree = null, Editor _editor = null) {
log = new Logger("FileSystem");
path = _path;
if (_tree == null) { // Inactive mode - the FileSystem isn't projecting to a TreeView
active = false;
}
else {
active = true;
editor = _editor;
}
tree = _tree;
if (active) {
tree.callback = onItemClicked;
}
scanPath("root",path);
}
private void scanPath(String place,String real_path) {
log.info($"Looking at directory: {place}");
IEnumerable<String> dirs;
try {
dirs = Directory.EnumerateDirectories(real_path);
}
catch {
log.error($"Cannot load path {real_path}");
return;
}
foreach (String dir in dirs) { // Look for directories
String real_dir = dir.Split(Path.DirectorySeparatorChar)[dir.Split(Path.DirectorySeparatorChar).Length-1];
log.info($"Found {dir}");
if (real_dir.StartsWith("mod_")) {
tree.addNode(place,real_dir.Replace("mod_",""),IndexTypes.Module,real_path+Path.DirectorySeparatorChar+real_dir+Path.DirectorySeparatorChar+real_dir+".mod.md");
scanPath(place+"/"+real_dir.Replace("mod_",""),real_path+Path.DirectorySeparatorChar+real_dir);
loadFunctions(place+"/"+real_dir.Replace("mod_",""),real_path+Path.DirectorySeparatorChar+real_dir+Path.DirectorySeparatorChar+real_dir+".mod.md");
}
else {
tree.addNode(place,real_dir,IndexTypes.Folder,real_path+Path.DirectorySeparatorChar+real_dir);
scanPath(place+"/"+real_dir,real_path+Path.DirectorySeparatorChar+real_dir);
}
}
foreach (String page in Directory.EnumerateFiles(real_path,"*.md")) { // Look for pages
String file = page.Split(Path.DirectorySeparatorChar)[page.Split(Path.DirectorySeparatorChar).Length-1];
log.info($"Found File {file}");
if (file.EndsWith(".mod.md")) {
// Ignore - It has already been processed above
}
else if (file.EndsWith(".class.md")) {
log.info($"Loading class at path {real_path+Path.DirectorySeparatorChar+file}");
tree.addNode(place,file.Replace(".class.md",""),IndexTypes.Class,real_path+Path.DirectorySeparatorChar+file); // Add to tree
loadFunctions(place+"/"+file.Replace(".class.md",""),real_path+Path.DirectorySeparatorChar+file);
}
else {
tree.addNode(place,file.Replace(".md",""),IndexTypes.Page, real_path+Path.DirectorySeparatorChar+file); // Add to tree
}
}
}
private void loadFunctions(String place, String real_path) {
FuncParser parser = new FuncParser(real_path);
foreach (String name in parser.getFunctions().Keys) {
log.info("Loading function at "+real_path);
tree.addNode(place,name,IndexTypes.Function, real_path);
}
}
bool onItemClicked(TreeIter iter) {
(bool,TreeIndex) result = tree.getIndexFromIter(iter,"root");
TreeIndex item = result.Item2;
log.info($"File path: {item.path}");
editor.openFile(item.path, item.label, item.type);
return true;
}
public bool insertPage(TreeIndex index, String name) {
if (name == "") {
return false;
}
if (index.type == IndexTypes.Module || index.type == IndexTypes.Folder) {
String bpath;
if (index.type == IndexTypes.Module) {
bpath = Path.GetDirectoryName(index.path);
}
else {
bpath = index.path;
}
File.WriteAllText(bpath+Path.DirectorySeparatorChar+name+".md","");
tree.addNode(index.tree_path,name,IndexTypes.Page,bpath+Path.DirectorySeparatorChar+name+".md");
editor.openFile(bpath+Path.DirectorySeparatorChar+name+".md",name,IndexTypes.Page);
return false;
}
else {
return true;
}
}
public bool insertClass(TreeIndex index, String name) {
if (name == "") {
return false;
}
if (index.type == IndexTypes.Module || index.type == IndexTypes.Folder) {
String bpath;
if (index.type == IndexTypes.Module) {
bpath = Path.GetDirectoryName(index.path);
}
else {
bpath = index.path;
}
File.WriteAllText(bpath+Path.DirectorySeparatorChar+name+".class.md",$@"
## Class **{name}**
");
tree.addNode(index.tree_path,name,IndexTypes.Class,bpath+Path.DirectorySeparatorChar+name+".class.md");
editor.openFile(bpath+Path.DirectorySeparatorChar+name+".class.md",name,IndexTypes.Class);
return false;
}
else {
return true;
}
}
public bool insertFolder(TreeIndex index, String name) {
if (name == "") {
return false;
}
if (index.type == IndexTypes.Module || index.type == IndexTypes.Folder) {
String bpath;
if (index.type == IndexTypes.Module) {
bpath = Path.GetDirectoryName(index.path);
}
else {
bpath = index.path;
}
Directory.CreateDirectory(bpath+Path.DirectorySeparatorChar+name);
tree.addNode(index.tree_path,name,IndexTypes.Folder,bpath+Path.DirectorySeparatorChar+name);
//editor.openFile(index.path,name,IndexTypes.Function);
return false;
}
else {
return true;
}
}
public bool insertModule(TreeIndex index, String name) {
if (name == "") {
return false;
}
if (index.type == IndexTypes.Module || index.type == IndexTypes.Folder) {
String bpath;
if (index.type == IndexTypes.Module) {
bpath = Path.GetDirectoryName(index.path);
}
else {
bpath = index.path;
}
Directory.CreateDirectory(bpath+Path.DirectorySeparatorChar+"mod_"+name);
File.WriteAllText(bpath+Path.DirectorySeparatorChar+"mod_"+name+Path.DirectorySeparatorChar+"mod_"+name+".mod.md",$@"
# Module **{name}**
");
tree.addNode(index.tree_path,name,IndexTypes.Module,bpath+Path.DirectorySeparatorChar+"mod_"+name+Path.DirectorySeparatorChar+"mod_"+name+".mod.md");
editor.openFile(bpath+Path.DirectorySeparatorChar+"mod_"+name+Path.DirectorySeparatorChar+"mod_"+name+".mod.md",name,IndexTypes.Module);
return false;
}
else {
return true;
}
}
public bool insertFunction(TreeIndex index, String name) {
if (name == "") {
return false;
}
if (index.type != IndexTypes.Page && index.type != IndexTypes.Folder) {
String bpath;
if (index.type == IndexTypes.Module) {
bpath = Path.GetDirectoryName(index.path);
}
else {
bpath = index.path;
}
FuncParser parser = new FuncParser(index.path);
parser[name] = $@"
### Function **{name}**()
";
parser.reSave();
tree.addNode(index.tree_path,name,IndexTypes.Function,index.path);
editor.openFile(index.path,name,IndexTypes.Function);
return false;
}
else {
return true;
}
}
public bool deleteNode(TreeIndex index, String name) {
editor.closeFile();
switch (index.type) {
case IndexTypes.Class: {
File.Delete(index.path);
tree.removeNode(index);
break;
}
case IndexTypes.Page: {
File.Delete(index.path);
tree.removeNode(index);
break;
}
case IndexTypes.Module: {
String bpath;
if (index.type == IndexTypes.Module) {
bpath = Path.GetDirectoryName(index.path);
}
else {
bpath = index.path;
}
System.IO.DirectoryInfo di = new DirectoryInfo(bpath);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
di.Delete();
tree.removeNode(index);
break;
}
case IndexTypes.Folder: {
System.IO.DirectoryInfo di = new DirectoryInfo(index.path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
di.Delete();
tree.removeNode(index);
break;
}
case IndexTypes.Function: {
FuncParser parser = new FuncParser(index.path);
parser.functions.Remove(index.label);
parser.reSave();
tree.removeNode(index);
break;
}
}
return false;
}
}
class FuncParser {
private String filename;
private String _head;
public Dictionary<String,String> functions;
private Logger log;
public FuncParser(String _filename) {
log = new Logger("FuncParser");
filename = _filename;
functions = new Dictionary<String,String>();
parse();
}
public void parse() {
functions = new Dictionary<String,String>();
String text = System.IO.File.ReadAllText(filename);
List<String> bits = new List<String>(text.Split("@##@"));
_head = bits[0];
bits.RemoveAt(0);
foreach (String func in bits) {
String[] name_chunks = func.Split("(!):");
if (name_chunks.Length < 2) {
log.error($"Can't load function {name_chunks[0]} in file {filename}");
continue;
}
log.info(name_chunks.ToString());
functions[name_chunks[0]] = name_chunks[1];
}
}
public void reSave() {
String output = head;
//output += Environment.NewLine;
foreach(String i in functions.Keys) {
output += $"@##@{i}(!):";
output += functions[i];
log.info($"Adding function {i} to {filename}");
}
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(filename, true))
{
file.Write(output);
file.Flush();
}
File.WriteAllText(filename,output);
log.info($"Saved file {filename}!");
}
public Dictionary<String,String> getFunctions() {
return functions;
}
public String getHead() {
return _head;
}
public String this[string index]
{
get
{
// get the item for that index.
return functions[index];
}
set
{
functions[index] = value;
}
}
public String head {
get {
return _head;
}
set {
_head = value;
}
}
}
}