home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classinc.pak / FILE.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  7KB  |  298 lines

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