Skip to content

Commit b699f9a

Browse files
committed
Refactoring
Fix for #3
1 parent 120c7bc commit b699f9a

File tree

5 files changed

+330
-214
lines changed

5 files changed

+330
-214
lines changed

scriptLib.pas

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ interface
3232
defaultAlias: THashedStringList;
3333
staticVars: THashedStringList; // these scripting variables are held for the whole run-time
3434

35-
function tryApplyMacrosAndSymbols(fs: TFileServer; var txt: String; var md: TmacroData; removeQuotings: Boolean=true): Boolean;
36-
function runScript(fs: TFileServer; const script:string; table:TstringDynArray=NIL; tpl_:Ttpl=NIL; f:Tfile=NIL; folder:Tfile=NIL; cd:TconnDataMain=NIL): String;
35+
function tryApplyMacrosAndSymbols(fs: TFileServer; var txt: UnicodeString; var md: TmacroData; removeQuotings: Boolean=true): Boolean;
36+
function runScript(fs: TFileServer; const script: UnicodeString; table:TstringDynArray=NIL; tpl_:Ttpl=NIL; f:Tfile=NIL; folder:Tfile=NIL; cd:TconnDataMain=NIL): UnicodeString;
3737
function runEventScript(fs: TFileServer; const event: String; table: TStringDynArray=NIL; cd: TconnDataMain=NIL): String;
3838
procedure resetLog();
3939
procedure runTimedEvents(fs: TFileServer);
@@ -115,23 +115,27 @@ function encodeMarkers(s:string):string;
115115
result:=s;
116116
end; // encodeMarkers
117117

118-
function noMacrosAllowed(s:string):string;
118+
function noMacrosAllowed(s: UnicodeString): UnicodeString;
119119
// prevent hack attempts
120120
var
121121
i: integer;
122122
begin
123+
if s = '' then
124+
Exit('');
123125
i:=1;
124126
enforceNUL(s);
125127
repeat
126-
i:=findMacroMarker(s, i);
127-
if i = 0 then break;
128-
replace(s, '&#'+intToStr(charToUnicode(s[i]))+';', i,i);
128+
i:=findMacroMarker(s, i);
129+
if i = 0 then
130+
break;
131+
replace(s, '&#'+intToStr(charToUnicode(s[i]))+';', i,i);
129132
until false;
130-
s:=reReplace(s,'%([-a-z0-9]+%)','%$1', 'mi');
131-
result:=s;
133+
// s := reReplace(s,'%([-a-z0-9]+%)','%$1', 'mi');
134+
// result:=s;
135+
Result := ReplaceStr(s, '%','%');
132136
end; // noMacrosAllowed
133137

134-
function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData: Pointer): String;
138+
function cbMacros(fs: TFileServer; const fullMacro: UnicodeString; pars: TPars; cbData: Pointer): UnicodeString;
135139
var
136140
md: ^TmacroData;
137141
name, p: string;
@@ -306,6 +310,20 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
306310
result := pars.ContainsKey(name);
307311
end; // parExist
308312

313+
function parExistVal(const name: String; var val: String; doTrim: boolean=TRUE): boolean;
314+
begin
315+
Result := false;
316+
val := '';
317+
if name > '' then
318+
begin
319+
Result := pars.TryGetValue(name, val);
320+
if Result then
321+
if doTrim then
322+
val := trim(val)
323+
end;
324+
end; // parExistVal
325+
326+
309327
procedure trueIf(condition:boolean);
310328
begin if condition then result:='1' else result:='' end;
311329

@@ -340,15 +358,15 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
340358
begin result:=getVarSpace(varname).values[varname] end;
341359

342360
// if par with name exists, then it's a var name, otherwise it's a constant value at specified index
343-
function parVar(parname:string; idx:integer):string; overload;
361+
function parVar(const parname: String; idx: Integer): String; overload;
344362
begin
345363
if parExist(parname) then
346364
result:=getVar(par(parname))
347365
else
348366
result:=pars[idx];
349367
end; // parVar
350368

351-
function setVar(varname, value:string; space:THashedStringList=NIL):boolean;
369+
function setVar(varname: String; const value: String; space: THashedStringList=NIL): Boolean;
352370
var
353371
o: Tobject;
354372
i: integer;
@@ -612,7 +630,7 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
612630
result:=macroQuote(result);
613631
end; // section
614632

615-
function urlVar(k:string):string;
633+
function urlVar(const k: String): String;
616634
var
617635
s: string;
618636
begin
@@ -629,10 +647,12 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
629647
except end;
630648
end; // urlVar
631649

