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 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;
|
---|
60 | function GetUserName: string;
|
---|
61 | function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
|
---|
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 Implode(Separator: Char; 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);
|
---|
83 | procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
|
---|
84 | procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
|
---|
85 | procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
|
---|
86 | procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
|
---|
87 | procedure SearchFiles(AList: TStrings; Dir: string;
|
---|
88 | FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
|
---|
89 | function SplitString(var Text: string; Count: Word): string;
|
---|
90 | function StripTags(const S: string): string;
|
---|
91 | function TryHexToInt(Data: string; out Value: Integer): Boolean;
|
---|
92 | function TryBinToInt(Data: string; out Value: Integer): Boolean;
|
---|
93 | procedure SortStrings(Strings: TStrings);
|
---|
94 |
|
---|
95 |
|
---|
96 | implementation
|
---|
97 |
|
---|
98 | function BinToInt(BinStr : string) : Int64;
|
---|
99 | var
|
---|
100 | i : byte;
|
---|
101 | RetVar : Int64;
|
---|
102 | begin
|
---|
103 | BinStr := UpperCase(BinStr);
|
---|
104 | if BinStr[length(BinStr)] = 'B' then Delete(BinStr,length(BinStr),1);
|
---|
105 | RetVar := 0;
|
---|
106 | for i := 1 to length(BinStr) do begin
|
---|
107 | if not (BinStr[i] in ['0','1']) then begin
|
---|
108 | RetVar := 0;
|
---|
109 | Break;
|
---|
110 | end;
|
---|
111 | RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1) ;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | Result := RetVar;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | function BinToHexString(Source: AnsiString): string;
|
---|
118 | var
|
---|
119 | I: Integer;
|
---|
120 | begin
|
---|
121 | Result := '';
|
---|
122 | for I := 1 to Length(Source) do begin
|
---|
123 | Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
|
---|
124 | end;
|
---|
125 | end;
|
---|
126 |
|
---|
127 |
|
---|
128 | procedure DeleteFiles(APath, AFileSpec: string);
|
---|
129 | var
|
---|
130 | SearchRec: TSearchRec;
|
---|
131 | Find: Integer;
|
---|
132 | Path: string;
|
---|
133 | begin
|
---|
134 | Path := IncludeTrailingPathDelimiter(APath);
|
---|
135 |
|
---|
136 | Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
|
---|
137 | while Find = 0 do begin
|
---|
138 | DeleteFile(Path + SearchRec.Name);
|
---|
139 |
|
---|
140 | Find := SysUtils.FindNext(SearchRec);
|
---|
141 | end;
|
---|
142 | FindClose(SearchRec);
|
---|
143 | end;
|
---|
144 |
|
---|
145 |
|
---|
146 | function GetFileFilterItemExt(Filter: string; Index: Integer): string;
|
---|
147 | var
|
---|
148 | List: TStringList;
|
---|
149 | begin
|
---|
150 | try
|
---|
151 | List := TStringList.Create;
|
---|
152 | List.Text := StringReplace(Filter, '|', #10, [rfReplaceAll]);
|
---|
153 | Result := List[Index * 2 + 1];
|
---|
154 | finally
|
---|
155 | List.Free;
|
---|
156 | end;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
|
---|
160 | var
|
---|
161 | FileExt: string;
|
---|
162 | begin
|
---|
163 | FileExt := GetFileFilterItemExt(FileDialog.Filter, FileDialog.FilterIndex - 1);
|
---|
164 | Delete(FileExt, 1, 1); // Remove symbol '*'
|
---|
165 | if FileExt <> '.*' then
|
---|
166 | FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt)
|
---|
167 | end;
|
---|
168 |
|
---|
169 | function GenerateNewName(OldName: string): string;
|
---|
170 | var
|
---|
171 | I: Integer;
|
---|
172 | Number: Integer;
|
---|
173 | begin
|
---|
174 | Number := 1;
|
---|
175 | // Find number on end
|
---|
176 | if Length(OldName) > 0 then begin
|
---|
177 | I := Length(OldName);
|
---|
178 | while (I > 1) and ((OldName[I] >= '0') and (OldName[I] <= '9')) do Dec(I);
|
---|
179 | TryStrToInt(Copy(OldName, I + 1, Length(OldName) - I), Number);
|
---|
180 | Inc(Number)
|
---|
181 | end;
|
---|
182 | Result := Copy(OldName, 1, I) + IntToStr(Number);
|
---|
183 | end;
|
---|
184 |
|
---|
185 | (*function DelTree(DirName : string): Boolean;
|
---|
186 | var
|
---|
187 | SHFileOpStruct : TSHFileOpStruct;
|
---|
188 | DirBuf : array [0..255] of char;
|
---|
189 | begin
|
---|
190 | DirName := UTF8Decode(DirName);
|
---|
191 | try
|
---|
192 | Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
|
---|
193 | FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
|
---|
194 | StrPCopy(DirBuf, DirName) ;
|
---|
195 | with SHFileOpStruct do begin
|
---|
196 | Wnd := 0;
|
---|
197 | pFrom := @DirBuf;
|
---|
198 | wFunc := FO_DELETE;
|
---|
199 | fFlags := FOF_ALLOWUNDO;
|
---|
200 | fFlags := fFlags or FOF_NOCONFIRMATION;
|
---|
201 | fFlags := fFlags or FOF_SILENT;
|
---|
202 | end;
|
---|
203 | Result := (SHFileOperation(SHFileOpStruct) = 0) ;
|
---|
204 | except
|
---|
205 | Result := False;
|
---|
206 | end;
|
---|
207 | end;*)
|
---|
208 |
|
---|
209 | function Implode(Separator: Char; 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 |
|
---|
220 | function LastPos(const SubStr: String; const S: String): Integer;
|
---|
221 | begin
|
---|
222 | Result := Pos(ReverseString(SubStr), ReverseString(S));
|
---|
223 | if (Result <> 0) then
|
---|
224 | Result := ((Length(S) - Length(SubStr)) + 1) - Result + 1;
|
---|
225 | end;
|
---|
226 |
|
---|
227 | function BCDToInt(Value: Byte): Byte;
|
---|
228 | begin
|
---|
229 | Result := (Value shr 4) * 10 + (Value and 15);
|
---|
230 | end;
|
---|
231 |
|
---|
232 | (*function GetSpecialFolderPath(Folder: Integer): string;
|
---|
233 | const
|
---|
234 | SHGFP_TYPE_CURRENT = 0;
|
---|
235 | var
|
---|
236 | Path: array[0..MAX_PATH] of Char;
|
---|
237 | begin
|
---|
238 | Result := 'C:\Test';
|
---|
239 | if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @path[0])) then
|
---|
240 | Result := path
|
---|
241 | else
|
---|
242 | Result := '';
|
---|
243 | end;*)
|
---|
244 |
|
---|
245 | function IntToBin(Data: Int64; Count: Byte): string;
|
---|
246 | var
|
---|
247 | I: Integer;
|
---|
248 | begin
|
---|
249 | Result := '';
|
---|
250 | for I := 0 to Count - 1 do
|
---|
251 | Result := IntToStr((Data shr I) and 1) + Result;
|
---|
252 | end;
|
---|
253 |
|
---|
254 | function IntToHex(Data: Cardinal; Count: Byte): string;
|
---|
255 | const
|
---|
256 | Chars: array[0..15] of Char = '0123456789ABCDEF';
|
---|
257 | var
|
---|
258 | I: Integer;
|
---|
259 | begin
|
---|
260 | Result := '';
|
---|
261 | for I := 0 to Count - 1 do
|
---|
262 | Result := Result + Chars[(Data shr (I * 4)) and 15];
|
---|
263 | end;
|
---|
264 |
|
---|
265 | function TryHexToInt(Data: string; out Value: Integer): Boolean;
|
---|
266 | var
|
---|
267 | I: Integer;
|
---|
268 | begin
|
---|
269 | Data := UpperCase(Data);
|
---|
270 | Result := True;
|
---|
271 | Value := 0;
|
---|
272 | for I := 0 to Length(Data) - 1 do begin
|
---|
273 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
|
---|
274 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
|
---|
275 | else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
|
---|
276 | Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
|
---|
277 | else Result := False;
|
---|
278 | end;
|
---|
279 | end;
|
---|
280 |
|
---|
281 | function TryBinToInt(Data: string; out Value: Integer): Boolean;
|
---|
282 | var
|
---|
283 | I: Integer;
|
---|
284 | begin
|
---|
285 | Result := True;
|
---|
286 | Value := 0;
|
---|
287 | for I := 0 to Length(Data) - 1 do begin
|
---|
288 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '1') then
|
---|
289 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1))
|
---|
290 | else Result := False;
|
---|
291 | end;
|
---|
292 | end;
|
---|
293 |
|
---|
294 | function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
|
---|
295 | var
|
---|
296 | I: Integer;
|
---|
297 | begin
|
---|
298 | if Length(Data1) = Length(Data2) then begin
|
---|
299 | Result := True;
|
---|
300 | for I := 0 to Length(Data1) - 1 do begin
|
---|
301 | if Data1[I] <> Data2[I] then begin
|
---|
302 | Result := False;
|
---|
303 | Break;
|
---|
304 | end
|
---|
305 | end;
|
---|
306 | end else Result := False;
|
---|
307 | end;
|
---|
308 |
|
---|
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
|
---|
323 | SetLength(Result, Length(Result) + 1);
|
---|
324 | Result[High(Result)] := Data;
|
---|
325 | end;
|
---|
326 | end;
|
---|
327 |
|
---|
328 | function Implode(Separator: Char; 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}
|
---|
340 | function GetUserName: string;
|
---|
341 | const
|
---|
342 | MAX_USERNAME_LENGTH = 256;
|
---|
343 | var
|
---|
344 | L: LongWord;
|
---|
345 | begin
|
---|
346 | L := MAX_USERNAME_LENGTH + 2;
|
---|
347 | Result := Default(string);
|
---|
348 | SetLength(Result, L);
|
---|
349 | if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
|
---|
350 | SetLength(Result, StrLen(PChar(Result)));
|
---|
351 | Result := UTF8Encode(Result);
|
---|
352 | end else Result := '';
|
---|
353 | end;
|
---|
354 |
|
---|
355 | function GetVersionInfo: TOSVersionInfo;
|
---|
356 | begin
|
---|
357 | Result.dwOSVersionInfoSize := SizeOf(Result);
|
---|
358 | if GetVersionEx(Result) then begin
|
---|
359 | end;
|
---|
360 | end;
|
---|
361 | {$ENDIF}
|
---|
362 |
|
---|
363 | function ComputerName: string;
|
---|
364 | {$IFDEF WINDOWS}
|
---|
365 | const
|
---|
366 | INFO_BUFFER_SIZE = 32767;
|
---|
367 | var
|
---|
368 | Buffer : array[0..INFO_BUFFER_SIZE] of WideChar;
|
---|
369 | Ret : DWORD;
|
---|
370 | begin
|
---|
371 | Ret := INFO_BUFFER_SIZE;
|
---|
372 | If (GetComputerNameW(@Buffer[0],Ret)) then begin
|
---|
373 | Result := UTF8Encode(WideString(Buffer));
|
---|
374 | end
|
---|
375 | else begin
|
---|
376 | Result := 'ERROR_NO_COMPUTERNAME_RETURNED';
|
---|
377 | end;
|
---|
378 | end;
|
---|
379 | {$ENDIF}
|
---|
380 | {$IFDEF UNIX}
|
---|
381 | var
|
---|
382 | Name: UtsName;
|
---|
383 | begin
|
---|
384 | Name := Default(UtsName);
|
---|
385 | fpuname(Name);
|
---|
386 | Result := Name.Nodename;
|
---|
387 | end;
|
---|
388 | {$ENDIF}
|
---|
389 |
|
---|
390 | {$IFDEF WINDOWS}
|
---|
391 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
---|
392 | const
|
---|
393 | MaxLength = 1000;
|
---|
394 | var
|
---|
395 | UserName: array[0..MaxLength] of Char;
|
---|
396 | VersionInfo: TOSVersionInfo;
|
---|
397 | Size: DWORD;
|
---|
398 | begin
|
---|
399 | VersionInfo := GetVersionInfo;
|
---|
400 | if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
|
---|
401 | Size := MaxLength;
|
---|
402 | GetUserNameEx(Integer(Format), @UserName, @Size);
|
---|
403 | //ShowMessage(SysErrorMessage(GetLastError));
|
---|
404 | if GetLastError = 0 then Result := UTF8Encode(UserName)
|
---|
405 | else Result := GetUserName;
|
---|
406 | end else Result := GetUserName;
|
---|
407 | end;
|
---|
408 | {$ELSE}
|
---|
409 | function GetUserName: string;
|
---|
410 | begin
|
---|
411 | Result := '';
|
---|
412 | end;
|
---|
413 |
|
---|
414 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
---|
415 | begin
|
---|
416 | Result := '';
|
---|
417 | end;
|
---|
418 |
|
---|
419 | {$ENDIF}
|
---|
420 |
|
---|
421 | function SplitString(var Text: string; Count: Word): string;
|
---|
422 | begin
|
---|
423 | Result := Copy(Text, 1, Count);
|
---|
424 | Delete(Text, 1, Count);
|
---|
425 | end;
|
---|
426 |
|
---|
427 | function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
|
---|
428 | var
|
---|
429 | I: Integer;
|
---|
430 | begin
|
---|
431 | Result := 0;
|
---|
432 | for I := 0 to MaxIndex - 1 do
|
---|
433 | if ((Variable shr I) and 1) = 1 then Inc(Result);
|
---|
434 | end;
|
---|
435 |
|
---|
436 | function GetBit(Variable:QWord;Index:Byte):Boolean;
|
---|
437 | begin
|
---|
438 | Result := ((Variable shr Index) and 1) = 1;
|
---|
439 | end;
|
---|
440 |
|
---|
441 | procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean);
|
---|
442 | begin
|
---|
443 | Variable := (Variable and ((1 shl Index) xor High(QWord))) or (Int64(State) shl Index);
|
---|
444 | end;
|
---|
445 |
|
---|
446 | procedure SetBit(var Variable:QWord;Index:Byte;State:Boolean); overload;
|
---|
447 | begin
|
---|
448 | Variable := (Variable and ((1 shl Index) xor High(QWord))) or (QWord(State) shl Index);
|
---|
449 | end;
|
---|
450 |
|
---|
451 | procedure SetBit(var Variable:Cardinal;Index:Byte;State:Boolean); overload;
|
---|
452 | begin
|
---|
453 | Variable := (Variable and ((1 shl Index) xor High(Cardinal))) or (Cardinal(State) shl Index);
|
---|
454 | end;
|
---|
455 |
|
---|
456 | procedure SetBit(var Variable:Word;Index:Byte;State:Boolean); overload;
|
---|
457 | begin
|
---|
458 | Variable := (Variable and ((1 shl Index) xor High(Word))) or (Word(State) shl Index);
|
---|
459 | end;
|
---|
460 |
|
---|
461 | function AddLeadingZeroes(const aNumber, Length : integer) : string;
|
---|
462 | begin
|
---|
463 | Result := SysUtils.Format('%.*d', [Length, aNumber]) ;
|
---|
464 | end;
|
---|
465 |
|
---|
466 | procedure LoadLibraries;
|
---|
467 | begin
|
---|
468 | {$IFDEF WINDOWS}
|
---|
469 | DLLHandle1 := LoadLibrary('secur32.dll');
|
---|
470 | if DLLHandle1 <> 0 then
|
---|
471 | begin
|
---|
472 | @GetUserNameEx := GetProcAddress(DLLHandle1, 'GetUserNameExA');
|
---|
473 | end;
|
---|
474 | {$ENDIF}
|
---|
475 | end;
|
---|
476 |
|
---|
477 | procedure FreeLibraries;
|
---|
478 | begin
|
---|
479 | {$IFDEF WINDOWS}
|
---|
480 | if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
|
---|
481 | {$ENDIF}
|
---|
482 | end;
|
---|
483 |
|
---|
484 | procedure ExecuteProgram(Executable: string; Parameters: array of string);
|
---|
485 | var
|
---|
486 | Process: TProcess;
|
---|
487 | I: Integer;
|
---|
488 | begin
|
---|
489 | try
|
---|
490 | Process := TProcess.Create(nil);
|
---|
491 | Process.Executable := Executable;
|
---|
492 | for I := 0 to Length(Parameters) - 1 do
|
---|
493 | Process.Parameters.Add(Parameters[I]);
|
---|
494 | Process.Options := [poNoConsole];
|
---|
495 | Process.Execute;
|
---|
496 | finally
|
---|
497 | Process.Free;
|
---|
498 | end;
|
---|
499 | end;
|
---|
500 |
|
---|
501 | procedure FreeThenNil(var Obj);
|
---|
502 | begin
|
---|
503 | TObject(Obj).Free;
|
---|
504 | TObject(Obj) := nil;
|
---|
505 | end;
|
---|
506 |
|
---|
507 | procedure OpenWebPage(URL: string);
|
---|
508 | begin
|
---|
509 | OpenURL(URL);
|
---|
510 | end;
|
---|
511 |
|
---|
512 | procedure OpenEmail(Email: string);
|
---|
513 | begin
|
---|
514 | OpenURL('mailto:' + Email);
|
---|
515 | end;
|
---|
516 |
|
---|
517 | procedure OpenFileInShell(FileName: string);
|
---|
518 | begin
|
---|
519 | ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
|
---|
520 | end;
|
---|
521 |
|
---|
522 | function RemoveQuotes(Text: string): string;
|
---|
523 | begin
|
---|
524 | Result := Text;
|
---|
525 | if (Pos('"', Text) = 1) and (Text[Length(Text)] = '"') then
|
---|
526 | Result := Copy(Text, 2, Length(Text) - 2);
|
---|
527 | end;
|
---|
528 |
|
---|
529 | function OccurenceOfChar(What: Char; Where: string): Integer;
|
---|
530 | var
|
---|
531 | I: Integer;
|
---|
532 | begin
|
---|
533 | Result := 0;
|
---|
534 | for I := 1 to Length(Where) do
|
---|
535 | if Where[I] = What then Inc(Result);
|
---|
536 | end;
|
---|
537 |
|
---|
538 | function GetDirCount(Dir: string): Integer;
|
---|
539 | begin
|
---|
540 | Result := OccurenceOfChar(DirectorySeparator, Dir);
|
---|
541 | if Copy(Dir, Length(Dir), 1) = DirectorySeparator then
|
---|
542 | Dec(Result);
|
---|
543 | end;
|
---|
544 |
|
---|
545 | function MergeArray(A, B: array of string): TStringArray;
|
---|
546 | var
|
---|
547 | I: Integer;
|
---|
548 | begin
|
---|
549 | Result := Default(TStringArray);
|
---|
550 | SetLength(Result, Length(A) + Length(B));
|
---|
551 | for I := 0 to Length(A) - 1 do
|
---|
552 | Result[I] := A[I];
|
---|
553 | for I := 0 to Length(B) - 1 do
|
---|
554 | Result[Length(A) + I] := B[I];
|
---|
555 | end;
|
---|
556 |
|
---|
557 | function LoadFileToStr(const FileName: TFileName): AnsiString;
|
---|
558 | var
|
---|
559 | FileStream: TFileStream;
|
---|
560 | Read: Integer;
|
---|
561 | begin
|
---|
562 | Result := '';
|
---|
563 | FileStream := TFileStream.Create(FileName, fmOpenRead);
|
---|
564 | try
|
---|
565 | if FileStream.Size > 0 then begin
|
---|
566 | SetLength(Result, FileStream.Size);
|
---|
567 | Read := FileStream.Read(Pointer(Result)^, FileStream.Size);
|
---|
568 | SetLength(Result, Read);
|
---|
569 | end;
|
---|
570 | finally
|
---|
571 | FileStream.Free;
|
---|
572 | end;
|
---|
573 | end;
|
---|
574 |
|
---|
575 | function DefaultSearchFilter(const FileName: string): Boolean;
|
---|
576 | begin
|
---|
577 | Result := True;
|
---|
578 | end;
|
---|
579 |
|
---|
580 | procedure SaveStringToFile(S, FileName: string);
|
---|
581 | var
|
---|
582 | F: TextFile;
|
---|
583 | begin
|
---|
584 | AssignFile(F, FileName);
|
---|
585 | try
|
---|
586 | ReWrite(F);
|
---|
587 | Write(F, S);
|
---|
588 | finally
|
---|
589 | CloseFile(F);
|
---|
590 | end;
|
---|
591 | end;
|
---|
592 |
|
---|
593 | procedure SearchFiles(AList: TStrings; Dir: string;
|
---|
594 | FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
|
---|
595 | var
|
---|
596 | SR: TSearchRec;
|
---|
597 | begin
|
---|
598 | Dir := IncludeTrailingPathDelimiter(Dir);
|
---|
599 | if FindFirst(Dir + '*', faAnyFile, SR) = 0 then
|
---|
600 | try
|
---|
601 | repeat
|
---|
602 | if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or
|
---|
603 | not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue;
|
---|
604 | if Assigned(FileNameMethod) then
|
---|
605 | FileNameMethod(Dir + SR.Name);
|
---|
606 | AList.Add(Dir + SR.Name);
|
---|
607 | if (SR.Attr and faDirectory) <> 0 then
|
---|
608 | SearchFiles(AList, Dir + SR.Name, FilterMethod);
|
---|
609 | until FindNext(SR) <> 0;
|
---|
610 | finally
|
---|
611 | FindClose(SR);
|
---|
612 | end;
|
---|
613 | end;
|
---|
614 |
|
---|
615 | function GetStringPart(var Text: string; Separator: string): string;
|
---|
616 | var
|
---|
617 | P: Integer;
|
---|
618 | begin
|
---|
619 | P := Pos(Separator, Text);
|
---|
620 | if P > 0 then begin
|
---|
621 | Result := Copy(Text, 1, P - 1);
|
---|
622 | Delete(Text, 1, P - 1 + Length(Separator));
|
---|
623 | end else begin
|
---|
624 | Result := Text;
|
---|
625 | Text := '';
|
---|
626 | end;
|
---|
627 | Result := Trim(Result);
|
---|
628 | Text := Trim(Text);
|
---|
629 | end;
|
---|
630 |
|
---|
631 | function StripTags(const S: string): string;
|
---|
632 | var
|
---|
633 | Len: Integer;
|
---|
634 |
|
---|
635 | function ReadUntil(const ReadFrom: Integer; const C: Char): Integer;
|
---|
636 | var
|
---|
637 | J: Integer;
|
---|
638 | begin
|
---|
639 | for J := ReadFrom to Len do
|
---|
640 | if (S[j] = C) then
|
---|
641 | begin
|
---|
642 | Result := J;
|
---|
643 | Exit;
|
---|
644 | end;
|
---|
645 | Result := Len + 1;
|
---|
646 | end;
|
---|
647 |
|
---|
648 | var
|
---|
649 | I, APos: Integer;
|
---|
650 | begin
|
---|
651 | Len := Length(S);
|
---|
652 | I := 0;
|
---|
653 | Result := '';
|
---|
654 | while (I <= Len) do begin
|
---|
655 | Inc(I);
|
---|
656 | APos := ReadUntil(I, '<');
|
---|
657 | Result := Result + Copy(S, I, APos - i);
|
---|
658 | I := ReadUntil(APos + 1, '>');
|
---|
659 | end;
|
---|
660 | end;
|
---|
661 |
|
---|
662 | function PosFromIndex(SubStr: string; Text: string;
|
---|
663 | StartIndex: Integer): Integer;
|
---|
664 | var
|
---|
665 | I, MaxLen: SizeInt;
|
---|
666 | Ptr: PAnsiChar;
|
---|
667 | begin
|
---|
668 | Result := 0;
|
---|
669 | if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
|
---|
670 | if Length(SubStr) > 0 then begin
|
---|
671 | MaxLen := Length(Text) - Length(SubStr) + 1;
|
---|
672 | I := StartIndex;
|
---|
673 | Ptr := @Text[StartIndex];
|
---|
674 | while (I <= MaxLen) do begin
|
---|
675 | if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
|
---|
676 | Result := I;
|
---|
677 | Exit;
|
---|
678 | end;
|
---|
679 | Inc(I);
|
---|
680 | Inc(Ptr);
|
---|
681 | end;
|
---|
682 | end;
|
---|
683 | end;
|
---|
684 |
|
---|
685 | function PosFromIndexReverse(SubStr: string; Text: string;
|
---|
686 | StartIndex: Integer): Integer;
|
---|
687 | var
|
---|
688 | I: SizeInt;
|
---|
689 | Ptr: PAnsiChar;
|
---|
690 | begin
|
---|
691 | Result := 0;
|
---|
692 | if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
|
---|
693 | if Length(SubStr) > 0 then begin
|
---|
694 | I := StartIndex;
|
---|
695 | Ptr := @Text[StartIndex];
|
---|
696 | while (I > 0) do begin
|
---|
697 | if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
|
---|
698 | Result := I;
|
---|
699 | Exit;
|
---|
700 | end;
|
---|
701 | Dec(I);
|
---|
702 | Dec(Ptr);
|
---|
703 | end;
|
---|
704 | end;
|
---|
705 | end;
|
---|
706 |
|
---|
707 | procedure CopyStringArray(Dest: TStringArray; Source: array of string);
|
---|
708 | var
|
---|
709 | I: Integer;
|
---|
710 | begin
|
---|
711 | SetLength(Dest, Length(Source));
|
---|
712 | for I := 0 to Length(Dest) - 1 do
|
---|
713 | Dest[I] := Source[I];
|
---|
714 | end;
|
---|
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 |
|
---|
746 |
|
---|
747 | initialization
|
---|
748 |
|
---|
749 | LoadLibraries;
|
---|
750 |
|
---|
751 |
|
---|
752 | finalization
|
---|
753 |
|
---|
754 | FreeLibraries;
|
---|
755 |
|
---|
756 | end.
|
---|