source: branches/2.19_branch/Library/ACLUtility.pas@ 309

Last change on this file since 309 was 309, checked in by RBRi, 17 years ago

more cleanup

  • Property svn:eol-style set to native
File size: 14.9 KB
Line 
1Unit ACLUtility;
2
3Interface
4
5Uses
6 Classes, SysUtils,
7{$ifdef os2}
8 Os2Def, IniFiles;
9{$endif}
10{$ifdef win32}
11 Registry;
12{$endif}
13
14function GetACLLibraryVersion: string;
15
16Type
17 TPrintOutput = procedure( TheText: string ) of object;
18 TTerminateCheck = function: boolean of object;
19 TProgressCallback = procedure( n, outof: integer;
20 Message: string ) of object;
21
22const
23 _MAX_PATH = 260; // max. length of full pathname
24 _MAX_DRIVE = 3; // max. length of drive component
25 _MAX_DIR = 256; // max. length of path component
26 _MAX_FNAME = 256; // max. length of file name component
27 _MAX_EXT = 256; // max. length of extension component
28
29 mrFailed = $c000 + 1;
30
31{$ifdef win32}
32Function GetAPIErrorString( ErrorCode: integer ): string;
33Function GetLastAPIErrorString: string;
34{$endif}
35
36Type
37 TMyIniFile =
38{$ifdef os2}
39 class( TAsciiIniFile )
40{$else}
41 class( TRegIniFile )
42{$endif}
43 constructor CreateMe( const Path: string );
44 destructor Destroy; override;
45 end;
46
47{$ifdef os2}
48function AllocateMemory( const Size: longint ): pointer;
49procedure DeallocateMemory( Var P: pointer );
50procedure Sleep( const milliseconds: longint );
51{$endif}
52
53// allocate a new copy of the given source memory
54procedure AllocMemCopy( const Source: pointer;
55 var Dest: pointer;
56 const Size: longint );
57
58procedure MemCopy( const Source: pointer;
59 const Dest: pointer;
60 const Size: longint );
61
62procedure FillMem( Dest: pointer;
63 Size: longint;
64 Data: Byte );
65
66// Returns A - B
67function PtrDiff( A, B: pointer ): longword;
68
69function Min( const a: longint;
70 const b: longint ): longint;
71
72function Max( const a: longint;
73 const b: longint ): longint;
74
75function Between( const Value: longint;
76 const Limit1: longint;
77 const Limit2: longint ): boolean;
78
79function PtrBetween( const Value: pointer;
80 const Limit1: pointer;
81 const Limit2: pointer ): boolean;
82
83Procedure AddList( Source, Dest: TList );
84
85Procedure AssignList( Source, Dest: TList );
86
87// Destroy the objects stored in List
88// and clear the list.
89Procedure ClearListAndObjects( List: TList );
90
91// Destroy the objects stored in the list
92// and then destroy the list itself
93// And set the reference to nil
94Procedure DestroyListAndObjects( Var List: TList );
95
96// Destroy the objects stored in the list.
97// You probably want to use one of the two functions above.
98Procedure DestroyListObjects( List: TList );
99
100// Returns the filename of the running app
101// (including drive/directory)
102Function GetApplicationFilename: string;
103
104// Returns the starting directory of the app
105Function GetApplicationDir: string;
106
107{$ifdef win32}
108type
109 TDaylightSavingStatus =
110 (
111 dssDisabled,
112 dssDaylightSaving,
113 dssNormal
114 );
115
116function GetDaylightSavingStatus: TDaylightSavingStatus;
117{$endif}
118
119type
120 TDataList = TList; // to help distinguish from TObjectList below
121
122type
123 TObjectListSortCompare = function ( Item1, Item2: TObject ): Integer;
124
125 // TObjectList has exactly the same functionality as TList,
126 // except that rather than using untyped pointers it expects
127 // object references. This means that incorrect typecasts can
128 // be detected when using the list
129 TObjectList = class( TList )
130 protected
131 function Get( index: integer ): TObject;
132 procedure Put( index: integer; Item: TObject );
133 public
134 function Add( item: TObject ): integer;
135 function First: TObject;
136 function IndexOf( item: TObject ): integer;
137 procedure Insert( Index: Integer; Item: TObject );
138 function Last: TObject;
139 function Remove( Item: TObject ): Integer;
140 procedure Sort( Compare: TObjectListSortCompare );
141 property Items[ index: integer ]: TObject read Get write Put; default;
142
143 // Additional:
144 // Copy the given list to this one.
145 procedure Assign( Source: TObjectList );
146 // Add the objects in the given list to this one
147 procedure AddList( Source: TObjectList );
148
149 end;
150
151// Raise an exception if the given Code is <> 0
152procedure CheckSystemError( Code: longword;
153 Message: string );
154
155{$ifdef os2}
156function GetUserProfileString( const AppName: string;
157 const KeyName: string;
158 const Default: string ): string;
159
160Procedure SetUserProfileString( const AppName: string;
161 const KeyName: string;
162 const Value: string );
163
164Procedure LoadDLLFunction( const DLLName: string;
165 const FunctionName: string;
166 var hDLL: HMODULE;
167 var F: pointer );
168
169{$endif}
170
171Implementation
172
173// Implementation ------------------------------------------
174
175Uses
176{$ifdef os2}
177 Dos, BseDos, BseTib, PmWin, PmGpi, PmDev, PmShl;
178{$else}
179 Windows, FileCtrl;
180{$endif}
181
182constructor TMyIniFile.CreateMe( const Path: string );
183begin
184{$ifdef os2}
185 Inherited Create( Path );
186{$else}
187 Inherited Create( Path );
188{$endif}
189end;
190
191destructor TMyIniFile.Destroy;
192begin
193{$ifdef os2}
194 Refresh;
195{$endif}
196 inherited Destroy;
197end;
198
199{$ifdef os2}
200function AllocateMemory( const Size: longint ): pointer;
201begin
202 GetMem( Result, size + sizeof( Size ) );
203 pLong( Result )^ := Size;
204 inc( Result, sizeof( Size ) );
205end;
206
207procedure DeallocateMemory( Var P: pointer );
208var
209 Size: longint;
210begin
211 if P = nil then
212 exit;
213
214 dec( P, sizeof( Size ) );
215 Size := pLong( P )^;
216 FreeMem( P, Size + sizeof( Size ) );
217 P := nil;
218end;
219{$endif}
220
221{$ifdef win32}
222Function GetAPIErrorString( ErrorCode: integer ): string;
223var
224 buffer: array[ 0..1000 ] of char;
225begin
226 if FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
227 nil, // no special message source
228 ErrorCode,
229 0, // use default language
230 Buffer,
231 Sizeof( Buffer ),
232 nil ) > 0
233 then // no arguments
234 Result:= IntToStr( ErrorCode ) + ': ' + Buffer
235 else
236 Result:= '(Unknown error)';
237
238end;
239
240Function GetLastAPIErrorString: string;
241begin
242 Result := GetAPIErrorString( GetLastError );
243end;
244{$endif}
245
246procedure AllocMemCopy( const Source: pointer;
247 var Dest: pointer;
248 const Size: longint );
249begin
250 GetMem( Dest, Size );
251 MemCopy( Source, Dest, Size );
252end;
253
254procedure MemCopy( const Source: pointer;
255 const Dest: pointer;
256 const Size: longint );
257begin
258 Move( Source^, Dest^, Size );
259end;
260
261procedure FillMem( Dest: pointer;
262 Size: longint;
263 Data: Byte );
264begin
265 FillChar( Dest^, Size, Data );
266end;
267
268function PtrDiff( A, B: pointer ): longword;
269begin
270 result:= longword( A ) - longword( B );
271end;
272
273Procedure AddList( Source, Dest: TList );
274var
275 i: longint;
276begin
277 // expand the destination list
278 // to what's required
279 Dest.Capacity := Dest.Capacity
280 + Source.Capacity;
281 for i:= 0 to Source.Count - 1 do
282 Dest.Add( Source[ i ] );
283end;
284
285Procedure AssignList( Source, Dest: TList );
286begin
287 Dest.Clear;
288 AddList( Source, Dest );
289end;
290
291{$ifdef win32}
292function GetDaylightSavingStatus: TDaylightSavingStatus;
293var
294 TimeZoneInfo: TIME_ZONE_INFORMATION;
295 ZoneID: DWORD;
296Begin
297 ZoneID:= GetTimeZoneInformation( TimeZoneInfo );
298 if TimeZoneInfo.DaylightBias = 0 then
299 Result:= dssDisabled
300 else if ZoneID = TIME_ZONE_ID_DAYLIGHT then
301 Result:= dssDaylightSaving
302 else
303 Result:= dssNormal;
304end;
305{$endif}
306
307// Destroy the objects stored in List
308// and clear the list.
309Procedure ClearListAndObjects( List: TList );
310begin
311 DestroyListObjects( List );
312 List.Clear;
313end;
314
315// Destroy the objects stored in the list
316// and then destroy the list itself.
317Procedure DestroyListAndObjects( Var List: TList );
318begin
319 if not Assigned( List ) then
320 exit;
321
322 DestroyListObjects( List );
323 List.Destroy;
324 List := nil;
325end;
326
327Procedure DestroyListObjects( List: TList );
328var
329 Index: longint;
330begin
331 for Index := 0 to List.Count - 1 do
332 begin
333 if List[ Index ] <> nil then
334 begin
335 TObject( List[ Index ] ).Destroy;
336 List[ Index ] := nil;
337 end;
338 end;
339end;
340
341function Min( const a: longint;
342 const b: longint ): longint;
343begin
344 if a<b then
345 result:= a
346 else
347 result:= b;
348end;
349
350function Max( const a: longint;
351 const b: longint ): longint;
352begin
353 if a>b then
354 result:= a
355 else
356 result:= b;
357end;
358
359function Between( const Value: longint;
360 const Limit1: longint;
361 const Limit2: longint ): boolean;
362begin
363 if Limit1 < Limit2 then
364 Result:= ( Value >= Limit1 ) and ( Value <= Limit2 )
365 else
366 Result:= ( Value >= Limit2 ) and ( Value <= Limit1 )
367end;
368
369function PtrBetween( const Value: pointer;
370 const Limit1: pointer;
371 const Limit2: pointer ): boolean;
372var
373 v, p1, p2: pchar;
374begin
375 v := Value;
376 p1 := Limit1;
377 p2 := Limit2;
378 if p1 < p2 then
379 Result:= ( V >= p1 ) and ( V <= p2 )
380 else
381 Result:= ( V >= p2 ) and ( V <= p1 )
382end;
383
384{$ifdef os2}
385Function GetApplicationFilename: string;
386var
387 pThreadInfo: PTIB;
388 pProcessInfo: PPIB;
389 ProcessName: cstring[ _MAX_PATH ];
390begin
391 DosGetInfoBlocks( pThreadInfo,
392 pProcessInfo );
393 DosQueryModuleName( pProcessInfo^.pib_hmte,
394 sizeof( ProcessName ),
395 ProcessName );
396 Result := ProcessName;
397end;
398{$else}
399Function GetApplicationFilename: string;
400var
401 ProcessName: array[ 0.._MAX_PATH ] of char;
402begin
403 GetModuleFileName( 0, // our own process
404 ProcessName,
405 sizeof( ProcessName ) );
406
407 Result := ProcessName;
408end;
409{$endif}
410
411Function GetApplicationDir: string;
412begin
413 Result := ExtractFilePath( GetApplicationFilename );
414end;
415
416{ TObjectList }
417
418function TObjectList.Add( item: TObject ): integer;
419begin
420 result := inherited Add( item );
421end;
422
423function TObjectList.First: TObject;
424begin
425 result := inherited First;
426end;
427
428function TObjectList.Get( index: integer ): TObject;
429begin
430 result := Items[ Index ];
431end;
432
433function TObjectList.IndexOf( item: TObject ): integer;
434begin
435 result := inherited IndexOf( item );
436end;
437
438procedure TObjectList.Insert( Index: Integer; Item: TObject );
439begin
440 inherited Insert( Index, Item );
441end;
442
443function TObjectList.Last: TObject;
444begin
445 result := inherited Last;
446end;
447
448procedure TObjectList.Put( index: integer; Item: TObject );
449begin
450 Items[ Index ] := Item;
451end;
452
453function TObjectList.Remove( Item: TObject ): Integer;
454begin
455 result := inherited Remove( Item );
456end;
457
458procedure QuickSortObjectList( SortList: PPointerList;
459 L, R: Integer;
460 CompareFunction: TObjectListSortCompare );
461var
462 I, J: Integer;
463 P, T: Pointer;
464begin
465 repeat
466 I := L;
467 J := R;
468 P := SortList^[(L + R) shr 1];
469 repeat
470 while CompareFunction( TObject( SortList^[I] ), P ) < 0 do
471 Inc(I);
472 while CompareFunction( TObject( SortList^[J] ), P) > 0 do
473 Dec(J);
474 if I <= J then
475 begin
476 T := SortList^[I];
477 SortList^[I] := SortList^[J];
478 SortList^[J] := T;
479 Inc(I);
480 Dec(J);
481 end;
482 until I > J;
483 if L < J then
484 QuickSortObjectList( SortList, L, J, CompareFunction );
485 L := I;
486 until I >= R;
487end;
488
489procedure TObjectList.Sort( Compare: TObjectListSortCompare );
490begin
491 if ( List <> nil ) and ( Count > 0 ) then
492 QuickSortObjectList( List, 0, Count - 1, Compare );
493end;
494
495procedure TObjectList.Assign( Source: TObjectList );
496begin
497 Clear;
498 AddList( Source );
499end;
500
501procedure TObjectList.AddList( Source: TObjectList );
502var
503 i: integer;
504begin
505 for i := 0 to Source.Count - 1 do
506 Add( Source[ i ] );
507end;
508
509{$ifdef os2}
510procedure Sleep( const milliseconds: longint );
511begin
512 DosSleep( milliseconds );
513end;
514{$endif}
515
516// Raise an exception if the given Code is <> 0
517procedure CheckSystemError( Code: longword;
518 Message: string );
519begin
520 if Code <> 0 then
521 raise Exception.Create( Message
522 + ': ['
523 + IntToStr( Code )
524 + '] '
525 + SysErrorMessage( Code ) );
526end;
527
528{$ifdef os2}
529function GetUserProfileString( const AppName: string;
530 const KeyName: string;
531 const Default: string ): string;
532var
533 Buffer: array[ 0..255 ] of char;
534 Len: longint;
535 i: longint;
536 szDefault: cstring;
537begin
538 szDefault := Default;
539 Len := PrfQueryProfileString( HINI_USERPROFILE, // user profile
540 AppName, // application
541 KeyName, // key
542 szDefault,
543 Buffer,
544 sizeof( Buffer ) );
545
546 // remove null terminator, if present
547 if Len > 0 then
548 if Buffer[ Len - 1 ] = #0 then
549 dec( Len );
550 Result := '';
551 for i := 0 to Len - 1 do
552 Result := Result + Buffer[ i ];
553end;
554
555Procedure SetUserProfileString( const AppName: string;
556 const KeyName: string;
557 const Value: string );
558begin
559 if not PrfWriteProfileString( HINI_USERPROFILE, // user profile
560 AppName, // application
561 KeyName, // key
562 Value ) then
563 raise Exception.Create(
564 'Error writing INI ['
565 + AppName
566 + '/'
567 + KeyName
568 + '] rc = '
569 + IntToHex( WinGetLastError( AppHandle ), 8 ) );
570end;
571
572procedure LoadDLLFunction( const DLLName: string;
573 const FunctionName: string;
574 var hDLL: HMODULE;
575 var F: pointer );
576var
577 ActualDLLName: string;
578 csErrorObject: cstring;
579 csFunctionName: cstring;
580 rc: APIRET;
581begin
582 F := nil;
583
584 if hDLL = NullHandle then
585 begin
586 ActualDLLName := GetApplicationDir + DLLName;
587 if not FileExists( ActualDLLName ) then
588 ActualDLLName := DLLName;
589
590 rc := DosLoadModule( csErrorObject,
591 sizeof( csErrorObject ),
592 ActualDLLName,
593 hDLL );
594 if rc <> 0 then
595 raise Exception.Create( DLLName
596 + ' could not be loaded: '
597 + SysErrorMessage( rc ) );
598
599 end;
600
601 csFunctionName := FunctionName;
602 rc := DosQueryProcAddr( hDLL,
603 0, // using by name
604 csFunctionName,
605 F );
606 if rc <> 0 then
607 raise Exception.Create( FunctionName
608 + ' in '
609 + DLLName
610 + ': '
611 + SysErrorMessage( rc ) );
612end;
613
614{$endif}
615
616const
617 LibVersion = 'V1.5.14'; // $SS_REQUIRE_NEW_VERSION$
618
619function GetACLLibraryVersion: string;
620begin
621 Result := LibVersion;
622end;
623
624Initialization
625End.
626
Note: See TracBrowser for help on using the repository browser.