Ignore:
Timestamp:
Nov 29, 2023, 2:35:44 PM (12 months ago)
Author:
chronos
Message:
  • Modified: HighDpi branch updated to trunk version.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/highdpi/Packages/Common/Common.pas

    r462 r463  
    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: Char; List: TList<string>): string;
     68function LastPos(const SubStr: String; const S: String): Integer;
     69function LoadFileToStr(const FileName: TFileName): AnsiString;
     70function LoggedOnUserNameEx(Format: TUserNameFormat): string;
     71function MergeArray(A, B: array of string): TStringArray;
     72function OccurenceOfChar(What: Char; Where: string): Integer;
     73procedure OpenWebPage(URL: string);
     74procedure OpenEmail(Email: string);
     75procedure OpenFileInShell(FileName: string);
     76function PosFromIndex(SubStr: string; Text: string;
     77  StartIndex: Integer): Integer;
     78function PosFromIndexReverse(SubStr: string; Text: string;
     79  StartIndex: Integer): Integer;
     80function RemoveQuotes(Text: string): string;
     81procedure SaveStringToFile(S, FileName: string);
    5682procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
    5783procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
    5884procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
    5985procedure 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);
    7786procedure SearchFiles(AList: TStrings; Dir: string;
    7887  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
    79 function GetStringPart(var Text: string; Separator: string): string;
     88function SplitString(var Text: string; Count: Word): string;
    8089function 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);
     90function TryHexToInt(Data: string; out Value: Integer): Boolean;
     91function TryBinToInt(Data: string; out Value: Integer): Boolean;
     92procedure SortStrings(Strings: TStrings);
    8693
    8794
     
    244251end;
    245252
    246 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     253function TryHexToInt(Data: string; out Value: Integer): Boolean;
    247254var
    248255  I: Integer;
     
    260267end;
    261268
    262 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     269function TryBinToInt(Data: string; out Value: Integer): Boolean;
    263270var
    264271  I: Integer;
     
    288295end;
    289296
    290 function Explode(Separator: char; Data: string): TArrayOfString;
    291 begin
    292   SetLength(Result, 0);
    293   while Pos(Separator, Data) > 0 do begin
     297function Explode(Separator: Char; Data: string): TStringArray;
     298var
     299  Index: Integer;
     300begin
     301  Result := Default(TStringArray);
     302  repeat
     303    Index := Pos(Separator, Data);
     304    if Index > 0 then begin
     305      SetLength(Result, Length(Result) + 1);
     306      Result[High(Result)] := Copy(Data, 1, Index - 1);
     307      Delete(Data, 1, Index);
     308    end else Break;
     309  until False;
     310  if Data <> '' then begin
    294311    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}
     312    Result[High(Result)] := Data;
     313  end;
     314end;
     315
     316function Implode(Separator: Char; List: TList<string>): string;
     317var
     318  I: Integer;
     319begin
     320  Result := '';
     321  for I := 0 to List.Count - 1 do begin
     322    Result := Result + List[I];
     323    if I < List.Count - 1 then Result := Result + Separator;
     324  end;
     325end;
     326
     327{$IFDEF WINDOWS}
    303328function GetUserName: string;
    304329const
     
    308333begin
    309334  L := MAX_USERNAME_LENGTH + 2;
     335  Result := Default(string);
    310336  SetLength(Result, L);
    311337  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    321347  end;
    322348end;
    323 {$endif}
     349{$ENDIF}
    324350
    325351function ComputerName: string;
    326 {$ifdef mswindows}
     352{$IFDEF WINDOWS}
    327353const
    328354 INFO_BUFFER_SIZE = 32767;
     
    339365  end;
    340366end;
    341 {$endif}
    342 {$ifdef unix}
     367{$ENDIF}
     368{$IFDEF UNIX}
    343369var
    344370  Name: UtsName;
    345371begin
     372  Name := Default(UtsName);
    346373  fpuname(Name);
    347374  Result := Name.Nodename;
    348375end;
    349 {$endif}
    350 
    351 {$ifdef windows}
     376{$ENDIF}
     377
     378{$IFDEF WINDOWS}
    352379function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    353380const
     
    427454procedure LoadLibraries;
    428455begin
    429   {$IFDEF Windows}
     456  {$IFDEF WINDOWS}
    430457  DLLHandle1 := LoadLibrary('secur32.dll');
    431458  if DLLHandle1 <> 0 then
     
    438465procedure FreeLibraries;
    439466begin
    440   {$IFDEF Windows}
     467  {$IFDEF WINDOWS}
    441468  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    442469  {$ENDIF}
     
    471498end;
    472499
     500procedure OpenEmail(Email: string);
     501begin
     502  OpenURL('mailto:' + Email);
     503end;
     504
    473505procedure OpenFileInShell(FileName: string);
    474506begin
     
    499531end;
    500532
    501 function MergeArray(A, B: array of string): TArrayOfString;
    502 var
    503   I: Integer;
    504 begin
     533function MergeArray(A, B: array of string): TStringArray;
     534var
     535  I: Integer;
     536begin
     537  Result := Default(TStringArray);
    505538  SetLength(Result, Length(A) + Length(B));
    506539  for I := 0 to Length(A) - 1 do
     
    669702end;
    670703
     704function CombinePaths(Path1, Path2: string): string;
     705begin
     706  Result := Path1;
     707  if Result <> '' then Result := Result + DirectorySeparator + Path2
     708    else Result := Path2;
     709end;
     710
     711procedure SortStrings(Strings: TStrings);
     712var
     713  Tmp: TStringList;
     714begin
     715  Strings.BeginUpdate;
     716  try
     717    if Strings is TStringList then begin
     718      TStringList(Strings).Sort;
     719    end else begin
     720      Tmp := TStringList.Create;
     721      try
     722        Tmp.Assign(Strings);
     723        Tmp.Sort;
     724        Strings.Assign(Tmp);
     725      finally
     726        Tmp.Free;
     727      end;
     728    end;
     729  finally
     730    Strings.EndUpdate;
     731  end;
     732end;
     733
    671734
    672735initialization
     
    680743
    681744end.
     745
Note: See TracChangeset for help on using the changeset viewer.