source: trunk/Demo/Packages/Common/UCommon.pas

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