Ignore:
Timestamp:
Mar 22, 2018, 8:31:19 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Update Common and CollTranslator packages to fix build under Lazarus 1.8.
  • Fixed: Some memory leaks.
File:
1 edited

Legend:

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

    r10 r15  
    66
    77uses
    8   {$IFDEF Windows}Windows,{$ENDIF}
     8  {$ifdef Windows}Windows,{$endif}
     9  {$ifdef Linux}baseunix,{$endif}
    910  Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
    1011  FileUtil; //, ShFolder, ShellAPI;
     
    6263procedure OpenWebPage(URL: string);
    6364procedure OpenFileInShell(FileName: string);
    64 procedure ExecuteProgram(CommandLine: string);
     65procedure ExecuteProgram(Executable: string; Parameters: array of string);
    6566procedure FreeThenNil(var Obj);
     67function RemoveQuotes(Text: string): string;
     68function ComputerName: string;
     69function OccurenceOfChar(What: Char; Where: string): Integer;
     70function GetDirCount(Dir: string): Integer;
     71function MergeArray(A, B: array of string): TArrayOfString;
     72function LoadFileToStr(const FileName: TFileName): AnsiString;
    6673
    6774
     
    105112  Path := IncludeTrailingPathDelimiter(APath);
    106113
    107   Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
     114  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
    108115  while Find = 0 do begin
    109     DeleteFileUTF8(Path + UTF8Encode(SearchRec.Name));
     116    DeleteFile(Path + SearchRec.Name);
    110117
    111118    Find := SysUtils.FindNext(SearchRec);
     
    286293  L: LongWord;
    287294begin
    288 
    289295  L := MAX_USERNAME_LENGTH + 2;
    290296  SetLength(Result, L);
     
    301307  end;
    302308end;
    303 
     309{$endif}
     310
     311function ComputerName: string;
     312{$ifdef mswindows}
     313const
     314 INFO_BUFFER_SIZE = 32767;
     315var
     316  Buffer : array[0..INFO_BUFFER_SIZE] of WideChar;
     317  Ret : DWORD;
     318begin
     319  Ret := INFO_BUFFER_SIZE;
     320  If (GetComputerNameW(@Buffer[0],Ret)) then begin
     321    Result := UTF8Encode(WideString(Buffer));
     322  end
     323  else begin
     324    Result := 'ERROR_NO_COMPUTERNAME_RETURNED';
     325  end;
     326end;
     327{$endif}
     328{$ifdef unix}
     329var
     330  Name: UtsName;
     331begin
     332  fpuname(Name);
     333  Result := Name.Nodename;
     334end;
     335{$endif}
     336
     337{$ifdef windows}
    304338function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    305339const
     
    395429end;
    396430
    397 procedure ExecuteProgram(CommandLine: string);
     431procedure ExecuteProgram(Executable: string; Parameters: array of string);
    398432var
    399433  Process: TProcess;
     434  I: Integer;
    400435begin
    401436  try
    402437    Process := TProcess.Create(nil);
    403     Process.CommandLine := CommandLine;
     438    Process.Executable := Executable;
     439    for I := 0 to Length(Parameters) - 1 do
     440      Process.Parameters.Add(Parameters[I]);
    404441    Process.Options := [poNoConsole];
    405442    Process.Execute;
     
    416453
    417454procedure OpenWebPage(URL: string);
    418 var
    419   Process: TProcess;
    420   Browser, Params: string;
    421455begin
    422456  OpenURL(URL);
    423   {try
    424     Process := TProcess.Create(nil);
    425     Browser := '';
    426     //FindDefaultBrowser(Browser, Params);
    427     //Process.Executable := Browser;
    428     //Process.Parameters.Add(Format(Params, [ApplicationInfo.HomePage]);
    429     Process.CommandLine := 'cmd.exe /c start ' + URL;
    430     Process.Options := [poNoConsole];
    431     Process.Execute;
     457end;
     458
     459procedure OpenFileInShell(FileName: string);
     460begin
     461  ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
     462end;
     463
     464function RemoveQuotes(Text: string): string;
     465begin
     466  Result := Text;
     467  if (Pos('"', Text) = 1) and (Text[Length(Text)] = '"') then
     468    Result := Copy(Text, 2, Length(Text) - 2);
     469end;
     470
     471function OccurenceOfChar(What: Char; Where: string): Integer;
     472var
     473  I: Integer;
     474begin
     475  Result := 0;
     476  for I := 1 to Length(Where) do
     477    if Where[I] = What then Inc(Result);
     478end;
     479
     480function GetDirCount(Dir: string): Integer;
     481begin
     482  Result := OccurenceOfChar(DirectorySeparator, Dir);
     483  if Copy(Dir, Length(Dir), 1) = DirectorySeparator then
     484    Dec(Result);
     485end;
     486
     487function MergeArray(A, B: array of string): TArrayOfString;
     488var
     489  I: Integer;
     490begin
     491  SetLength(Result, Length(A) + Length(B));
     492  for I := 0 to Length(A) - 1 do
     493    Result[I] := A[I];
     494  for I := 0 to Length(B) - 1 do
     495    Result[Length(A) + I] := B[I];
     496end;
     497
     498function LoadFileToStr(const FileName: TFileName): AnsiString;
     499var
     500  FileStream: TFileStream;
     501  Read: Integer;
     502begin
     503  Result := '';
     504  FileStream := TFileStream.Create(FileName, fmOpenRead);
     505  try
     506    if FileStream.Size > 0 then begin
     507      SetLength(Result, FileStream.Size);
     508      Read := FileStream.Read(Pointer(Result)^, FileStream.Size);
     509      SetLength(Result, Read);
     510    end;
    432511  finally
    433     Process.Free;
    434   end;}
    435 end;
    436 
    437 procedure OpenFileInShell(FileName: string);
    438 begin
    439   ExecuteProgram('cmd.exe /c start "' + FileName + '"');
    440 end;
     512    FileStream.Free;
     513  end;
     514end;
     515
     516
    441517
    442518initialization
Note: See TracChangeset for help on using the changeset viewer.