home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 20.ddi / CLASSSRC.PAK / FILE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  4.2 KB  |  151 lines

  1. /*--------------------------------------------------------------------*/
  2. /*                                                                    */
  3. /*  FILE.CPP                                                          */
  4. /*                                                                    */
  5. /*  Copyright (c) Borland International 1993                          */
  6. /*  All Rights Reserved                                               */
  7. /*                                                                    */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. #if !defined( __LIMITS_H )
  11. #include <limits.h>
  12. #endif    // __LIMITS_H
  13.  
  14. #if !defined( __DIR_H )
  15. #include <dir.h>
  16. #endif  // __DIR_H
  17.  
  18. #if !defined( __DOS_H )
  19. #include <dos.h>
  20. #endif  // __DOS_H
  21.  
  22. #if !defined( __IOSTREAM_H )
  23. #include <iostream.h>
  24. #endif  // __IOSTREAM_H
  25.  
  26. #if !defined( __CLASSLIB_FILE_H )
  27. #include "classlib\file.h"
  28. #endif
  29.  
  30. int TFile::Open( const char _BIDSFAR *name, uint16 access, uint16 permission )
  31. {
  32.     const unsigned shareFlags =
  33.         Compat | DenyNone | DenyRead | DenyWrite | DenyRdWr | NoInherit;
  34.     if( IsOpen() )
  35.         return 0;
  36.     Handle = ::sopen( name,
  37.                       access & ~shareFlags,
  38.                       access & shareFlags,
  39.                       permission );
  40.     return IsOpen();
  41. }
  42.  
  43. int TFile::Close()
  44. {
  45.     if( IsOpen() && ::close(Handle) == 0) 
  46.         {
  47.         Handle = FileNull;
  48.         return 1;
  49.         }
  50.     else
  51.         return 0;
  52. }
  53.  
  54. long TFile::Length() const
  55. {
  56.     return ::filelength( Handle );
  57. }
  58.  
  59. int TFile::GetStatus( TFileStatus _BIDSFAR & status ) const
  60. {
  61.     struct ftime ftime;
  62.     if( ::getftime(Handle, &ftime) != 0 )
  63.         return 0;
  64.     TDate fileDate( ftime.ft_day, ftime.ft_month, ftime.ft_year+80 );
  65.     status.createTime = TTime( fileDate,
  66.                                  ftime.ft_hour,
  67.                                  ftime.ft_min,
  68.                                  ftime.ft_tsec*2 );
  69.     status.modifyTime = status.createTime;
  70.     status.accessTime = status.createTime;
  71.     status.size = Length();
  72.     status.attribute = 0;
  73.     status.fullName[0] = '\0';
  74.     return 1;
  75. }
  76.  
  77. struct dos_ftime
  78. {
  79.     unsigned tsec : 5;
  80.     unsigned min  : 6;
  81.     unsigned hour : 5;
  82. };
  83.  
  84. struct dos_fdate
  85. {
  86.     unsigned day  : 5;
  87.     unsigned mon  : 4;
  88.     unsigned year : 7;
  89. };
  90.  
  91. int TFile::GetStatus( const char _BIDSFAR *name, TFileStatus _BIDSFAR & status )
  92. {
  93.     if( ::_fullpath( status.fullName, name, sizeof(status.fullName) ) == 0 )
  94.         {
  95.         status.fullName[0] = '\0';
  96.         return 0;
  97.         }
  98.     ffblk blk;
  99.     const uint16 FA_ALL = FA_RDONLY | FA_HIDDEN | FA_SYSTEM |
  100.                           FA_LABEL | FA_DIREC | FA_ARCH;
  101.     if( findfirst( status.fullName, &blk, FA_ALL ) != 0 )
  102.         return 0;
  103.  
  104.     union
  105.     {
  106.         dos_ftime time;
  107.         dos_fdate date;
  108.         unsigned value;
  109.     };
  110.  
  111.     value = blk.ff_fdate;
  112.     TDate fileDate( date.day, date.mon, date.year+80 );
  113.     value = blk.ff_ftime;
  114.     status.createTime = TTime( fileDate,
  115.                                  time.hour,
  116.                                  time.min,
  117.                                  time.tsec*2 );
  118.     
  119.     status.modifyTime = status.createTime;
  120.     status.accessTime = status.createTime;
  121.     status.size = blk.ff_fsize;
  122.     status.attribute = blk.ff_attrib;
  123.     return 1;
  124. }
  125.  
  126. int TFile::SetStatus( const char _BIDSFAR *name, const TFileStatus _BIDSFAR & status )
  127. {
  128.     int attr = ::_rtl_chmod( name, 0 );
  129.     if( attr & FA_RDONLY )
  130.         return 0;
  131.  
  132.     ftime fileTime;
  133.     fileTime.ft_tsec = status.createTime.Second()/2;
  134.     fileTime.ft_min = status.createTime.Minute();
  135.     fileTime.ft_hour = status.createTime.Hour();
  136.     TDate date( status.createTime );
  137.     fileTime.ft_day = date.DayOfMonth();
  138.     fileTime.ft_month = date.Month();
  139.     fileTime.ft_year = date.Year()-80;
  140.  
  141.     TFile file( name, ReadWrite | DenyWrite );
  142.     if( ::setftime( file.GetHandle(), &fileTime ) != 0 )
  143.         return 0;
  144.     if( ::chsize( file.GetHandle(), status.size ) != 0 )
  145.         return 0;
  146.     if( ::_rtl_chmod( name, 1, status.attribute ) == -1 )
  147.         return 0;
  148.     return 1;
  149. }
  150.  
  151.