-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfstree.pas
286 lines (253 loc) · 6.94 KB
/
fstree.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
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
unit FSTree;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TFileSystemNode = class;
{ TFileSystem }
TFileSystem = class(TComponent)
private
FRoot: TFileSystemNode;
class function GetNodeSeparator: string;
function GetRoot: TFileSystemNode;
public
function FindNodeByFileName(AFileName: string): TFileSystemNode; overload;
function FindNodeByFileName(CurrentNode: TFileSystemNode; AFileName: string):
TFileSystemNode; overload;
property NodeSeparator: string read GetNodeSeparator;
property Root: TFileSystemNode read GetRoot;
end;
{ TFileSystemNode }
TFileSystemNode = class(TComponent)
private
FFirstChild, FNext, FParent: TFileSystemNode;
FSearchRecord: TSearchRec;
function GetFileName: string;
function GetFirstChild: TFileSystemNode;
function GetNext: TFileSystemNode;
function GetNodeName: string;
function GetPrevious: TFileSystemNode;
function GetRoot: TFileSystemNode;
procedure SetPrevious(AValue: TFileSystemNode);
procedure SetRoot(AValue: TFileSystemNode);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
public
constructor Create(AFileSystem: TFileSystem; ANodeName: string); virtual;
constructor Create(AFileSystem: TFileSystem; AParent: TFileSystemNode; ASearchRec: TSearchRec); virtual;
destructor Destroy; override;
function IsDirectory: Boolean;
function IsValid: Boolean;
property FileName: string read GetFileName;
property FirstChild: TFileSystemNode read GetFirstChild;
property Next: TFileSystemNode read GetNext;
property NodeName: string read GetNodeName;
property Parent: TFileSystemNode read FParent;
property Previous: TFileSystemNode read GetPrevious write SetPrevious;
property Root: TFileSystemNode read GetRoot write SetRoot;
end;
implementation
uses Patch, Op;
{ TFileSystem }
function TFileSystem.GetRoot: TFileSystemNode;
begin
if FRoot = nil then begin
FRoot := TFileSystemNode.Create(Self, NodeSeparator);
end;
Result := FRoot;
end;
function TFileSystem.FindNodeByFileName(AFileName: string): TFileSystemNode;
var
Right, NN: string;
begin
NN := Parse(AFileName, NodeSeparator, Right);
if NN = '' then begin
Result := Root;
if Right <> '' then Result := FindNodeByFileName(Root, Right)
end
else Exception.CreateFmt('"%s" is an invalid filename.', [AFileName]);
end;
function TFileSystem.FindNodeByFileName(CurrentNode: TFileSystemNode;
AFileName: string): TFileSystemNode;
var
NN, Right: string;
begin
NN := Parse(AFileName, NodeSeparator, Right);
while Right <> '' do {... Was fehlt hier?};
Result := CurrentNode.FirstChild;
while NN <> '' do
while Result <> nil do
if Result.NodeName = NN then begin
NN := Parse(Right, NodeSeparator, Right);
if NN <> '' then begin
Result := Result.FirstChild;
Break
end;
end
else Result := Result.Next;
end;
{ TFileSystemNode }
function TFileSystemNode.GetFileName: string;
begin
Result := NodeName;
if Parent <> nil then Result := BuildFileName(Parent.FileName, Result)
end;
function TFileSystemNode.GetFirstChild: TFileSystemNode;
var
R: Longint;
SR: TSearchRec;
begin
Result := nil;
if Assigned(FFirstChild) then
if FFirstChild.IsValid then Result := FFirstChild
else begin
FFirstChild.Free;
Result := GetFirstChild
end
else
if IsDirectory then begin
R := FindFirst(BuildFileName(FileName, '*'), faAnyFile, SR);
if R = 0 then begin
FFirstChild := TFileSystemNode.Create(Owner as TFileSystem, Self, SR);
FFirstChild.FreeNotification(Self);
Result := FFirstChild
end
else FindClose(SR)
end
end;
function TFileSystemNode.GetNext: TFileSystemNode;
var
R: Longint;
SR: TSearchRec;
begin
Result := nil;
if Assigned(FNext) then
if FNext.IsValid then Result := FNext
else begin
FNext.Free;
Result := GetNext
end
else begin
SR := FSearchRecord;
R := FindNext(SR);
if R = 0 then begin
FNext := TFileSystemNode.Create(Owner as TFileSystem, Parent, SR);
FNext.FreeNotification(Self);
Result := FNext
end
else begin
FindClose(SR);
end;
end;
end;
function TFileSystemNode.GetNodeName: string;
begin
Result := FSearchRecord.Name;
if Result = '' then Result := '/'
end;
function TFileSystemNode.GetPrevious: TFileSystemNode;
var
x: TFileSystemNode;
begin
Result := nil;
if Self = Root then Exit;
if Parent <> nil then begin
x := Parent.FirstChild;
if x = Self then Exit;
while Assigned(x) do
if x.Next = Self then begin
Result := x;
Break
end
else x := x.Next
end
end;
function TFileSystemNode.GetRoot: TFileSystemNode;
begin
Result := (Owner as TFileSystem).Root
end;
procedure TFileSystemNode.SetPrevious(AValue: TFileSystemNode);
begin
if AValue = Previous then Exit;
if Previous = Parent.FirstChild then begin
Parent.FFirstChild.Free;
Parent.FFirstChild := AValue;
Parent.FFirstChild.FreeNotification(Self)
end
else
with Previous.Previous do begin
FNext.Free;
FNext := AValue;
FNext.FreeNotification(Self);
end;
end;
procedure TFileSystemNode.SetRoot(AValue: TFileSystemNode);
begin
(Owner as TFileSystem).FRoot := AValue
end;
procedure TFileSystemNode.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
case Operation of
opRemove:
if AComponent <> nil then
if AComponent = FFirstChild then begin
FFirstChild.RemoveFreeNotification(Self);
FFirstChild := nil
end
else if AComponent = FNext then begin
FNext.RemoveFreeNotification(Self);
FNext := nil
end
else if AComponent = FParent then begin
FParent.RemoveFreeNotification(Self);
end;
end;
end;
class function TFileSystem.GetNodeSeparator: string;
begin
{$ifdef Windows}
Result := '\'
{$else}
Result := '/'
{$endif}
end;
constructor TFileSystemNode.Create(AFileSystem: TFileSystem; ANodeName: string);
var
SR: TSearchRec;
R: Longint;
begin
inherited Create(AFileSystem);
R := FindFirst(ANodeName, faAnyFile, SR);
if R = 0 then begin
FSearchRecord := SR;
end
else raise Exception.CreateFmt('File "%s" does not exist.', [ANodeName])
end;
constructor TFileSystemNode.Create(AFileSystem: TFileSystem;
AParent: TFileSystemNode; ASearchRec: TSearchRec);
begin
inherited Create(AFileSystem);
FSearchRecord := ASearchRec;
FParent := AParent;
FParent.FreeNotification(Self);
end;
destructor TFileSystemNode.Destroy;
begin
FFirstChild.Free;
FNext.Free;
inherited Destroy;
end;
function TFileSystemNode.IsDirectory: Boolean;
begin
Result := FSearchRecord.Attr and faDirectory <> 0;
end;
function TFileSystemNode.IsValid: Boolean;
begin
if IsDirectory then Result := DirectoryExists(FileName)
else Result := FileExists(FileName)
end;
end.