Changeset 463 for branches/highdpi/Packages/Common/Common.pas
- Timestamp:
- Nov 29, 2023, 2:35:44 PM (12 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/highdpi/Packages/Common/Common.pas
r462 r463 1 unit UCommon; 2 3 {$mode delphi} 1 unit Common; 4 2 5 3 interface 6 4 7 5 uses 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; 12 10 13 11 type 14 12 TArrayOfByte = array of Byte; 15 TArrayOfString = array of string;16 13 TExceptionEvent = procedure(Sender: TObject; E: Exception) of object; 17 14 … … 35 32 DLLHandle1: HModule; 36 33 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 39 const 40 clLightBlue = TColor($FF8080); 41 clLightGreen = TColor($80FF80); 42 clLightRed = TColor($8080FF); 43 44 function AddLeadingZeroes(const aNumber, Length : integer) : string; 43 45 function BinToInt(BinStr: string): Int64; 44 function TryHexToInt(Data: string; var Value: Integer): Boolean;45 function TryBinToInt(Data: string; var Value: Integer): Boolean;46 46 function BinToHexString(Source: AnsiString): string; 47 47 //function DelTree(DirName : string): Boolean; … … 49 49 function BCDToInt(Value: Byte): Byte; 50 50 function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean; 51 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 52 function CombinePaths(Path1, Path2: string): string; 53 function ComputerName: string; 54 procedure DeleteFiles(APath, AFileSpec: string); 55 function Explode(Separator: Char; Data: string): TStringArray; 56 procedure ExecuteProgram(Executable: string; Parameters: array of string); 57 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog); 58 procedure FreeThenNil(var Obj); 59 function GetDirCount(Dir: string): Integer; 51 60 function GetUserName: string; 52 function LoggedOnUserNameEx(Format: TUserNameFormat): string;53 function SplitString(var Text: string; Count: Word): string;54 61 function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer; 55 62 function GetBit(Variable: QWord; Index: Byte): Boolean; 63 function GetStringPart(var Text: string; Separator: string): string; 64 function GenerateNewName(OldName: string): string; 65 function GetFileFilterItemExt(Filter: string; Index: Integer): string; 66 function IntToBin(Data: Int64; Count: Byte): string; 67 function Implode(Separator: Char; List: TList<string>): string; 68 function LastPos(const SubStr: String; const S: String): Integer; 69 function LoadFileToStr(const FileName: TFileName): AnsiString; 70 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 71 function MergeArray(A, B: array of string): TStringArray; 72 function OccurenceOfChar(What: Char; Where: string): Integer; 73 procedure OpenWebPage(URL: string); 74 procedure OpenEmail(Email: string); 75 procedure OpenFileInShell(FileName: string); 76 function PosFromIndex(SubStr: string; Text: string; 77 StartIndex: Integer): Integer; 78 function PosFromIndexReverse(SubStr: string; Text: string; 79 StartIndex: Integer): Integer; 80 function RemoveQuotes(Text: string): string; 81 procedure SaveStringToFile(S, FileName: string); 56 82 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload; 57 83 procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload; 58 84 procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload; 59 85 procedure 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);77 86 procedure SearchFiles(AList: TStrings; Dir: string; 78 87 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 79 function GetStringPart(var Text: string; Separator: string): string;88 function SplitString(var Text: string; Count: Word): string; 80 89 function 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); 90 function TryHexToInt(Data: string; out Value: Integer): Boolean; 91 function TryBinToInt(Data: string; out Value: Integer): Boolean; 92 procedure SortStrings(Strings: TStrings); 86 93 87 94 … … 244 251 end; 245 252 246 function TryHexToInt(Data: string; varValue: Integer): Boolean;253 function TryHexToInt(Data: string; out Value: Integer): Boolean; 247 254 var 248 255 I: Integer; … … 260 267 end; 261 268 262 function TryBinToInt(Data: string; varValue: Integer): Boolean;269 function TryBinToInt(Data: string; out Value: Integer): Boolean; 263 270 var 264 271 I: Integer; … … 288 295 end; 289 296 290 function Explode(Separator: char; Data: string): TArrayOfString; 291 begin 292 SetLength(Result, 0); 293 while Pos(Separator, Data) > 0 do begin 297 function Explode(Separator: Char; Data: string): TStringArray; 298 var 299 Index: Integer; 300 begin 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 294 311 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; 314 end; 315 316 function Implode(Separator: Char; List: TList<string>): string; 317 var 318 I: Integer; 319 begin 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; 325 end; 326 327 {$IFDEF WINDOWS} 303 328 function GetUserName: string; 304 329 const … … 308 333 begin 309 334 L := MAX_USERNAME_LENGTH + 2; 335 Result := Default(string); 310 336 SetLength(Result, L); 311 337 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin … … 321 347 end; 322 348 end; 323 {$ endif}349 {$ENDIF} 324 350 325 351 function ComputerName: string; 326 {$ ifdef mswindows}352 {$IFDEF WINDOWS} 327 353 const 328 354 INFO_BUFFER_SIZE = 32767; … … 339 365 end; 340 366 end; 341 {$ endif}342 {$ ifdef unix}367 {$ENDIF} 368 {$IFDEF UNIX} 343 369 var 344 370 Name: UtsName; 345 371 begin 372 Name := Default(UtsName); 346 373 fpuname(Name); 347 374 Result := Name.Nodename; 348 375 end; 349 {$ endif}350 351 {$ ifdef windows}376 {$ENDIF} 377 378 {$IFDEF WINDOWS} 352 379 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 353 380 const … … 427 454 procedure LoadLibraries; 428 455 begin 429 {$IFDEF W indows}456 {$IFDEF WINDOWS} 430 457 DLLHandle1 := LoadLibrary('secur32.dll'); 431 458 if DLLHandle1 <> 0 then … … 438 465 procedure FreeLibraries; 439 466 begin 440 {$IFDEF W indows}467 {$IFDEF WINDOWS} 441 468 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1); 442 469 {$ENDIF} … … 471 498 end; 472 499 500 procedure OpenEmail(Email: string); 501 begin 502 OpenURL('mailto:' + Email); 503 end; 504 473 505 procedure OpenFileInShell(FileName: string); 474 506 begin … … 499 531 end; 500 532 501 function MergeArray(A, B: array of string): TArrayOfString; 502 var 503 I: Integer; 504 begin 533 function MergeArray(A, B: array of string): TStringArray; 534 var 535 I: Integer; 536 begin 537 Result := Default(TStringArray); 505 538 SetLength(Result, Length(A) + Length(B)); 506 539 for I := 0 to Length(A) - 1 do … … 669 702 end; 670 703 704 function CombinePaths(Path1, Path2: string): string; 705 begin 706 Result := Path1; 707 if Result <> '' then Result := Result + DirectorySeparator + Path2 708 else Result := Path2; 709 end; 710 711 procedure SortStrings(Strings: TStrings); 712 var 713 Tmp: TStringList; 714 begin 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; 732 end; 733 671 734 672 735 initialization … … 680 743 681 744 end. 745
Note:
See TracChangeset
for help on using the changeset viewer.