Ignore:
Timestamp:
May 1, 2018, 10:18:03 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Updated newer Common package files.
File:
1 edited

Legend:

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

    r116 r192  
    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;
     
    7071function GetDirCount(Dir: string): Integer;
    7172function MergeArray(A, B: array of string): TArrayOfString;
     73function LoadFileToStr(const FileName: TFileName): AnsiString;
     74procedure SearchFiles(AList: TStrings; Dir: string;
     75  FilterMethod: TFilterMethodMethod);
     76function GetStringPart(var Text: string; Separator: string): string;
    7277
    7378
     
    111116  Path := IncludeTrailingPathDelimiter(APath);
    112117
    113   Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
     118  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
    114119  while Find = 0 do begin
    115     DeleteFile(Path + UTF8Encode(SearchRec.Name));
     120    DeleteFile(Path + SearchRec.Name);
    116121
    117122    Find := SysUtils.FindNext(SearchRec);
     
    428433end;
    429434
    430 procedure ExecuteProgram(CommandLine: string);
     435procedure ExecuteProgram(Executable: string; Parameters: array of string);
    431436var
    432437  Process: TProcess;
     438  I: Integer;
    433439begin
    434440  try
    435441    Process := TProcess.Create(nil);
    436     Process.CommandLine := CommandLine;
     442    Process.Executable := Executable;
     443    for I := 0 to Length(Parameters) - 1 do
     444      Process.Parameters.Add(Parameters[I]);
    437445    Process.Options := [poNoConsole];
    438446    Process.Execute;
     
    455463procedure OpenFileInShell(FileName: string);
    456464begin
    457   ExecuteProgram('cmd.exe /c start "' + FileName + '"');
     465  ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
    458466end;
    459467
     
    492500end;
    493501
     502function LoadFileToStr(const FileName: TFileName): AnsiString;
     503var
     504  FileStream: TFileStream;
     505  Read: Integer;
     506begin
     507  Result := '';
     508  FileStream := TFileStream.Create(FileName, fmOpenRead);
     509  try
     510    if FileStream.Size > 0 then begin
     511      SetLength(Result, FileStream.Size);
     512      Read := FileStream.Read(Pointer(Result)^, FileStream.Size);
     513      SetLength(Result, Read);
     514    end;
     515  finally
     516    FileStream.Free;
     517  end;
     518end;
     519
     520function DefaultSearchFilter(const FileName: string): Boolean;
     521begin
     522  Result := True;
     523end;
     524
     525procedure SearchFiles(AList: TStrings; Dir: string;
     526  FilterMethod: TFilterMethodMethod);
     527var
     528  SR: TSearchRec;
     529begin
     530  Dir := IncludeTrailingPathDelimiter(Dir);
     531  if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
     532    try
     533      repeat
     534        if (SR.Name = '.') or (SR.Name = '..') or not FilterMethod(SR.Name) or
     535          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name) then Continue;
     536        AList.Add(Dir + SR.Name);
     537        if (SR.Attr and faDirectory) <> 0 then
     538          SearchFiles(AList, Dir + SR.Name, FilterMethod);
     539      until FindNext(SR) <> 0;
     540    finally
     541      FindClose(SR);
     542    end;
     543end;
     544
     545function GetStringPart(var Text: string; Separator: string): string;
     546var
     547  P: Integer;
     548begin
     549  P := Pos(Separator, Text);
     550  if P > 0 then begin
     551    Result := Copy(Text, 1, P - 1);
     552    Delete(Text, 1, P - 1 + Length(Separator));
     553  end else begin
     554    Result := Text;
     555    Text := '';
     556  end;
     557  Result := Trim(Result);
     558  Text := Trim(Text);
     559end;
     560
    494561
    495562
Note: See TracChangeset for help on using the changeset viewer.