Ignore:
Timestamp:
Jun 7, 2024, 11:59:43 AM (5 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r84 r85  
    1 unit UCommon;
    2 
    3 {$mode delphi}
     1unit Common;
    42
    53interface
    64
    75uses
    8   {$ifdef Windows}Windows,{$endif}
    9   {$ifdef Linux}baseunix,{$endif}
    10   Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
    11   FileUtil; //, ShFolder, ShellAPI;
     6  {$IFDEF WINDOWS}Windows,{$ENDIF}
     7  {$IFDEF UNIX}baseunix,{$ENDIF}
     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}
    41 
    42 function IntToBin(Data: Int64; Count: Byte): string;
     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);
     43
     44function AddLeadingZeroes(const aNumber, Length : integer) : string;
    4345function BinToInt(BinStr: string): Int64;
    44 function TryHexToInt(Data: string; var Value: Integer): Boolean;
    45 function TryBinToInt(Data: string; var Value: Integer): Boolean;
    4646function BinToHexString(Source: AnsiString): string;
    4747//function DelTree(DirName : string): Boolean;
     
    4949function BCDToInt(Value: Byte): Byte;
    5050function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
     51procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     52function CombinePaths(Path1, Path2: string): string;
     53function ComputerName: string;
     54procedure DeleteFiles(APath, AFileSpec: string);
     55function Explode(Separator: Char; Data: string): TStringArray;
     56procedure ExecuteProgram(Executable: string; Parameters: array of string);
     57procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
     58procedure FreeThenNil(var Obj);
     59function GetDirCount(Dir: string): Integer;
    5160function GetUserName: string;
    52 function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    53 function SplitString(var Text: string; Count: Word): string;
    5461function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
    5562function GetBit(Variable: QWord; Index: Byte): Boolean;
     63function GetStringPart(var Text: string; Separator: string): string;
     64function GenerateNewName(OldName: string): string;
     65function GetFileFilterItemExt(Filter: string; Index: Integer): string;
     66function IntToBin(Data: Int64; Count: Byte): string;
     67function Implode(Separator: string; List: TList<string>): string;
     68function Implode(Separator: string; List: TStringList; Around: string = ''): string;
     69function LastPos(const SubStr: String; const S: String): Integer;
     70function LoadFileToStr(const FileName: TFileName): AnsiString;
     71function LoggedOnUserNameEx(Format: TUserNameFormat): string;
     72function MergeArray(A, B: array of string): TStringArray;
     73function OccurenceOfChar(What: Char; Where: string): Integer;
     74procedure OpenWebPage(URL: string);
     75procedure OpenEmail(Email: string);
     76procedure OpenFileInShell(FileName: string);
     77function PosFromIndex(SubStr: string; Text: string;
     78  StartIndex: Integer): Integer;
     79function PosFromIndexReverse(SubStr: string; Text: string;
     80  StartIndex: Integer): Integer;
     81function RemoveQuotes(Text: string): string;
     82procedure SaveStringToFile(S, FileName: string);
    5683procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
    5784procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
    5885procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
    5986procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
    60 function AddLeadingZeroes(const aNumber, Length : integer) : string;
    61 function LastPos(const SubStr: String; const S: String): Integer;
    62 function GenerateNewName(OldName: string): string;
    63 function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    64 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
    65 procedure DeleteFiles(APath, AFileSpec: string);
    66 procedure OpenWebPage(URL: string);
    67 procedure OpenFileInShell(FileName: string);
    68 procedure ExecuteProgram(Executable: string; Parameters: array of string);
    69 procedure FreeThenNil(var Obj);
    70 function RemoveQuotes(Text: string): string;
    71 function ComputerName: string;
    72 function OccurenceOfChar(What: Char; Where: string): Integer;
    73 function GetDirCount(Dir: string): Integer;
    74 function MergeArray(A, B: array of string): TArrayOfString;
    75 function LoadFileToStr(const FileName: TFileName): AnsiString;
    76 procedure SaveStringToFile(S, FileName: string);
    7787procedure SearchFiles(AList: TStrings; Dir: string;
    7888  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
    79 function GetStringPart(var Text: string; Separator: string): string;
     89function SplitString(var Text: string; Count: Word): string;
    8090function StripTags(const S: string): string;
    81 function PosFromIndex(SubStr: string; Text: string;
    82   StartIndex: Integer): Integer;
    83 function PosFromIndexReverse(SubStr: string; Text: string;
    84   StartIndex: Integer): Integer;
    85 procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     91function TryHexToInt(Data: string; out Value: Integer): Boolean;
     92function TryBinToInt(Data: string; out Value: Integer): Boolean;
     93procedure SortStrings(Strings: TStrings);
    8694
    8795
     
    199207end;*)
    200208
     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
    201220function LastPos(const SubStr: String; const S: String): Integer;
    202221begin
     
    244263end;
    245264
    246 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     265function TryHexToInt(Data: string; out Value: Integer): Boolean;
    247266var
    248267  I: Integer;
     
    260279end;
    261280
    262 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     281function TryBinToInt(Data: string; out Value: Integer): Boolean;
    263282var
    264283  I: Integer;
     
    288307end;
    289308
    290 function Explode(Separator: char; Data: string): TArrayOfString;
    291 begin
    292   SetLength(Result, 0);
    293   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
    294323    SetLength(Result, Length(Result) + 1);
    295     Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
    296     Delete(Data, 1, Pos(Separator, Data));
    297   end;
    298   SetLength(Result, Length(Result) + 1);
    299   Result[High(Result)] := Data;
    300 end;
    301 
    302 {$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}
    303340function GetUserName: string;
    304341const
     
    308345begin
    309346  L := MAX_USERNAME_LENGTH + 2;
     347  Result := Default(string);
    310348  SetLength(Result, L);
    311349  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    321359  end;
    322360end;
    323 {$endif}
     361{$ENDIF}
    324362
    325363function ComputerName: string;
    326 {$ifdef mswindows}
     364{$IFDEF WINDOWS}
    327365const
    328366 INFO_BUFFER_SIZE = 32767;
     
    339377  end;
    340378end;
    341 {$endif}
    342 {$ifdef unix}
     379{$ENDIF}
     380{$IFDEF UNIX}
    343381var
    344382  Name: UtsName;
    345383begin
     384  Name := Default(UtsName);
    346385  fpuname(Name);
    347386  Result := Name.Nodename;
    348387end;
    349 {$endif}
    350 
    351 {$ifdef windows}
     388{$ENDIF}
     389
     390{$IFDEF WINDOWS}
    352391function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    353392const
     
    427466procedure LoadLibraries;
    428467begin
    429   {$IFDEF Windows}
     468  {$IFDEF WINDOWS}
    430469  DLLHandle1 := LoadLibrary('secur32.dll');
    431470  if DLLHandle1 <> 0 then
     
    438477procedure FreeLibraries;
    439478begin
    440   {$IFDEF Windows}
     479  {$IFDEF WINDOWS}
    441480  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    442481  {$ENDIF}
     
    471510end;
    472511
     512procedure OpenEmail(Email: string);
     513begin
     514  OpenURL('mailto:' + Email);
     515end;
     516
    473517procedure OpenFileInShell(FileName: string);
    474518begin
     
    499543end;
    500544
    501 function MergeArray(A, B: array of string): TArrayOfString;
    502 var
    503   I: Integer;
    504 begin
     545function MergeArray(A, B: array of string): TStringArray;
     546var
     547  I: Integer;
     548begin
     549  Result := Default(TStringArray);
    505550  SetLength(Result, Length(A) + Length(B));
    506551  for I := 0 to Length(A) - 1 do
     
    669714end;
    670715
     716function CombinePaths(Path1, Path2: string): string;
     717begin
     718  Result := Path1;
     719  if Result <> '' then Result := Result + DirectorySeparator + Path2
     720    else Result := Path2;
     721end;
     722
     723procedure SortStrings(Strings: TStrings);
     724var
     725  Tmp: TStringList;
     726begin
     727  Strings.BeginUpdate;
     728  try
     729    if Strings is TStringList then begin
     730      TStringList(Strings).Sort;
     731    end else begin
     732      Tmp := TStringList.Create;
     733      try
     734        Tmp.Assign(Strings);
     735        Tmp.Sort;
     736        Strings.Assign(Tmp);
     737      finally
     738        Tmp.Free;
     739      end;
     740    end;
     741  finally
     742    Strings.EndUpdate;
     743  end;
     744end;
     745
    671746
    672747initialization
Note: See TracChangeset for help on using the changeset viewer.