Ignore:
Timestamp:
Jul 20, 2018, 10:25:06 AM (6 years ago)
Author:
chronos
Message:
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        66compiled
        77heaptrclog.trc
         8LazFuck.dbg
  • trunk/Packages/Common/UCommon.pas

    r74 r93  
    2828    unfDNSDomainName = 11);
    2929
     30  TFilterMethodMethod = function (FileName: string): Boolean of object;
    3031var
    3132  ExceptionHandler: TExceptionEvent;
     
    6364procedure OpenWebPage(URL: string);
    6465procedure OpenFileInShell(FileName: string);
    65 procedure ExecuteProgram(CommandLine: string);
     66procedure ExecuteProgram(Executable: string; Parameters: array of string);
    6667procedure FreeThenNil(var Obj);
    6768function RemoveQuotes(Text: string): string;
     
    7172function MergeArray(A, B: array of string): TArrayOfString;
    7273function LoadFileToStr(const FileName: TFileName): AnsiString;
     74procedure SearchFiles(AList: TStrings; Dir: string;
     75  FilterMethod: TFilterMethodMethod = nil);
     76function GetStringPart(var Text: string; Separator: string): string;
     77function PosFromIndex(SubStr: string; Text: string;
     78  StartIndex: Integer): Integer;
     79function PosFromIndexReverse(SubStr: string; Text: string;
     80  StartIndex: Integer): Integer;
    7381
    7482
     
    112120  Path := IncludeTrailingPathDelimiter(APath);
    113121
    114   Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
     122  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
    115123  while Find = 0 do begin
    116     DeleteFile(Path + UTF8Encode(SearchRec.Name));
     124    DeleteFile(Path + SearchRec.Name);
    117125
    118126    Find := SysUtils.FindNext(SearchRec);
     
    429437end;
    430438
    431 procedure ExecuteProgram(CommandLine: string);
     439procedure ExecuteProgram(Executable: string; Parameters: array of string);
    432440var
    433441  Process: TProcess;
     442  I: Integer;
    434443begin
    435444  try
    436445    Process := TProcess.Create(nil);
    437     Process.CommandLine := CommandLine;
     446    Process.Executable := Executable;
     447    for I := 0 to Length(Parameters) - 1 do
     448      Process.Parameters.Add(Parameters[I]);
    438449    Process.Options := [poNoConsole];
    439450    Process.Execute;
     
    456467procedure OpenFileInShell(FileName: string);
    457468begin
    458   ExecuteProgram('cmd.exe /c start "' + FileName + '"');
     469  ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
    459470end;
    460471
     
    511522end;
    512523
    513 
     524function DefaultSearchFilter(const FileName: string): Boolean;
     525begin
     526  Result := True;
     527end;
     528
     529procedure SearchFiles(AList: TStrings; Dir: string;
     530  FilterMethod: TFilterMethodMethod = nil);
     531var
     532  SR: TSearchRec;
     533begin
     534  Dir := IncludeTrailingPathDelimiter(Dir);
     535  if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
     536    try
     537      repeat
     538        if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
     539          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
     540        AList.Add(Dir + SR.Name);
     541        if (SR.Attr and faDirectory) <> 0 then
     542          SearchFiles(AList, Dir + SR.Name, FilterMethod);
     543      until FindNext(SR) <> 0;
     544    finally
     545      FindClose(SR);
     546    end;
     547end;
     548
     549function GetStringPart(var Text: string; Separator: string): string;
     550var
     551  P: Integer;
     552begin
     553  P := Pos(Separator, Text);
     554  if P > 0 then begin
     555    Result := Copy(Text, 1, P - 1);
     556    Delete(Text, 1, P - 1 + Length(Separator));
     557  end else begin
     558    Result := Text;
     559    Text := '';
     560  end;
     561  Result := Trim(Result);
     562  Text := Trim(Text);
     563end;
     564
     565function PosFromIndex(SubStr: string; Text: string;
     566  StartIndex: Integer): Integer;
     567var
     568  I, MaxLen: SizeInt;
     569  Ptr: PAnsiChar;
     570begin
     571  Result := 0;
     572  if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
     573  if Length(SubStr) > 0 then begin
     574    MaxLen := Length(Text) - Length(SubStr) + 1;
     575    I := StartIndex;
     576    Ptr := @Text[StartIndex];
     577    while (I <= MaxLen) do begin
     578      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     579        Result := I;
     580        Exit;
     581      end;
     582      Inc(I);
     583      Inc(Ptr);
     584    end;
     585  end;
     586end;
     587
     588function PosFromIndexReverse(SubStr: string; Text: string;
     589  StartIndex: Integer): Integer;
     590var
     591  I: SizeInt;
     592  Ptr: PAnsiChar;
     593begin
     594  Result := 0;
     595  if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
     596  if Length(SubStr) > 0 then begin
     597    I := StartIndex;
     598    Ptr := @Text[StartIndex];
     599    while (I > 0) do begin
     600      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     601        Result := I;
     602        Exit;
     603      end;
     604      Dec(I);
     605      Dec(Ptr);
     606    end;
     607  end;
     608end;
    514609
    515610initialization
Note: See TracChangeset for help on using the changeset viewer.