home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / acl-lib.zip / ACLUtility.pas < prev    next >
Pascal/Delphi Source File  |  2000-07-19  |  7KB  |  283 lines

  1. Unit ACLUtility;
  2.  
  3. Interface
  4.  
  5. Uses
  6.   Classes, SysUtils, Forms, StdCtrls;
  7.  
  8. Type
  9.   TPrintOutput = procedure( TheText: string ) of object;
  10.   TTerminateCheck = function: boolean of object;
  11.   TProgressCallback = procedure( n, outof: integer;
  12.                                  Message: string ) of object;
  13.  
  14. const
  15.    _MAX_PATH = 260;    /* max. length of full pathname           */
  16.    _MAX_DRIVE = 3;     /* max. length of drive component         */
  17.    _MAX_DIR = 256;     /* max. length of path component          */
  18.    _MAX_FNAME = 256;   /* max. length of file name component     */
  19.    _MAX_EXT = 256;     /* max. length of extension component     */
  20.  
  21. Procedure MergeStringLists( Dest: TStringList;
  22.                             AdditionalList: TStringList );
  23.  
  24. procedure MemCopy( const Source, Dest: pointer; const Size: longint );
  25.  
  26. procedure FillMem( Dest: pointer;
  27.                    Size: longint;
  28.                    Data: Byte );
  29.  
  30. function Min( a, b: longint ): longint;
  31.  
  32. Procedure AssignList( Source, Dest: TList );
  33.  
  34. // Reverse the given list. It must be set to not sorted
  35. Procedure ReverseList( TheList: TStrings );
  36.  
  37. // Sort the given list into reverse alphabetical order
  38. //Procedure ReverseSortList( TheList: TStringList );
  39.  
  40. // Find the given string in the given list, using
  41. // case insensitive searching (and trimming)
  42. // returns -1 if not found
  43. Function FindString( TheString: string; TheList:TStrings ):longint;
  44.  
  45. Function SearchPath( PathEnvVar: string;
  46.                      Filename: string;
  47.                      Var FilenameFound: string ): boolean;
  48.  
  49. Function RunProgram( FileName: string;
  50.                      Parameters: string ): boolean;
  51.  
  52. {$ifdef win32}
  53. type
  54.   TDaylightSavingStatus =
  55.   (
  56.     dssDisabled,
  57.     dssDaylightSaving,
  58.     dssNormal
  59.   );
  60.  
  61. function GetDaylightSavingStatus: TDaylightSavingStatus;
  62. {$endif}
  63.  
  64. Implementation
  65.  
  66. Uses
  67. {$ifdef os2}
  68.   Dos, BseDos, Os2Def,
  69. {$else}
  70.   Windows, FileCtrl,
  71. {$endif}
  72.   ACLStringUtility, Dialogs,
  73.   ACLFindFunctions;
  74.  
  75. function Min( a, b: longint ): longint;
  76. begin
  77.   if a<b then
  78.    result:= a
  79.   else
  80.    result:= b;
  81. end;
  82.  
  83. Procedure MergeStringLists( Dest: TStringList;
  84.                             AdditionalList: TStringList );
  85. var
  86.   i: integer;
  87.   s: string;
  88. begin
  89.   for i:= 0 to AdditionalList.Count - 1 do
  90.   begin
  91.     s:= AdditionalList[ i ];
  92.     if FindString( s, Dest ) = -1 then
  93.       Dest.AddObject( s, AdditionalList.Objects[ i ] );
  94.   end;
  95. end;
  96.  
  97. procedure MemCopy( const Source, Dest: pointer; const Size: longint );
  98. begin
  99.   Move( Source^, Dest^, Size );
  100. end;
  101.  
  102. procedure FillMem( Dest: pointer;
  103.                    Size: longint;
  104.                    Data: Byte );
  105. begin
  106.   FillChar( Dest^, Size, Data );
  107. {  while Size > 0 do
  108.   begin
  109.     PBYTE( Dest )^:= Data;
  110.     inc( Dest );
  111.     dec( Size );
  112.   end;}
  113. end;
  114.  
  115. Procedure AssignList( Source, Dest: TList );
  116. var
  117.   i: longint;
  118. begin
  119.   Dest.Clear;
  120.   for i:= 0 to Source.Count - 1 do
  121.     Dest.Add( Source[ i ] );
  122. end;
  123.  
  124. {$ifdef os2}
  125.  
  126. Function SearchPath( PathEnvVar: string;
  127.                      Filename: string;
  128.                      Var FilenameFound: string ): boolean;
  129. var
  130.   szEnvVar: cstring;
  131.   szFilename: cstring;
  132.   szFilenameFound: cstring;
  133.   rc: APIRET;
  134. begin
  135.   Result:= false;
  136.   FilenameFound:= '';
  137.  
  138.   szEnvVar:= PathEnvVar;
  139.   szFilename:= Filename;
  140.   rc:= DosSearchPath( SEARCH_IGNORENETERRS
  141.                       + SEARCH_ENVIRONMENT
  142.                       + SEARCH_CUR_DIRECTORY,
  143.                       szEnvVar,
  144.                       szFilename,
  145.                       szFilenameFound,
  146.                       sizeof( szFilenameFound ) );
  147.  
  148.   if rc = 0 then
  149.   begin
  150.     Result:= true;
  151.     FilenameFound:= szFilenameFound;
  152.   end
  153. end;
  154.  
  155. Function RunProgram( FileName: string;
  156.                      Parameters: string ): boolean;
  157. var
  158.   Dir: string;
  159.   Found: boolean;
  160.   Dummy: string;
  161.   rc: longint;
  162.   Extension: string;
  163. begin
  164.   Dir:= ExtractFilePath( FileName );
  165.   if Dir = '' then
  166.     Found:= SearchPath( 'PATH',
  167.                         Filename,
  168.                         Dummy )
  169.   else
  170.     // file path specified...
  171.     Found:= FileExists( FileName );
  172.  
  173.   if not Found then
  174.   begin
  175.     Result:= false;
  176.     exit;
  177.   end;
  178.  
  179.   Result:= true;
  180.  
  181.   Extension:= ExtractFileExt( FileName );
  182.   if StringsSame( Extension, '.exe' ) then
  183.     Exec( FileName,
  184.           Parameters )
  185.   else
  186.     Exec( 'cmd.exe',
  187.           '/c '
  188.           + FileName
  189.           + ' '
  190.           + Parameters );
  191.  
  192. end;
  193. {$else}
  194. Function RunProgram( FileName: string;
  195.                      Parameters: string ): boolean;
  196. Var
  197.   StartupInfo: TStartupInfo;
  198.   ProcessInfo: TProcessInformation;
  199.   NameAndArgs: string;
  200. Begin
  201.   NameAndArgs:= FileName+' '+Parameters;
  202.  
  203.   // Initialize some variables to create a process
  204.   ZeroMemory( @StartupInfo, SizeOf( StartupInfo ) );
  205.  
  206.   StartupInfo.cb := SizeOf( StartupInfo );
  207.   StartupInfo.dwFlags := STARTF_USESTDHANDLES;
  208.  
  209.   // Create the process
  210.   Result:= CreateProcess( Nil, // use next param for exe name
  211.                           PChar( NameAndArgs ), // command line
  212.                           Nil, // no security attributes
  213.                           Nil, // no thread security attributes
  214.                           True, // do inherit handles
  215.                           CREATE_NEW_PROCESS_GROUP, // so we can send
  216.                           // it Ctrl signals
  217.                           Nil, // no new environment
  218.                           Nil, // use current directory
  219.                           StartupInfo,
  220.                           ProcessInfo );
  221.   if not Result then
  222.     exit;
  223.  
  224. end;
  225. {$endif}
  226.  
  227. Procedure ReverseList( TheList:TStrings );
  228. Var
  229.   TempList: TStringList;
  230.   i: integer;
  231. Begin
  232.   TempList:= TStringList.Create;
  233.   for i:=TheList.count-1 downto 0 do
  234.   begin
  235.     TempList.AddObject( TheList.Strings[i],
  236.                         TheList.Objects[i] );
  237.   end;
  238.   TheList.Assign( TempList );
  239.   TempList.Destroy;
  240. end;
  241.  
  242. Procedure ReverseSortList( TheList: TStringList );
  243. Begin
  244.   TheList.Sort;
  245.   ReverseList( TheList );
  246. end;
  247.  
  248. Function FindString( TheString: string; TheList:TStrings ):longint;
  249. Var
  250.   i: longint;
  251. Begin
  252.   for i:=0 to TheList.count-1 do
  253.   begin
  254.     if StringsSame( TheString, TheList[ i ] ) then
  255.     begin
  256.       // found
  257.       Result:=i;
  258.       exit;
  259.     end;
  260.   end;
  261.   Result:=-1;
  262. End;
  263.  
  264. {$ifdef win32}
  265. function GetDaylightSavingStatus: TDaylightSavingStatus;
  266. var
  267.   TimeZoneInfo: TIME_ZONE_INFORMATION;
  268.   ZoneID: DWORD;
  269. Begin
  270.   ZoneID:= GetTimeZoneInformation( TimeZoneInfo );
  271.   if TimeZoneInfo.DaylightBias = 0 then
  272.     Result:= dssDisabled
  273.   else if ZoneID = TIME_ZONE_ID_DAYLIGHT then
  274.     Result:= dssDaylightSaving
  275.   else
  276.     Result:= dssNormal;
  277. end;
  278. {$endif}
  279.  
  280. Initialization
  281. End.
  282.  
  283.