Ignore:
Timestamp:
Jun 4, 2024, 12:22:49 AM (4 months ago)
Author:
chronos
Message:
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r74 r75  
    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
     
    2825    unfDNSDomainName = 11);
    2926
     27  TFilterMethod = function (FileName: string): Boolean of object;
     28  TFileNameMethod = procedure (FileName: string) of object;
     29
    3030var
    3131  ExceptionHandler: TExceptionEvent;
    3232  DLLHandle1: HModule;
    3333
    34 {$IFDEF Windows}
    35   GetUserNameEx: procedure (NameFormat: DWORD;
    36     lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
    37 {$ENDIF}
    38 
    39 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;
    4045function BinToInt(BinStr: string): Int64;
    41 function TryHexToInt(Data: string; var Value: Integer): Boolean;
    42 function TryBinToInt(Data: string; var Value: Integer): Boolean;
    4346function BinToHexString(Source: AnsiString): string;
    4447//function DelTree(DirName : string): Boolean;
     
    4649function BCDToInt(Value: Byte): Byte;
    4750function 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;
    4860function GetUserName: string;
    49 function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    50 function SplitString(var Text: string; Count: Word): string;
    5161function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
    5262function 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);
    5383procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
    5484procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
    5585procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
    5686procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
    57 function AddLeadingZeroes(const aNumber, Length : integer) : string;
    58 function LastPos(const SubStr: String; const S: String): Integer;
    59 function GenerateNewName(OldName: string): string;
    60 function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    61 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
    62 procedure DeleteFiles(APath, AFileSpec: string);
    63 procedure OpenWebPage(URL: string);
    64 procedure OpenFileInShell(FileName: string);
    65 procedure ExecuteProgram(CommandLine: string);
    66 procedure FreeThenNil(var Obj);
    67 function RemoveQuotes(Text: string): string;
    68 function ComputerName: string;
    69 function OccurenceOfChar(What: Char; Where: string): Integer;
    70 function GetDirCount(Dir: string): Integer;
    71 function MergeArray(A, B: array of string): TArrayOfString;
    72 function LoadFileToStr(const FileName: TFileName): AnsiString;
     87procedure SearchFiles(AList: TStrings; Dir: string;
     88  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     89function SplitString(var Text: string; Count: Word): string;
     90function StripTags(const S: string): string;
     91function TryHexToInt(Data: string; out Value: Integer): Boolean;
     92function TryBinToInt(Data: string; out Value: Integer): Boolean;
     93procedure SortStrings(Strings: TStrings);
    7394
    7495
     
    98119  I: Integer;
    99120begin
     121  Result := '';
    100122  for I := 1 to Length(Source) do begin
    101123    Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
     
    112134  Path := IncludeTrailingPathDelimiter(APath);
    113135
    114   Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
     136  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
    115137  while Find = 0 do begin
    116     DeleteFile(Path + UTF8Encode(SearchRec.Name));
     138    DeleteFile(Path + SearchRec.Name);
    117139
    118140    Find := SysUtils.FindNext(SearchRec);
     
    185207end;*)
    186208
     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
    187220function LastPos(const SubStr: String; const S: String): Integer;
    188221begin
     
    230263end;
    231264
    232 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     265function TryHexToInt(Data: string; out Value: Integer): Boolean;
    233266var
    234267  I: Integer;
     
    246279end;
    247280
    248 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     281function TryBinToInt(Data: string; out Value: Integer): Boolean;
    249282var
    250283  I: Integer;
     
    274307end;
    275308
    276 function Explode(Separator: char; Data: string): TArrayOfString;
    277 begin
    278   SetLength(Result, 0);
    279   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
    280323    SetLength(Result, Length(Result) + 1);
    281     Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
    282     Delete(Data, 1, Pos(Separator, Data));
    283   end;
    284   SetLength(Result, Length(Result) + 1);
    285   Result[High(Result)] := Data;
    286 end;
    287 
    288 {$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}
    289340function GetUserName: string;
    290341const
     
    294345begin
    295346  L := MAX_USERNAME_LENGTH + 2;
     347  Result := Default(string);
    296348  SetLength(Result, L);
    297349  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    307359  end;
    308360end;
    309 {$endif}
     361{$ENDIF}
    310362
    311363function ComputerName: string;
    312 {$ifdef mswindows}
     364{$IFDEF WINDOWS}
    313365const
    314366 INFO_BUFFER_SIZE = 32767;
     
    325377  end;
    326378end;
    327 {$endif}
    328 {$ifdef unix}
     379{$ENDIF}
     380{$IFDEF UNIX}
    329381var
    330382  Name: UtsName;
    331383begin
     384  Name := Default(UtsName);
    332385  fpuname(Name);
    333386  Result := Name.Nodename;
    334387end;
    335 {$endif}
    336 
    337 {$ifdef windows}
     388{$ENDIF}
     389
     390{$IFDEF WINDOWS}
    338391function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    339392const
     
    413466procedure LoadLibraries;
    414467begin
    415   {$IFDEF Windows}
     468  {$IFDEF WINDOWS}
    416469  DLLHandle1 := LoadLibrary('secur32.dll');
    417470  if DLLHandle1 <> 0 then
     
    424477procedure FreeLibraries;
    425478begin
    426   {$IFDEF Windows}
     479  {$IFDEF WINDOWS}
    427480  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    428481  {$ENDIF}
    429482end;
    430483
    431 procedure ExecuteProgram(CommandLine: string);
     484procedure ExecuteProgram(Executable: string; Parameters: array of string);
    432485var
    433486  Process: TProcess;
     487  I: Integer;
    434488begin
    435489  try
    436490    Process := TProcess.Create(nil);
    437     Process.CommandLine := CommandLine;
     491    Process.Executable := Executable;
     492    for I := 0 to Length(Parameters) - 1 do
     493      Process.Parameters.Add(Parameters[I]);
    438494    Process.Options := [poNoConsole];
    439495    Process.Execute;
     
    454510end;
    455511
     512procedure OpenEmail(Email: string);
     513begin
     514  OpenURL('mailto:' + Email);
     515end;
     516
    456517procedure OpenFileInShell(FileName: string);
    457518begin
    458   ExecuteProgram('cmd.exe /c start "' + FileName + '"');
     519  ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
    459520end;
    460521
     
    482543end;
    483544
    484 function MergeArray(A, B: array of string): TArrayOfString;
    485 var
    486   I: Integer;
    487 begin
     545function MergeArray(A, B: array of string): TStringArray;
     546var
     547  I: Integer;
     548begin
     549  Result := Default(TStringArray);
    488550  SetLength(Result, Length(A) + Length(B));
    489551  for I := 0 to Length(A) - 1 do
     
    511573end;
    512574
     575function DefaultSearchFilter(const FileName: string): Boolean;
     576begin
     577  Result := True;
     578end;
     579
     580procedure SaveStringToFile(S, FileName: string);
     581var
     582  F: TextFile;
     583begin
     584  AssignFile(F, FileName);
     585  try
     586    ReWrite(F);
     587    Write(F, S);
     588  finally
     589    CloseFile(F);
     590  end;
     591end;
     592
     593procedure SearchFiles(AList: TStrings; Dir: string;
     594  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     595var
     596  SR: TSearchRec;
     597begin
     598  Dir := IncludeTrailingPathDelimiter(Dir);
     599  if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
     600    try
     601      repeat
     602        if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
     603          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
     604        if Assigned(FileNameMethod) then
     605          FileNameMethod(Dir + SR.Name);
     606        AList.Add(Dir + SR.Name);
     607        if (SR.Attr and faDirectory) <> 0 then
     608          SearchFiles(AList, Dir + SR.Name, FilterMethod);
     609      until FindNext(SR) <> 0;
     610    finally
     611      FindClose(SR);
     612    end;
     613end;
     614
     615function GetStringPart(var Text: string; Separator: string): string;
     616var
     617  P: Integer;
     618begin
     619  P := Pos(Separator, Text);
     620  if P > 0 then begin
     621    Result := Copy(Text, 1, P - 1);
     622    Delete(Text, 1, P - 1 + Length(Separator));
     623  end else begin
     624    Result := Text;
     625    Text := '';
     626  end;
     627  Result := Trim(Result);
     628  Text := Trim(Text);
     629end;
     630
     631function StripTags(const S: string): string;
     632var
     633  Len: Integer;
     634
     635  function ReadUntil(const ReadFrom: Integer; const C: Char): Integer;
     636  var
     637    J: Integer;
     638  begin
     639    for J := ReadFrom to Len do
     640      if (S[j] = C) then
     641      begin
     642        Result := J;
     643        Exit;
     644      end;
     645    Result := Len + 1;
     646  end;
     647
     648var
     649  I, APos: Integer;
     650begin
     651  Len := Length(S);
     652  I := 0;
     653  Result := '';
     654  while (I <= Len) do begin
     655    Inc(I);
     656    APos := ReadUntil(I, '<');
     657    Result := Result + Copy(S, I, APos - i);
     658    I := ReadUntil(APos + 1, '>');
     659  end;
     660end;
     661
     662function PosFromIndex(SubStr: string; Text: string;
     663  StartIndex: Integer): Integer;
     664var
     665  I, MaxLen: SizeInt;
     666  Ptr: PAnsiChar;
     667begin
     668  Result := 0;
     669  if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
     670  if Length(SubStr) > 0 then begin
     671    MaxLen := Length(Text) - Length(SubStr) + 1;
     672    I := StartIndex;
     673    Ptr := @Text[StartIndex];
     674    while (I <= MaxLen) do begin
     675      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     676        Result := I;
     677        Exit;
     678      end;
     679      Inc(I);
     680      Inc(Ptr);
     681    end;
     682  end;
     683end;
     684
     685function PosFromIndexReverse(SubStr: string; Text: string;
     686  StartIndex: Integer): Integer;
     687var
     688  I: SizeInt;
     689  Ptr: PAnsiChar;
     690begin
     691  Result := 0;
     692  if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
     693  if Length(SubStr) > 0 then begin
     694    I := StartIndex;
     695    Ptr := @Text[StartIndex];
     696    while (I > 0) do begin
     697      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     698        Result := I;
     699        Exit;
     700      end;
     701      Dec(I);
     702      Dec(Ptr);
     703    end;
     704  end;
     705end;
     706
     707procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     708var
     709  I: Integer;
     710begin
     711  SetLength(Dest, Length(Source));
     712  for I := 0 to Length(Dest) - 1 do
     713    Dest[I] := Source[I];
     714end;
     715
     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;
    513745
    514746
Note: See TracChangeset for help on using the changeset viewer.