632-
function maybeUrlvar(k:string):string;
650+
function maybeUrlvar(const k: String): String;
633651
begin
634-
if (k = '') or (k[1] <> '?') then result:=k
635-
else result:=urlvar(copy(k,2,MAXINT));
652+
if (k = '') or (k[1] <> '?') then
653+
result := k
654+
else
655+
result := urlvar(copy(k,2,MAXINT));
636656
end; // maybeUrlvar
637657

638658
function compare(op,p1,p2:string):boolean;
@@ -669,7 +689,7 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
669689
end;
670690
end; // infixOperators
671691

672-
procedure call(code:string; ofs:integer=0);
692+
procedure call(const code: String; ofs: Integer=0);
673693
var
674694
i: integer;
675695
begin
@@ -823,9 +843,10 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
823843

824844
if p = 'virtual' then
825845
begin
826-
name:=par(1);
827-
if not validateAndExtractParent() then exit;
828-
f:=Tfile.createVirtualFolder(fs.rootFile.mainTree, name);
846+
name:=par(1);
847+
if not validateAndExtractParent() then
848+
exit;
849+
f := Tfile.createVirtualFolder(fs.rootFile.mainTree, name);
829850
end
830851
else
831852
begin
@@ -882,11 +903,13 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
882903
uniqueStrings(result);
883904
end;
884905

