Ignore:
Timestamp:
Apr 18, 2019, 3:34:38 PM (6 years ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Components/Common/UCommon.pas

    r34 r38  
    2727    unfNameServicePrincipal = 10,  // Generalized service principal name
    2828    unfDNSDomainName = 11);
     29
     30  TFilterMethod = function (FileName: string): Boolean of object;
     31  TFileNameMethod = procedure (FileName: string) of object;
    2932
    3033var
     
    6366procedure OpenWebPage(URL: string);
    6467procedure OpenFileInShell(FileName: string);
    65 procedure ExecuteProgram(CommandLine: string);
     68procedure ExecuteProgram(Executable: string; Parameters: array of string);
    6669procedure FreeThenNil(var Obj);
    6770function RemoveQuotes(Text: string): string;
     
    7174function MergeArray(A, B: array of string): TArrayOfString;
    7275function LoadFileToStr(const FileName: TFileName): AnsiString;
     76procedure SaveStringToFile(S, FileName: string);
     77procedure SearchFiles(AList: TStrings; Dir: string;
     78  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     79function GetStringPart(var Text: string; Separator: string): string;
     80function StripTags(const S: string): string;
     81function PosFromIndex(SubStr: string; Text: string;
     82  StartIndex: Integer): Integer;
     83function PosFromIndexReverse(SubStr: string; Text: string;
     84  StartIndex: Integer): Integer;
     85procedure CopyStringArray(Dest: TStringArray; Source: array of string);
    7386
    7487
     
    98111  I: Integer;
    99112begin
     113  Result := '';
    100114  for I := 1 to Length(Source) do begin
    101115    Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
     
    112126  Path := IncludeTrailingPathDelimiter(APath);
    113127
    114   Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
     128  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
    115129  while Find = 0 do begin
    116     DeleteFile(Path + UTF8Encode(SearchRec.Name));
     130    DeleteFile(Path + SearchRec.Name);
    117131
    118132    Find := SysUtils.FindNext(SearchRec);
     
    429443end;
    430444
    431 procedure ExecuteProgram(CommandLine: string);
     445procedure ExecuteProgram(Executable: string; Parameters: array of string);
    432446var
    433447  Process: TProcess;
     448  I: Integer;
    434449begin
    435450  try
    436451    Process := TProcess.Create(nil);
    437     Process.CommandLine := CommandLine;
     452    Process.Executable := Executable;
     453    for I := 0 to Length(Parameters) - 1 do
     454      Process.Parameters.Add(Parameters[I]);
    438455    Process.Options := [poNoConsole];
    439456    Process.Execute;
     
    456473procedure OpenFileInShell(FileName: string);
    457474begin
    458   ExecuteProgram('cmd.exe /c start "' + FileName + '"');
     475  ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
    459476end;
    460477
     
    511528end;
    512529
     530function DefaultSearchFilter(const FileName: string): Boolean;
     531begin
     532  Result := True;
     533end;
     534
     535procedure SaveStringToFile(S, FileName: string);
     536var
     537  F: TextFile;
     538begin
     539  AssignFile(F, FileName);
     540  try
     541    ReWrite(F);
     542    Write(F, S);
     543  finally
     544    CloseFile(F);
     545  end;
     546end;
     547
     548procedure SearchFiles(AList: TStrings; Dir: string;
     549  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     550var
     551  SR: TSearchRec;
     552begin
     553  Dir := IncludeTrailingPathDelimiter(Dir);
     554  if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
     555    try
     556      repeat
     557        if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
     558          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
     559        if Assigned(FileNameMethod) then
     560          FileNameMethod(Dir + SR.Name);
     561        AList.Add(Dir + SR.Name);
     562        if (SR.Attr and faDirectory) <> 0 then
     563          SearchFiles(AList, Dir + SR.Name, FilterMethod);
     564      until FindNext(SR) <> 0;
     565    finally
     566      FindClose(SR);
     567    end;
     568end;
     569
     570function GetStringPart(var Text: string; Separator: string): string;
     571var
     572  P: Integer;
     573begin
     574  P := Pos(Separator, Text);
     575  if P > 0 then begin
     576    Result := Copy(Text, 1, P - 1);
     577    Delete(Text, 1, P - 1 + Length(Separator));
     578  end else begin
     579    Result := Text;
     580    Text := '';
     581  end;
     582  Result := Trim(Result);
     583  Text := Trim(Text);
     584end;
     585
     586function StripTags(const S: string): string;
     587var
     588  Len: Integer;
     589
     590  function ReadUntil(const ReadFrom: Integer; const C: Char): Integer;
     591  var
     592    J: Integer;
     593  begin
     594    for J := ReadFrom to Len do
     595      if (S[j] = C) then
     596      begin
     597        Result := J;
     598        Exit;
     599      end;
     600    Result := Len + 1;
     601  end;
     602
     603var
     604  I, APos: Integer;
     605begin
     606  Len := Length(S);
     607  I := 0;
     608  Result := '';
     609  while (I <= Len) do begin
     610    Inc(I);
     611    APos := ReadUntil(I, '<');
     612    Result := Result + Copy(S, I, APos - i);
     613    I := ReadUntil(APos + 1, '>');
     614  end;
     615end;
     616
     617function PosFromIndex(SubStr: string; Text: string;
     618  StartIndex: Integer): Integer;
     619var
     620  I, MaxLen: SizeInt;
     621  Ptr: PAnsiChar;
     622begin
     623  Result := 0;
     624  if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
     625  if Length(SubStr) > 0 then begin
     626    MaxLen := Length(Text) - Length(SubStr) + 1;
     627    I := StartIndex;
     628    Ptr := @Text[StartIndex];
     629    while (I <= MaxLen) do begin
     630      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     631        Result := I;
     632        Exit;
     633      end;
     634      Inc(I);
     635      Inc(Ptr);
     636    end;
     637  end;
     638end;
     639
     640function PosFromIndexReverse(SubStr: string; Text: string;
     641  StartIndex: Integer): Integer;
     642var
     643  I: SizeInt;
     644  Ptr: PAnsiChar;
     645begin
     646  Result := 0;
     647  if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
     648  if Length(SubStr) > 0 then begin
     649    I := StartIndex;
     650    Ptr := @Text[StartIndex];
     651    while (I > 0) do begin
     652      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     653        Result := I;
     654        Exit;
     655      end;
     656      Dec(I);
     657      Dec(Ptr);
     658    end;
     659  end;
     660end;
     661
     662procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     663var
     664  I: Integer;
     665begin
     666  SetLength(Dest, Length(Source));
     667  for I := 0 to Length(Dest) - 1 do
     668    Dest[I] := Source[I];
     669end;
    513670
    514671
Note: See TracChangeset for help on using the changeset viewer.