| 1 | unit Common;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | {$IFDEF WINDOWS}Windows,{$ENDIF}
|
|---|
| 7 | {$IFDEF UNIX}baseunix,{$ENDIF}
|
|---|
| 8 | Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, Graphics,
|
|---|
| 9 | FileUtil, Generics.Collections; //, ShFolder, ShellAPI;
|
|---|
| 10 |
|
|---|
| 11 | type
|
|---|
| 12 | TArrayOfByte = array of Byte;
|
|---|
| 13 | TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
|
|---|
| 14 |
|
|---|
| 15 | TUserNameFormat = (
|
|---|
| 16 | unfNameUnknown = 0, // Unknown name type.
|
|---|
| 17 | unfNameFullyQualifiedDN = 1, // Fully qualified distinguished name
|
|---|
| 18 | unfNameSamCompatible = 2, // Windows NT® 4.0 account name
|
|---|
| 19 | unfNameDisplay = 3, // A "friendly" display name
|
|---|
| 20 | unfNameUniqueId = 6, // GUID string that the IIDFromString function returns
|
|---|
| 21 | unfNameCanonical = 7, // Complete canonical name
|
|---|
| 22 | unfNameUserPrincipal = 8, // User principal name
|
|---|
| 23 | unfNameCanonicalEx = 9,
|
|---|
| 24 | unfNameServicePrincipal = 10, // Generalized service principal name
|
|---|
| 25 | unfDNSDomainName = 11);
|
|---|
| 26 |
|
|---|
| 27 | TFilterMethod = function (FileName: string): Boolean of object;
|
|---|
| 28 | TFileNameMethod = procedure (FileName: string) of object;
|
|---|
| 29 |
|
|---|
| 30 | var
|
|---|
| 31 | ExceptionHandler: TExceptionEvent;
|
|---|
| 32 | DLLHandle1: HModule;
|
|---|
| 33 |
|
|---|
| 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;
|
|---|
| 45 | function BinToInt(BinStr: string): Int64;
|
|---|
| 46 | function BinToHexString(Source: AnsiString): string;
|
|---|
| 47 | //function DelTree(DirName : string): Boolean;
|
|---|
| 48 | //function GetSpecialFolderPath(Folder: Integer): string;
|
|---|
| 49 | function BCDToInt(Value: Byte): Byte;
|
|---|
| 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 EndsWith(Text, What: string): Boolean;
|
|---|
| 56 | function Explode(Separator: Char; Data: string): TStringArray;
|
|---|
| 57 | procedure ExecuteProgram(Executable: string; Parameters: array of string;
|
|---|
| 58 | Environment: array of string; CurrentDirectory: string = '');
|
|---|
| 59 | procedure ExecuteProgramOutput(Executable: string; Parameters: array of string;
|
|---|
| 60 | Environment: array of string; out Output, Error: string;
|
|---|
| 61 | out ExitCode: Integer; CurrentDirectory: string = '');
|
|---|
| 62 | procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
|
|---|
| 63 | procedure FreeThenNil(var Obj);
|
|---|
| 64 | function GetDirCount(Dir: string): Integer;
|
|---|
| 65 | function GetUserName: string;
|
|---|
| 66 | function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
|
|---|
| 67 | function GetBit(Variable: QWord; Index: Byte): Boolean;
|
|---|
| 68 | function GetStringPart(var Text: string; Separator: string): string;
|
|---|
| 69 | function GetEnvironmentVariables: TStringArray;
|
|---|
| 70 | function GenerateNewName(OldName: string): string;
|
|---|
| 71 | function GetFileFilterItemExt(Filter: string; Index: Integer): string;
|
|---|
| 72 | function IntToBin(Data: Int64; Count: Byte): string;
|
|---|
| 73 | function Implode(Separator: string; List: TList<string>): string; overload;
|
|---|
| 74 | function Implode(Separator: string; List: array of string): string; overload;
|
|---|
| 75 | function Implode(Separator: string; List: TStringList; Around: string = ''): string; overload;
|
|---|
| 76 | function LastPos(const SubStr: String; const S: String): Integer;
|
|---|
| 77 | function LoadFileToStr(const FileName: TFileName): AnsiString;
|
|---|
| 78 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
|---|
| 79 | function MergeArray(A, B: array of string): TStringArray;
|
|---|
| 80 | function OccurenceOfChar(What: Char; Where: string): Integer;
|
|---|
| 81 | procedure OpenWebPage(URL: string);
|
|---|
| 82 | procedure OpenEmail(Email: string);
|
|---|
| 83 | procedure OpenFileInShell(FileName: string);
|
|---|
| 84 | function PosFromIndex(SubStr: string; Text: string;
|
|---|
| 85 | StartIndex: Integer): Integer;
|
|---|
| 86 | function PosFromIndexReverse(SubStr: string; Text: string;
|
|---|
| 87 | StartIndex: Integer): Integer;
|
|---|
| 88 | function RemoveQuotes(Text: string): string;
|
|---|
| 89 | procedure SaveStringToFile(S, FileName: string);
|
|---|
| 90 | procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
|
|---|
| 91 | procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
|
|---|
| 92 | procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
|
|---|
| 93 | procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
|
|---|
| 94 | procedure SearchFiles(AList: TStrings; Dir: string;
|
|---|
| 95 | FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
|
|---|
| 96 | procedure SortStrings(Strings: TStrings);
|
|---|
| 97 | function SplitString(var Text: string; Count: Word): string;
|
|---|
| 98 | function StripTags(const S: string): string;
|
|---|
| 99 | function StartsWith(Text, What: string): Boolean;
|
|---|
| 100 | function TryHexToInt(Data: string; out Value: Integer): Boolean;
|
|---|
| 101 | function TryBinToInt(Data: string; out Value: Integer): Boolean;
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | implementation
|
|---|
| 105 |
|
|---|
| 106 | resourcestring
|
|---|
| 107 | SExecutionError = 'Excution error: %s (exit code: %d)';
|
|---|
| 108 |
|
|---|
| 109 | function StartsWith(Text, What: string): Boolean;
|
|---|
| 110 | begin
|
|---|
| 111 | Result := Copy(Text, 1, Length(Text)) = What;
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 | function EndsWith(Text, What: string): Boolean;
|
|---|
| 115 | begin
|
|---|
| 116 | Result := Copy(Text, Length(Text) - Length(What) + 1, MaxInt) = What;
|
|---|
| 117 | end;
|
|---|
| 118 |
|
|---|
| 119 | function BinToInt(BinStr: string): Int64;
|
|---|
| 120 | var
|
|---|
| 121 | I: Byte;
|
|---|
| 122 | RetVar: Int64;
|
|---|
| 123 | begin
|
|---|
| 124 | BinStr := UpperCase(BinStr);
|
|---|
| 125 | if BinStr[length(BinStr)] = 'B' then Delete(BinStr, Length(BinStr), 1);
|
|---|
| 126 | RetVar := 0;
|
|---|
| 127 | for I := 1 to Length(BinStr) do begin
|
|---|
| 128 | if not (BinStr[I] in ['0','1']) then begin
|
|---|
| 129 | RetVar := 0;
|
|---|
| 130 | Break;
|
|---|
| 131 | end;
|
|---|
| 132 | RetVar := (RetVar shl 1) + (Byte(BinStr[I]) and 1);
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | Result := RetVar;
|
|---|
| 136 | end;
|
|---|
| 137 |
|
|---|
| 138 | function BinToHexString(Source: AnsiString): string;
|
|---|
| 139 | var
|
|---|
| 140 | I: Integer;
|
|---|
| 141 | begin
|
|---|
| 142 | Result := '';
|
|---|
| 143 | for I := 1 to Length(Source) do begin
|
|---|
| 144 | Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
|
|---|
| 145 | end;
|
|---|
| 146 | end;
|
|---|
| 147 |
|
|---|
| 148 | procedure DeleteFiles(APath, AFileSpec: string);
|
|---|
| 149 | var
|
|---|
| 150 | SearchRec: TSearchRec;
|
|---|
| 151 | Find: Integer;
|
|---|
| 152 | Path: string;
|
|---|
| 153 | begin
|
|---|
| 154 | Path := IncludeTrailingPathDelimiter(APath);
|
|---|
| 155 |
|
|---|
| 156 | Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
|
|---|
| 157 | while Find = 0 do begin
|
|---|
| 158 | DeleteFile(Path + SearchRec.Name);
|
|---|
| 159 |
|
|---|
| 160 | Find := SysUtils.FindNext(SearchRec);
|
|---|
| 161 | end;
|
|---|
| 162 | FindClose(SearchRec);
|
|---|
| 163 | end;
|
|---|
| 164 |
|
|---|
| 165 | function GetFileFilterItemExt(Filter: string; Index: Integer): string;
|
|---|
| 166 | var
|
|---|
| 167 | List: TStringList;
|
|---|
| 168 | begin
|
|---|
| 169 | try
|
|---|
| 170 | List := TStringList.Create;
|
|---|
| 171 | List.Text := StringReplace(Filter, '|', #10, [rfReplaceAll]);
|
|---|
| 172 | Result := List[Index * 2 + 1];
|
|---|
| 173 | finally
|
|---|
| 174 | List.Free;
|
|---|
| 175 | end;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
|
|---|
| 179 | var
|
|---|
| 180 | FileExt: string;
|
|---|
| 181 | begin
|
|---|
| 182 | FileExt := GetFileFilterItemExt(FileDialog.Filter, FileDialog.FilterIndex - 1);
|
|---|
| 183 | Delete(FileExt, 1, 1); // Remove symbol '*'
|
|---|
| 184 | if FileExt <> '.*' then
|
|---|
| 185 | FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt)
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | function GetEnvironmentVariables: TStringArray;
|
|---|
| 189 | var
|
|---|
| 190 | I: Integer;
|
|---|
| 191 | begin
|
|---|
| 192 | Result := Default(TStringArray);
|
|---|
| 193 | SetLength(Result, GetEnvironmentVariableCount);
|
|---|
| 194 | for I := 0 to GetEnvironmentVariableCount - 1 do
|
|---|
| 195 | Result[I] := GetEnvironmentString(I);
|
|---|
| 196 | end;
|
|---|
| 197 |
|
|---|
| 198 | function GenerateNewName(OldName: string): string;
|
|---|
| 199 | var
|
|---|
| 200 | I: Integer;
|
|---|
| 201 | Number: Integer;
|
|---|
| 202 | begin
|
|---|
| 203 | Number := 1;
|
|---|
| 204 | // Find number on end
|
|---|
| 205 | if Length(OldName) > 0 then begin
|
|---|
| 206 | I := Length(OldName);
|
|---|
| 207 | while (I > 1) and ((OldName[I] >= '0') and (OldName[I] <= '9')) do Dec(I);
|
|---|
| 208 | TryStrToInt(Copy(OldName, I + 1, Length(OldName) - I), Number);
|
|---|
| 209 | Inc(Number)
|
|---|
| 210 | end;
|
|---|
| 211 | Result := Copy(OldName, 1, I) + IntToStr(Number);
|
|---|
| 212 | end;
|
|---|
| 213 |
|
|---|
| 214 | (*function DelTree(DirName : string): Boolean;
|
|---|
| 215 | var
|
|---|
| 216 | SHFileOpStruct : TSHFileOpStruct;
|
|---|
| 217 | DirBuf : array [0..255] of char;
|
|---|
| 218 | begin
|
|---|
| 219 | DirName := UTF8Decode(DirName);
|
|---|
| 220 | try
|
|---|
| 221 | Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
|
|---|
| 222 | FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
|
|---|
| 223 | StrPCopy(DirBuf, DirName) ;
|
|---|
| 224 | with SHFileOpStruct do begin
|
|---|
| 225 | Wnd := 0;
|
|---|
| 226 | pFrom := @DirBuf;
|
|---|
| 227 | wFunc := FO_DELETE;
|
|---|
| 228 | fFlags := FOF_ALLOWUNDO;
|
|---|
| 229 | fFlags := fFlags or FOF_NOCONFIRMATION;
|
|---|
| 230 | fFlags := fFlags or FOF_SILENT;
|
|---|
| 231 | end;
|
|---|
| 232 | Result := (SHFileOperation(SHFileOpStruct) = 0) ;
|
|---|
| 233 | except
|
|---|
| 234 | Result := False;
|
|---|
| 235 | end;
|
|---|
| 236 | end;*)
|
|---|
| 237 |
|
|---|
| 238 | function Implode(Separator: string; List: array of string): string;
|
|---|
| 239 | var
|
|---|
| 240 | I: Integer;
|
|---|
| 241 | begin
|
|---|
| 242 | Result := '';
|
|---|
| 243 | for I := 0 to Length(List) - 1 do begin
|
|---|
| 244 | Result := Result + List[I];
|
|---|
| 245 | if I < Length(List) - 1 then Result := Result + Separator;
|
|---|
| 246 | end;
|
|---|
| 247 | end;
|
|---|
| 248 |
|
|---|
| 249 | function Implode(Separator: string; List: TStringList; Around: string = ''): string;
|
|---|
| 250 | var
|
|---|
| 251 | I: Integer;
|
|---|
| 252 | begin
|
|---|
| 253 | Result := '';
|
|---|
| 254 | for I := 0 to List.Count - 1 do begin
|
|---|
| 255 | Result := Result + Around + List[I] + Around;
|
|---|
| 256 | if I < List.Count - 1 then Result := Result + Separator;
|
|---|
| 257 | end;
|
|---|
| 258 | end;
|
|---|
| 259 |
|
|---|
| 260 | function LastPos(const SubStr: String; const S: String): Integer;
|
|---|
| 261 | begin
|
|---|
| 262 | Result := Pos(ReverseString(SubStr), ReverseString(S));
|
|---|
| 263 | if (Result <> 0) then
|
|---|
| 264 | Result := ((Length(S) - Length(SubStr)) + 1) - Result + 1;
|
|---|
| 265 | end;
|
|---|
| 266 |
|
|---|
| 267 | function BCDToInt(Value: Byte): Byte;
|
|---|
| 268 | begin
|
|---|
| 269 | Result := (Value shr 4) * 10 + (Value and 15);
|
|---|
| 270 | end;
|
|---|
| 271 |
|
|---|
| 272 | (*function GetSpecialFolderPath(Folder: Integer): string;
|
|---|
| 273 | const
|
|---|
| 274 | SHGFP_TYPE_CURRENT = 0;
|
|---|
| 275 | var
|
|---|
| 276 | Path: array[0..MAX_PATH] of Char;
|
|---|
| 277 | begin
|
|---|
| 278 | Result := 'C:\Test';
|
|---|
| 279 | if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @path[0])) then
|
|---|
| 280 | Result := path
|
|---|
| 281 | else
|
|---|
| 282 | Result := '';
|
|---|
| 283 | end;*)
|
|---|
| 284 |
|
|---|
| 285 | function IntToBin(Data: Int64; Count: Byte): string;
|
|---|
| 286 | var
|
|---|
| 287 | I: Integer;
|
|---|
| 288 | begin
|
|---|
| 289 | Result := '';
|
|---|
| 290 | for I := 0 to Count - 1 do
|
|---|
| 291 | Result := IntToStr((Data shr I) and 1) + Result;
|
|---|
| 292 | end;
|
|---|
| 293 |
|
|---|
| 294 | function IntToHex(Data: Cardinal; Count: Byte): string;
|
|---|
| 295 | const
|
|---|
| 296 | Chars: array[0..15] of Char = '0123456789ABCDEF';
|
|---|
| 297 | var
|
|---|
| 298 | I: Integer;
|
|---|
| 299 | begin
|
|---|
| 300 | Result := '';
|
|---|
| 301 | for I := 0 to Count - 1 do
|
|---|
| 302 | Result := Result + Chars[(Data shr (I * 4)) and 15];
|
|---|
| 303 | end;
|
|---|
| 304 |
|
|---|
| 305 | function TryHexToInt(Data: string; out Value: Integer): Boolean;
|
|---|
| 306 | var
|
|---|
| 307 | I: Integer;
|
|---|
| 308 | begin
|
|---|
| 309 | Data := UpperCase(Data);
|
|---|
| 310 | Result := True;
|
|---|
| 311 | Value := 0;
|
|---|
| 312 | for I := 0 to Length(Data) - 1 do begin
|
|---|
| 313 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
|
|---|
| 314 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
|
|---|
| 315 | else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
|
|---|
| 316 | Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
|
|---|
| 317 | else Result := False;
|
|---|
| 318 | end;
|
|---|
| 319 | end;
|
|---|
| 320 |
|
|---|
| 321 | function TryBinToInt(Data: string; out Value: Integer): Boolean;
|
|---|
| 322 | var
|
|---|
| 323 | I: Integer;
|
|---|
| 324 | begin
|
|---|
| 325 | Result := True;
|
|---|
| 326 | Value := 0;
|
|---|
| 327 | for I := 0 to Length(Data) - 1 do begin
|
|---|
| 328 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '1') then
|
|---|
| 329 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1))
|
|---|
| 330 | else Result := False;
|
|---|
| 331 | end;
|
|---|
| 332 | end;
|
|---|
| 333 |
|
|---|
| 334 | function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
|
|---|
| 335 | var
|
|---|
| 336 | I: Integer;
|
|---|
| 337 | begin
|
|---|
| 338 | if Length(Data1) = Length(Data2) then begin
|
|---|
| 339 | Result := True;
|
|---|
| 340 | for I := 0 to Length(Data1) - 1 do begin
|
|---|
| 341 | if Data1[I] <> Data2[I] then begin
|
|---|
| 342 | Result := False;
|
|---|
| 343 | Break;
|
|---|
| 344 | end
|
|---|
| 345 | end;
|
|---|
| 346 | end else Result := False;
|
|---|
| 347 | end;
|
|---|
| 348 |
|
|---|
| 349 | function Explode(Separator: Char; Data: string): TStringArray;
|
|---|
| 350 | var
|
|---|
| 351 | Index: Integer;
|
|---|
| 352 | begin
|
|---|
| 353 | Result := Default(TStringArray);
|
|---|
| 354 | repeat
|
|---|
| 355 | Index := Pos(Separator, Data);
|
|---|
| 356 | if Index > 0 then begin
|
|---|
| 357 | SetLength(Result, Length(Result) + 1);
|
|---|
| 358 | Result[High(Result)] := Copy(Data, 1, Index - 1);
|
|---|
| 359 | Delete(Data, 1, Index);
|
|---|
| 360 | end else Break;
|
|---|
| 361 | until False;
|
|---|
| 362 | if Data <> '' then begin
|
|---|
| 363 | SetLength(Result, Length(Result) + 1);
|
|---|
| 364 | Result[High(Result)] := Data;
|
|---|
| 365 | end;
|
|---|
| 366 | end;
|
|---|
| 367 |
|
|---|
| 368 | function Implode(Separator: string; List: TList<string>): string;
|
|---|
| 369 | var
|
|---|
| 370 | I: Integer;
|
|---|
| 371 | begin
|
|---|
| 372 | Result := '';
|
|---|
| 373 | for I := 0 to List.Count - 1 do begin
|
|---|
| 374 | Result := Result + List[I];
|
|---|
| 375 | if I < List.Count - 1 then Result := Result + Separator;
|
|---|
| 376 | end;
|
|---|
| 377 | end;
|
|---|
| 378 |
|
|---|
| 379 | {$IFDEF WINDOWS}
|
|---|
| 380 | function GetUserName: string;
|
|---|
| 381 | const
|
|---|
| 382 | MAX_USERNAME_LENGTH = 256;
|
|---|
| 383 | var
|
|---|
| 384 | L: LongWord;
|
|---|
| 385 | begin
|
|---|
| 386 | L := MAX_USERNAME_LENGTH + 2;
|
|---|
| 387 | Result := Default(string);
|
|---|
| 388 | SetLength(Result, L);
|
|---|
| 389 | if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
|
|---|
| 390 | SetLength(Result, StrLen(PChar(Result)));
|
|---|
| 391 | Result := UTF8Encode(Result);
|
|---|
| 392 | end else Result := '';
|
|---|
| 393 | end;
|
|---|
| 394 |
|
|---|
| 395 | function GetVersionInfo: TOSVersionInfo;
|
|---|
| 396 | begin
|
|---|
| 397 | Result.dwOSVersionInfoSize := SizeOf(Result);
|
|---|
| 398 | if GetVersionEx(Result) then begin
|
|---|
| 399 | end;
|
|---|
| 400 | end;
|
|---|
| 401 | {$ENDIF}
|
|---|
| 402 |
|
|---|
| 403 | function ComputerName: string;
|
|---|
| 404 | {$IFDEF WINDOWS}
|
|---|
| 405 | const
|
|---|
| 406 | INFO_BUFFER_SIZE = 32767;
|
|---|
| 407 | var
|
|---|
| 408 | Buffer : array[0..INFO_BUFFER_SIZE] of WideChar;
|
|---|
| 409 | Ret : DWORD;
|
|---|
| 410 | begin
|
|---|
| 411 | Ret := INFO_BUFFER_SIZE;
|
|---|
| 412 | If (GetComputerNameW(@Buffer[0],Ret)) then begin
|
|---|
| 413 | Result := UTF8Encode(WideString(Buffer));
|
|---|
| 414 | end
|
|---|
| 415 | else begin
|
|---|
| 416 | Result := 'ERROR_NO_COMPUTERNAME_RETURNED';
|
|---|
| 417 | end;
|
|---|
| 418 | end;
|
|---|
| 419 | {$ENDIF}
|
|---|
| 420 | {$IFDEF UNIX}
|
|---|
| 421 | var
|
|---|
| 422 | Name: UtsName;
|
|---|
| 423 | begin
|
|---|
| 424 | Name := Default(UtsName);
|
|---|
| 425 | fpuname(Name);
|
|---|
| 426 | Result := Name.Nodename;
|
|---|
| 427 | end;
|
|---|
| 428 | {$ENDIF}
|
|---|
| 429 |
|
|---|
| 430 | {$IFDEF WINDOWS}
|
|---|
| 431 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
|---|
| 432 | const
|
|---|
| 433 | MaxLength = 1000;
|
|---|
| 434 | var
|
|---|
| 435 | UserName: array[0..MaxLength] of Char;
|
|---|
| 436 | VersionInfo: TOSVersionInfo;
|
|---|
| 437 | Size: DWORD;
|
|---|
| 438 | begin
|
|---|
| 439 | VersionInfo := GetVersionInfo;
|
|---|
| 440 | if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
|
|---|
| 441 | Size := MaxLength;
|
|---|
| 442 | GetUserNameEx(Integer(Format), @UserName, @Size);
|
|---|
| 443 | //ShowMessage(SysErrorMessage(GetLastError));
|
|---|
| 444 | if GetLastError = 0 then Result := UTF8Encode(UserName)
|
|---|
| 445 | else Result := GetUserName;
|
|---|
| 446 | end else Result := GetUserName;
|
|---|
| 447 | end;
|
|---|
| 448 | {$ELSE}
|
|---|
| 449 | function GetUserName: string;
|
|---|
| 450 | begin
|
|---|
| 451 | Result := '';
|
|---|
| 452 | end;
|
|---|
| 453 |
|
|---|
| 454 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
|---|
| 455 | begin
|
|---|
| 456 | Result := '';
|
|---|
| 457 | end;
|
|---|
| 458 |
|
|---|
| 459 | {$ENDIF}
|
|---|
| 460 |
|
|---|
| 461 | function SplitString(var Text: string; Count: Word): string;
|
|---|
| 462 | begin
|
|---|
| 463 | Result := Copy(Text, 1, Count);
|
|---|
| 464 | Delete(Text, 1, Count);
|
|---|
| 465 | end;
|
|---|
| 466 |
|
|---|
| 467 | function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
|
|---|
| 468 | var
|
|---|
| 469 | I: Integer;
|
|---|
| 470 | begin
|
|---|
| 471 | Result := 0;
|
|---|
| 472 | for I := 0 to MaxIndex - 1 do
|
|---|
| 473 | if ((Variable shr I) and 1) = 1 then Inc(Result);
|
|---|
| 474 | end;
|
|---|
| 475 |
|
|---|
| 476 | function GetBit(Variable:QWord;Index:Byte):Boolean;
|
|---|
| 477 | begin
|
|---|
| 478 | Result := ((Variable shr Index) and 1) = 1;
|
|---|
| 479 | end;
|
|---|
| 480 |
|
|---|
| 481 | procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean);
|
|---|
| 482 | begin
|
|---|
| 483 | Variable := (Variable and ((1 shl Index) xor High(QWord))) or (Int64(State) shl Index);
|
|---|
| 484 | end;
|
|---|
| 485 |
|
|---|
| 486 | procedure SetBit(var Variable:QWord;Index:Byte;State:Boolean); overload;
|
|---|
| 487 | begin
|
|---|
| 488 | Variable := (Variable and ((1 shl Index) xor High(QWord))) or (QWord(State) shl Index);
|
|---|
| 489 | end;
|
|---|
| 490 |
|
|---|
| 491 | procedure SetBit(var Variable:Cardinal;Index:Byte;State:Boolean); overload;
|
|---|
| 492 | begin
|
|---|
| 493 | Variable := (Variable and ((1 shl Index) xor High(Cardinal))) or (Cardinal(State) shl Index);
|
|---|
| 494 | end;
|
|---|
| 495 |
|
|---|
| 496 | procedure SetBit(var Variable:Word;Index:Byte;State:Boolean); overload;
|
|---|
| 497 | begin
|
|---|
| 498 | Variable := (Variable and ((1 shl Index) xor High(Word))) or (Word(State) shl Index);
|
|---|
| 499 | end;
|
|---|
| 500 |
|
|---|
| 501 | function AddLeadingZeroes(const aNumber, Length : integer) : string;
|
|---|
| 502 | begin
|
|---|
| 503 | Result := SysUtils.Format('%.*d', [Length, aNumber]) ;
|
|---|
| 504 | end;
|
|---|
| 505 |
|
|---|
| 506 | procedure LoadLibraries;
|
|---|
| 507 | begin
|
|---|
| 508 | {$IFDEF WINDOWS}
|
|---|
| 509 | DLLHandle1 := LoadLibrary('secur32.dll');
|
|---|
| 510 | if DLLHandle1 <> 0 then
|
|---|
| 511 | begin
|
|---|
| 512 | @GetUserNameEx := GetProcAddress(DLLHandle1, 'GetUserNameExA');
|
|---|
| 513 | end;
|
|---|
| 514 | {$ENDIF}
|
|---|
| 515 | end;
|
|---|
| 516 |
|
|---|
| 517 | procedure FreeLibraries;
|
|---|
| 518 | begin
|
|---|
| 519 | {$IFDEF WINDOWS}
|
|---|
| 520 | if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
|
|---|
| 521 | {$ENDIF}
|
|---|
| 522 | end;
|
|---|
| 523 |
|
|---|
| 524 | procedure ExecuteProgram(Executable: string; Parameters: array of string;
|
|---|
| 525 | Environment: array of string; CurrentDirectory: string = '');
|
|---|
| 526 | var
|
|---|
| 527 | Process: TProcess;
|
|---|
| 528 | I: Integer;
|
|---|
| 529 | begin
|
|---|
| 530 | Process := TProcess.Create(nil);
|
|---|
| 531 | try
|
|---|
| 532 | Process.Executable := Executable;
|
|---|
| 533 | for I := 0 to Length(Parameters) - 1 do
|
|---|
| 534 | Process.Parameters.Add(Parameters[I]);
|
|---|
| 535 | for I := 0 to Length(Environment) - 1 do
|
|---|
| 536 | Process.Environment.Add(Environment[I]);
|
|---|
| 537 | Process.CurrentDirectory := CurrentDirectory;
|
|---|
| 538 | Process.ShowWindow := swoHIDE;
|
|---|
| 539 | Process.Options := [poNoConsole];
|
|---|
| 540 | Process.Execute;
|
|---|
| 541 | finally
|
|---|
| 542 | Process.Free;
|
|---|
| 543 | end;
|
|---|
| 544 | end;
|
|---|
| 545 |
|
|---|
| 546 | procedure ExecuteProgramOutput(Executable: string; Parameters: array of string;
|
|---|
| 547 | Environment: array of string; out Output, Error: string; out ExitCode: Integer;
|
|---|
| 548 | CurrentDirectory: string);
|
|---|
| 549 | var
|
|---|
| 550 | Process: TProcess;
|
|---|
| 551 | I: Integer;
|
|---|
| 552 | ReadCount: Integer;
|
|---|
| 553 | Buffer: string;
|
|---|
| 554 | const
|
|---|
| 555 | BufferSize = 1000;
|
|---|
| 556 | begin
|
|---|
| 557 | Process := TProcess.Create(nil);
|
|---|
| 558 | try
|
|---|
| 559 | Process.Executable := Executable;
|
|---|
| 560 | for I := 0 to Length(Parameters) - 1 do
|
|---|
| 561 | Process.Parameters.Add(Parameters[I]);
|
|---|
| 562 | for I := 0 to Length(Environment) - 1 do
|
|---|
| 563 | Process.Environment.Add(Environment[I]);
|
|---|
| 564 | Process.CurrentDirectory := CurrentDirectory;
|
|---|
| 565 | Process.ShowWindow := swoHIDE;
|
|---|
| 566 | Process.Options := [poNoConsole, poUsePipes];
|
|---|
| 567 | Process.Execute;
|
|---|
| 568 |
|
|---|
| 569 | Output := '';
|
|---|
| 570 | Error := '';
|
|---|
| 571 | Buffer := '';
|
|---|
| 572 | SetLength(Buffer, BufferSize);
|
|---|
| 573 | while Process.Running do begin
|
|---|
| 574 | if Process.Output.NumBytesAvailable > 0 then begin
|
|---|
| 575 | ReadCount := Process.Output.Read(Buffer[1], Length(Buffer));
|
|---|
| 576 | Output := Output + Copy(Buffer, 1, ReadCount);
|
|---|
| 577 | end;
|
|---|
| 578 |
|
|---|
| 579 | if Process.Stderr.NumBytesAvailable > 0 then begin
|
|---|
| 580 | ReadCount := Process.Stderr.Read(Buffer[1], Length(Buffer));
|
|---|
| 581 | Error := Error + Copy(Buffer, 1, ReadCount)
|
|---|
| 582 | end;
|
|---|
| 583 |
|
|---|
| 584 | Sleep(10);
|
|---|
| 585 | end;
|
|---|
| 586 |
|
|---|
| 587 | if Process.Output.NumBytesAvailable > 0 then begin
|
|---|
| 588 | ReadCount := Process.Output.Read(Buffer[1], Length(Buffer));
|
|---|
| 589 | Output := Output + Copy(Buffer, 1, ReadCount);
|
|---|
| 590 | end;
|
|---|
| 591 |
|
|---|
| 592 | if Process.Stderr.NumBytesAvailable > 0 then begin
|
|---|
| 593 | ReadCount := Process.Stderr.Read(Buffer[1], Length(Buffer));
|
|---|
| 594 | Error := Error + Copy(Buffer, 1, ReadCount);
|
|---|
| 595 | end;
|
|---|
| 596 |
|
|---|
| 597 | ExitCode := Process.ExitCode;
|
|---|
| 598 |
|
|---|
| 599 | if (ExitCode <> 0) or (Error <> '') then
|
|---|
| 600 | raise Exception.Create(Format(SExecutionError, [Output + Error, ExitCode]));
|
|---|
| 601 | finally
|
|---|
| 602 | Process.Free;
|
|---|
| 603 | end;
|
|---|
| 604 | end;
|
|---|
| 605 |
|
|---|
| 606 | procedure FreeThenNil(var Obj);
|
|---|
| 607 | begin
|
|---|
| 608 | TObject(Obj).Free;
|
|---|
| 609 | TObject(Obj) := nil;
|
|---|
| 610 | end;
|
|---|
| 611 |
|
|---|
| 612 | procedure OpenWebPage(URL: string);
|
|---|
| 613 | begin
|
|---|
| 614 | OpenURL(URL);
|
|---|
| 615 | end;
|
|---|
| 616 |
|
|---|
| 617 | procedure OpenEmail(Email: string);
|
|---|
| 618 | begin
|
|---|
| 619 | OpenURL('mailto:' + Email);
|
|---|
| 620 | end;
|
|---|
| 621 |
|
|---|
| 622 | procedure OpenFileInShell(FileName: string);
|
|---|
| 623 | begin
|
|---|
| 624 | ExecuteProgram('cmd.exe', ['/c', 'start', FileName], []);
|
|---|
| 625 | end;
|
|---|
| 626 |
|
|---|
| 627 | function RemoveQuotes(Text: string): string;
|
|---|
| 628 | begin
|
|---|
| 629 | Result := Text;
|
|---|
| 630 | if (Pos('"', Text) = 1) and (Text[Length(Text)] = '"') then
|
|---|
| 631 | Result := Copy(Text, 2, Length(Text) - 2);
|
|---|
| 632 | end;
|
|---|
| 633 |
|
|---|
| 634 | function OccurenceOfChar(What: Char; Where: string): Integer;
|
|---|
| 635 | var
|
|---|
| 636 | I: Integer;
|
|---|
| 637 | begin
|
|---|
| 638 | Result := 0;
|
|---|
| 639 | for I := 1 to Length(Where) do
|
|---|
| 640 | if Where[I] = What then Inc(Result);
|
|---|
| 641 | end;
|
|---|
| 642 |
|
|---|
| 643 | function GetDirCount(Dir: string): Integer;
|
|---|
| 644 | begin
|
|---|
| 645 | Result := OccurenceOfChar(DirectorySeparator, Dir);
|
|---|
| 646 | if Copy(Dir, Length(Dir), 1) = DirectorySeparator then
|
|---|
| 647 | Dec(Result);
|
|---|
| 648 | end;
|
|---|
| 649 |
|
|---|
| 650 | function MergeArray(A, B: array of string): TStringArray;
|
|---|
| 651 | var
|
|---|
| 652 | I: Integer;
|
|---|
| 653 | begin
|
|---|
| 654 | Result := Default(TStringArray);
|
|---|
| 655 | SetLength(Result, Length(A) + Length(B));
|
|---|
| 656 | for I := 0 to Length(A) - 1 do
|
|---|
| 657 | Result[I] := A[I];
|
|---|
| 658 | for I := 0 to Length(B) - 1 do
|
|---|
| 659 | Result[Length(A) + I] := B[I];
|
|---|
| 660 | end;
|
|---|
| 661 |
|
|---|
| 662 | function LoadFileToStr(const FileName: TFileName): AnsiString;
|
|---|
| 663 | var
|
|---|
| 664 | FileStream: TFileStream;
|
|---|
| 665 | Read: Integer;
|
|---|
| 666 | begin
|
|---|
| 667 | Result := '';
|
|---|
| 668 | FileStream := TFileStream.Create(FileName, fmOpenRead);
|
|---|
| 669 | try
|
|---|
| 670 | if FileStream.Size > 0 then begin
|
|---|
| 671 | SetLength(Result, FileStream.Size);
|
|---|
| 672 | Read := FileStream.Read(Pointer(Result)^, FileStream.Size);
|
|---|
| 673 | SetLength(Result, Read);
|
|---|
| 674 | end;
|
|---|
| 675 | finally
|
|---|
| 676 | FileStream.Free;
|
|---|
| 677 | end;
|
|---|
| 678 | end;
|
|---|
| 679 |
|
|---|
| 680 | function DefaultSearchFilter(const FileName: string): Boolean;
|
|---|
| 681 | begin
|
|---|
| 682 | Result := True;
|
|---|
| 683 | end;
|
|---|
| 684 |
|
|---|
| 685 | procedure SaveStringToFile(S, FileName: string);
|
|---|
| 686 | var
|
|---|
| 687 | F: TextFile;
|
|---|
| 688 | begin
|
|---|
| 689 | AssignFile(F, FileName);
|
|---|
| 690 | try
|
|---|
| 691 | ReWrite(F);
|
|---|
| 692 | Write(F, S);
|
|---|
| 693 | finally
|
|---|
| 694 | CloseFile(F);
|
|---|
| 695 | end;
|
|---|
| 696 | end;
|
|---|
| 697 |
|
|---|
| 698 | procedure SearchFiles(AList: TStrings; Dir: string;
|
|---|
| 699 | FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
|
|---|
| 700 | var
|
|---|
| 701 | SR: TSearchRec;
|
|---|
| 702 | begin
|
|---|
| 703 | Dir := IncludeTrailingPathDelimiter(Dir);
|
|---|
| 704 | if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
|
|---|
| 705 | try
|
|---|
| 706 | repeat
|
|---|
| 707 | if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
|
|---|
| 708 | not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
|
|---|
| 709 | if Assigned(FileNameMethod) then
|
|---|
| 710 | FileNameMethod(Dir + SR.Name);
|
|---|
| 711 | AList.Add(Dir + SR.Name);
|
|---|
| 712 | if (SR.Attr and faDirectory) <> 0 then
|
|---|
| 713 | SearchFiles(AList, Dir + SR.Name, FilterMethod);
|
|---|
| 714 | until FindNext(SR) <> 0;
|
|---|
| 715 | finally
|
|---|
| 716 | FindClose(SR);
|
|---|
| 717 | end;
|
|---|
| 718 | end;
|
|---|
| 719 |
|
|---|
| 720 | function GetStringPart(var Text: string; Separator: string): string;
|
|---|
| 721 | var
|
|---|
| 722 | P: Integer;
|
|---|
| 723 | begin
|
|---|
| 724 | P := Pos(Separator, Text);
|
|---|
| 725 | if P > 0 then begin
|
|---|
| 726 | Result := Copy(Text, 1, P - 1);
|
|---|
| 727 | Delete(Text, 1, P - 1 + Length(Separator));
|
|---|
| 728 | end else begin
|
|---|
| 729 | Result := Text;
|
|---|
| 730 | Text := '';
|
|---|
| 731 | end;
|
|---|
| 732 | Result := Trim(Result);
|
|---|
| 733 | Text := Trim(Text);
|
|---|
| 734 | end;
|
|---|
| 735 |
|
|---|
| 736 | function StripTags(const S: string): string;
|
|---|
| 737 | var
|
|---|
| 738 | Len: Integer;
|
|---|
| 739 |
|
|---|
| 740 | function ReadUntil(const ReadFrom: Integer; const C: Char): Integer;
|
|---|
| 741 | var
|
|---|
| 742 | J: Integer;
|
|---|
| 743 | begin
|
|---|
| 744 | for J := ReadFrom to Len do
|
|---|
| 745 | if (S[j] = C) then
|
|---|
| 746 | begin
|
|---|
| 747 | Result := J;
|
|---|
| 748 | Exit;
|
|---|
| 749 | end;
|
|---|
| 750 | Result := Len + 1;
|
|---|
| 751 | end;
|
|---|
| 752 |
|
|---|
| 753 | var
|
|---|
| 754 | I, APos: Integer;
|
|---|
| 755 | begin
|
|---|
| 756 | Len := Length(S);
|
|---|
| 757 | I := 0;
|
|---|
| 758 | Result := '';
|
|---|
| 759 | while (I <= Len) do begin
|
|---|
| 760 | Inc(I);
|
|---|
| 761 | APos := ReadUntil(I, '<');
|
|---|
| 762 | Result := Result + Copy(S, I, APos - i);
|
|---|
| 763 | I := ReadUntil(APos + 1, '>');
|
|---|
| 764 | end;
|
|---|
| 765 | end;
|
|---|
| 766 |
|
|---|
| 767 | function PosFromIndex(SubStr: string; Text: string;
|
|---|
| 768 | StartIndex: Integer): Integer;
|
|---|
| 769 | var
|
|---|
| 770 | I, MaxLen: SizeInt;
|
|---|
| 771 | Ptr: PAnsiChar;
|
|---|
| 772 | begin
|
|---|
| 773 | Result := 0;
|
|---|
| 774 | if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
|
|---|
| 775 | if Length(SubStr) > 0 then begin
|
|---|
| 776 | MaxLen := Length(Text) - Length(SubStr) + 1;
|
|---|
| 777 | I := StartIndex;
|
|---|
| 778 | Ptr := @Text[StartIndex];
|
|---|
| 779 | while (I <= MaxLen) do begin
|
|---|
| 780 | if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
|
|---|
| 781 | Result := I;
|
|---|
| 782 | Exit;
|
|---|
| 783 | end;
|
|---|
| 784 | Inc(I);
|
|---|
| 785 | Inc(Ptr);
|
|---|
| 786 | end;
|
|---|
| 787 | end;
|
|---|
| 788 | end;
|
|---|
| 789 |
|
|---|
| 790 | function PosFromIndexReverse(SubStr: string; Text: string;
|
|---|
| 791 | StartIndex: Integer): Integer;
|
|---|
| 792 | var
|
|---|
| 793 | I: SizeInt;
|
|---|
| 794 | Ptr: PAnsiChar;
|
|---|
| 795 | begin
|
|---|
| 796 | Result := 0;
|
|---|
| 797 | if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
|
|---|
| 798 | if Length(SubStr) > 0 then begin
|
|---|
| 799 | I := StartIndex;
|
|---|
| 800 | Ptr := @Text[StartIndex];
|
|---|
| 801 | while (I > 0) do begin
|
|---|
| 802 | if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
|
|---|
| 803 | Result := I;
|
|---|
| 804 | Exit;
|
|---|
| 805 | end;
|
|---|
| 806 | Dec(I);
|
|---|
| 807 | Dec(Ptr);
|
|---|
| 808 | end;
|
|---|
| 809 | end;
|
|---|
| 810 | end;
|
|---|
| 811 |
|
|---|
| 812 | procedure CopyStringArray(Dest: TStringArray; Source: array of string);
|
|---|
| 813 | var
|
|---|
| 814 | I: Integer;
|
|---|
| 815 | begin
|
|---|
| 816 | SetLength(Dest, Length(Source));
|
|---|
| 817 | for I := 0 to Length(Dest) - 1 do
|
|---|
| 818 | Dest[I] := Source[I];
|
|---|
| 819 | end;
|
|---|
| 820 |
|
|---|
| 821 | function CombinePaths(Path1, Path2: string): string;
|
|---|
| 822 | begin
|
|---|
| 823 | Result := Path1;
|
|---|
| 824 | if Result <> '' then Result := Result + DirectorySeparator + Path2
|
|---|
| 825 | else Result := Path2;
|
|---|
| 826 | end;
|
|---|
| 827 |
|
|---|
| 828 | procedure SortStrings(Strings: TStrings);
|
|---|
| 829 | var
|
|---|
| 830 | Tmp: TStringList;
|
|---|
| 831 | begin
|
|---|
| 832 | Strings.BeginUpdate;
|
|---|
| 833 | try
|
|---|
| 834 | if Strings is TStringList then begin
|
|---|
| 835 | TStringList(Strings).Sort;
|
|---|
| 836 | end else begin
|
|---|
| 837 | Tmp := TStringList.Create;
|
|---|
| 838 | try
|
|---|
| 839 | Tmp.Assign(Strings);
|
|---|
| 840 | Tmp.Sort;
|
|---|
| 841 | Strings.Assign(Tmp);
|
|---|
| 842 | finally
|
|---|
| 843 | Tmp.Free;
|
|---|
| 844 | end;
|
|---|
| 845 | end;
|
|---|
| 846 | finally
|
|---|
| 847 | Strings.EndUpdate;
|
|---|
| 848 | end;
|
|---|
| 849 | end;
|
|---|
| 850 |
|
|---|
| 851 |
|
|---|
| 852 | initialization
|
|---|
| 853 |
|
|---|
| 854 | LoadLibraries;
|
|---|
| 855 |
|
|---|
| 856 |
|
|---|
| 857 | finalization
|
|---|
| 858 |
|
|---|
| 859 | FreeLibraries;
|
|---|
| 860 |
|
|---|
| 861 | end.
|
|---|