-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.pas
559 lines (500 loc) · 14.2 KB
/
files.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
Files Class for the Mufasa Macro Library
}
unit files;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, MufasaTypes;
const
File_AccesError = -1;
File_EventError = -2;
type
{ TMFiles }
TMFiles = class(TObject)
public
OpenFileEvent : TOpenFileEvent;
WriteFileEvent: TWriteFileEvent;
function CreateFile(Path: string): Integer;
function OpenFile(Path: string; Shared: Boolean): Integer;
function RewriteFile(Path: string; Shared: Boolean): Integer;
function AppendFile(Path: string): Integer;
function DeleteFile(Filename: string): Boolean;
function RenameFile(OldName, NewName: string): Boolean;
procedure CloseFile(FileNum: Integer);
procedure WriteINI(const Section, KeyName, NewString : string; FileName : string);
function ReadINI(const Section, KeyName : string; FileName : string) : string;
procedure DeleteINI(const Section, KeyName : string; FileName : string);
function EndOfFile(FileNum: Integer): Boolean;
function FileSizeMuf(FileNum: Integer): LongInt;
function ReadFileString(FileNum: Integer; out s: string; x: Integer): Boolean;
function WriteFileString(FileNum: Integer;const s: string): Boolean;
function SetFileCharPointer(FileNum, cChars, Origin: Integer): Integer;
function FilePointerPos(FileNum: Integer): Integer;
constructor Create(Owner : TObject);
destructor Destroy; override;
private
MFiles: TMufasaFilesArray;
FreeSpots: Array Of Integer;
Client : TObject;
procedure CheckFileNum(FileNum : integer);
procedure FreeFileList;
function AddFileToManagedList(Path: string; FS: TFileStream; Mode: Integer): Integer;
end;
// We don't need one per object. :-)
function GetFiles(Path, Ext: string): TStringArray;
function GetDirectories(Path: string): TstringArray;
function FindFile(var Filename: string; const Dirs: array of string): boolean;
implementation
uses
{$IFDEF MSWINDOWS}Windows,{$ENDIF} IniFiles,Client,FileUtil;
{ GetFiles in independant of the TMFiles class }
function GetFiles(Path, Ext: string): TstringArray;
var
SearchRec : TSearchRec;
c : integer;
begin
c := 0;
if FindFirst(Path + '*.' + ext, faAnyFile, SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and faDirectory) = faDirectory then
Continue;
inc(c);
SetLength(Result,c);
Result[c-1] := SearchRec.Name;
until FindNext(SearchRec) <> 0;
SysUtils.FindClose(SearchRec);
end;
end;
function GetDirectories(Path: string): TstringArray;
var
SearchRec : TSearchRec;
c : integer;
begin
c := 0;
if FindFirst(Path + '*', faDirectory, SearchRec) = 0 then
begin
repeat
if (SearchRec.Name[1] = '.') or ((SearchRec.Attr and faDirectory) <> faDirectory) then
continue;
inc(c);
SetLength(Result,c);
Result[c-1] := SearchRec.Name;
until FindNext(SearchRec) <> 0;
SysUtils.FindClose(SearchRec);
end;
end;
function FindFile(var Filename: string; const Dirs: array of string): boolean;
var
I, H: LongInt;
begin;
Result := False;
H := High(Dirs);
for I := 0 to H do
if FileExistsUTF8(IncludeTrailingPathDelimiter(Dirs[I]) + Filename) then
begin
Filename := ExpandFileName(IncludeTrailingPathDelimiter(Dirs[I]) + Filename);
Result := True;
Exit;
end;
end;
constructor TMFiles.Create(Owner : TObject);
begin
inherited Create;
self.Client := Owner;
SetLength(Self.MFiles, 0);
SetLength(Self.FreeSpots, 0);
end;
procedure TMFiles.FreeFileList;
var
I : integer;
begin;
For I := 0 To High(MFiles) Do
if MFiles[i].FS <> nil then
begin
TClient(Client).Writeln(Format('File[%s] has not been freed in the script, freeing it now.',[MFiles[i].Path]));
try
MFiles[I].FS.Free;
except
TClient(Client).Writeln('FreeFileList - Exception when freeing FileStream');
end;
end;
SetLength(MFiles, 0);
SetLength(FreeSpots, 0);
end;
destructor TMFiles.Destroy;
begin
FreeFileList;
inherited;
end;
procedure TMFiles.CheckFileNum(FileNum: integer);
begin
if(FileNum < 0) or (FileNum >= Length(MFiles)) then
raise Exception.CreateFmt('Invalid FileNum passed: %d',[FileNum]);
end;
function TMFiles.AddFileToManagedList(Path: String; FS: TFileStream; Mode: Integer): Integer;
var
tFile: TMufasaFile;
begin
tFile.Path := Path;
tFile.FS := FS;
tFile.Mode := Mode;
tFile.BytesRead := 0;
if Length(FreeSpots) > 0 then
begin
MFiles[FreeSpots[High(FreeSpots)]] := tFile;
Result := FreeSpots[High(FreeSpots)];
SetLength(FreeSpots, High(FreeSpots));
end else
begin
SetLength(MFiles, Length(MFiles) + 1);
MFiles[High(MFiles)] := tFile;
Result := High(MFiles);
end;
end;
function TMFiles.SetFileCharPointer(FileNum, cChars, Origin: Integer): Integer;
begin
CheckFileNum(FileNum);
case Origin of
fsFromBeginning:
if(cChars < 0) then
raise Exception.CreateFmt('fsFromBeginning takes no negative cChars. (%d)',[cChars]);
fsFromCurrent:
;
fsFromEnd:
if(cChars > 0) then
raise Exception.CreateFmt('fsFromEnd takes no positive cChars. (%d)',[cChars]);
else
raise Exception.CreateFmt('Invalid Origin: %d',[Origin]);
end;
try
Result := MFiles[FileNum].FS.Seek(cChars, Origin);
except
TClient(Client).Writeln('SetFileCharPointer - Exception Occured.');
Result := File_AccesError;
end;
//Result := FileSeek(Files[FileNum].Handle, cChars, Origin);
end;
{/\
Creates a file for reading/writing.
Returns the handle (index) to the File Array.
Returns File_AccesError if unsuccesfull.
/\}
function TMFiles.CreateFile(Path: string): Integer;
var
FS: TFileStream;
Continue : Boolean;
begin
if Assigned(WriteFileEvent) then
begin;
Continue := true;
WriteFileEvent(Self,path,continue);
if not Continue then
exit(File_EventError);
end;
try
FS := TFileStream.Create(UTF8ToSys(Path), fmCreate);
Result := AddFileToManagedList(Path, FS, fmCreate);
except
Result := File_AccesError;
TClient(Client).Writeln(Format('CreateFile - Exception. Could not create file: %s',[path]));
end;
end;
{/\
Opens a file for reading.
Returns the handle (index) to the File Array.
Returns File_AccesError if unsuccesfull.
/\}
function TMFiles.OpenFile(Path: string; Shared: Boolean): Integer;
var
FS: TFileStream;
fMode: Integer;
Continue : Boolean;
begin
if Assigned(OpenFileEvent) then
begin;
Continue := true;
OpenFileEvent(Self,path,continue);
if not Continue then
exit(File_EventError);
end;
if Shared then
fMode := fmOpenRead or fmShareDenyNone
else
fMode := fmOpenRead or fmShareExclusive;
try
FS := TFileStream.Create(UTF8ToSys(Path), fMode)
except
Result := File_AccesError;
TClient(Client).Writeln(Format('OpenFile - Exception. Could not open file: %s',[path]));
Exit;
end;
Result := AddFileToManagedList(Path, FS, fMode);
end;
function TMFiles.AppendFile(Path: string): Integer;
var
FS: TFileStream;
fMode: Integer;
Continue : Boolean;
begin
if Assigned(WriteFileEvent) then
begin
Continue := true;
WriteFileEvent(Self,path,continue);
if not Continue then
exit(File_EventError);
end;
fMode := fmOpenReadWrite;
if not FileExists(Path) then
fMode := fMode or fmCreate;
try
FS := TFileStream.Create(UTF8ToSys(Path), fMode);
FS.Seek(0, fsFromEnd);
Result := AddFileToManagedList(Path, FS, fMode);
except
Result := File_AccesError;
TClient(Client).Writeln(Format('AppendFile - Exception. Could not create file: %s',[path]));
end;
end;
{/\
Reads key from INI file
/\}
function TMFiles.ReadINI(const Section, KeyName: string; FileName: string): string;
var
Continue : boolean;
begin
FileName := ExpandFileNameUTF8(FileName);
if Assigned(OpenFileEvent) then
begin
Continue := True;
OpenFileEvent(Self, FileName, Continue);
if not Continue then
exit('');
end;
with TINIFile.Create(FileName, True) do
try
Result := ReadString(Section, KeyName, '');
finally
Free;
end;
end;
{/\
Deletes a key from INI file
/\}
procedure TMFiles.DeleteINI(const Section, KeyName : string; FileName : string);
var
Continue : boolean;
begin;
FileName := ExpandFileNameUTF8(FileName);
if Assigned(WriteFileEvent) then
begin
Continue := True;
WriteFileEvent(Self, FileName, Continue);
if not Continue then
exit;
end;
with TIniFile.Create(FileName, True) do
try
if KeyName = '' then
EraseSection(Section)
else
DeleteKey(Section, KeyName);
finally
Free;
end;
end;
{/\
Writes a key to INI file
/\}
procedure TMFiles.WriteINI(const Section, KeyName, NewString : string; FileName : string);
var
Continue : boolean;
begin;
FileName := ExpandFileNameUTF8(FileName);
if Assigned(WriteFileEvent) then
begin
Continue := True;
WriteFileEvent(Self, FileName, Continue);
if not Continue then
exit;
end;
with TINIFile.Create(FileName, True) do
try
WriteString(Section, KeyName, NewString);
finally
Free;
end;
end;
{/\
Opens a file for writing. And deletes the contents.
Returns the handle (index) to the File Array.
Returns File_AccesError if unsuccesfull.
/\}
function TMFiles.RewriteFile(Path: string; Shared: Boolean): Integer;
var
FS: TFileStream;
fMode: Integer;
Continue : Boolean;
begin
if Assigned(WriteFileEvent) then
begin;
Continue := true;
WriteFileEvent(Self,path,continue);
if not Continue then
exit(File_EventError);
end;
if Shared then
fMode := fmOpenReadWrite or fmShareDenyNone or fmCreate
else
fMode := fmOpenReadWrite or fmShareDenyWrite or fmShareDenyRead or fmCreate;
try
FS := TFileStream.Create(UTF8ToSys(Path), fMode);
FS.Size:=0;
Result := AddFileToManagedList(Path, FS, fMode);
except
Result := File_AccesError;
TClient(Client).Writeln(Format('ReWriteFile - Exception. Could not create file: %s',[path]));
end;
end;
function TMFiles.DeleteFile(Filename: string): Boolean;
var
Continue : Boolean;
begin
if Assigned(WriteFileEvent) then
begin;
Continue := true;
WriteFileEvent(Self, Filename, continue);
if not Continue then
exit(False);
end;
Result := DeleteFileUTF8(Filename);
end;
function TMFiles.RenameFile(OldName, NewName: string): Boolean;
var
Continue : Boolean;
begin
if Assigned(WriteFileEvent) then
begin;
Continue := true;
WriteFileEvent(Self, OldName, continue);
if not Continue then
exit(False);
WriteFileEvent(Self, NewName, continue);
if not Continue then
exit(False);
end;
Result := RenameFileUTF8(OldName, NewName);
end;
{/\
Free's the given File at the given index.
/\}
procedure TMFiles.CloseFile(FileNum: Integer);
begin
CheckFileNum(filenum);
try
MFiles[FileNum].FS.Free;
MFiles[FileNum].FS := nil;
SetLength(FreeSpots, Length(FreeSpots) + 1);
FreeSpots[High(FreeSpots)] := FileNum;
except
TClient(Client).Writeln(Format('CloseFile, exception when freeing the file: %d',[filenum]));
end;
end;
{/\
Returns true if the BytesRead of the given FileNum (Index) has been reached.
Also returns true if the FileNum is not valid.
/\}
function TMFiles.EndOfFile(FileNum: Integer): Boolean;
begin
CheckFileNum(filenum);
if MFiles[FileNum].FS = nil then
begin
TClient(Client).Writeln(format('EndOfFile: Invalid Internal Handle of File: %d',[filenum]));
Result := True;
Exit;
end;
Result := FilePointerPos(FileNum) >= FileSizeMuf(FileNum);
end;
{/\
Returns the FileSize of the given index (FileNum)
/\}
function TMFiles.FileSizeMuf(FileNum: Integer): LongInt;
begin
CheckFileNum(filenum);
if MFiles[FileNum].FS = nil then
begin
TClient(Client).Writeln(format('FileSize: Invalid Internal Handle of File: %d',[filenum]));
Result := File_AccesError;
Exit;
end;
Result := MFiles[FileNum].FS.Size;
end;
function TMFiles.FilePointerPos(FileNum: Integer): Integer;
begin
CheckFileNum(filenum);
if MFiles[FileNum].FS = nil then
begin
TClient(Client).Writeln(format('FilePointerPos: Invalid Internal Handle of File: %d',[filenum]));
Result := File_AccesError;
Exit;
end;
try
Result := MFiles[FileNum].FS.Seek(0, fsFromCurrent);
except
TClient(Client).Writeln('Exception in FilePointerPos');
end;
end;
{/\
Reads x numbers of characters from a file, and stores it into s.
/\}
function TMFiles.ReadFileString(FileNum: Integer; out s: string; x: Integer): Boolean;
begin
CheckFileNum(filenum);
if MFiles[FileNum].FS = nil then
begin
TClient(Client).Writeln(format('ReadFileString: Invalid Internal Handle of File: %d',[filenum]));
Exit;
end;
SetLength(S, X);
Result := MFiles[FileNum].FS.Read(S[1], x) = x;
{Files[FileNum].BytesRead := Files[FileNum].BytesRead + X;
FileRead(Files[FileNum].Handle, S[1], X);
SetLength(S, X); }
end;
{/\
Writes s in the given File.
/\}
function TMFiles.WriteFileString(FileNum: Integer;const s: string): Boolean;
begin
result := false;
CheckFileNum(filenum);
if(MFiles[FileNum].FS = nil) then
begin
TClient(Client).Writeln(format('WriteFileString: Invalid Internal Handle of File: %d',[filenum]));
Exit;
end;
if (MFiles[FileNum].Mode and (fmOpenWrite or fmOpenReadWrite)) = 0 then //Checks if we have write rights..
exit;
try
Result := MFiles[FileNum].FS.Write(S[1], Length(S)) <> 1;
except
TClient(Client).Writeln('Exception - WriteFileString.');
Result := False;
end;
end;
end.