-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpLuaUtils.pas
588 lines (488 loc) · 13.3 KB
/
pLuaUtils.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
unit pLuaUtils;
{$mode objfpc}{$H+}
{$I pLua.inc}
interface
uses {$IFDEF LINUX}
BaseUnix
{$ENDIF}
{$IFDEF WINDOWS}
Windows
{$ENDIF}
, Classes, SysUtils,
Lua, pLua;
//fs namespace support
procedure plua_fs_register(L:Plua_State);
//dbg namespace support
procedure plua_dbg_register(L:Plua_State);
implementation
uses Forms, gstack, masks, pLuaObject;
const
Package_fs = 'fs';
Package_dbg = 'dbg';
Package_system = '__system';
const
AllMask = {$IFDEF WINDOWS}'*.*'{$ELSE}'*'{$ENDIF};
type
TFindFilesIteratorParams = record
Dir, WildCard:string;
Recursive:boolean
end;
{ TFindFilesIterator }
TFindFilesIteratorState = (stInit, stFindDirectoriesLoop, stFindDirectoriesNext, stFindFiles, stFindFilesLoop, stFindFilesNext, stComplete);
TFindFilesIteratorStackFrame = record
SR:TSearchRec;
Dir:string;
State:TFindFilesIteratorState;
end;
PFindFilesIteratorStackFrame = ^TFindFilesIteratorStackFrame;
TFindFilesIteratorStack = specialize TStack<PFindFilesIteratorStackFrame>;
TFindFilesIterator = class
private
FParams:TFindFilesIteratorParams;
FStack:TFindFilesIteratorStack;
procedure Clear;
function ProcessDir: string;
procedure PushFrame(const Dir:string);
procedure PopFrame(Force: boolean=False);
public
constructor Create(const Dir, WildCard:string; Recursive:boolean);
destructor Destroy;override;
function FetchNext:string;
end;
procedure FindFilesToList(const Dir, WildCard:string; Recursive:boolean; L:TStrings);
var Iter:TFindFilesIterator;
f:string;
begin
L.Clear;
Iter:=TFindFilesIterator.Create(Dir, WildCard, Recursive);
try
while true do
begin
f:=Iter.FetchNext;
if f = '' then break;
L.Add(f);
end;
finally
Iter.Free;
end;
end;
procedure FindFilesArgs(l : Plua_State; paramcount: Integer;
out Dir:string; out Recursive:boolean; out Mask:string );
begin
if not (paramcount in [2,3]) then
pLua_RaiseException(l, 'Dir, Recursive, [Mask] are expected.');
if paramcount < 3 then
begin
Mask:=AllMask;
end
else
begin
Mask:=plua_tostring(l, -1);
lua_pop(l, 1);
end;
Recursive:=lua_toboolean(l, -1);
lua_pop(l, 1);
Dir:=plua_tostring(l, -1);
lua_pop(l, 1);
end;
function plua_findfiles(l : PLua_State; paramcount: Integer) : integer;
var S:TStringList;
Recursive:boolean;
Dir, Mask:string;
begin
Result:=0;
FindFilesArgs(l, paramcount, Dir, Recursive, Mask);
S:=TStringList.Create;
try
FindFilesToList(Dir, Mask, Recursive, S);
plua_pushstrings(l, S);
Result:=1;
finally
S.Free;
end;
end;
function plua_findfiles_iterator(l : PLua_State) : integer; extdecl;
var paramcount:Integer;
user_obj:^TObject;
f:String;
begin
paramcount:=lua_gettop(l);
if paramcount <> 2 then
pLua_RaiseException(l, 'Invalid findfiles_iterator params');
//second param is not used actually
lua_pop(l, 1);
if not lua_isuserdata(l, -1) then
pLua_RaiseException(l, 'Invalid findfiles_iterator param1');
user_obj:=lua_touserdata(l, -1);
f:=TFindFilesIterator(user_obj^).FetchNext;
if f = '' then
lua_pushnil(l)
else
plua_pushstring(l, f);
Result:=1;
end;
function plua_iterfiles(l : PLua_State; paramcount: Integer) : integer;
var Iter:TFindFilesIterator;
f:string;
Recursive:boolean;
Dir, Mask:string;
begin
Result:=0;
FindFilesArgs(l, paramcount, Dir, Recursive, Mask);
Iter:=TFindFilesIterator.Create(Dir, Mask, Recursive);
f:=Iter.FetchNext;
lua_pushcfunction(l, @plua_findfiles_iterator);
plua_PushObjectAsUserData(l, Iter);
if f = '' then
lua_pushnil(l)
else
plua_pushstring(l, f);
Result:=3;
end;
{$IFDEF WINDOWS}
function FileTime2DateTime(FileTime: TFileTime): TDateTime;
var
LocalFileTime: TFileTime;
SystemTime: TSystemTime;
begin
FileTimeToLocalFileTime(FileTime, LocalFileTime) ;
FileTimeToSystemTime(LocalFileTime, SystemTime) ;
Result := SystemTimeToDateTime(SystemTime) ;
end;
{$ENDIF}
{
a recent FPC (at least 2.7.1) is required to have FileAge working properly under Windows
if not - uncomment following function.
function FileAge(const FileName: string; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
Var
Info : TSearchRec;
A : Integer;
begin
for A:=1 to Length(FileName) do
If (FileName[A] in ['?','*']) then
Exit(False);
A:=faAnyFile; // <- fix for Windows is here
if Not FollowLink then
A:=A or faSymLink;
Result:=FindFirst(FileName,A,Info)=0;
If Result then
FileDateTime:=FileDatetoDateTime (Info.Time);
FindClose(Info);
end;
}
function plua_file_time(l : PLua_State; paramcount: Integer) : integer;
//returns creation and modification time for a file
var Filename:string;
MTime, CTime:TDateTime;
{$IFDEF LINUX}
fstat:stat;
{$ENDIF}
{$IFDEF WINDOWS}
fl:TSearchRec;
{$ENDIF}
begin
Result:=0;
if paramcount <> 1 then
pLua_RaiseException(l, 'Filename is expected.');
Filename:=plua_tostring(l, -1);
lua_pop(l, 1);
//modification time
if not FileAge(Filename, MTime, True) then
pLua_RaiseException(l, 'Error getting file modification time.');
lua_pushnumber(l, MTime);
//creation time
{$IFDEF LINUX}
if FpStat(Filename, {%H-}fstat)<>0 then
pLua_RaiseException(l, 'Error getting file creation time.');
CTime:=FileDatetoDateTime(fstat.st_ctime);
{$ENDIF}
{$IFDEF WINDOWS}
if FindFirst(FileName, faAnyFile, fl) <> 0 then
begin
FindClose(fl);
pLua_RaiseException(l, 'Error getting file creation time.');
end;
CTime:=FileTime2DateTime(fl.FindData.ftCreationTime);
FindClose(fl);
{$ENDIF}
lua_pushnumber(l, CTime);
Result:=2;
end;
function plua_file_size(l : PLua_State; paramcount: Integer) : integer;
var S:int64;
Filename:string;
{$IFDEF LINUX}
fstat:stat;
{$ENDIF}
{$IFDEF WINDOWS}
fl:TSearchRec;
{$ENDIF}
begin
Result:=0;
if paramcount <> 1 then
pLua_RaiseException(l, 'Filename is expected.');
Filename:=plua_tostring(l, -1);
lua_pop(l, 1);
{$IFDEF LINUX}
if FpStat(Filename, {%H-}fstat)<>0 then
pLua_RaiseException(l, 'Error getting file size.');
S:=fstat.st_size;
{$ENDIF}
{$IFDEF WINDOWS}
if FindFirst(FileName, faAnyFile, fl) <> 0 then
begin
FindClose(fl);
pLua_RaiseException(l, 'Error getting file size.');
end;
S:=((int64(fl.FindData.nFileSizeHigh)) shl 32) + fl.FindData.nFileSizeLow;
FindClose(fl);
{$ENDIF}
lua_pushnumber(l, S);
Result:=1;
end;
function plua_file_exists(l : PLua_State; paramcount: Integer) : integer;
var S:int64;
Filename:string;
{$IFDEF LINUX}
fstat:stat;
{$ENDIF}
{$IFDEF WINDOWS}
fl:TSearchRec;
{$ENDIF}
begin
Result:=0;
if paramcount <> 1 then
pLua_RaiseException(l, 'Filename is expected.');
Filename:=plua_tostring(l, -1);
lua_pop(l, 1);
lua_pushboolean(l, FileExists(FileName));
Result:=1;
end;
function plua_file_slurp(l : PLua_State; paramcount: Integer) : integer;
var Filename, S:string;
Size:int64;
F:TFileStream;
begin
Result:=0;
if paramcount <> 1 then
pLua_RaiseException(l, 'Filename is expected.');
Filename:=plua_tostring(l, -1);
lua_pop(l, 1);
if not FileExists(Filename) then
pLua_RaiseException(l, 'File %s does not exist.', [FileName]);
// allow reading files in several threads...
F:=TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Size:=F.Size;
SetLength(S, Size);
F.ReadBuffer((@S[1])^, Size);
finally
FreeAndNil(F);
end;
lua_pushlstring(l, PChar(S), Size);
Result:=1;
end;
function plua_dir_create(l : PLua_State; paramcount: Integer) : integer;
var Dir:string;
begin
Result:=0;
if paramcount <> 1 then
pLua_RaiseException(l, 'Directory name is expected.');
Dir:=plua_tostring(l, -1);
lua_pop(l, 1);
lua_pushboolean(l, ForceDirectories(Dir) );
Result:=1;
end;
function ExtractFilenameOnly(const Filename:string):string;
var F, Ext:string;
begin
F:=ExtractFileName(Filename);
Ext:=ExtractFileExt(F);
Result := Copy(F, 1, Length(F) - Length(Ext))
end;
function plua_parse_filename(l : PLua_State; paramcount: Integer) : integer;
var Filename:string;
begin
Result:=0;
if paramcount <> 1 then
pLua_RaiseException(l, 'File name is expected.');
Filename:=plua_tostring(l, -1);
lua_pop(l, 1);
plua_pushstring(l, ExtractFilePath(Filename));
plua_pushstring(l, ExtractFileName(Filename));
plua_pushstring(l, ExtractFilenameOnly(Filename));
plua_pushstring(l, ExtractFileExt(Filename));
Result:=4;
end;
function plua_match_mask(l : PLua_State; paramcount: Integer) : integer;
var Filename, mask:string;
case_sens:boolean;
begin
Result:=0;
if paramcount <> 3 then
pLua_RaiseException(l, 'File name, mask and is_case_sensitive are expected.');
case_sens:=lua_toboolean(l, -1);
lua_pop(l, 1);
mask:=plua_tostring(l, -1);
lua_pop(l, 1);
Filename:=plua_tostring(l, -1);
lua_pop(l, 1);
lua_pushboolean(l, MatchesMask(Filename, mask, case_sens));
Result:=1;
end;
function plua_process_messages(l : PLua_State; {%H-}paramcount: Integer) : integer;
begin
Application.ProcessMessages;
plua_EnsureStackBalance(l, 0);
Result:=0;
end;
procedure plua_fs_register(L: Plua_State);
begin
plua_RegisterMethod(l, Package_fs, 'findfiles', @plua_findfiles);
plua_RegisterMethod(l, Package_fs, 'iterfiles', @plua_iterfiles);
plua_RegisterMethod(l, Package_fs, 'filetime', @plua_file_time);
plua_RegisterMethod(l, Package_fs, 'filesize', @plua_file_size);
plua_RegisterMethod(l, Package_fs, 'fileexists', @plua_file_exists);
plua_RegisterMethod(l, Package_fs, 'fileslurp', @plua_file_slurp);
plua_RegisterMethod(l, Package_fs, 'dircreate', @plua_dir_create);
plua_RegisterMethod(l, Package_fs, 'parsefilename', @plua_parse_filename);
plua_RegisterMethod(l, Package_fs, 'matchmask', @plua_match_mask);
end;
procedure plua_dbg_register(L: Plua_State);
begin
plua_RegisterMethod(l, Package_dbg, 'ProcessMessages', @plua_process_messages);
plua_RegisterMethod(l, Package_system, 'ProcessMessages', @plua_process_messages);
end;
{ TFindFilesIterator }
procedure TFindFilesIterator.Clear;
begin
while not FStack.IsEmpty do
begin
PopFrame;
end;
end;
constructor TFindFilesIterator.Create(const Dir, WildCard: string;
Recursive: boolean);
begin
FStack:=TFindFilesIteratorStack.Create;
FParams.Dir:=Dir;
FParams.WildCard:=WildCard;
FParams.Recursive:=Recursive;
PushFrame(Dir);
end;
destructor TFindFilesIterator.Destroy;
begin
Clear;
FStack.Free;
inherited Destroy;
end;
procedure TFindFilesIterator.PushFrame(const Dir: string);
var F:PFindFilesIteratorStackFrame;
begin
New(F);
F^.Dir:=Dir;
F^.State:=stInit;
FillByte(F^.SR, SizeOf(F^.SR), 0);
FStack.Push(F);
end;
procedure TFindFilesIterator.PopFrame(Force:boolean=False);
var Frame:PFindFilesIteratorStackFrame;
begin
Frame:=FStack.Top();
if Force or not (Frame^.State in [stInit, stComplete]) then
FindClose(Frame^.SR);
Dispose(Frame);
FStack.Pop();
end;
function TFindFilesIterator.ProcessDir:string;
label
NextIter;
var Found:Integer;
Frame:PFindFilesIteratorStackFrame;
begin
NextIter:
Result:='';
Frame:=FStack.Top();
while true do
case Frame^.State of
stInit:
begin
//first pass - directories
Frame^.State:=stFindFiles;
if FParams.Recursive then
begin
Found:=FindFirst(Frame^.Dir + DirectorySeparator + AllMask, faDirectory, Frame^.SR);
if Found = 0 then
Frame^.State:=stFindDirectoriesLoop;
end;
end;
stFindDirectoriesLoop:
begin
Frame^.State:=stFindDirectoriesNext;
with Frame^.SR do
begin
if (Name <> '.') and (Name <> '..') then
if (Attr and faDirectory) > 0 then
begin
PushFrame(Frame^.Dir + DirectorySeparator + Name);
goto NextIter;
end;
end;
end;
stFindDirectoriesNext:
begin
Found:=FindNext(Frame^.SR);
if Found = 0 then
Frame^.State:=stFindDirectoriesLoop
else
begin
FindClose(Frame^.SR);
Frame^.State:=stFindFiles;
end;
end;
stFindFiles:
begin
//second pass - files
Found:=FindFirst(Frame^.Dir + DirectorySeparator + FParams.WildCard, faAnyFile, Frame^.SR);
if Found = 0 then
Frame^.State:=stFindFilesLoop
else
Frame^.State:=stComplete;
end;
stFindFilesLoop:
begin
Frame^.State:=stFindFilesNext;
with Frame^.SR do
begin
if (Name <> '.') and (Name <> '..') then
if (Attr and faDirectory) = 0 then
begin
Result:=Frame^.Dir + DirectorySeparator + Name;
break;
end;
end;
end;
stFindFilesNext:
begin
Found:=FindNext(Frame^.SR);
if Found = 0 then
Frame^.State:=stFindFilesLoop
else
Frame^.State:=stComplete;
end;
stComplete:
begin
PopFrame(True);
if FStack.IsEmpty() then
break;
goto NextIter;
end;
end;
end;
function TFindFilesIterator.FetchNext: string;
begin
Result:='';
if not FStack.IsEmpty() then
Result:=ProcessDir;
end;
end.