Changeset 85 for trunk/Packages/Common/Common.pas
- Timestamp:
- Jun 7, 2024, 11:59:43 AM (5 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Common.pas
r84 r85 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: string; List: TList<string>): string; 68 function Implode(Separator: string; List: TStringList; Around: string = ''): string; 69 function LastPos(const SubStr: String; const S: String): Integer; 70 function LoadFileToStr(const FileName: TFileName): AnsiString; 71 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 72 function MergeArray(A, B: array of string): TStringArray; 73 function OccurenceOfChar(What: Char; Where: string): Integer; 74 procedure OpenWebPage(URL: string); 75 procedure OpenEmail(Email: string); 76 procedure OpenFileInShell(FileName: string); 77 function PosFromIndex(SubStr: string; Text: string; 78 StartIndex: Integer): Integer; 79 function PosFromIndexReverse(SubStr: string; Text: string; 80 StartIndex: Integer): Integer; 81 function RemoveQuotes(Text: string): string; 82 procedure SaveStringToFile(S, FileName: string); 56 83 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload; 57 84 procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload; 58 85 procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload; 59 86 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 87 procedure SearchFiles(AList: TStrings; Dir: string; 78 88 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 79 function GetStringPart(var Text: string; Separator: string): string;89 function SplitString(var Text: string; Count: Word): string; 80 90 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); 91 function TryHexToInt(Data: string; out Value: Integer): Boolean; 92 function TryBinToInt(Data: string; out Value: Integer): Boolean; 93 procedure SortStrings(Strings: TStrings); 86 94 87 95 … … 199 207 end;*) 200 208 209 function Implode(Separator: string; List: TStringList; Around: string = ''): string; 210 var 211 I: Integer; 212 begin 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; 218 end; 219 201 220 function LastPos(const SubStr: String; const S: String): Integer; 202 221 begin … … 244 263 end; 245 264 246 function TryHexToInt(Data: string; varValue: Integer): Boolean;265 function TryHexToInt(Data: string; out Value: Integer): Boolean; 247 266 var 248 267 I: Integer; … … 260 279 end; 261 280 262 function TryBinToInt(Data: string; varValue: Integer): Boolean;281 function TryBinToInt(Data: string; out Value: Integer): Boolean; 263 282 var 264 283 I: Integer; … … 288 307 end; 289 308 290 function Explode(Separator: char; Data: string): TArrayOfString; 291 begin 292 SetLength(Result, 0); 293 while Pos(Separator, Data) > 0 do begin 309 function Explode(Separator: Char; Data: string): TStringArray; 310 var 311 Index: Integer; 312 begin 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 294 323 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} 324 Result[High(Result)] := Data; 325 end; 326 end; 327 328 function Implode(Separator: string; List: TList<string>): string; 329 var 330 I: Integer; 331 begin 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; 337 end; 338 339 {$IFDEF WINDOWS} 303 340 function GetUserName: string; 304 341 const … … 308 345 begin 309 346 L := MAX_USERNAME_LENGTH + 2; 347 Result := Default(string); 310 348 SetLength(Result, L); 311 349 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin … … 321 359 end; 322 360 end; 323 {$ endif}361 {$ENDIF} 324 362 325 363 function ComputerName: string; 326 {$ ifdef mswindows}364 {$IFDEF WINDOWS} 327 365 const 328 366 INFO_BUFFER_SIZE = 32767; … … 339 377 end; 340 378 end; 341 {$ endif}342 {$ ifdef unix}379 {$ENDIF} 380 {$IFDEF UNIX} 343 381 var 344 382 Name: UtsName; 345 383 begin 384 Name := Default(UtsName); 346 385 fpuname(Name); 347 386 Result := Name.Nodename; 348 387 end; 349 {$ endif}350 351 {$ ifdef windows}388 {$ENDIF} 389 390 {$IFDEF WINDOWS} 352 391 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 353 392 const … … 427 466 procedure LoadLibraries; 428 467 begin 429 {$IFDEF W indows}468 {$IFDEF WINDOWS} 430 469 DLLHandle1 := LoadLibrary('secur32.dll'); 431 470 if DLLHandle1 <> 0 then … … 438 477 procedure FreeLibraries; 439 478 begin 440 {$IFDEF W indows}479 {$IFDEF WINDOWS} 441 480 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1); 442 481 {$ENDIF} … … 471 510 end; 472 511 512 procedure OpenEmail(Email: string); 513 begin 514 OpenURL('mailto:' + Email); 515 end; 516 473 517 procedure OpenFileInShell(FileName: string); 474 518 begin … … 499 543 end; 500 544 501 function MergeArray(A, B: array of string): TArrayOfString; 502 var 503 I: Integer; 504 begin 545 function MergeArray(A, B: array of string): TStringArray; 546 var 547 I: Integer; 548 begin 549 Result := Default(TStringArray); 505 550 SetLength(Result, Length(A) + Length(B)); 506 551 for I := 0 to Length(A) - 1 do … … 669 714 end; 670 715 716 function CombinePaths(Path1, Path2: string): string; 717 begin 718 Result := Path1; 719 if Result <> '' then Result := Result + DirectorySeparator + Path2 720 else Result := Path2; 721 end; 722 723 procedure SortStrings(Strings: TStrings); 724 var 725 Tmp: TStringList; 726 begin 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; 744 end; 745 671 746 672 747 initialization
Note:
See TracChangeset
for help on using the changeset viewer.