Ignore:
Timestamp:
Jun 29, 2018, 11:44:07 PM (6 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Updated IPTV prices.
  • Added: deb package build script.
File:
1 edited

Legend:

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

    r114 r122  
    66
    77uses
    8   {$IFDEF Windows}Windows,{$ENDIF}
    9   Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, LazFileUtils,
     8  {$ifdef Windows}Windows,{$endif}
     9  {$ifdef Linux}baseunix,{$endif}
     10  Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
    1011  FileUtil; //, ShFolder, ShellAPI;
    1112
     
    2728    unfDNSDomainName = 11);
    2829
     30  TFilterMethodMethod = function (FileName: string): Boolean of object;
    2931var
    3032  ExceptionHandler: TExceptionEvent;
     
    4850function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    4951function SplitString(var Text: string; Count: Word): string;
     52function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
    5053function GetBit(Variable: QWord; Index: Byte): Boolean;
     54procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
    5155procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
    5256procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
     
    6064procedure OpenWebPage(URL: string);
    6165procedure OpenFileInShell(FileName: string);
    62 procedure ExecuteProgram(CommandLine: string);
     66procedure ExecuteProgram(Executable: string; Parameters: array of string);
    6367procedure FreeThenNil(var Obj);
     68function RemoveQuotes(Text: string): string;
     69function ComputerName: string;
     70function OccurenceOfChar(What: Char; Where: string): Integer;
     71function GetDirCount(Dir: string): Integer;
     72function MergeArray(A, B: array of string): TArrayOfString;
     73function LoadFileToStr(const FileName: TFileName): AnsiString;
     74procedure SearchFiles(AList: TStrings; Dir: string;
     75  FilterMethod: TFilterMethodMethod = nil);
     76function GetStringPart(var Text: string; Separator: string): string;
    6477
    6578
     
    103116  Path := IncludeTrailingPathDelimiter(APath);
    104117
    105   Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
     118  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
    106119  while Find = 0 do begin
    107     DeleteFileUTF8(Path + UTF8Encode(SearchRec.Name));
     120    DeleteFile(Path + SearchRec.Name);
    108121
    109122    Find := SysUtils.FindNext(SearchRec);
     
    284297  L: LongWord;
    285298begin
    286 
    287299  L := MAX_USERNAME_LENGTH + 2;
    288300  SetLength(Result, L);
     
    299311  end;
    300312end;
    301 
     313{$endif}
     314
     315function ComputerName: string;
     316{$ifdef mswindows}
     317const
     318 INFO_BUFFER_SIZE = 32767;
     319var
     320  Buffer : array[0..INFO_BUFFER_SIZE] of WideChar;
     321  Ret : DWORD;
     322begin
     323  Ret := INFO_BUFFER_SIZE;
     324  If (GetComputerNameW(@Buffer[0],Ret)) then begin
     325    Result := UTF8Encode(WideString(Buffer));
     326  end
     327  else begin
     328    Result := 'ERROR_NO_COMPUTERNAME_RETURNED';
     329  end;
     330end;
     331{$endif}
     332{$ifdef unix}
     333var
     334  Name: UtsName;
     335begin
     336  fpuname(Name);
     337  Result := Name.Nodename;
     338end;
     339{$endif}
     340
     341{$ifdef windows}
    302342function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    303343const
     
    336376end;
    337377
     378function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
     379var
     380  I: Integer;
     381begin
     382  Result := 0;
     383  for I := 0 to MaxIndex - 1 do
     384    if ((Variable shr I) and 1) = 1 then Inc(Result);
     385end;
     386
    338387function GetBit(Variable:QWord;Index:Byte):Boolean;
    339388begin
     
    341390end;
    342391
     392procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean);
     393begin
     394  Variable := (Variable and ((1 shl Index) xor High(QWord))) or (Int64(State) shl Index);
     395end;
     396
    343397procedure SetBit(var Variable:QWord;Index:Byte;State:Boolean); overload;
    344398begin
    345   Variable := (Variable and ((1 shl Index) xor QWord($ffffffffffffffff))) or (QWord(State) shl Index);
     399  Variable := (Variable and ((1 shl Index) xor High(QWord))) or (QWord(State) shl Index);
    346400end;
    347401
    348402procedure SetBit(var Variable:Cardinal;Index:Byte;State:Boolean); overload;
    349403begin
    350   Variable := (Variable and ((1 shl Index) xor Cardinal($ffffffff))) or (Cardinal(State) shl Index);
     404  Variable := (Variable and ((1 shl Index) xor High(Cardinal))) or (Cardinal(State) shl Index);
    351405end;
    352406
    353407procedure SetBit(var Variable:Word;Index:Byte;State:Boolean); overload;
    354408begin
    355   Variable := (Variable and ((1 shl Index) xor Word($ffff))) or (Word(State) shl Index);
     409  Variable := (Variable and ((1 shl Index) xor High(Word))) or (Word(State) shl Index);
    356410end;
    357411
     
    379433end;
    380434
    381 procedure ExecuteProgram(CommandLine: string);
     435procedure ExecuteProgram(Executable: string; Parameters: array of string);
    382436var
    383437  Process: TProcess;
     438  I: Integer;
    384439begin
    385440  try
    386441    Process := TProcess.Create(nil);
    387     Process.CommandLine := CommandLine;
     442    Process.Executable := Executable;
     443    for I := 0 to Length(Parameters) - 1 do
     444      Process.Parameters.Add(Parameters[I]);
    388445    Process.Options := [poNoConsole];
    389446    Process.Execute;
     
    400457
    401458procedure OpenWebPage(URL: string);
    402 var
    403   Process: TProcess;
    404   Browser, Params: string;
    405459begin
    406460  OpenURL(URL);
    407   {try
    408     Process := TProcess.Create(nil);
    409     Browser := '';
    410     //FindDefaultBrowser(Browser, Params);
    411     //Process.Executable := Browser;
    412     //Process.Parameters.Add(Format(Params, [ApplicationInfo.HomePage]);
    413     Process.CommandLine := 'cmd.exe /c start ' + URL;
    414     Process.Options := [poNoConsole];
    415     Process.Execute;
     461end;
     462
     463procedure OpenFileInShell(FileName: string);
     464begin
     465  ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
     466end;
     467
     468function RemoveQuotes(Text: string): string;
     469begin
     470  Result := Text;
     471  if (Pos('"', Text) = 1) and (Text[Length(Text)] = '"') then
     472    Result := Copy(Text, 2, Length(Text) - 2);
     473end;
     474
     475function OccurenceOfChar(What: Char; Where: string): Integer;
     476var
     477  I: Integer;
     478begin
     479  Result := 0;
     480  for I := 1 to Length(Where) do
     481    if Where[I] = What then Inc(Result);
     482end;
     483
     484function GetDirCount(Dir: string): Integer;
     485begin
     486  Result := OccurenceOfChar(DirectorySeparator, Dir);
     487  if Copy(Dir, Length(Dir), 1) = DirectorySeparator then
     488    Dec(Result);
     489end;
     490
     491function MergeArray(A, B: array of string): TArrayOfString;
     492var
     493  I: Integer;
     494begin
     495  SetLength(Result, Length(A) + Length(B));
     496  for I := 0 to Length(A) - 1 do
     497    Result[I] := A[I];
     498  for I := 0 to Length(B) - 1 do
     499    Result[Length(A) + I] := B[I];
     500end;
     501
     502function LoadFileToStr(const FileName: TFileName): AnsiString;
     503var
     504  FileStream: TFileStream;
     505  Read: Integer;
     506begin
     507  Result := '';
     508  FileStream := TFileStream.Create(FileName, fmOpenRead);
     509  try
     510    if FileStream.Size > 0 then begin
     511      SetLength(Result, FileStream.Size);
     512      Read := FileStream.Read(Pointer(Result)^, FileStream.Size);
     513      SetLength(Result, Read);
     514    end;
    416515  finally
    417     Process.Free;
    418   end;}
    419 end;
    420 
    421 procedure OpenFileInShell(FileName: string);
    422 begin
    423   ExecuteProgram('cmd.exe /c start "' + FileName + '"');
    424 end;
     516    FileStream.Free;
     517  end;
     518end;
     519
     520function DefaultSearchFilter(const FileName: string): Boolean;
     521begin
     522  Result := True;
     523end;
     524
     525procedure SearchFiles(AList: TStrings; Dir: string;
     526  FilterMethod: TFilterMethodMethod = nil);
     527var
     528  SR: TSearchRec;
     529begin
     530  Dir := IncludeTrailingPathDelimiter(Dir);
     531  if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
     532    try
     533      repeat
     534        if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
     535          not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
     536        AList.Add(Dir + SR.Name);
     537        if (SR.Attr and faDirectory) <> 0 then
     538          SearchFiles(AList, Dir + SR.Name, FilterMethod);
     539      until FindNext(SR) <> 0;
     540    finally
     541      FindClose(SR);
     542    end;
     543end;
     544
     545function GetStringPart(var Text: string; Separator: string): string;
     546var
     547  P: Integer;
     548begin
     549  P := Pos(Separator, Text);
     550  if P > 0 then begin
     551    Result := Copy(Text, 1, P - 1);
     552    Delete(Text, 1, P - 1 + Length(Separator));
     553  end else begin
     554    Result := Text;
     555    Text := '';
     556  end;
     557  Result := Trim(Result);
     558  Text := Trim(Text);
     559end;
     560
     561
    425562
    426563initialization
Note: See TracChangeset for help on using the changeset viewer.