Ignore:
Timestamp:
Mar 18, 2022, 1:37:03 PM (3 years ago)
Author:
chronos
Message:
  • Modified: Update Common package.
File:
1 edited

Legend:

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

    r122 r131  
    66
    77uses
    8   {$ifdef Windows}Windows,{$endif}
    9   {$ifdef Linux}baseunix,{$endif}
    10   Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
     8  {$IFDEF WINDOWS}Windows,{$ENDIF}
     9  {$IFDEF UNIX}baseunix,{$ENDIF}
     10  Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, Graphics,
    1111  FileUtil; //, ShFolder, ShellAPI;
    1212
    1313type
    1414  TArrayOfByte = array of Byte;
    15   TArrayOfString = array of string;
    1615  TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
    1716
     
    2827    unfDNSDomainName = 11);
    2928
    30   TFilterMethodMethod = function (FileName: string): Boolean of object;
     29  TFilterMethod = function (FileName: string): Boolean of object;
     30  TFileNameMethod = procedure (FileName: string) of object;
     31
    3132var
    3233  ExceptionHandler: TExceptionEvent;
    3334  DLLHandle1: HModule;
    3435
    35 {$IFDEF Windows}
     36const
     37  clLightBlue = TColor($FF8080);
     38  clLightGreen = TColor($80FF80);
     39  clLightRed = TColor($8080FF);
     40
     41{$IFDEF WINDOWS}
    3642  GetUserNameEx: procedure (NameFormat: DWORD;
    3743    lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
    3844{$ENDIF}
    3945
    40 function IntToBin(Data: Int64; Count: Byte): string;
     46function AddLeadingZeroes(const aNumber, Length : integer) : string;
    4147function BinToInt(BinStr: string): Int64;
    42 function TryHexToInt(Data: string; var Value: Integer): Boolean;
    43 function TryBinToInt(Data: string; var Value: Integer): Boolean;
    4448function BinToHexString(Source: AnsiString): string;
    4549//function DelTree(DirName : string): Boolean;
     
    4751function BCDToInt(Value: Byte): Byte;
    4852function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
     53procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     54function CombinePaths(Path1, Path2: string): string;
     55function ComputerName: string;
     56procedure DeleteFiles(APath, AFileSpec: string);
     57function Explode(Separator: Char; Data: string): TStringArray;
     58procedure ExecuteProgram(Executable: string; Parameters: array of string);
     59procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
     60procedure FreeThenNil(var Obj);
     61function GetDirCount(Dir: string): Integer;
    4962function GetUserName: string;
    50 function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    51 function SplitString(var Text: string; Count: Word): string;
    5263function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
    5364function GetBit(Variable: QWord; Index: Byte): Boolean;
     65function GetStringPart(var Text: string; Separator: string): string;
     66function GenerateNewName(OldName: string): string;
     67function GetFileFilterItemExt(Filter: string; Index: Integer): string;
     68function IntToBin(Data: Int64; Count: Byte): 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);
    5483procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
    5584procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
    5685procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
    5786procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
    58 function AddLeadingZeroes(const aNumber, Length : integer) : string;
    59 function LastPos(const SubStr: String; const S: String): Integer;
    60 function GenerateNewName(OldName: string): string;
    61 function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    62 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
    63 procedure DeleteFiles(APath, AFileSpec: string);
    64 procedure OpenWebPage(URL: string);
    65 procedure OpenFileInShell(FileName: string);
    66 procedure ExecuteProgram(Executable: string; Parameters: array of string);
    67 procedure FreeThenNil(var Obj);
    68 function RemoveQuotes(Text: string): string;
    69 function ComputerName: string;
    70 function OccurenceOfChar(What: Char; Where: string): Integer;
    71 function GetDirCount(Dir: string): Integer;
    72 function MergeArray(A, B: array of string): TArrayOfString;
    73 function LoadFileToStr(const FileName: TFileName): AnsiString;
    7487procedure SearchFiles(AList: TStrings; Dir: string;
    75   FilterMethod: TFilterMethodMethod = nil);
    76 function GetStringPart(var Text: string; Separator: string): 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);
    7794
    7895
     
    102119  I: Integer;
    103120begin
     121  Result := '';
    104122  for I := 1 to Length(Source) do begin
    105123    Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
     
    234252end;
    235253
    236 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     254function TryHexToInt(Data: string; out Value: Integer): Boolean;
    237255var
    238256  I: Integer;
     
    250268end;
    251269
    252 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     270function TryBinToInt(Data: string; out Value: Integer): Boolean;
    253271var
    254272  I: Integer;
     
    278296end;
    279297
    280 function Explode(Separator: char; Data: string): TArrayOfString;
    281 begin
    282   SetLength(Result, 0);
    283   while Pos(Separator, Data) > 0 do begin
     298function Explode(Separator: Char; Data: string): TStringArray;
     299var
     300  Index: Integer;
     301begin
     302  Result := Default(TStringArray);
     303  repeat
     304    Index := Pos(Separator, Data);
     305    if Index > 0 then begin
     306      SetLength(Result, Length(Result) + 1);
     307      Result[High(Result)] := Copy(Data, 1, Index - 1);
     308      Delete(Data, 1, Index);
     309    end else Break;
     310  until False;
     311  if Data <> '' then begin
    284312    SetLength(Result, Length(Result) + 1);
    285     Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
    286     Delete(Data, 1, Pos(Separator, Data));
    287   end;
    288   SetLength(Result, Length(Result) + 1);
    289   Result[High(Result)] := Data;
    290 end;
    291 
    292 {$IFDEF Windows}
     313    Result[High(Result)] := Data;
     314  end;
     315end;
     316
     317{$IFDEF WINDOWS}
    293318function GetUserName: string;
    294319const
     
    298323begin
    299324  L := MAX_USERNAME_LENGTH + 2;
     325  Result := Default(string);
    300326  SetLength(Result, L);
    301327  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    311337  end;
    312338end;
    313 {$endif}
     339{$ENDIF}
    314340
    315341function ComputerName: string;
    316 {$ifdef mswindows}
     342{$IFDEF WINDOWS}
    317343const
    318344 INFO_BUFFER_SIZE = 32767;
     
    329355  end;
    330356end;
    331 {$endif}
    332 {$ifdef unix}
     357{$ENDIF}
     358{$IFDEF UNIX}
    333359var
    334360  Name: UtsName;
    335361begin
     362  Name := Default(UtsName);
    336363  fpuname(Name);
    337364  Result := Name.Nodename;
    338365end;
    339 {$endif}
    340 
    341 {$ifdef windows}
     366{$ENDIF}
     367
     368{$IFDEF WINDOWS}
    342369function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    343370const
     
    417444procedure LoadLibraries;
    418445begin
    419   {$IFDEF Windows}
     446  {$IFDEF WINDOWS}
    420447  DLLHandle1 := LoadLibrary('secur32.dll');
    421448  if DLLHandle1 <> 0 then
     
    428455procedure FreeLibraries;
    429456begin
    430   {$IFDEF Windows}
     457  {$IFDEF WINDOWS}
    431458  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    432459  {$ENDIF}
     
    461488end;
    462489
     490procedure OpenEmail(Email: string);
     491begin
     492  OpenURL('mailto:' + Email);
     493end;
     494
    463495procedure OpenFileInShell(FileName: string);
    464496begin
     
    489521end;
    490522
    491 function MergeArray(A, B: array of string): TArrayOfString;
    492 var
    493   I: Integer;
    494 begin
     523function MergeArray(A, B: array of string): TStringArray;
     524var
     525  I: Integer;
     526begin
     527  Result := Default(TStringArray);
    495528  SetLength(Result, Length(A) + Length(B));
    496529  for I := 0 to Length(A) - 1 do
     
    523556end;
    524557
     558procedure SaveStringToFile(S, FileName: string);
     559var
     560  F: TextFile;
     561begin
     562  AssignFile(F, FileName);
     563  try
     564    ReWrite(F);
     565    Write(F, S);
     566  finally
     567    CloseFile(F);
     568  end;
     569end;
     570
    525571procedure SearchFiles(AList: TStrings; Dir: string;
    526   FilterMethod: TFilterMethodMethod = nil);
     572  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
    527573var
    528574  SR: TSearchRec;
     
    534580        if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
    535581          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
     582        if Assigned(FileNameMethod) then
     583          FileNameMethod(Dir + SR.Name);
    536584        AList.Add(Dir + SR.Name);
    537585        if (SR.Attr and faDirectory) <> 0 then
     
    559607end;
    560608
     609function StripTags(const S: string): string;
     610var
     611  Len: Integer;
     612
     613  function ReadUntil(const ReadFrom: Integer; const C: Char): Integer;
     614  var
     615    J: Integer;
     616  begin
     617    for J := ReadFrom to Len do
     618      if (S[j] = C) then
     619      begin
     620        Result := J;
     621        Exit;
     622      end;
     623    Result := Len + 1;
     624  end;
     625
     626var
     627  I, APos: Integer;
     628begin
     629  Len := Length(S);
     630  I := 0;
     631  Result := '';
     632  while (I <= Len) do begin
     633    Inc(I);
     634    APos := ReadUntil(I, '<');
     635    Result := Result + Copy(S, I, APos - i);
     636    I := ReadUntil(APos + 1, '>');
     637  end;
     638end;
     639
     640function PosFromIndex(SubStr: string; Text: string;
     641  StartIndex: Integer): Integer;
     642var
     643  I, MaxLen: SizeInt;
     644  Ptr: PAnsiChar;
     645begin
     646  Result := 0;
     647  if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
     648  if Length(SubStr) > 0 then begin
     649    MaxLen := Length(Text) - Length(SubStr) + 1;
     650    I := StartIndex;
     651    Ptr := @Text[StartIndex];
     652    while (I <= MaxLen) do begin
     653      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     654        Result := I;
     655        Exit;
     656      end;
     657      Inc(I);
     658      Inc(Ptr);
     659    end;
     660  end;
     661end;
     662
     663function PosFromIndexReverse(SubStr: string; Text: string;
     664  StartIndex: Integer): Integer;
     665var
     666  I: SizeInt;
     667  Ptr: PAnsiChar;
     668begin
     669  Result := 0;
     670  if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
     671  if Length(SubStr) > 0 then begin
     672    I := StartIndex;
     673    Ptr := @Text[StartIndex];
     674    while (I > 0) 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      Dec(I);
     680      Dec(Ptr);
     681    end;
     682  end;
     683end;
     684
     685procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     686var
     687  I: Integer;
     688begin
     689  SetLength(Dest, Length(Source));
     690  for I := 0 to Length(Dest) - 1 do
     691    Dest[I] := Source[I];
     692end;
     693
     694function CombinePaths(Path1, Path2: string): string;
     695begin
     696  Result := Path1;
     697  if Result <> '' then Result := Result + DirectorySeparator + Path2
     698    else Result := Path2;
     699end;
     700
     701procedure SortStrings(Strings: TStrings);
     702var
     703  Tmp: TStringList;
     704begin
     705  Strings.BeginUpdate;
     706  try
     707    if Strings is TStringList then begin
     708      TStringList(Strings).Sort;
     709    end else begin
     710      Tmp := TStringList.Create;
     711      try
     712        Tmp.Assign(Strings);
     713        Tmp.Sort;
     714        Strings.Assign(Tmp);
     715      finally
     716        Tmp.Free;
     717      end;
     718    end;
     719  finally
     720    Strings.EndUpdate;
     721  end;
     722end;
    561723
    562724
Note: See TracChangeset for help on using the changeset viewer.