885-
procedure setAttr(a:TfileAttribute; parName:string);
906+
procedure setAttr(a: TfileAttribute; const parName: String);
907+
var
908+
v: String;
886909
begin
887-
if parExist(parname) then
910+
if parExistVal(parname, v) then
888911
try
889-
if isTrue(parEx(parname)) then
912+
if isTrue(v) then
890913
include(f.flags, a)
891914
else
892915
exclude(f.flags, a);
@@ -903,7 +926,7 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
903926
f.setDynamicComment(LP, macroDequote(parEx('comment')))
904927
except end;
905928
try
906-
f.name:=parEx('name');
929+
f.name := parEx('name');
907930
if assigned(f.node) then
908931
f.node.text:=f.name;
909932
except end;
@@ -1292,10 +1315,9 @@ function cbMacros(fs: TFileServer; const fullMacro: String; pars: TPars; cbData:
12921315
i: integer;
12931316
v: string;
12941317
begin
1295-
if parExist('var') then
1318+
if parExistVal('var', v) then
12961319
try
1297-
v := parEx('var');
1298-
result:=getVar(v);
1320+
result := getVar(v);
12991321
except
13001322
result := pars[pars.count-1]
13011323
end
@@ -1349,8 +1371,8 @@ t_s2c = record s: String; val: byte; end;
13491371
if not icon and buttons then
13501372
inc(code, MB_ICONQUESTION);
13511373
case msgDlg(p, code, par(2)) of
1352-
MRYES, MROK: result:=if_(buttons, '1'); // if only OK button is available, then return nothing
1353-
MRCANCEL: result:=if_(code and MB_YESNOCANCEL = MB_YESNOCANCEL, 'cancel'); // for the YESNOCANCEL, we return cancel to allow to tell NO from CANCEL
1374+
MRYES, MROK: result := if_(buttons, '1'); // if only OK button is available, then return nothing
1375+
MRCANCEL: result := if_(code and MB_YESNOCANCEL = MB_YESNOCANCEL, 'cancel'); // for the YESNOCANCEL, we return cancel to allow to tell NO from CANCEL
13541376
else result:='';
13551377
end;
13561378
end; // dialog
@@ -1658,8 +1680,9 @@ t_s2c = record s: String; val: byte; end;
16581680
end; // getPairs
16591681

16601682
begin
1661-
if not satisfied(md.cd) then exit;
1662-
result:='';
1683+
if not satisfied(md.cd) then
1684+
exit;
1685+
result:='';
16631686
if parExist('value') then
16641687
try md.cd.conn.setCookie(p, parEx('value'), getPairs());
16651688
except result:=noMacrosAllowed(md.cd.conn.getCookie(p)) end // there was no "value" to set, so just read
@@ -1699,11 +1722,12 @@ t_s2c = record s: String; val: byte; end;
16991722
setVar(parEx('sub'), s);
17001723
except end;
17011724

1702-
try
1703-
result:=reReplace(subj, p, parEx('replace'), mods);
1704-
setVar(parEx('var'), result); // we put the output where we got the input
1705-
result:='';
1706-
except end;
1725+
try
1726+
result:=reReplace(subj, p, parEx('replace'), mods);
1727+
setVar(parEx('var'), result); // we put the output where we got the input
1728+
result:='';
1729+
except
1730+
end;
17071731
end; // regexp
17081732

17091733
procedure dir();
@@ -1926,7 +1950,7 @@ t_s2c = record s: String; val: byte; end;
19261950
end; // handleSymbol
19271951

19281952

1929-
function stringTotrayMessageType(s:string): TBalloonIconType;
1953+
function stringTotrayMessageType(const s: String): TBalloonIconType;
19301954
begin
19311955
if compareText(s,'warning') = 0 then
19321956
result:= bitWarning
@@ -1938,18 +1962,18 @@ t_s2c = record s: String; val: byte; end;
19381962
result:= bitNone
19391963
end; // stringTotrayMessageType
19401964

1941-
function renameIt(src,dst:string):boolean;
1965+
function renameIt(const src, dst: String): Boolean;
19421966
var
19431967
srcReal, dstReal: string;
19441968
begin
1945-
srcReal:=uri2diskMaybe(src,NIL,FALSE);
1946-
dstReal:=uri2diskMaybeFolder(dst);
1947-
if isExtension(srcReal, '.lnk')
1948-
and not isExtension(src, '.lnk') then
1949-
dstReal:=dstReal+'.lnk';
1950-
if extractFilePath(dstReal)='' then
1951-
dstReal:=extractFilePath(srcReal)+dstReal;
1952-
result:=renameFile(srcReal, dstReal)
1969+
srcReal:=uri2diskMaybe(src,NIL,FALSE);
1970+
dstReal:=uri2diskMaybeFolder(dst);
1971+
if isExtension(srcReal, '.lnk')
1972+
and not isExtension(src, '.lnk') then
1973+
dstReal:=dstReal+'.lnk';
1974+
if extractFilePath(dstReal)='' then
1975+
dstReal:=extractFilePath(srcReal)+dstReal;
1976+
result := renameFile(srcReal, dstReal)
19531977
end; // renameIt
19541978

19551979
var

srv/IconsLib.pas

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ TIconsDM = class(TDataModule)
4949
{$ELSE ~FMX}
5050
systemimages: Timagelist; // system icons
5151
{$ENDIF FMX}
52+
function GetBitmap(idx: Integer; Size: Integer): TBitmap;
5253
function getImageIndexForFile(fn: string): integer;
5354
end;
5455

@@ -73,6 +74,7 @@ TIconsDM = class(TDataModule)
7374
function str2pic(const s: RawByteString; imgSize: Integer): integer;
7475
function strGif2pic(const gs: RawByteString; imgSize: Integer): integer;
7576
function ico2str(hndl: THandle; icoNdx: Integer; imgSize: Integer): RawByteString;
77+
function ico2bmp(hndl: THandle; icoNdx: Integer; imgSize: Integer): TBitmap;
7678

7779
function stringPNG2BMP(const s: RawByteString): TBitmap;
7880

@@ -130,9 +132,11 @@ function getSystemimages(): TImageList;
130132
result := TImageList.Create(NIL);
131133
{$ELSE ~FMX}
132134
result := Timagelist.Create(NIL);
133-
Result.ColorDepth := cd32Bit;
134135
result.ShareImages := TRUE;
136+
{$IFNDEF FPC}
137+
Result.ColorDepth := cd32Bit;
135138
result.handle := hs;
139+
{$ENDIF FPC}
136140
{$ENDIF FMX}
137141
end; // loadSystemimages
138142

@@ -332,8 +336,8 @@ function gif2png(const s: RawByteString): RawByteString;
332336
begin
333337
Result := '';
334338
ss := TRawByteStringStream.create(s);
339+
bmp := TBitmap.Create;
335340
try
336-
bmp := TBitmap.Create;
337341
{$IFDEF FMX}
338342
bmp.loadFromStream(ss);
339343
{$ELSE FMX}
@@ -344,6 +348,7 @@ function gif2png(const s: RawByteString): RawByteString;
344348
Result := bmp2str(bmp);
345349
finally
346350
ss.free;
351+
bmp.Free;
347352
{$IFNDEF FMX}
348353
gif.Free;
349354
{$ENDIF ~FMX}
@@ -376,7 +381,7 @@ function bmp2str(bmp: Tbitmap): RawByteString;
376381
PRGBAArray = ^TRGBAArray;
377382
{$IFNDEF FMX}
378383
var
379-
png: TPNGImage;
384+
png: TPNGImage;
380385
RowInOut: PRGBAArray;
381386
RowAlpha: PByteArray;
382387
{$ENDIF FMX}
@@ -387,6 +392,9 @@ function bmp2str(bmp: Tbitmap): RawByteString;
387392
png := TPNGImage.Create();
388393
try
389394
// png.ColorReduction:=rmQuantize;
395+
{$IFDEF FPC}
396+
png.LoadFromBitmapHandles(bmp.Handle, bmp.MaskHandle);
397+
{$ELSE ~FPC}
390398
png.Assign(bmp);
391399
{$IFDEF FMX}
392400
if bmp.PixelFormat in [TPixelFormat.RGBA, TPixelFormat.BGRA, TPixelFormat.RGBA16] then
@@ -403,6 +411,7 @@ function bmp2str(bmp: Tbitmap): RawByteString;
403411
RowAlpha[X] := RowInOut[X].rgbReserved;
404412
end;
405413
end;
414+
{$ENDIF ~FPC}
406415
result := png2str(png);
407416
finally png.free;
408417
end;
@@ -433,11 +442,7 @@ function pic2str(idx: integer; imgSize: Integer): RawByteString;
433442

434443
bmp := nil;
435444
try
436-
{$IFDEF FMX}
437-
bmp := IconsDM.Images.Bitmap(TSizeF.Create(imgSize, imgSize), idx);
438-
{$ELSE FMX}
439-
bmp := IconsDM.ImgCollection.GetBitmap(idx, imgSize, imgSize);
440-
{$ENDIF FMX}
445+
bmp := IconsDM.GetBitmap(idx, imgSize);
441446

442447
if Assigned(bmp) then
443448
begin
@@ -471,6 +476,22 @@ function ico2str(hndl: THandle; icoNdx: Integer; imgSize: Integer): RawByteStrin
471476
end;
472477
end;
473478

479+
function ico2bmp(hndl: THandle; icoNdx: Integer; imgSize: Integer): TBitmap;
480+
//var
481+
//bmp: TBitmap;
482+
begin
483+
Result := TBitmap.Create;
484+
try
485+
Result.PixelFormat := pf32bit;
486+
Result.SetSize(imgSize, imgSize);
487+
{$IFDEF FPC} Result.BeginUpdate(True); {$ENDIF FPC}
488+
ImageList_DrawEx(hndl, icoNdx, Result.Canvas.Handle, 0, 0, imgSize, ImgSize, CLR_NONE, CLR_NONE, ILD_SCALE or ILD_PRESERVEALPHA);
489+
{$IFDEF FPC} Result.EndUpdate; {$ENDIF FPC}
490+
finally
491+
//bmp.Free;
492+
end;
493+
end;
494+
474495
function str2pic(const s: RawByteString; imgSize: Integer): Integer;
475496
var
476497
{$IFDEF FMX}
@@ -559,6 +580,25 @@ function stringPNG2BMP(const s: RawByteString): TBitmap;
559580
{$ENDIF FMX}
560581
end;
561582

583+
function TIconsDM.GetBitmap(idx: Integer; Size: Integer): TBitmap;
584+
begin
585+
{$IFDEF FMX}
586+
result := Images.Bitmap(TSizeF.Create(Size, Size), idx);
587+
{$ELSE FMX}
588+
{$IFDEF FPC}
589+
Result := NIL;
590+
if Self.images.Count > idx then
591+
begin
592+
Result := TBitmap.Create;
593+
Result.SetSize(Size, Size);
594+
Self.images.GetBitmap(idx, Result);
595+
end;
596+
{$ELSE ~FPC}
597+
Result:= imgCollection.GetBitmap(idx, size, size);
598+
{$ENDIF FPC}
599+
{$ENDIF FMX}
600+
end;
601+
562602
function TIconsDM.getImageIndexForFile(fn: String): Integer;
563603
var
564604
newIdx, n: integer;
@@ -588,7 +628,8 @@ function TIconsDM.getImageIndexForFile(fn: String): Integer;
588628
{$IFNDEF FMX}
589629

590630
// have we already met this sysidx before?
591-
for var i:=0 to length(sysidx2index)-1 do
631+
if length(sysidx2index) > 0 then
632+
for var i:=0 to length(sysidx2index)-1 do
592633
if sysidx2index[i].sysidx = shfi.iIcon then
593634
begin
594635
result := sysidx2index[i].idx;

0 commit comments

Comments
 (0)