home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / h.z / FSTREAM.H < prev    next >
C/C++ Source or Header  |  1996-07-24  |  6KB  |  206 lines

  1. //
  2. //  fstream.h    File I/O streams
  3. //
  4. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  5. //
  6. #ifndef _FSTREAM_H_INCLUDED
  7. #define _FSTREAM_H_INCLUDED
  8.  
  9. #ifndef __cplusplus
  10. #error fstream.h is for use with C++
  11. #endif
  12.  
  13. #ifndef _COMDEF_H_INCLUDED
  14.  #include <_comdef.h>
  15. #endif
  16. #ifndef _IOSTREAM_H_INCLUDED
  17.  #include <iostream.h>
  18. #endif
  19.  
  20. // POSIX file handle:
  21. typedef int filedesc;
  22.  
  23. // **************************** FILEBUF **************************************
  24. #if defined(_M_IX86)
  25.   #pragma pack(__push,1);
  26. #else
  27.   #pragma pack(__push,8);
  28. #endif
  29. class _WPRTLINK filebuf : public streambuf {
  30. public:
  31.     static int const openprot;  // default file protection
  32.     enum sh_mode {              // sharing mode
  33.         sh_compat = 0x0000,     // - compatibility mode
  34.         sh_read   = 0x1000,     // - allow others to read
  35.         sh_write  = 0x2000,     // - allow others to write
  36.         sh_none   = 0x4000      // - do not allow others to read or write
  37.     };
  38.     typedef int shmode;
  39.  
  40.     filebuf();
  41.     filebuf( filedesc __fd );
  42.     filebuf( filedesc __fd, char *__buf, int __len );
  43.     ~filebuf();
  44.  
  45.     int       is_open() const;
  46.     filedesc  fd() const;
  47.     filebuf  *attach( filedesc __fd );
  48.     filebuf  *open( char const    *__name,
  49.                     ios::openmode  __mode,
  50.                     int            __prot = openprot );
  51.     filebuf  *close();
  52.  
  53.     virtual int        pbackfail( int __c );
  54.     virtual int        overflow( int = EOF );
  55.     virtual int        underflow();
  56.     virtual streambuf *setbuf( char *__buf, int __len );
  57.     virtual streampos  seekoff( streamoff     __offset,
  58.                                 ios::seekdir  __direction,
  59.                                 ios::openmode __ignored );
  60.     virtual int        sync();
  61.  
  62. private:
  63.     filedesc      __file_handle;
  64.     ios::openmode __file_mode;
  65.     char          __unbuffered_get_area[ DEFAULT_PUTBACK_SIZE+1 ];
  66.     char          __attached : 1;
  67.     int           : 0;
  68. };
  69. #pragma pack(__pop);
  70.  
  71. inline filedesc filebuf::fd() const {
  72.     return( __file_handle );
  73. }
  74.  
  75. inline int filebuf::is_open() const {
  76.     return( __file_handle != EOF );
  77. }
  78.  
  79. // **************************** FSTREAMBASE **********************************
  80. #if defined(_M_IX86)
  81.   #pragma pack(__push,1);
  82. #else
  83.   #pragma pack(__push,8);
  84. #endif
  85. class _WPRTLINK fstreambase : virtual public ios {
  86. public:
  87.     int       is_open() const;
  88.     filedesc  fd() const;
  89.     void      attach( filedesc __fd );
  90.     void      open( char const    *__name,
  91.                     ios::openmode  __mode,
  92.                     int            __prot = filebuf::openprot );
  93.     void      close();
  94.     filebuf  *rdbuf() const;
  95.     void      setbuf( char *__buf, int __len );
  96.  
  97. protected:
  98.     fstreambase();
  99.     fstreambase( char const    *__name,
  100.                  ios::openmode  __mode,
  101.                  int            __prot = filebuf::openprot );
  102.     fstreambase( filedesc __fd );
  103.     fstreambase( filedesc __fd, char *__buf, int __len );
  104.     ~fstreambase();
  105.  
  106. private:
  107.     filebuf   __flbuf;
  108. };
  109. #pragma pack(__pop);
  110.  
  111. inline filedesc fstreambase::fd() const {
  112.     __lock_it( __i_lock );
  113.     filebuf *__fb = rdbuf();
  114.     return( (__fb == NULL) ? EOF : __fb->fd() );
  115. }
  116.  
  117. inline int fstreambase::is_open() const {
  118.     __lock_it( __i_lock );
  119.     filebuf *__fb = rdbuf();
  120.     return( (__fb == NULL) ? 0 : __fb->is_open() );
  121. }
  122.  
  123. inline filebuf *fstreambase::rdbuf() const {
  124.     return( (filebuf *) ios::rdbuf() );
  125. }
  126.  
  127. // **************************** IFSTREAM *************************************
  128. #if defined(_M_IX86)
  129.   #pragma pack(__push,1);
  130. #else
  131.   #pragma pack(__push,8);
  132. #endif
  133. class _WPRTLINK ifstream : public fstreambase, public istream {
  134. public:
  135.     ifstream();
  136.     ifstream( char const    *__name,
  137.               ios::openmode  __mode = ios::in,
  138.               int            __prot = filebuf::openprot );
  139.     ifstream( filedesc __fd );
  140.     ifstream( filedesc __fd, char *__buf, int __len );
  141.     ~ifstream();
  142.  
  143.     void open( char const    *__name,
  144.                ios::openmode  __mode = ios::in,
  145.                int            __prot = filebuf::openprot );
  146. };
  147. #pragma pack(__pop);
  148.  
  149. inline void ifstream::open( char const *__n, ios::openmode __m, int __p ) {
  150.     fstreambase::open( __n, __m, __p );
  151. }
  152.  
  153. // **************************** OFSTREAM *************************************
  154. #if defined(_M_IX86)
  155.   #pragma pack(__push,1);
  156. #else
  157.   #pragma pack(__push,8);
  158. #endif
  159. class _WPRTLINK ofstream : public fstreambase, public ostream {
  160. public:
  161.     ofstream();
  162.     ofstream( char const    *__name,
  163.               ios::openmode  __mode = ios::out,
  164.               int            __prot = filebuf::openprot );
  165.     ofstream( filedesc __fd );
  166.     ofstream( filedesc __fd, char *__buf, int __len );
  167.     ~ofstream();
  168.  
  169.     void open( char const    *__name,
  170.                ios::openmode  __mode = ios::out,
  171.                int            __prot = filebuf::openprot );
  172. };
  173. #pragma pack(__pop);
  174.  
  175. inline void ofstream::open( char const *__n, ios::openmode __m, int __p ) {
  176.     fstreambase::open( __n, __m, __p );
  177. }
  178.  
  179. // **************************** FSTREAM **************************************
  180. #if defined(_M_IX86)
  181.   #pragma pack(__push,1);
  182. #else
  183.   #pragma pack(__push,8);
  184. #endif
  185. class _WPRTLINK fstream : public fstreambase, public iostream {
  186. public:
  187.     fstream();
  188.     fstream( char const    *__name,
  189.              ios::openmode  __mode = ios::in|ios::out,
  190.              int            __prot = filebuf::openprot );
  191.     fstream( filedesc __fd );
  192.     fstream( filedesc __fd, char *__buf, int __len );
  193.     ~fstream();
  194.  
  195.     void open( char const    *__name,
  196.                ios::openmode  __mode = ios::in|ios::out,
  197.                int            __prot = filebuf::openprot );
  198. };
  199. #pragma pack(__pop);
  200.  
  201. inline void fstream::open( char const *__n, ios::openmode __m, int __p ) {
  202.     fstreambase::open( __n, __m, __p );
  203. }
  204.  
  205. #endif
  206.