Skip to content

Commit

Permalink
fix: unicode problems (file comments)
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed May 9, 2020
1 parent 99af92c commit edd0fad
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
8 changes: 0 additions & 8 deletions classesLib.pas
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,19 @@ Ttpl = class
last: record section:string; idx:integer; end; // cache for getIdx()
fileExts: TStringDynArray;
strTable: THashedStringList;
fUTF8: boolean;
fOver: Ttpl;
function getIdx(section:string):integer;
function getTxt(section:string):string;
function newSection(section:string):PtplSection;
procedure fromString(txt:string);
procedure setOver(v:Ttpl);
procedure updateUTF8();
public
onChange: TNotifyEvent;
sections: array of TtplSection;
constructor create(txt:string=''; over:Ttpl=NIL);
destructor Destroy; override;
property txt[section:string]:string read getTxt; default;
property fullText:string read src write fromString;
property utf8:boolean read fUTF8;
property over:Ttpl read fOver write setOver;
function sectionExist(section:string):boolean;
function getTxtByExt(fileExt:string):string;
Expand Down Expand Up @@ -1109,20 +1106,15 @@ procedure Ttpl.appendString(txt:string);
ptxt:=succ(ansiStrPos(bos, #10)); // get to the end of line (and then beyond)
first:=FALSE;
until ptxt = NIL;
updateUTF8();
if assigned(onChange) then
onChange(self);
end; // appendString

procedure Ttpl.setOver(v:Ttpl);
begin
fOver:=v;
updateUTF8();
end; // setOver

procedure Ttpl.updateUTF8();
begin fUTF8:=assigned(over) and over.utf8 or utf8test(fullText) end;

function Ttpl.getSections():TStringDynArray;
var
i: integer;
Expand Down
22 changes: 11 additions & 11 deletions default.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ z-index:1; /* without this .item-menu will be over*/ }
"speed":"%smart-speed%"
},
{.if| {.length|%user%.} |{:
{.set item|{.force ansi|%folder%%item-name%.}|comment={.!uploaded by.} %user%.}
{.set item|%folder%%item-name%|comment={.!uploaded by.} %user%.}
:}.}
[upload-failed]
Expand Down Expand Up @@ -534,7 +534,7 @@ z-index:1; /* without this .item-menu will be over*/ }
{.set|x|{.postvar|name.}.}
{.break|if={.pos|\|var=x.}{.pos|/|var=x.}|result=forbidden.}
{.break|if={.not|{.can mkdir.}.}|result=not authorized.}
{.set|x|{.force ansi|%folder%{.^x.}.}.}
{.set|x|%folder%{.^x.}.}
{.break|if={.exists|{.^x.}.}|result=exists.}
{.break|if={.not|{.length|{.mkdir|{.^x.}.}.}.}|result=failed.}
{.add to log|{.!User.} %user% {.!created folder.} "{.^x.}".}
Expand All @@ -545,9 +545,9 @@ z-index:1; /* without this .item-menu will be over*/ }
{.break|if={.not|{.can rename.}.}|result=forbidden.}
{.break|if={.is file protected|{.postvar|from.}.}|result=forbidden.}
{.break|if={.is file protected|{.postvar|to.}.}|result=forbidden.}
{.set|x|{.force ansi|%folder%{.postvar|from.}.}.}
{.set|yn|{.force ansi|{.postvar|to.}.}.}
{.set|y|{.force ansi|%folder%.}{.^yn.}.}
{.set|x|%folder%{.postvar|from.}.}
{.set|yn|{.postvar|to.}.}
{.set|y|%folder%{.^yn.}.}
{.break|if={.not|{.exists|{.^x.}.}.}|result=not found.}
{.break|if={.exists|{.^y.}.}|result=exists.}
{.set|comment| {.get item|{.^x.}|comment.} .}
Expand All @@ -561,12 +561,12 @@ z-index:1; /* without this .item-menu will be over*/ }
[ajax.move|no log]
{.check session.}
{.set|dst|{.force ansi|{.postvar|dst.}.}.}
{.set|dst|{.postvar|dst.}.}
{.break|if={.not|{.and|{.can move.}|{.get|can delete.}|{.get|can upload|path={.^dst.}.}/and.}.} |result=forbidden.}
{.set|log|{.!Moving items to.} {.^dst.}.}
{.for each|fn|{.replace|:|{.no pipe||.}|{.force ansi|{.postvar|files.}.}.}|{:
{.for each|fn|{.replace|:|{.no pipe||.}|{.postvar|files.}.}|{:
{.break|if={.is file protected|var=fn.}|result=forbidden.}
{.set|x|{.force ansi|%folder%.}{.^fn.}.}
{.set|x|%folder%{.^fn.}.}
{.set|y|{.^dst.}/{.^fn.}.}
{.if not |{.exists|{.^x.}.}|{.^x.}: {.!not found.}|{:
{.if|{.exists|{.^y.}.}|{.^y.}: {.!already exists.}|{:
Expand All @@ -578,7 +578,7 @@ z-index:1; /* without this .item-menu will be over*/ }
{.set item|{.^y.}|comment={.^comment.}.}
:} | {:
{.set|log|{.chr|13.}{.^fn.} (failed)|mode=append.}
{.maybe utf8|{.^fn.}.}: {.!not moved.}
{.^fn.}: {.!not moved.}
:}/if.}
:}/if.}
:}.}
Expand All @@ -591,14 +591,14 @@ z-index:1; /* without this .item-menu will be over*/ }
{.break|if={.not|{.can comment.}.} |result=forbidden.}
{.for each|fn|{.replace|:|{.no pipe||.}|{.postvar|files.}.}|{:
{.break|if={.is file protected|var=fn.}|result=forbidden.}
{.set item|{.force ansi|%folder%{.^fn.}.}|comment={.force ansi|{.postvar|text.}.}.}
{.set item|%folder%{.^fn.}|comment={.postvar|text.}.}
:}.}
{.pipe|ok.}
[ajax.changepwd|no log]
{.check session.}
{.break|if={.not|{.can change pwd.}.} |result=forbidden.}
{.if|{.length|{.set account||password={.force ansi|{.postvar|new.}.}.}/length.}|ok|failed.}
{.if|{.length|{.set account||password={.postvar|new.}.}/length.}|ok|failed.}
[special:alias]
check session=if|{.{.cookie|HFS_SID_.} != {.postvar|token.}.}|{:{.cookie|HFS_SID_|value=|expires=-1.} {.break|result=bad session.}:}
Expand Down
2 changes: 1 addition & 1 deletion main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ function loadDescriptionFile(fn:string):string;
begin
result:=loadTextFile(fn);
if result = '' then
result:=loadTextFile(fn+'\descript.ion');
result:=loadTextFile(fn+'\'+DESCRIPT_ION);
if (result > '') and mainfrm.oemForIonChk.checked then
OEMToCharBuff(@result[1], @result[1], length(result)); //to test
end; // loadDescriptionFile
Expand Down
14 changes: 4 additions & 10 deletions utillib.pas
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ function getTill(ss, s:string; included:boolean=FALSE):string; overload;
function getTill(i:integer; s:string):string; overload;
function singleLine(s:string):boolean;
function poss(chars:TcharSet; s:string; ofs:integer=1):integer;
function optAnsi(bool:boolean; s:string):string;
function utf8Test(s:string):boolean;
function jsEncode(s, chars:string):string;
function nonEmptyConcat(pre,s:string; post:string=''):string;
function first(a,b:integer):integer; overload;
Expand Down Expand Up @@ -933,7 +931,9 @@ function validFilepath(fn:string; acceptUnits:boolean=TRUE):boolean;
function loadTextFile(fn:string):string;
begin
try result:=IOUtils.TFile.ReadAllText(fn)
except result:='' end
except result:='' end;
try result:=UTF8decode(result)
except end;
end;

function loadFile(fn:string; from:int64=0; size:int64=-1):ansistring;
Expand Down Expand Up @@ -2043,9 +2043,6 @@ function getTempDir():string;
setLength(result, getTempPath(length(result), @result[1]));
end; // getTempDir

function optAnsi(bool:boolean; s:string):string;
begin if bool then result:=UTF8toAnsi(s) else result:=s end;

function blend(from,to_:Tcolor; perc:real):Tcolor;
var
i: integer;
Expand Down Expand Up @@ -2083,9 +2080,6 @@ function jsEncode(s, chars:string):string;
function holdingKey(key:integer):boolean;
begin result:=getAsyncKeyState(key) and $8000 <> 0 end;

function utf8Test(s:string):boolean;
begin result:=ansiContainsText(s, 'charset=UTF-8') end;

// concat pre+s+post only if s is non empty
function nonEmptyConcat(pre, s:string; post:string=''):string;
begin if s = '' then result:='' else result:=pre+s+post end;
Expand Down Expand Up @@ -2414,7 +2408,7 @@ procedure urlToStrings(s:string; sl:Tstrings);
while i <= l do
begin
p:=posEx('&',s,i);
t:=decodeURL(replaceStr(substr(s,i,if_(p=0,0,p-1)), '+',' '), FALSE); // TODO should we instead try to decode utf-8? doing so may affect calls to {.force ansi.} in the template
t:=decodeURL(replaceStr(substr(s,i,if_(p=0,0,p-1)), '+',' ')); // TODO should we instead try to decode utf-8? doing so may affect calls to {.force ansi.} in the template
sl.add(t);
if p = 0 then exit;
i:=p+1;
Expand Down

0 comments on commit edd0fad

Please sign in to comment.