From bf54543518ab45c76305bd4c0772e373041c4b2d Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 22 Jun 2019 13:37:47 +0000 Subject: [PATCH] * Add SplitCommandLine git-svn-id: trunk@42269 - --- packages/rtl-objpas/src/inc/strutils.pp | 153 ++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/packages/rtl-objpas/src/inc/strutils.pp b/packages/rtl-objpas/src/inc/strutils.pp index b26387e7bb..cc959ba05f 100644 --- a/packages/rtl-objpas/src/inc/strutils.pp +++ b/packages/rtl-objpas/src/inc/strutils.pp @@ -266,6 +266,15 @@ Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceF Function StringReplace(const S, OldPattern, NewPattern: unicodestring; Flags: TReplaceFlags): unicodestring; overload; Function StringReplace(const S, OldPattern, NewPattern: widestring; Flags: TReplaceFlags): widestring; overload; + +Type + TRawByteStringArray = Array of RawByteString; + TUnicodeStringArray = Array of UnicodeString; + +Function SplitCommandLine(S : RawByteString) : TRawByteStringArray; +Function SplitCommandLine(S : UnicodeString) : TUnicodeStringArray; + + implementation (* @@ -3271,4 +3280,148 @@ begin end; +Function SplitCommandLine(S : RawByteString) : TRawByteStringArray; + + Function GetNextWord : RawByteString; + + Const + WhiteSpace = [' ',#9,#10,#13]; + Literals = ['"','''']; + + Var + Wstart,wend : Integer; + InLiteral : Boolean; + LastLiteral : AnsiChar; + + Procedure AppendToResult; + + begin + Result:=Result+Copy(S,WStart,WEnd-WStart); + WStart:=Wend+1; + end; + + begin + Result:=''; + WStart:=1; + While (WStart<=Length(S)) and charinset(S[WStart],WhiteSpace) do + Inc(WStart); + WEnd:=WStart; + InLiteral:=False; + LastLiteral:=#0; + While (Wend<=Length(S)) and (Not charinset(S[Wend],WhiteSpace) or InLiteral) do + begin + if charinset(S[Wend],Literals) then + If InLiteral then + begin + InLiteral:=Not (S[Wend]=LastLiteral); + if not InLiteral then + AppendToResult; + end + else + begin + InLiteral:=True; + LastLiteral:=S[Wend]; + AppendToResult; + end; + inc(wend); + end; + AppendToResult; + While (WEnd<=Length(S)) and (S[Wend] in WhiteSpace) do + inc(Wend); + Delete(S,1,WEnd-1); + end; + +Var + W : RawByteString; + len : Integer; + +begin + Len:=0; + Result:=Default(TRawByteStringArray); + SetLength(Result,(Length(S) div 2)+1); + While Length(S)>0 do + begin + W:=GetNextWord; + If (W<>'') then + begin + Result[Len]:=W; + Inc(Len); + end; + end; + SetLength(Result,Len); +end; + + +Function SplitCommandLine(S : UnicodeString) : TUnicodeStringArray; + + Function GetNextWord : UnicodeString; + + Const + WhiteSpace = [' ',#9,#10,#13]; + Literals = ['"','''']; + + Var + Wstart,wend : Integer; + InLiteral : Boolean; + LastLiteral : AnsiChar; + + Procedure AppendToResult; + + begin + Result:=Result+Copy(S,WStart,WEnd-WStart); + WStart:=Wend+1; + end; + + begin + Result:=''; + WStart:=1; + While (WStart<=Length(S)) and charinset(S[WStart],WhiteSpace) do + Inc(WStart); + WEnd:=WStart; + InLiteral:=False; + LastLiteral:=#0; + While (Wend<=Length(S)) and (Not charinset(S[Wend],WhiteSpace) or InLiteral) do + begin + if charinset(S[Wend],Literals) then + If InLiteral then + begin + InLiteral:=Not (S[Wend]=LastLiteral); + if not InLiteral then + AppendToResult; + end + else + begin + InLiteral:=True; + LastLiteral:=S[Wend]; + AppendToResult; + end; + inc(wend); + end; + AppendToResult; + While (WEnd<=Length(S)) and (S[Wend] in WhiteSpace) do + inc(Wend); + Delete(S,1,WEnd-1); + end; + +Var + W : UnicodeString; + len : Integer; + +begin + Len:=0; + Result:=Default(TUnicodeStringArray); + SetLength(Result,(Length(S) div 2)+1); + While Length(S)>0 do + begin + W:=GetNextWord; + If (W<>'') then + begin + Result[Len]:=W; + Inc(Len); + end; + end; + SetLength(Result,Len); +end; + + end.