Ignore:
Timestamp:
Jun 19, 2024, 11:15:44 PM (5 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r314 r315  
    1 unit UCommon;
    2 
    3 {$mode delphi}
     1unit Common;
    42
    53interface
     
    86  {$IFDEF WINDOWS}Windows,{$ENDIF}
    97  {$IFDEF UNIX}baseunix,{$ENDIF}
    10   Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
    11   FileUtil; //, ShFolder, ShellAPI;
     8  Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, Graphics,
     9  FileUtil, Generics.Collections; //, ShFolder, ShellAPI;
    1210
    1311type
    1412  TArrayOfByte = array of Byte;
    15   TArrayOfString = array of string;
    1613  TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
    1714
     
    3532  DLLHandle1: HModule;
    3633
    37 {$IFDEF WINDOWS}
    38   GetUserNameEx: procedure (NameFormat: DWORD;
    39     lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
    40 {$ENDIF}
     34  {$IFDEF WINDOWS}
     35    GetUserNameEx: procedure (NameFormat: DWORD;
     36      lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
     37  {$ENDIF}
     38
     39const
     40  clLightBlue = TColor($FF8080);
     41  clLightGreen = TColor($80FF80);
     42  clLightRed = TColor($8080FF);
    4143
    4244function AddLeadingZeroes(const aNumber, Length : integer) : string;
     
    5153function ComputerName: string;
    5254procedure DeleteFiles(APath, AFileSpec: string);
     55function Explode(Separator: Char; Data: string): TStringArray;
    5356procedure ExecuteProgram(Executable: string; Parameters: array of string);
    5457procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
     
    6265function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    6366function IntToBin(Data: Int64; Count: Byte): string;
     67function Implode(Separator: string; List: TList<string>): string;
     68function Implode(Separator: string; List: TStringList; Around: string = ''): string;
    6469function LastPos(const SubStr: String; const S: String): Integer;
    6570function LoadFileToStr(const FileName: TFileName): AnsiString;
    6671function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    67 function MergeArray(A, B: array of string): TArrayOfString;
     72function MergeArray(A, B: array of string): TStringArray;
    6873function OccurenceOfChar(What: Char; Where: string): Integer;
    6974procedure OpenWebPage(URL: string);
     75procedure OpenEmail(Email: string);
    7076procedure OpenFileInShell(FileName: string);
    7177function PosFromIndex(SubStr: string; Text: string;
     
    8389function SplitString(var Text: string; Count: Word): string;
    8490function StripTags(const S: string): string;
    85 function TryHexToInt(Data: string; var Value: Integer): Boolean;
    86 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     91function TryHexToInt(Data: string; out Value: Integer): Boolean;
     92function TryBinToInt(Data: string; out Value: Integer): Boolean;
    8793procedure SortStrings(Strings: TStrings);
    8894
     
    201207end;*)
    202208
     209function Implode(Separator: string; List: TStringList; Around: string = ''): string;
     210var
     211  I: Integer;
     212begin
     213  Result := '';
     214  for I := 0 to List.Count - 1 do begin
     215    Result := Result + Around + List[I] + Around;
     216    if I < List.Count - 1 then Result := Result + Separator;
     217  end;
     218end;
     219
    203220function LastPos(const SubStr: String; const S: String): Integer;
    204221begin
     
    246263end;
    247264
    248 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     265function TryHexToInt(Data: string; out Value: Integer): Boolean;
    249266var
    250267  I: Integer;
     
    262279end;
    263280
    264 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     281function TryBinToInt(Data: string; out Value: Integer): Boolean;
    265282var
    266283  I: Integer;
     
    290307end;
    291308
    292 function Explode(Separator: char; Data: string): TArrayOfString;
    293 begin
    294   Result := nil;
    295   SetLength(Result, 0);
    296   while Pos(Separator, Data) > 0 do begin
     309function Explode(Separator: Char; Data: string): TStringArray;
     310var
     311  Index: Integer;
     312begin
     313  Result := Default(TStringArray);
     314  repeat
     315    Index := Pos(Separator, Data);
     316    if Index > 0 then begin
     317      SetLength(Result, Length(Result) + 1);
     318      Result[High(Result)] := Copy(Data, 1, Index - 1);
     319      Delete(Data, 1, Index);
     320    end else Break;
     321  until False;
     322  if Data <> '' then begin
    297323    SetLength(Result, Length(Result) + 1);
    298     Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
    299     Delete(Data, 1, Pos(Separator, Data));
    300   end;
    301   SetLength(Result, Length(Result) + 1);
    302   Result[High(Result)] := Data;
    303 end;
    304 
    305 {$IFDEF Windows}
     324    Result[High(Result)] := Data;
     325  end;
     326end;
     327
     328function Implode(Separator: string; List: TList<string>): string;
     329var
     330  I: Integer;
     331begin
     332  Result := '';
     333  for I := 0 to List.Count - 1 do begin
     334    Result := Result + List[I];
     335    if I < List.Count - 1 then Result := Result + Separator;
     336  end;
     337end;
     338
     339{$IFDEF WINDOWS}
    306340function GetUserName: string;
    307341const
     
    311345begin
    312346  L := MAX_USERNAME_LENGTH + 2;
     347  Result := Default(string);
    313348  SetLength(Result, L);
    314349  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    324359  end;
    325360end;
    326 {$endif}
     361{$ENDIF}
    327362
    328363function ComputerName: string;
    329 {$ifdef mswindows}
     364{$IFDEF WINDOWS}
    330365const
    331366 INFO_BUFFER_SIZE = 32767;
     
    342377  end;
    343378end;
    344 {$endif}
    345 {$ifdef unix}
     379{$ENDIF}
     380{$IFDEF UNIX}
    346381var
    347382  Name: UtsName;
     
    351386  Result := Name.Nodename;
    352387end;
    353 {$endif}
    354 
    355 {$ifdef windows}
     388{$ENDIF}
     389
     390{$IFDEF WINDOWS}
    356391function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    357392const
     
    431466procedure LoadLibraries;
    432467begin
    433   {$IFDEF Windows}
     468  {$IFDEF WINDOWS}
    434469  DLLHandle1 := LoadLibrary('secur32.dll');
    435470  if DLLHandle1 <> 0 then
     
    442477procedure FreeLibraries;
    443478begin
    444   {$IFDEF Windows}
     479  {$IFDEF WINDOWS}
    445480  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    446481  {$ENDIF}
     
    475510end;
    476511
     512procedure OpenEmail(Email: string);
     513begin
     514  OpenURL('mailto:' + Email);
     515end;
     516
    477517procedure OpenFileInShell(FileName: string);
    478518begin
     
    503543end;
    504544
    505 function MergeArray(A, B: array of string): TArrayOfString;
    506 var
    507   I: Integer;
    508 begin
    509   Result := Default(TArrayOfString);
     545function MergeArray(A, B: array of string): TStringArray;
     546var
     547  I: Integer;
     548begin
     549  Result := Default(TStringArray);
    510550  SetLength(Result, Length(A) + Length(B));
    511551  for I := 0 to Length(A) - 1 do
Note: See TracChangeset for help on using the changeset viewer.