Browse Source

add parmstr functions

KngStr 3 years ago
parent
commit
a4780f641c
1 changed files with 156 additions and 1 deletions
  1. 156 1
      Source/ksString.pas

+ 156 - 1
Source/ksString.pas

@@ -5,7 +5,7 @@
 {       CopyRight (C) 2018-2020 KngStr                  }
 {                                                       }
 {   Some Code from                                      }
-{     QDAC of swish                                     }
+{     QDAC of swish, Inno                               }
 {   Thanks                                              }
 {                                                       }
 {*******************************************************}
@@ -70,9 +70,51 @@ function SizeUnitToInt(AUnit: Char): Int64; overload;
 /// <param name="AOnlyNum">仅允许数字和开头的+</param>
 function IsChineseMobileNumber(S: string; AOnlyNum: Boolean = False): Boolean;
 
+{
+  文本参数解析,来自Inno
+  Differences from Delphi's ParamStr:
+  - No limits on parameter length
+  - Doesn't ignore empty parameters ("")
+  - Handles the empty argv[0] case like MSVC: if GetCommandLine() returns
+    " a b" then NewParamStr(1) should return "a", not "b"
+}
+
+/// <summary>
+/// 获取指定参数, one-base
+/// </summary>
+/// <remarks>
+/// 按命令行拆分方式
+/// </remarks>
+function StrParamStr(Command: string; Index: Integer): string; overload;
+function StrParamStr(Command: PChar; Index: Integer): string; overload;
+/// <summary>
+/// 获取参数个数
+/// </summary>
+/// <remarks>
+/// 按命令行拆分方式
+/// </remarks>
+function StrParamCount(Command: string): Integer; overload;
+function StrParamCount(Command: PChar): Integer; overload;
+/// <summary>
+/// 获取指定命令行参数的值,/x=xxx 和 -x=xxx
+/// </summary>
+/// <remarks>
+/// 按命令行拆分方式
+/// </remarks>
+function GetStrParamVal(Command: string; const Param, Default: String): String; overload;
+function GetStrParamVal(Command: PChar; const Param, Default: String): String; overload;
+/// <summary>
+/// 获取下一个参数,并返回剩余内容
+/// </summary>
+/// <remarks>
+/// 按命令行拆分方式
+/// </remarks>
+function GetStrParamStr(P: PChar; var Param: string): PChar;
+
 implementation
 
 uses
+  {$IFDEF MSWINDOWS}Windows,{$ENDIF}
   Classes, SysUtils, IOUtils;
 
 function CreateBlankFile(S: string): Boolean;
@@ -319,4 +361,117 @@ begin
     Result := StrToUInt64Def(s, Result) * SizeUnitToInt(c);
 end;
 
+function GetStrParamStr(P: PChar; var Param: String): PChar;
+
+  function Extract(P: PChar; const Buffer: PChar; var Len: Integer): PChar;
+  var
+    InQuote: Boolean;
+  begin
+    Len := 0;
+    InQuote := False;
+    while (P^ <> #0) and ((P^ > ' ') or InQuote) do begin
+      if P^ = '"' then
+        InQuote := not InQuote
+      else begin
+        if Assigned(Buffer) then
+          Buffer[Len] := P^;
+        Inc(Len);
+      end;
+      Inc(P);
+    end;
+    Result := P;
+  end;
+
+var
+  Len: Integer;
+  Buffer: String;
+begin
+  Extract(P, nil, Len);
+  SetString(Buffer, nil, Len);
+  Result := Extract(P, @Buffer[1], Len);
+  Param := Buffer;
+  while (Result^ <> #0) and (Result^ <= ' ') do
+    Inc(Result);
+end;
+
+function GetStrParamVal(Command: PChar; const Param, Default: String): String;
+var
+  I, PCount: Integer;
+  Z: String;
+begin
+  PCount := StrParamCount(Command);
+  for I := 1 to PCount do
+  begin
+    Z := StrParamStr(Command, I);
+    if (StrLIComp(PChar(Z), PChar('/' + Param + '='), Length(Param) + 2) = 0)
+      or (StrLIComp(PChar(Z), PChar('-' + Param + '='), Length(Param) + 2) = 0) then
+    begin
+      Delete(Z, 1, Length(Param) + 2);
+      Result := Z;
+      Exit;
+    end;
+  end;
+
+  Result := Default;
+end;
+
+function GetStrParamVal(Command: string; const Param, Default: String): String;
+begin
+  Result := GetStrParamVal(PChar(Command), Param, Default);
+end;
+
+function StrParamCount(Command: PChar): Integer;
+var
+  P: PChar;
+  S: String;
+begin
+  P := GetStrParamStr(Command, S);
+  Result := 0;
+  while P^ <> #0 do begin
+    Inc(Result);
+    P := GetStrParamStr(P, S);
+  end;
+end;
+
+function StrParamCount(Command: string): Integer;
+begin
+  Result := StrParamCount(PChar(Command));
+end;
+
+function StrParamStr(Command: PChar; Index: Integer): string;
+{ Returns the Indexth command line parameter, or an empty string if Index is
+  out of range.
+  Differences from Delphi's ParamStr:
+  - No limits on parameter length
+  - Doesn't ignore empty parameters ("")
+  - Handles the empty argv[0] case like MSVC: if GetCommandLine() returns
+    " a b" then NewParamStr(1) should return "a", not "b" }
+var
+  Buffer: array[0..MAX_PATH-1] of Char;
+  S: String;
+  P: PChar;
+begin
+  if Index <= 0 then begin
+    Result := '';
+  end
+  else begin
+    P := Command;
+    while True do begin
+      if P^ = #0 then begin
+        S := '';
+        Break;
+      end;
+      P := GetStrParamStr(P, S);
+      if Index = 0 then Break;
+      Dec(Index);
+    end;
+    Result := S;
+  end;
+end;
+
+function StrParamStr(Command: string; Index: Integer): string;
+begin
+  Result := StrParamStr(PChar(Command), Index);
+end;
+
 end.