source: tags/1.3.0/Packages/Common/UCommon.pas

Last change on this file was 3, checked in by chronos, 8 years ago
  • Added: Remember last form position.
File size: 12.2 KB
Line 
1unit UCommon;
2
3{$mode delphi}
4
5interface
6
7uses
8 {$IFDEF Windows}Windows,{$ENDIF}
9 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
10 FileUtil; //, ShFolder, ShellAPI;
11
12type
13 TArrayOfByte = array of Byte;
14 TArrayOfString = array of string;
15 TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
16
17 TUserNameFormat = (
18 unfNameUnknown = 0, // Unknown name type.
19 unfNameFullyQualifiedDN = 1, // Fully qualified distinguished name
20 unfNameSamCompatible = 2, // Windows NT® 4.0 account name
21 unfNameDisplay = 3, // A "friendly" display name
22 unfNameUniqueId = 6, // GUID string that the IIDFromString function returns
23 unfNameCanonical = 7, // Complete canonical name
24 unfNameUserPrincipal = 8, // User principal name
25 unfNameCanonicalEx = 9,
26 unfNameServicePrincipal = 10, // Generalized service principal name
27 unfDNSDomainName = 11);
28
29var
30 ExceptionHandler: TExceptionEvent;
31 DLLHandle1: HModule;
32
33{$IFDEF Windows}
34 GetUserNameEx: procedure (NameFormat: DWORD;
35 lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
36{$ENDIF}
37
38function IntToBin(Data: Int64; Count: Byte): string;
39function BinToInt(BinStr: string): Int64;
40function TryHexToInt(Data: string; var Value: Integer): Boolean;
41function TryBinToInt(Data: string; var Value: Integer): Boolean;
42function BinToHexString(Source: AnsiString): string;
43//function DelTree(DirName : string): Boolean;
44//function GetSpecialFolderPath(Folder: Integer): string;
45function BCDToInt(Value: Byte): Byte;
46function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
47function GetUserName: string;
48function LoggedOnUserNameEx(Format: TUserNameFormat): string;
49function SplitString(var Text: string; Count: Word): string;
50function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
51function GetBit(Variable: QWord; Index: Byte): Boolean;
52procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload;
53procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
54procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
55procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
56function AddLeadingZeroes(const aNumber, Length : integer) : string;
57function LastPos(const SubStr: String; const S: String): Integer;
58function GenerateNewName(OldName: string): string;
59function GetFileFilterItemExt(Filter: string; Index: Integer): string;
60procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
61procedure DeleteFiles(APath, AFileSpec: string);
62procedure OpenWebPage(URL: string);
63procedure OpenFileInShell(FileName: string);
64procedure ExecuteProgram(CommandLine: string);
65procedure FreeThenNil(var Obj);
66function RemoveQuotes(Text: string): string;
67
68
69implementation
70
71function BinToInt(BinStr : string) : Int64;
72var
73 i : byte;
74 RetVar : Int64;
75begin
76 BinStr := UpperCase(BinStr);
77 if BinStr[length(BinStr)] = 'B' then Delete(BinStr,length(BinStr),1);
78 RetVar := 0;
79 for i := 1 to length(BinStr) do begin
80 if not (BinStr[i] in ['0','1']) then begin
81 RetVar := 0;
82 Break;
83 end;
84 RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1) ;
85 end;
86
87 Result := RetVar;
88end;
89
90function BinToHexString(Source: AnsiString): string;
91var
92 I: Integer;
93begin
94 for I := 1 to Length(Source) do begin
95 Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
96 end;
97end;
98
99
100procedure DeleteFiles(APath, AFileSpec: string);
101var
102 SearchRec: TSearchRec;
103 Find: Integer;
104 Path: string;
105begin
106 Path := IncludeTrailingPathDelimiter(APath);
107
108 Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
109 while Find = 0 do begin
110 DeleteFile(Path + UTF8Encode(SearchRec.Name));
111
112 Find := SysUtils.FindNext(SearchRec);
113 end;
114 FindClose(SearchRec);
115end;
116
117
118function GetFileFilterItemExt(Filter: string; Index: Integer): string;
119var
120 List: TStringList;
121begin
122 try
123 List := TStringList.Create;
124 List.Text := StringReplace(Filter, '|', #10, [rfReplaceAll]);
125 Result := List[Index * 2 + 1];
126 finally
127 List.Free;
128 end;
129end;
130
131procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
132var
133 FileExt: string;
134begin
135 FileExt := GetFileFilterItemExt(FileDialog.Filter, FileDialog.FilterIndex - 1);
136 Delete(FileExt, 1, 1); // Remove symbol '*'
137 if FileExt <> '.*' then
138 FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt)
139end;
140
141function GenerateNewName(OldName: string): string;
142var
143 I: Integer;
144 Number: Integer;
145begin
146 Number := 1;
147 // Find number on end
148 if Length(OldName) > 0 then begin
149 I := Length(OldName);
150 while (I > 1) and ((OldName[I] >= '0') and (OldName[I] <= '9')) do Dec(I);
151 TryStrToInt(Copy(OldName, I + 1, Length(OldName) - I), Number);
152 Inc(Number)
153 end;
154 Result := Copy(OldName, 1, I) + IntToStr(Number);
155end;
156
157(*function DelTree(DirName : string): Boolean;
158var
159 SHFileOpStruct : TSHFileOpStruct;
160 DirBuf : array [0..255] of char;
161begin
162 DirName := UTF8Decode(DirName);
163 try
164 Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
165 FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
166 StrPCopy(DirBuf, DirName) ;
167 with SHFileOpStruct do begin
168 Wnd := 0;
169 pFrom := @DirBuf;
170 wFunc := FO_DELETE;
171 fFlags := FOF_ALLOWUNDO;
172 fFlags := fFlags or FOF_NOCONFIRMATION;
173 fFlags := fFlags or FOF_SILENT;
174 end;
175 Result := (SHFileOperation(SHFileOpStruct) = 0) ;
176 except
177 Result := False;
178 end;
179end;*)
180
181function LastPos(const SubStr: String; const S: String): Integer;
182begin
183 Result := Pos(ReverseString(SubStr), ReverseString(S));
184 if (Result <> 0) then
185 Result := ((Length(S) - Length(SubStr)) + 1) - Result + 1;
186end;
187
188function BCDToInt(Value: Byte): Byte;
189begin
190 Result := (Value shr 4) * 10 + (Value and 15);
191end;
192
193(*function GetSpecialFolderPath(Folder: Integer): string;
194const
195 SHGFP_TYPE_CURRENT = 0;
196var
197 Path: array[0..MAX_PATH] of Char;
198begin
199 Result := 'C:\Test';
200 if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @path[0])) then
201 Result := path
202 else
203 Result := '';
204end;*)
205
206function IntToBin(Data: Int64; Count: Byte): string;
207var
208 I: Integer;
209begin
210 Result := '';
211 for I := 0 to Count - 1 do
212 Result := IntToStr((Data shr I) and 1) + Result;
213end;
214
215function IntToHex(Data: Cardinal; Count: Byte): string;
216const
217 Chars: array[0..15] of Char = '0123456789ABCDEF';
218var
219 I: Integer;
220begin
221 Result := '';
222 for I := 0 to Count - 1 do
223 Result := Result + Chars[(Data shr (I * 4)) and 15];
224end;
225
226function TryHexToInt(Data: string; var Value: Integer): Boolean;
227var
228 I: Integer;
229begin
230 Data := UpperCase(Data);
231 Result := True;
232 Value := 0;
233 for I := 0 to Length(Data) - 1 do begin
234 if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
235 Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
236 else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
237 Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
238 else Result := False;
239 end;
240end;
241
242function TryBinToInt(Data: string; var Value: Integer): Boolean;
243var
244 I: Integer;
245begin
246 Result := True;
247 Value := 0;
248 for I := 0 to Length(Data) - 1 do begin
249 if (Data[I + 1] >= '0') and (Data[I + 1] <= '1') then
250 Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1))
251 else Result := False;
252 end;
253end;
254
255function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
256var
257 I: Integer;
258begin
259 if Length(Data1) = Length(Data2) then begin
260 Result := True;
261 for I := 0 to Length(Data1) - 1 do begin
262 if Data1[I] <> Data2[I] then begin
263 Result := False;
264 Break;
265 end
266 end;
267 end else Result := False;
268end;
269
270function Explode(Separator: char; Data: string): TArrayOfString;
271begin
272 SetLength(Result, 0);
273 while Pos(Separator, Data) > 0 do begin
274 SetLength(Result, Length(Result) + 1);
275 Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
276 Delete(Data, 1, Pos(Separator, Data));
277 end;
278 SetLength(Result, Length(Result) + 1);
279 Result[High(Result)] := Data;
280end;
281
282{$IFDEF Windows}
283function GetUserName: string;
284const
285 MAX_USERNAME_LENGTH = 256;
286var
287 L: LongWord;
288begin
289
290 L := MAX_USERNAME_LENGTH + 2;
291 SetLength(Result, L);
292 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
293 SetLength(Result, StrLen(PChar(Result)));
294 Result := UTF8Encode(Result);
295 end else Result := '';
296end;
297
298function GetVersionInfo: TOSVersionInfo;
299begin
300 Result.dwOSVersionInfoSize := SizeOf(Result);
301 if GetVersionEx(Result) then begin
302 end;
303end;
304
305function LoggedOnUserNameEx(Format: TUserNameFormat): string;
306const
307 MaxLength = 1000;
308var
309 UserName: array[0..MaxLength] of Char;
310 VersionInfo: TOSVersionInfo;
311 Size: DWORD;
312begin
313 VersionInfo := GetVersionInfo;
314 if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
315 Size := MaxLength;
316 GetUserNameEx(Integer(Format), @UserName, @Size);
317 //ShowMessage(SysErrorMessage(GetLastError));
318 if GetLastError = 0 then Result := UTF8Encode(UserName)
319 else Result := GetUserName;
320 end else Result := GetUserName;
321end;
322{$ELSE}
323function GetUserName: string;
324begin
325 Result := '';
326end;
327
328function LoggedOnUserNameEx(Format: TUserNameFormat): string;
329begin
330 Result := '';
331end;
332
333{$ENDIF}
334
335function SplitString(var Text: string; Count: Word): string;
336begin
337 Result := Copy(Text, 1, Count);
338 Delete(Text, 1, Count);
339end;
340
341function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer;
342var
343 I: Integer;
344begin
345 Result := 0;
346 for I := 0 to MaxIndex - 1 do
347 if ((Variable shr I) and 1) = 1 then Inc(Result);
348end;
349
350function GetBit(Variable:QWord;Index:Byte):Boolean;
351begin
352 Result := ((Variable shr Index) and 1) = 1;
353end;
354
355procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean);
356begin
357 Variable := (Variable and ((1 shl Index) xor High(QWord))) or (Int64(State) shl Index);
358end;
359
360procedure SetBit(var Variable:QWord;Index:Byte;State:Boolean); overload;
361begin
362 Variable := (Variable and ((1 shl Index) xor High(QWord))) or (QWord(State) shl Index);
363end;
364
365procedure SetBit(var Variable:Cardinal;Index:Byte;State:Boolean); overload;
366begin
367 Variable := (Variable and ((1 shl Index) xor High(Cardinal))) or (Cardinal(State) shl Index);
368end;
369
370procedure SetBit(var Variable:Word;Index:Byte;State:Boolean); overload;
371begin
372 Variable := (Variable and ((1 shl Index) xor High(Word))) or (Word(State) shl Index);
373end;
374
375function AddLeadingZeroes(const aNumber, Length : integer) : string;
376begin
377 Result := SysUtils.Format('%.*d', [Length, aNumber]) ;
378end;
379
380procedure LoadLibraries;
381begin
382 {$IFDEF Windows}
383 DLLHandle1 := LoadLibrary('secur32.dll');
384 if DLLHandle1 <> 0 then
385 begin
386 @GetUserNameEx := GetProcAddress(DLLHandle1, 'GetUserNameExA');
387 end;
388 {$ENDIF}
389end;
390
391procedure FreeLibraries;
392begin
393 {$IFDEF Windows}
394 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
395 {$ENDIF}
396end;
397
398procedure ExecuteProgram(CommandLine: string);
399var
400 Process: TProcess;
401begin
402 try
403 Process := TProcess.Create(nil);
404 Process.CommandLine := CommandLine;
405 Process.Options := [poNoConsole];
406 Process.Execute;
407 finally
408 Process.Free;
409 end;
410end;
411
412procedure FreeThenNil(var Obj);
413begin
414 TObject(Obj).Free;
415 TObject(Obj) := nil;
416end;
417
418procedure OpenWebPage(URL: string);
419var
420 Process: TProcess;
421 Browser, Params: string;
422begin
423 OpenURL(URL);
424 {try
425 Process := TProcess.Create(nil);
426 Browser := '';
427 //FindDefaultBrowser(Browser, Params);
428 //Process.Executable := Browser;
429 //Process.Parameters.Add(Format(Params, [ApplicationInfo.HomePage]);
430 Process.CommandLine := 'cmd.exe /c start ' + URL;
431 Process.Options := [poNoConsole];
432 Process.Execute;
433 finally
434 Process.Free;
435 end;}
436end;
437
438procedure OpenFileInShell(FileName: string);
439begin
440 ExecuteProgram('cmd.exe /c start "' + FileName + '"');
441end;
442
443function RemoveQuotes(Text: string): string;
444begin
445 Result := Text;
446 if (Pos('"', Text) = 1) and (Text[Length(Text)] = '"') then
447 Result := Copy(Text, 2, Length(Text) - 2);
448end;
449
450
451initialization
452
453LoadLibraries;
454
455
456finalization
457
458FreeLibraries;
459
460end.
Note: See TracBrowser for help on using the repository browser.