lazutils: added MergeCmdLineParams

git-svn-id: trunk@42244 -
This commit is contained in:
mattias 2013-07-30 15:37:48 +00:00
parent 63f070abd3
commit 45db0258ac
2 changed files with 134 additions and 44 deletions

View File

@ -120,6 +120,7 @@ function GetDarwinSystemFilename(Filename: string): string;
procedure SplitCmdLineParams(const Params: string; ParamList: TStrings;
ReadBackslash: boolean = false);
function StrToCmdLineParam(const Param: string): string;
function MergeCmdLineParams(ParamList: TStrings): string;
type
@ -1078,59 +1079,115 @@ begin
end;
end;
function StrToCmdLineParam(const Param: string): string;
{ <empty> -> ''
word -> word
word1 word2 -> 'word1 word2'
word's -> "word's"
a" -> 'a"'
"a" -> '"a"'
'a' -> "'a'"
#0 character -> cut the rest
}
const
NoQuot = ' ';
AnyQuot = '*';
var
Quot: Char;
p: PChar;
i: Integer;
begin
Result:=Param;
if Result='' then
Result:=''''''
else begin
p:=PChar(Result);
Quot:=NoQuot;
repeat
case p^ of
#0:
begin
i:=p-PChar(Result);
if i<length(Result) then
Delete(Result,i+1,length(Result));
case Quot of
AnyQuot: Result:=''''+Result+'''';
'''': Result+='''';
'"': Result+='"';
end;
break;
end;
' ',#9,#10,#13:
begin
if Quot=NoQuot then
Quot:=AnyQuot;
inc(p);
end;
'''':
begin
case Quot of
NoQuot,AnyQuot:
begin
// need "
Quot:='"';
i:=p-PChar(Result);
System.Insert('"',Result,1);
p:=PChar(Result)+i+1;
end;
'"':
inc(p);
'''':
begin
// ' within a '
// => end ', start "
i:=p-PChar(Result)+1;
System.Insert('''"',Result,i);
p:=PChar(Result)+i+1;
Quot:='"';
end;
end;
end;
'"':
begin
case Quot of
NoQuot,AnyQuot:
begin
// need '
Quot:='''';
i:=p-PChar(Result);
System.Insert('''',Result,1);
p:=PChar(Result)+i+1;
end;
'''':
inc(p);
'"':
begin
// " within a "
// => end ", start '
i:=p-PChar(Result)+1;
System.Insert('"''',Result,i);
p:=PChar(Result)+i+1;
Quot:='''';
end;
end;
end;
else
inc(p);
end;
until false;
end;
end;
function MergeCmdLineParams(ParamList: TStrings): string;
var
i: Integer;
HasSpace: Boolean;
HasApos: Boolean;
HasQuot: Boolean;
Param: String;
j: Integer;
begin
Result:='';
if ParamList=nil then exit;
for i:=0 to ParamList.Count-1 do
begin
Param:=ParamList[i];
if Param='' then
Param:='""'
else begin
end;
HasSpace:=false;
HasApos:=false;
HasQuot:=false;
for j:=1 to length(Param) do begin
case Param[i] of
' ',#9,#10,#13: HasSpace:=true;
'''': HasApos:=true;
'"': HasQuot:=true;
end;
end;
if HasQuot then begin
if HasApos then begin
end else begin
// quot, no apos
end;
end else begin
// no quot
if HasApos then begin
// apos, no quot
end else begin
if HasSpace or (Param='') then begin
// no quot, no apos, has space -> quote
Param:=''''+Param+'''';
end else begin
// simple parameter => pass unaltered
end;
end;
end;
if i>0 then Result+=' ';
Result+=Param;
Result+=StrToCmdLineParam(ParamList[i]);
end;
end;

View File

@ -6,6 +6,7 @@
./runtests --format=plain --suite=TestReplaceSubstring
./runtests --format=plain --suite=TestSplitCmdLineParams
./runtests --format=plain --suite=TestExpandFilename
./runtests --format=plain --suite=TestMergeCmdLineParams
}
unit TestLazUtils;
@ -26,6 +27,7 @@ type
procedure TestReplaceSubstring;
procedure TestSplitCmdLineParams;
procedure TestExpandFilename;
procedure TestMergeCmdLineParams;
end;
implementation
@ -109,6 +111,37 @@ begin
AssertEquals('basedir','/ide',ExpandFileNameUTF8('/ide','/opt'));
end;
procedure TTestLazUtils.TestMergeCmdLineParams;
procedure t(Title, Param, Expected: string);
var
l: TStringList;
Actual: String;
begin
l:=TStringList.Create;
try
l.Add(Param);
Actual:=MergeCmdLineParams(l);
AssertEquals(Title,'['+Expected+']','['+Actual+']');
finally
l.Free;
end;
end;
begin
t('empty','','''''');
t('word','a','a');
t('space',' ',''' ''');
t('two words','a b','''a b''');
t('single quot','"','''"''');
t('two quots','"a"','''"a"''');
t('single apos','''','"''"');
t('two apos','''a''','"''a''"');
t('quot apos','"''','''"''"''"');
t('wordquot','a"','''a"''');
t('null#0char','null'#0'char','null');
end;
initialization
AddToLazUtilsTestSuite(TTestLazUtils);