-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlpeval_wrappers_file.inc
519 lines (412 loc) · 15 KB
/
lpeval_wrappers_file.inc
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
{
Author: Niels A.D
Project: Lape (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
This include constains all the wrappers for file related functions.
}
{%MainUnit lpeval.pas}
procedure _LapeFindAllFiles(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
procedure Find(Directory: lpString; Recursive: Boolean; var Files: TStringArray);
var
List: TStringList;
Info: TSearchRec;
FileName: lpString;
Len, i: Integer;
begin
List := TStringList.Create();
Directory := IncludeTrailingPathDelimiter(ExpandFileName(Directory));
if (FindFirst(Directory + '*', faAnyFile, Info) = 0) then
begin
repeat
if (Info.Name = '.') or (Info.Name = '..') then
Continue;
FileName := Directory + Info.Name;
if (Info.Attr and faDirectory <> 0) then
begin
if Recursive then
Find(IncludeTrailingPathDelimiter(FileName), Recursive, Files);
Continue;
end;
List.Add(FileName);
until (FindNext(Info) <> 0);
SysUtils.FindClose(Info);
end;
Len := Length(Files);
SetLength(Files, Len + List.Count);
for i := 0 to List.Count - 1 do
Files[Len + i] := List[i];
List.Free();
end;
begin
Find(PlpString(Params^[0])^, PBoolean(Params^[1])^, PStringArray(Result)^);
end;
procedure _LapeFindFiles(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
procedure Find(Directory: lpString; Extensions: TStringArray; Recursive: Boolean; var Files: TStringArray);
var
List: TStringList;
Info: TSearchRec;
FileName, FileExtension: lpString;
Len, i: Integer;
begin
List := TStringList.Create();
Directory := IncludeTrailingPathDelimiter(ExpandFileName(Directory));
if (FindFirst(Directory + '*', faAnyFile, Info) = 0) then
begin
repeat
if (Info.Name = '.') or (Info.Name = '..') then
Continue;
FileName := Directory + Info.Name;
FileExtension := ExtractFileExt(Info.Name);
if (Info.Attr and faDirectory <> 0) then
begin
if Recursive then
Find(FileName, Extensions, Recursive, Files);
Continue;
end;
for i := 0 to High(Extensions) do
if (Extensions[i] = '*') or (Extensions[i] = FileExtension) or ('.' + Extensions[i] = FileExtension) then
begin
List.Add(FileName);
Break;
end;
until (FindNext(Info) <> 0);
SysUtils.FindClose(Info);
end;
Len := Length(Files);
SetLength(Files, Len + List.Count);
for i := 0 to List.Count - 1 do
Files[Len + i] := List[i];
List.Free();
end;
begin
Find(PlpString(Params^[0])^, PStringArray(Params^[1])^, PBoolean(Params^[2])^, PStringArray(Result)^);
end;
procedure _LapeFindDirectories(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
procedure Find(Directory: String; Recursive: Boolean; var Directories: TStringArray);
var
List: TStringList;
Info: TSearchRec;
FileName: lpString;
Len, i: Integer;
begin
List := TStringList.Create();
Directory := IncludeTrailingPathDelimiter(ExpandFileName(Directory));
if (FindFirst(Directory + '*', faAnyFile, Info) = 0) then
begin
repeat
if (Info.Name = '.') or (Info.Name = '..') then
Continue;
FileName := Directory + Info.Name;
if (Info.Attr and faDirectory <> 0) then
begin
List.Add(FileName);
if Recursive then
Find(FileName, Recursive, Directories);
end;
until (FindNext(Info) <> 0);
SysUtils.FindClose(Info);
end;
Len := Length(Directories);
SetLength(Directories, Len + List.Count);
for i := 0 to List.Count - 1 do
Directories[Len + i] := List[i];
List.Free();
end;
begin
Find(PlpString(Params^[0])^, PBoolean(Params^[1])^, PStringArray(Result)^);
end;
procedure _LapeAppendFileContents(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function AppendFileContents(const FileName: lpString; const Text: lpString): Boolean;
var
Stream: TFileStream;
begin
Result := False;
if (Length(Text) = 0) then
Exit;
if FileExists(FileName) then
Stream := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite)
else
Stream := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
Stream.Seek(0, soEnd);
Result := Stream.Write(Text[1], Length(Text)) = Length(Text);
finally
Stream.Free();
end;
end;
begin
PBoolean(Result)^ := AppendFileContents(PlpString(Params^[0])^, PlpString(Params^[1])^);
end;
procedure _LapeWriteFileContents(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function WriteFileContents(const FileName: lpString; const Text: lpString): Boolean;
var
Stream: TFileStream;
begin
Result := False;
if (Length(Text) = 0) then
Exit;
if FileExists(FileName) then
Stream := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite)
else
Stream := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
Stream.Size := 0;
Result := Stream.Write(Text[1], Length(Text)) = Length(Text);
finally
Stream.Free();
end;
end;
begin
PBoolean(Result)^ := WriteFileContents(PlpString(Params^[0])^, PlpString(Params^[1])^);
end;
procedure _LapeReadFileContents(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function ReadFileContents(FileName: lpString): lpString;
var
Stream: TFileStream;
begin
SetLength(Result, 0);
if FileExists(FileName) then
begin
Stream := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite);
try
SetLength(Result, Stream.Size);
Stream.Read(Result[1], Length(Result));
finally
Stream.Free();
end;
end;
end;
begin
PlpString(Result)^ := ReadFileContents(PlpString(Params^[0])^);
end;
procedure _LapeForceDirectories(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := ForceDirectories(PlpString(Params^[0])^);
end;
procedure _LapeCreateDirectory(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := CreateDir(PlpString(Params^[0])^);
end;
procedure _LapeDeleteDirectory(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function DeleteDirectory(Directory: lpString; OnlyChildren: Boolean): Boolean;
var
Info: TSearchRec;
FileName: String;
begin
Directory := IncludeTrailingPathDelimiter(ExpandFileName(Directory));
if (FindFirst(Directory + '*', faAnyFile, Info) = 0) then
begin
repeat
if (Info.Name = '.') or (Info.Name = '..') then
Continue;
FileName := Directory + Info.Name;
if (Info.Attr and faDirectory <> 0) then
begin
if (not DeleteDirectory(FileName, False)) then
Exit(False);
Continue;
end;
if (not SysUtils.DeleteFile(FileName)) then
Exit(False);
until (FindNext(Info) <> 0);
SysUtils.FindClose(Info);
end;
Result := OnlyChildren or RemoveDir(Directory);
end;
begin
PBoolean(Result)^ := DeleteDirectory(PlpString(Params^[0])^, PBoolean(Params^[1])^);
end;
procedure _LapeCreateFile(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function CreateFile(const FileName: String): Boolean;
const
INVALID_FILE_HANDLE = THandle(-1);
var
Handle: THandle;
begin
if FileExists(FileName) then
Exit(True);
Handle := FileCreate(FileName);
Result := Handle <> INVALID_FILE_HANDLE;
if Result then
FileClose(Handle);
end;
begin
PBoolean(Result)^ := CreateFile(PlpString(Params^[0])^);
end;
procedure _LapeDeleteFile(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := SysUtils.DeleteFile(PlpString(Params^[0])^);
end;
procedure _LapeRenameFile(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := RenameFile(PlpString(Params^[0])^, PlpString(Params^[1])^);
end;
procedure _LapeCopyFile(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function CopyFile(const SourceFileName, DestFileName: lpString; Overwrite: Boolean = False): Boolean;
var
SourceStream, DestStream: TFileStream;
begin
Result := False;
if (not FileExists(SourceFileName)) or ((not Overwrite) and FileExists(DestFileName)) then
Exit;
SourceStream := TFileStream.Create(SourceFileName, fmOpenReadWrite or fmShareDenyWrite);
try
if FileExists(DestFileName) then
DestStream := TFileStream.Create(DestFileName, fmOpenReadWrite or fmShareDenyWrite)
else
DestStream := TFileStream.Create(DestFileName, fmCreate or fmShareDenyWrite);
try
Result := DestStream.CopyFrom(SourceStream, SourceStream.Size) = SourceStream.Size;
finally
DestStream.Free();
end;
finally
SourceStream.Free();
end;
end;
begin
PBoolean(Result)^ := CopyFile(PlpString(Params^[0])^, PlpString(Params^[1])^, PBoolean(Params^[2])^);
end;
procedure _LapeFileExists(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := FileExists(PlpString(Params^[0])^);
end;
procedure _LapeDirectoryExists(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := DirectoryExists(PlpString(Params^[0])^);
end;
procedure _LapeFileSize(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function FileSize(const FileName: String): Int64;
var
Stream: TFileStream;
begin
Result := -1;
if not FileExists(FileName) then
Exit;
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Result := Stream.Seek(0, soEnd);
finally
Stream.Free();
end;
end;
begin
PInt64(Result)^ := FileSize(PlpString(Params^[0])^);
end;
procedure _LapeFileAge(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PInteger(Result)^ := FileAge(PlpString(Params^[0])^);
end;
procedure _LapeFileAgeDateTime(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PBoolean(Result)^ := FileAge(PlpString(Params^[0])^, PDateTime(Params^[1])^);
end;
procedure _LapeExtractFilePath(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExtractFilePath(PlpString(Params^[0])^);
end;
procedure _LapeExtractFileDrive(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExtractFileDrive(PlpString(Params^[0])^);
end;
procedure _LapeExtractFileName(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExtractFileName(PlpString(Params^[0])^);
end;
procedure _LapeExtractFileExt(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExtractFileExt(PlpString(Params^[0])^);
end;
procedure _LapeExtractFileDir(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExtractFileDir(PlpString(Params^[0])^);
end;
procedure _LapeExpandFileName(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExpandFileName(PlpString(Params^[0])^);
end;
procedure _LapeExtractRelativePath(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExtractRelativePath(PlpString(Params^[0])^, PlpString(Params^[1])^);
end;
procedure _LapeIncludeTrailingPathDelimiter(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := IncludeTrailingPathDelimiter(PlpString(Params^[0])^);
end;
procedure _LapeExcludeTrailingPathDelimiter(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExcludeTrailingPathDelimiter(PlpString(Params^[0])^);
end;
procedure _LapeIncludeTrailingBackslash(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := IncludeTrailingBackslash(PlpString(Params^[0])^);
end;
procedure _LapeExcludeTrailingBackslash(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ExcludeTrailingBackslash(PlpString(Params^[0])^);
end;
procedure _LapeIncludeLeadingPathDelimiter(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
{$IFNDEF FPC}
function IncludeLeadingPathDelimiter(const Path: lpString): lpString;
begin
Result := Path;
If (Length(Result) = 0) or (Result[1] <> DirectorySeparator) then
Result := DirectorySeparator + Result;
end;
{$ENDIF}
begin
PlpString(Result)^ := IncludeLeadingPathDelimiter(PlpString(Params^[0])^);
end;
procedure _LapeExcludeLeadingPathDelimiter(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
{$IFNDEF FPC}
function ExcludeLeadingPathDelimiter(const Path: lpString): lpString;
begin
Result := Path;
If (Length(Result) > 0) and (Result[1] = DirectorySeparator) then
Delete(Result, 1, 1);
end;
{$ENDIF}
begin
PlpString(Result)^ := ExcludeLeadingPathDelimiter(PlpString(Params^[0])^);
end;
procedure _LapeChangeFileExtension(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
begin
PlpString(Result)^ := ChangeFileExt(PlpString(Params^[0])^, PlpString(Params^[1])^);
end;
procedure _LapeSetPathSeperators(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
function SetPathSeparators(const Path: lpString): lpString;
var
i: Integer;
begin
Result := Path;
for i := 1 to Length(Result) do
if CharInSet(Result[I], ['\', '/']) then
Result[I] := DirectorySeparator;
end;
begin
PlpString(Result)^ := SetPathSeparators(PlpString(Params^[0])^);
end;
procedure _LapeJoinPaths(const Params: PParamArray; const Result: Pointer); {$IFDEF Lape_CDECL}cdecl;{$ENDIF}
{$IFNDEF FPC}
function ExcludeLeadingPathDelimiter(const Path: lpString): lpString;
begin
Result := Path;
If (Length(Result) > 0) and (Result[1] = DirectorySeparator) then
Delete(Result, 1, 1);
end;
{$ENDIF}
function JoinPaths(const Paths: TStringArray): lpString;
var
I: Integer;
begin
if Length(Paths) > 0 then
begin
Result := Paths[0];
for I := 1 to Length(Paths) - 1 do
Result := IncludeTrailingPathDelimiter(Result) + ExcludeLeadingPathDelimiter(Paths[I]);
end else
Result := '';
end;
begin
PlpString(Result)^ := JoinPaths(PStringArray(Params^[0])^);
end;