-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileManager.pas
135 lines (120 loc) · 4.23 KB
/
FileManager.pas
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
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of SimpleRay *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
Unit FileManager;
{$MODE Delphi}
Interface
Uses sysutils, classes, uTokenizer, uncommenter;
Type
TFiledata = Record
Filename: String;
Tokens: TTokenarray;
End;
TFilemanager = Class
private
FFiles: Array Of TFiledata;
public
Constructor Create;
Destructor Destroy; override;
Function ParseFile(Filename: String; Tokenizer: TTokenizer; Uncommenter: TUnCommenter): TTokenarray;
Procedure Clear;
End;
Implementation
{ TFilemanager }
Procedure TFilemanager.Clear;
Var
i: Integer;
Begin
For i := 0 To high(FFiles) Do
Setlength(FFiles[i].Tokens, 0);
Setlength(FFIles, 0);
End;
Constructor TFilemanager.Create;
Begin
Inherited;
Setlength(FFIles, 0);
End;
Destructor TFilemanager.Destroy;
Begin
// Inherited;
clear;
End;
(*
Im Prinzip ist das hier das Laden und das Fparse des TRayParsers in einem.
*)
Function TFilemanager.ParseFile(Filename: String; Tokenizer: TTokenizer;
Uncommenter: TUnCommenter): TTokenarray;
Var
tmp: TTokenarray;
rlc, n: Integer;
i: Integer;
l: Tstringlist;
Begin
setlength(Result, 0);
// Filename := lowercase(Filename); -- Bei Linux ist das der Todestoß
// If fileexists(Filename) Then Begin // braucht nicht, da das EatExternal schon macht
// Prüfen ob die File schon mal geladen wurde
For i := 0 To High(FFiles) Do
If lowercase(Ffiles[i].Filename) = lowercase(Filename) Then Begin
result := Ffiles[i].Tokens;
exit;
End;
// Laden der File
Setlength(FFiles, high(FFiles) + 2);
FFIles[high(FFIles)].Filename := Filename;
l := TStringList.create;
l.LoadFromFile(Filename);
(******************************************************************************)
tmp := Tokenizer.scan(Uncommenter.Uncomment(l.text));
l.free;
// Umformatieren das die Zeilennummern stimmen, und Rauswerfen der zusatzinformationen.
rlc := 0;
// Gab es überhaupt irgendwas zum Parsen ?
If High(tmp) <> -1 Then Begin
// Einlesen der Zeilennummern aus den Tokens
n := -1;
For i := High(tmp) Downto 1 Do Begin
If tmp[i - 1].value = '~' Then Begin
n := strtoint(tmp[i].value);
inc(rlc, 2);
End;
If n <> -1 Then
tmp[i].Line := n;
End;
tmp[0].line := n; // Beim 1. Token mus das Manuel gemacht werden.
// Übernehmen der Daten in die Globale Tokenliste
Setlength(FFIles[high(FFiles)].tokens, High(tmp) + 1 - rlc);
n := 0;
i := 0;
While i <= High(tmp) Do Begin
If tmp[i].value <> '~' Then Begin
FFIles[high(FFiles)].tokens[n] := Tmp[i];
inc(n);
End
Else Begin
inc(i);
End;
inc(i);
End;
setlength(tmp, 0);
// So nun da alles Geparst ist gehts an Analysieren der Tokens
If High(FFIles[high(FFiles)].tokens) <> -1 Then Begin
setlength(FFIles[high(FFiles)].tokens, high(FFIles[high(FFiles)].tokens) + 2);
FFIles[high(FFiles)].tokens[high(FFIles[high(FFiles)].tokens)].Value := '"End of File"';
FFIles[high(FFiles)].tokens[high(FFIles[high(FFiles)].tokens)].Line := FFIles[high(FFiles)].tokens[high(FFIles[high(FFiles)].tokens) - 1].Line;
Result := FFIles[high(FFiles)].tokens;
End;
End;
End;
End.