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

  1. Unit ACLFileUtility;
  2.  
  3. Interface
  4.  
  5. uses
  6.   SysUtils, Classes;
  7.  
  8. Function StripDrive( Path: string ): string;
  9.  
  10. Function ChangeDrive( Path: string;
  11.                       NewDrive: string ): string;
  12.                       
  13. Procedure MakeFileReadOnly( Filename: string );
  14.  
  15. Procedure MakeFileReadWrite( Filename: string );
  16.  
  17. // Deletes files incl readonly
  18. Function MyDeleteFile( Path: string ): boolean;
  19.  
  20. // Adds a slash to dir if not present
  21. Function AddSlash( Dir: string ): string;
  22.  
  23. // Remove slash from end of dir if present
  24. Function RemoveSlash( Dir: string ): string;
  25.  
  26. // Returns true if it succeeds in removing the directory
  27. // Always removes readonly files
  28. Function DeleteTree( path: string ): boolean;
  29.  
  30. Procedure ClearDirectory( Directory: string );
  31.  
  32. // Get the TMP directory
  33. Function TempDir: string;
  34.  
  35. // Return a list of files in the given Dir
  36. Procedure GetFilesForDir( Dir: string; List: TStrings );
  37.  
  38. // Return a list of files in the given Dir. using the given filter
  39. Procedure GetFilteredFilesForDir( Dir: string;
  40.                                   Filter: string;
  41.                                   List: TStrings );
  42.  
  43. // In the directory startpath, create directory and subdirectories
  44. // specified in DirsString
  45. // e.g. bob\bill\fred will make bob, then bill in bob, then fred in bob
  46. // returns path to lowest dir created
  47. Function MakeDirs( FullPath: string ):string;
  48.  
  49. // Returns current path incl drive
  50. Function GetCurrentDir: string;
  51.  
  52. // Returns date/time of last modification of file
  53. // Returns 0.0 if error
  54. Function FileDateTime( filename: string ): TDateTime;
  55.  
  56. // Returns true if file exists and is read only
  57. Function FileIsReadOnly( filename: string ):Boolean;
  58.  
  59. {$ifdef os2}
  60. Function ExtractFileDrive( Path: string ): string;
  61.  
  62. Function DirectoryExists( Dir: string ):boolean;
  63.  
  64. Procedure AnsiReadLn( Var TheFile: Text;
  65.                       Var Line: AnsiString );
  66.  
  67. Function GetFileSize( Filename: string ): longint;
  68.  
  69. {$endif}
  70.  
  71. Implementation
  72.  
  73. uses
  74. {$ifdef os2}
  75.   BseDos, DOS, Os2Def,
  76. {$else}
  77.   Windows, FileCtrl,
  78. {$endif}
  79.   ACLFindFunctions, ACLStringUtility;
  80.  
  81. {$ifdef os2}
  82. Function ExtractFileDrive( Path: string ): string;
  83. begin
  84.   Result:= '';
  85.   if Length( Path ) < 2 then
  86.     exit;
  87.   if Path[ 2 ] = ':' then
  88.     Result:= Copy( Path, 1, 2 );
  89. end;
  90. {$endif}
  91.  
  92. Function ChangeDrive( Path: string;
  93.                       NewDrive: string ): string;
  94. var
  95.   CurrentDrive: string;
  96. begin
  97.   Result:= Path;
  98.   CurrentDrive:= ExtractFileDrive( Path );
  99.   Result:= RemoveSlash( NewDrive )
  100.            + RightFrom( Path, Length( CurrentDrive ) + 1 );
  101. end;
  102.  
  103. Function StripDrive( Path: string ): string;
  104. begin
  105.   Result:= ChangeDrive( Path, '' );
  106. end;
  107.  
  108. Procedure MakeFileReadOnly( Filename: string );
  109. var
  110.   Attributes: longint;
  111. begin
  112.   Attributes:= FileGetAttr( FileName );
  113.   Attributes:= Attributes or faReadonly;
  114.   FileSetAttr( FileName, Attributes );
  115. end;
  116.  
  117. Procedure MakeFileReadWrite( Filename: string );
  118. var
  119.   Attributes: longint;
  120. begin
  121.   Attributes:= FileGetAttr( FileName );
  122.   Attributes:= Attributes and not faReadonly;
  123.   FileSetAttr( FileName, Attributes );
  124. end;
  125.  
  126. // Deletes files incl readonly
  127. Function MyDeleteFile( Path: string ): boolean;
  128. begin
  129.   MakeFileReadWrite( Path );
  130.   {$ifdef os2}
  131.   Result:= DeleteFile( Path );
  132.   {$else}
  133.   Result:= DeleteFile( PChar( Path ) );
  134.   {$endif}
  135. end;
  136.  
  137. // Adds a slash if need to Dir
  138. function AddSlash( Dir: string ): string;
  139. begin
  140.   if Dir='' then
  141.     Result:= '\'
  142.   else
  143.     if Dir[ length( Dir ) ]<>'\' then
  144.       Result:= Dir + '\'
  145.     else
  146.       Result:= Dir;
  147. end;
  148.  
  149. // Remove slash from end of dir if present
  150. function RemoveSlash( Dir: string ): string;
  151. begin
  152.   Result:= Dir;
  153.   if Dir<>'' then
  154.     if Result[ length( Result ) ]='\' then
  155.       Delete( Result, length( Result ), 1 );
  156. end;
  157.  
  158. Function DeleteTree( path: string ): boolean;
  159. Var
  160.   SearchResults: TSearchData;
  161.   rc:integer;
  162.   Directories: TStringList;
  163.   DirectoryIndex: longint;
  164.   FullPath: string;
  165. Begin
  166.   path:= AddSlash( path );
  167.   Directories:= TStringList.Create;
  168.   rc:= MyFindFirst( path+'*', SearchResults );
  169.   while rc = 0 do
  170.   begin
  171.     if ( SearchResults.Name <> '.' )
  172.        and ( SearchResults.Name <> '..' ) then
  173.     begin
  174.       FullPath:= path + SearchResults.Name;
  175.       if SearchResults.Attr And faDirectory > 0 then
  176.         Directories.Add( FullPath )
  177.       else
  178.         MyDeleteFile( FullPath );
  179.     end;
  180.     rc:= MyFindNext( SearchResults );
  181.   end;
  182.  
  183.   SysUtils.FindClose( SearchResults );
  184.  
  185.   // Now delete directories
  186.   for DirectoryIndex:= 0 to Directories.Count-1 do
  187.     DeleteTree( Directories[ DirectoryIndex ] );
  188.  
  189.   Directories.Destroy;
  190.  
  191.   // Finally remove the directory itself
  192.   RmDir( LeftWithout( path, 1 ) );
  193.   Result:= (IOResult=0);
  194. End;
  195.  
  196. Procedure ClearDirectory( Directory: string );
  197. Var
  198.   SearchResults: TSearchData;
  199.   rc:integer;
  200.   FileName: string;
  201. Begin
  202.   Directory:= AddSlash( Directory );
  203.   rc:= MyFindFirst( Directory + '*', SearchResults );
  204.   while rc=0 do
  205.   begin
  206.     FileName:= Directory + SearchResults.Name;
  207.     if SearchResults.Attr and faDirectory = 0 then
  208.       MyDeleteFile( FileName );
  209.     rc:= MyFindNext( SearchResults );
  210.   end;
  211.   SysUtils.FindClose( SearchResults );
  212. End;
  213.  
  214. Function TempDir: string;
  215. {$ifdef win32}
  216. var
  217.   Buffer: array[ 0.. MAX_PATH ] of char;
  218. {$endif}
  219. Begin
  220. {$ifdef os2}
  221.   Result:= GetEnv( 'TMP' );
  222. {$else}
  223.   GetTempPath( sizeof( Buffer ), Buffer );
  224.   Result:= StrPas( Buffer );
  225. {$endif}
  226.   Result:= AddSlash( Result );
  227. end;
  228.  
  229. Procedure GetFilesForDir( Dir: string; List: TStrings );
  230. Begin
  231.   GetFilteredFilesForDir( Dir, '*', List );
  232. End;
  233.  
  234. // Return a list of files in the given Dir. using the given filter
  235. Procedure GetFilteredFilesForDir( Dir: string;
  236.                                   Filter: string;
  237.                                   List: TStrings );
  238. Var
  239.   SearchResults: TSearchData;
  240.   rc:integer;
  241. Begin
  242.   Dir:= AddSlash( Dir );
  243.   rc:= MyFindFirst( Dir+Filter, SearchResults );
  244.   while rc=0 do
  245.   begin
  246.     if SearchResults.Attr and faDirectory = 0 then
  247.       List.Add( dir + SearchResults.Name );
  248.     rc:= MyFindNext( SearchResults );
  249.   end;
  250.   MyFindClose( SearchResults );
  251. End;
  252.  
  253. Function MakeDirs( FullPath: string ): string;
  254. Var
  255.   RemainingDirs: string;
  256.   NewDir: string;
  257.   CreatePath:string;
  258. Begin
  259.   CreatePath:= '';
  260.  
  261.   // Iterate thru specified dirs
  262.   RemainingDirs:= FullPath;
  263.   while trim( RemainingDirs )<>'' do
  264.   begin
  265.     NewDir:= ExtractNextValue( RemainingDirs, '\' );
  266.     if NewDir<>'' then
  267.     begin
  268.       CreatePath:= CreatePath + NewDir;
  269.       if not DirectoryExists( CreatePath ) then
  270.         MkDir( CreatePath );
  271.       CreatePath:= CreatePath + '\';
  272.     end;
  273.   end;
  274.   // Remove the end \
  275.   Result:= RemoveSlash( CreatePath );
  276. end;
  277.  
  278. // Returns current path incl drive
  279. {$ifdef os2}
  280. Function GetCurrentDir: string;
  281. Var
  282.   CurrentDir: cstring[ 200 ];
  283.   CurrentDirLen: longword;
  284.   CurrentDisk: longword;
  285.   DiskMap: longword;
  286. Begin
  287.   CurrentDirLen:= sizeof( CurrentDir );
  288.   DosQueryCurrentDisk( CurrentDisk, DiskMap );
  289.   DosQueryCurrentDir( CurrentDisk,
  290.                       CurrentDir,
  291.                       CurrentDirLen );
  292.  
  293.   // Form drive part
  294.   Result:= Chr( Ord( 'A' ) + CurrentDisk - 1 ) + ':\';
  295.   // Add directory
  296.   Result:= AddSlash( Result + CurrentDir );
  297. End;
  298. {$else}
  299. Function GetCurrentDir: string;
  300. begin
  301.   GetDir( 0, Result );
  302. end;
  303. {$endif}
  304.  
  305. Function FileDateTime( filename: string ):TDateTime;
  306. Var
  307.   FileDate: longint;
  308. Begin
  309.   FileDate:=FileAge( filename );
  310.   if FileDate=-1 then
  311.   begin
  312.     Result:=0.0;
  313.     exit;
  314.   end;
  315.   Result:=FileDateToDateTime( FileDate );
  316. end;
  317.  
  318. Function FileIsReadOnly( filename: string ):Boolean;
  319. Begin
  320.   Result:=( FileGetAttr( filename ) and faReadonly ) >0;
  321. End;
  322.  
  323. Procedure AnsiReadLn( Var TheFile: Text;
  324.                       Var Line: AnsiString );
  325. Var
  326.   C: Char;
  327.   FoundCR: boolean;
  328. Begin
  329.   Line:= '';
  330.   FoundCR:= false;
  331.   while not eof( TheFile ) do
  332.   begin
  333.     Read( TheFile, C );
  334.     if ( C=#10 ) then
  335.     begin
  336.       if FoundCR then
  337.         exit; // reached end of line
  338.     end
  339.     else
  340.     begin
  341.       if FoundCR then
  342.         // last CR was not part of CR/LF so add to string
  343.         line:= line+#13;
  344.     end;
  345.     FoundCR:= (C=#13);
  346.     if not FoundCR then // don't handle 13's till later
  347.     begin
  348.       line:= line+C;
  349.     end;
  350.   end;
  351.  
  352.   if FoundCR then
  353.   // CR was last char of file, but no LF so add to string
  354.     line:= line+#13;
  355. End;
  356.  
  357. {$ifdef os2}
  358. Function DirectoryExists( Dir: string ):boolean;
  359. Var
  360.   SearchRec: TSearchData;
  361.   rc: longint;
  362.   DriveMap: LongWord;
  363.   ActualDrive: LongWord;
  364.   Drive: Char;
  365.   DriveNum: longword;
  366.   DriveBit: longword;
  367. Begin
  368.   Result:= false;
  369.   Dir:= RemoveSlash( Dir );
  370.   if Dir = '' then
  371.   begin
  372.     Result:= true;
  373.     exit;
  374.   end;
  375.   if length( Dir ) = 2 then
  376.     if Dir[ 2 ] = ':' then
  377.     begin
  378.       // a drive only has been specified
  379.       Drive:= UpCase( Dir[ 1 ] );
  380.       if ( Drive < 'A' ) or ( Drive > 'Z' ) then
  381.         exit;
  382.       DosQueryCurrentDisk( ActualDrive, DriveMap );
  383.       DriveNum:= Ord( Drive ) - Ord( 'A' ) + 1; // A -> 1, B -> 2...
  384.       DriveBit:= 1 shl (DriveNum-1); // 2^DriveNum
  385.       if ( DriveMap and ( DriveBit ) > 0 ) then
  386.         Result:= true;
  387.       exit;
  388.     end;
  389.  
  390.   rc:= MyFindFirst( Dir, SearchRec );
  391.   if rc = 0 then
  392.     if ( SearchRec.Attr and faDirectory )>0 then
  393.       Result:= true;
  394.   MyFindClose( SearchRec );
  395. End;
  396.  
  397. Function GetFileSize( Filename: string ): longint;
  398. var
  399.   szFilename: Cstring;
  400.   FileInfo: FILESTATUS3;     /* File info buffer */
  401.   rc: APIRET;                   /* Return code */
  402. begin
  403.   szFilename:= FileName;
  404.   rc := DosQueryPathInfo( szFilename,
  405.                           1,
  406.                           FileInfo,
  407.                           sizeof( FileInfo ) );
  408.   if rc = 0 then
  409.     Result:= FileInfo.cbFile
  410.   else
  411.     Result:= -1;
  412. end;
  413.  
  414. {$endif}
  415.  
  416. Initialization
  417. End.
  418.