source: trunk/Packages/Common/UCommon.pas

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