From 5bcaa75c069a6776fc122cdda549ee9a0ee10831 Mon Sep 17 00:00:00 2001 From: mattias Date: Thu, 3 Jan 2013 12:10:48 +0000 Subject: [PATCH] lazutils: moved SplitCmdLineParms to LazFileUtils git-svn-id: trunk@39741 - --- components/lazutils/fileutil.inc | 88 --------------------------- components/lazutils/fileutil.pas | 2 - components/lazutils/lazfileutils.pas | 91 ++++++++++++++++++++++++++++ test/lazutils/testlazutils.pas | 2 +- 4 files changed, 92 insertions(+), 91 deletions(-) diff --git a/components/lazutils/fileutil.inc b/components/lazutils/fileutil.inc index c7f3f042f0..5096ca09f6 100644 --- a/components/lazutils/fileutil.inc +++ b/components/lazutils/fileutil.inc @@ -173,94 +173,6 @@ begin Result:=SysToUTF8(SysUtils.SysErrorMessage(ErrorCode)); end; -procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; - ReadBackslash: boolean = false); -// split spaces, quotes are parsed as single parameter -// if ReadBackslash=true then \" is replaced to " and not treated as quote -// #0 is always end -type - TMode = (mNormal,mApostrophe,mQuote); -var - p: Integer; - Mode: TMode; - Param: String; -begin - p:=1; - while p<=length(Params) do - begin - // skip whitespace - while (p<=length(Params)) and (Params[p] in [' ',#9,#10,#13]) do inc(p); - if (p>length(Params)) or (Params[p]=#0) then - break; - //writeln('SplitCmdLineParams After Space p=',p,'=[',Params[p],']'); - // read param - Param:=''; - Mode:=mNormal; - while p<=length(Params) do - begin - case Params[p] of - #0: - break; - '\': - begin - inc(p); - if ReadBackslash then - begin - // treat next character as normal character - if (p>length(Params)) or (Params[p]=#0) then - break; - if ord(Params[p])<128 then - begin - Param+=Params[p]; - inc(p); - end else begin - // next character is already a normal character - end; - end else begin - // treat backslash as normal character - Param+='\'; - end; - end; - '''': - begin - inc(p); - case Mode of - mNormal: - Mode:=mApostrophe; - mApostrophe: - Mode:=mNormal; - mQuote: - Param+=''''; - end; - end; - '"': - begin - inc(p); - case Mode of - mNormal: - Mode:=mQuote; - mApostrophe: - Param+='"'; - mQuote: - Mode:=mNormal; - end; - end; - ' ',#9,#10,#13: - begin - if Mode=mNormal then break; - Param+=Params[p]; - inc(p); - end; - else - Param+=Params[p]; - inc(p); - end; - end; - //writeln('SplitCmdLineParams Param=#'+Param+'#'); - ParamList.Add(Param); - end; -end; - function DirPathExists(const FileName: String): Boolean; var F: Longint; diff --git a/components/lazutils/fileutil.pas b/components/lazutils/fileutil.pas index 5605a24537..e15a1881d9 100644 --- a/components/lazutils/fileutil.pas +++ b/components/lazutils/fileutil.pas @@ -239,8 +239,6 @@ function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; // other function SysErrorMessageUTF8(ErrorCode: Integer): String; -procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; - ReadBackslash: boolean = false); implementation diff --git a/components/lazutils/lazfileutils.pas b/components/lazutils/lazfileutils.pas index cbca9d2dfa..ec8cbab58e 100644 --- a/components/lazutils/lazfileutils.pas +++ b/components/lazutils/lazfileutils.pas @@ -87,6 +87,9 @@ function ForceDirectoriesUTF8(const Dir: string): Boolean; function IsUNCPath(const {%H-}Path: String): Boolean; function ExtractUNCVolume(const {%H-}Path: String): String; +procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; + ReadBackslash: boolean = false); + type TInvalidateFileStateCacheEvent = procedure(const Filename: string); var @@ -1295,6 +1298,94 @@ begin OnInvalidateFileStateCache(Filename); end; +procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; + ReadBackslash: boolean = false); +// split spaces, quotes are parsed as single parameter +// if ReadBackslash=true then \" is replaced to " and not treated as quote +// #0 is always end +type + TMode = (mNormal,mApostrophe,mQuote); +var + p: Integer; + Mode: TMode; + Param: String; +begin + p:=1; + while p<=length(Params) do + begin + // skip whitespace + while (p<=length(Params)) and (Params[p] in [' ',#9,#10,#13]) do inc(p); + if (p>length(Params)) or (Params[p]=#0) then + break; + //writeln('SplitCmdLineParams After Space p=',p,'=[',Params[p],']'); + // read param + Param:=''; + Mode:=mNormal; + while p<=length(Params) do + begin + case Params[p] of + #0: + break; + '\': + begin + inc(p); + if ReadBackslash then + begin + // treat next character as normal character + if (p>length(Params)) or (Params[p]=#0) then + break; + if ord(Params[p])<128 then + begin + Param+=Params[p]; + inc(p); + end else begin + // next character is already a normal character + end; + end else begin + // treat backslash as normal character + Param+='\'; + end; + end; + '''': + begin + inc(p); + case Mode of + mNormal: + Mode:=mApostrophe; + mApostrophe: + Mode:=mNormal; + mQuote: + Param+=''''; + end; + end; + '"': + begin + inc(p); + case Mode of + mNormal: + Mode:=mQuote; + mApostrophe: + Param+='"'; + mQuote: + Mode:=mNormal; + end; + end; + ' ',#9,#10,#13: + begin + if Mode=mNormal then break; + Param+=Params[p]; + inc(p); + end; + else + Param+=Params[p]; + inc(p); + end; + end; + //writeln('SplitCmdLineParams Param=#'+Param+'#'); + ParamList.Add(Param); + end; +end; + function IsUNCPath(const Path: String): Boolean; begin {$IFDEF Windows} diff --git a/test/lazutils/testlazutils.pas b/test/lazutils/testlazutils.pas index 9c19d69aeb..aee63be003 100644 --- a/test/lazutils/testlazutils.pas +++ b/test/lazutils/testlazutils.pas @@ -13,7 +13,7 @@ unit TestLazUtils; interface uses - Classes, SysUtils, fpcunit, testglobals, LazLogger, FileUtil; + Classes, SysUtils, fpcunit, testglobals, LazLogger, LazFileUtils; type