Ignore:
Timestamp:
Sep 10, 2022, 6:54:43 PM (20 months ago)
Author:
chronos
Message:
  • Modified: CoolTranslator replaced by Common package.
  • Modified: Update common package.
File:
1 edited

Legend:

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

    r15 r25  
    11unit UCommon;
    22
    3 {$mode delphi}
    4 
    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: 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);
    5382procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
    5483procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
    5584procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
    5685procedure 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(Executable: string; Parameters: array of 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;
     86procedure SearchFiles(AList: TStrings; Dir: string;
     87  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     88function SplitString(var Text: string; Count: Word): string;
     89function StripTags(const S: string): string;
     90function TryHexToInt(Data: string; out Value: Integer): Boolean;
     91function TryBinToInt(Data: string; out Value: Integer): Boolean;
     92procedure SortStrings(Strings: TStrings);
    7393
    7494
     
    98118  I: Integer;
    99119begin
     120  Result := '';
    100121  for I := 1 to Length(Source) do begin
    101122    Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
     
    230251end;
    231252
    232 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     253function TryHexToInt(Data: string; out Value: Integer): Boolean;
    233254var
    234255  I: Integer;
     
    246267end;
    247268
    248 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     269function TryBinToInt(Data: string; out Value: Integer): Boolean;
    249270var
    250271  I: Integer;
     
    274295end;
    275296
    276 function Explode(Separator: char; Data: string): TArrayOfString;
    277 begin
    278   SetLength(Result, 0);
    279   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
    280311    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}
     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}
    289328function GetUserName: string;
    290329const
     
    294333begin
    295334  L := MAX_USERNAME_LENGTH + 2;
     335  Result := Default(string);
    296336  SetLength(Result, L);
    297337  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    307347  end;
    308348end;
    309 {$endif}
     349{$ENDIF}
    310350
    311351function ComputerName: string;
    312 {$ifdef mswindows}
     352{$IFDEF WINDOWS}
    313353const
    314354 INFO_BUFFER_SIZE = 32767;
     
    325365  end;
    326366end;
    327 {$endif}
    328 {$ifdef unix}
     367{$ENDIF}
     368{$IFDEF UNIX}
    329369var
    330370  Name: UtsName;
    331371begin
     372  Name := Default(UtsName);
    332373  fpuname(Name);
    333374  Result := Name.Nodename;
    334375end;
    335 {$endif}
    336 
    337 {$ifdef windows}
     376{$ENDIF}
     377
     378{$IFDEF WINDOWS}
    338379function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    339380const
     
    413454procedure LoadLibraries;
    414455begin
    415   {$IFDEF Windows}
     456  {$IFDEF WINDOWS}
    416457  DLLHandle1 := LoadLibrary('secur32.dll');
    417458  if DLLHandle1 <> 0 then
     
    424465procedure FreeLibraries;
    425466begin
    426   {$IFDEF Windows}
     467  {$IFDEF WINDOWS}
    427468  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    428469  {$ENDIF}
     
    457498end;
    458499
     500procedure OpenEmail(Email: string);
     501begin
     502  OpenURL('mailto:' + Email);
     503end;
     504
    459505procedure OpenFileInShell(FileName: string);
    460506begin
     
    485531end;
    486532
    487 function MergeArray(A, B: array of string): TArrayOfString;
    488 var
    489   I: Integer;
    490 begin
     533function MergeArray(A, B: array of string): TStringArray;
     534var
     535  I: Integer;
     536begin
     537  Result := Default(TStringArray);
    491538  SetLength(Result, Length(A) + Length(B));
    492539  for I := 0 to Length(A) - 1 do
     
    514561end;
    515562
     563function DefaultSearchFilter(const FileName: string): Boolean;
     564begin
     565  Result := True;
     566end;
     567
     568procedure SaveStringToFile(S, FileName: string);
     569var
     570  F: TextFile;
     571begin
     572  AssignFile(F, FileName);
     573  try
     574    ReWrite(F);
     575    Write(F, S);
     576  finally
     577    CloseFile(F);
     578  end;
     579end;
     580
     581procedure SearchFiles(AList: TStrings; Dir: string;
     582  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     583var
     584  SR: TSearchRec;
     585begin
     586  Dir := IncludeTrailingPathDelimiter(Dir);
     587  if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
     588    try
     589      repeat
     590        if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
     591          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
     592        if Assigned(FileNameMethod) then
     593          FileNameMethod(Dir + SR.Name);
     594        AList.Add(Dir + SR.Name);
     595        if (SR.Attr and faDirectory) <> 0 then
     596          SearchFiles(AList, Dir + SR.Name, FilterMethod);
     597      until FindNext(SR) <> 0;
     598    finally
     599      FindClose(SR);
     600    end;
     601end;
     602
     603function GetStringPart(var Text: string; Separator: string): string;
     604var
     605  P: Integer;
     606begin
     607  P := Pos(Separator, Text);
     608  if P > 0 then begin
     609    Result := Copy(Text, 1, P - 1);
     610    Delete(Text, 1, P - 1 + Length(Separator));
     611  end else begin
     612    Result := Text;
     613    Text := '';
     614  end;
     615  Result := Trim(Result);
     616  Text := Trim(Text);
     617end;
     618
     619function StripTags(const S: string): string;
     620var
     621  Len: Integer;
     622
     623  function ReadUntil(const ReadFrom: Integer; const C: Char): Integer;
     624  var
     625    J: Integer;
     626  begin
     627    for J := ReadFrom to Len do
     628      if (S[j] = C) then
     629      begin
     630        Result := J;
     631        Exit;
     632      end;
     633    Result := Len + 1;
     634  end;
     635
     636var
     637  I, APos: Integer;
     638begin
     639  Len := Length(S);
     640  I := 0;
     641  Result := '';
     642  while (I <= Len) do begin
     643    Inc(I);
     644    APos := ReadUntil(I, '<');
     645    Result := Result + Copy(S, I, APos - i);
     646    I := ReadUntil(APos + 1, '>');
     647  end;
     648end;
     649
     650function PosFromIndex(SubStr: string; Text: string;
     651  StartIndex: Integer): Integer;
     652var
     653  I, MaxLen: SizeInt;
     654  Ptr: PAnsiChar;
     655begin
     656  Result := 0;
     657  if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
     658  if Length(SubStr) > 0 then begin
     659    MaxLen := Length(Text) - Length(SubStr) + 1;
     660    I := StartIndex;
     661    Ptr := @Text[StartIndex];
     662    while (I <= MaxLen) do begin
     663      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     664        Result := I;
     665        Exit;
     666      end;
     667      Inc(I);
     668      Inc(Ptr);
     669    end;
     670  end;
     671end;
     672
     673function PosFromIndexReverse(SubStr: string; Text: string;
     674  StartIndex: Integer): Integer;
     675var
     676  I: SizeInt;
     677  Ptr: PAnsiChar;
     678begin
     679  Result := 0;
     680  if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
     681  if Length(SubStr) > 0 then begin
     682    I := StartIndex;
     683    Ptr := @Text[StartIndex];
     684    while (I > 0) do begin
     685      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     686        Result := I;
     687        Exit;
     688      end;
     689      Dec(I);
     690      Dec(Ptr);
     691    end;
     692  end;
     693end;
     694
     695procedure CopyStringArray(Dest: TStringArray; Source: array of string);
     696var
     697  I: Integer;
     698begin
     699  SetLength(Dest, Length(Source));
     700  for I := 0 to Length(Dest) - 1 do
     701    Dest[I] := Source[I];
     702end;
     703
     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;
    516733
    517734
Note: See TracChangeset for help on using the changeset viewer.