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

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  FILE.H                                                                */
  4. /*                                                                        */
  5. /*  Copyright (c) 1993 Borland International                              */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __CLASSLIB_FILE_H )
  11. #define __CLASSLIB_FILE_H
  12.  
  13. #ifndef __STDLIB_H
  14. #include <stdlib.h>
  15. #endif
  16.  
  17. #ifndef __STDIO_H
  18. #include <stdio.h>
  19. #endif
  20.  
  21. #ifndef __FCNTL_H
  22. #include <Fcntl.h>
  23. #endif
  24.  
  25. #ifndef __STAT_H
  26. #include <sys/stat.h>
  27. #endif
  28.  
  29. #ifndef __SHARE_H
  30. #include <share.h>
  31. #endif
  32.  
  33. #ifndef __IO_H
  34. #include <io.h>
  35. #endif
  36.  
  37. #if !defined( __SYSTYPES_H )
  38. #include <systypes.h>
  39. #endif
  40.  
  41. #if !defined( __CLASSLIB_DEFS_H )
  42. #include "classlib\defs.h"
  43. #endif  // __CLASSLIB_DEFS_H
  44.  
  45. #if !defined( __CLASSLIB__TIME_H )
  46. #include "classlib\time.h"
  47. #endif
  48.  
  49. #if !defined( __CLASSLIB__DATE_H )
  50. #include "classlib\date.h"
  51. #endif
  52.  
  53. #if defined( BI_CLASSLIB_NO_po )
  54. #pragma option -po-
  55. #endif
  56.  
  57. struct TFileStatus
  58. {
  59.     TTime createTime;
  60.     TTime modifyTime;
  61.     TTime accessTime;
  62.     long size;
  63.     uint8 attribute;
  64.     char fullName[_MAX_PATH];
  65. };
  66.  
  67. class _EXPCLASS ostream;
  68.  
  69. ostream _BIDSFAR & _BIDSFUNC operator << ( ostream _BIDSFAR &, const TFileStatus _BIDSFAR & );
  70.  
  71. class _BIDSCLASS TFile
  72. {
  73.  
  74. public:
  75.  
  76.     enum { FileNull = -1 };
  77.  
  78.     enum{
  79.     //  Open mode
  80.         ReadOnly    = O_RDONLY,
  81.         ReadWrite   = O_RDWR,
  82.         WriteOnly   = O_WRONLY,
  83.         Create      = O_CREAT | O_TRUNC,
  84.         CreateExcl  = O_CREAT | O_EXCL,
  85.         Append      = O_APPEND,
  86.  
  87.     //  Share mode
  88. #if defined( __WIN32__ )
  89.         Compat      = 0,        // SH_COMPAT
  90.         DenyNone    = SH_DENYNO,
  91. #else
  92.         Compat      = SH_COMPAT,
  93.         DenyNone    = SH_DENYNONE,
  94. #endif
  95.         DenyRead    = SH_DENYRD,
  96.         DenyWrite   = SH_DENYWR,
  97.         DenyRdWr    = SH_DENYRW,
  98.         NoInherit   = O_NOINHERIT
  99.         };
  100.  
  101.     enum{
  102.     //  Permission flags for Create
  103.         PermRead    = S_IREAD,
  104.         PermWrite   = S_IWRITE,
  105.         PermRdWr    = S_IREAD | S_IWRITE
  106.         };
  107.  
  108.     enum{
  109.     //  DOS file type flags
  110.         Normal      = 0x00,
  111.         RdOnly      = 0x01,
  112.         Hidden      = 0x02,
  113.         System      = 0x04,
  114.         Volume      = 0x08,
  115.         Directory   = 0x10,
  116.         Archive     = 0x20
  117.         };
  118.  
  119.     enum seek_dir
  120.         { 
  121.         beg = 0, 
  122.         cur = 1, 
  123.         end = 2 
  124.         };
  125.  
  126.     TFile();
  127.     TFile( int handle );
  128.     TFile( const TFile _BIDSFAR & file );
  129.     TFile( const char _BIDSFAR * name,
  130.            uint16 access=ReadOnly,
  131.            uint16 permission=PermRdWr );
  132.    ~TFile();
  133.  
  134.     int Open( const char _BIDSFAR * name, uint16 access, uint16 permission );
  135.     int Close();
  136.  
  137.     int GetHandle() const;
  138.  
  139.     long Position() const;
  140.     long Length() const;
  141.     void Length( long newLen );
  142.     long Seek( long offset, int origin = beg );
  143.     long SeekToBegin();
  144.     long SeekToEnd();
  145.     int GetStatus( TFileStatus _BIDSFAR & status ) const;
  146.     int IsOpen() const;
  147.  
  148.     int Read( void _BIDSFAR *buffer, int numBytes );
  149. #if defined( BI_HAS_HREADWRITE )
  150.     long Read( void __huge *buffer, long numBytes );
  151. #endif
  152.     int Write( const void _BIDSFAR *buffer, int numBytes );
  153. #if defined( BI_HAS_HREADWRITE )
  154.     long Write( const void __huge *buffer, long numBytes );
  155. #endif
  156.     void Flush();
  157.  
  158.     void LockRange( long position, uint32 count );
  159.     void UnlockRange(long Position, uint32 count );
  160.  
  161.     static int GetStatus( const char _BIDSFAR *name, TFileStatus _BIDSFAR & status );
  162.     static int SetStatus( const char _BIDSFAR *name, const TFileStatus _BIDSFAR & status );
  163.     static void Remove( const char _BIDSFAR *name );
  164.     static void Rename( const char _BIDSFAR *oldName, const char _BIDSFAR *newName );
  165.  
  166. private:
  167.  
  168.     int Handle;         // Low-level C file handle
  169.     int ShouldClose;    // Should C++ object close file on dtor
  170.  
  171. };
  172.  
  173. //---------------------------------------------------------------------------
  174. // Inlines
  175. //---------------------------------------------------------------------------
  176.  
  177. inline TFile::TFile() : Handle( FileNull ), ShouldClose(0)
  178. {
  179. }
  180.  
  181. inline TFile::TFile( int h ) : Handle(h), ShouldClose(0)
  182. {
  183. }
  184.  
  185. inline TFile::TFile( const TFile& file) : ShouldClose(1)
  186. {
  187.     Handle = ::dup( file.Handle );
  188. }
  189.  
  190. inline TFile::TFile(const char *name, uint16 access, uint16 permission) :
  191.     Handle(FileNull),
  192.     ShouldClose(1)
  193. {
  194.     Open( name, access, permission );
  195. }
  196.  
  197. inline TFile::~TFile()
  198. {
  199.     if( IsOpen() && ShouldClose )
  200.         ::close(Handle);
  201. }
  202.  
  203. inline int TFile::GetHandle() const
  204.     return Handle; 
  205. }
  206.  
  207. inline long TFile::Position() const
  208. {
  209.     return ::tell(Handle);
  210. }
  211.  
  212. inline int TFile::IsOpen() const
  213.     return Handle > FileNull; 
  214. }
  215.  
  216. inline void TFile::Length( long newLen )
  217. {
  218.     ::chsize( Handle, newLen);
  219. }
  220.  
  221. inline long TFile::Seek( long offset, int origin )
  222. {
  223.     return ::lseek( Handle, offset, origin);
  224. }
  225.  
  226. inline long TFile::SeekToBegin()
  227. {
  228.     return ::lseek( Handle, 0, beg );
  229. }
  230.  
  231. inline long TFile::SeekToEnd()
  232. {
  233.     return ::lseek( Handle, 0, end );
  234. }
  235.  
  236. inline int TFile::Read( void *buffer, int numBytes )
  237. {
  238.     return ::_read( Handle, buffer, numBytes );
  239. }
  240.  
  241. inline int TFile::Write( const void *buffer, int numBytes)
  242. {
  243.     return ::_write( Handle, buffer, numBytes );
  244. }
  245.  
  246. inline void TFile::Flush()
  247. {
  248.     ::close(::dup(Handle));
  249. }
  250.  
  251. inline void TFile::LockRange( long position, uint32 count )
  252. {
  253.     ::lock( Handle, position, count );
  254. }
  255.  
  256. inline void TFile::UnlockRange( long position, uint32 count )
  257. {
  258.     ::unlock( Handle, position, count );
  259. }
  260.  
  261. inline void TFile::Remove( const char *name )
  262. {
  263.     ::remove(name);
  264. }
  265.  
  266. inline void TFile::Rename( const char *oldName, const char *newName )
  267. {
  268.     ::rename( oldName, newName );
  269. }
  270.  
  271. #if defined( BI_HAS_HREADWRITE )
  272. inline long TFile::Read(void __huge* Buffer, long NumBytes)
  273. {
  274.     return _hread( Handle, Buffer, NumBytes );
  275. }
  276.  
  277. inline long TFile::Write(const void __huge* Buffer, long NumBytes)
  278. {
  279.     return _hwrite(Handle, Buffer, NumBytes);
  280. }
  281. #endif
  282.  
  283. #if defined( BI_CLASSLIB_NO_po )
  284. #pragma option -po.
  285. #endif
  286.  
  287. #endif  // __CLASSLIB_FILE_H
  288.  
  289.