home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / modhead1.zip / FSTREAM.H < prev    next >
C/C++ Source or Header  |  1995-09-17  |  5KB  |  218 lines

  1. /*
  2.  *   fstream.h
  3.  *
  4.  *   C++ IOStreams specialized for use with files.
  5.  *
  6.  *           Copyright (c) 1991-1992, MetaWare Incorporated
  7.  */
  8.  
  9. #ifndef __FSTREAM_H
  10. #define __FSTREAM_H
  11. #pragma push_align_members(64);
  12.  
  13. #if _MSDOS && !__EXCEPTIONS__
  14.     #pragma off(exception_aware_class);
  15. #elif _MSDOS || _MSNT || _OS2
  16.     #pragma on(exception_aware_class);
  17. #endif
  18.  
  19. #if _MSNT && _DLL && !_DLLBUILD
  20.     #pragma on(dllimport);
  21. #endif
  22.  
  23. #c_include <iostream.h>
  24.  
  25. #pragma on(nodebug)
  26. /*
  27.  *   Class filebuf:
  28.  *
  29.  *      streambuf specialized for use with files.
  30.  *      Supports associated file descriptor, seek, read and write
  31.  *      to external file.
  32.  *
  33.  */
  34.  
  35. class  filebuf : public streambuf {
  36.  
  37. private:
  38.     void        fb_checkstate();
  39.  
  40. protected:
  41.     int        xfd;        // File descriptor for this filebuf.
  42.     int        mode;        // Open mode of file descriptor.
  43.     char        opened;        // TRUE == xfd is open.
  44.     streampos    last_seek;    // Position after last seek.
  45.     char*         in_start;
  46.     int        last_op();    // ???  Not implemented.
  47.     char        lahead[2];    // Input buffer for unbuffered filebufs.
  48.     int        phy_in_avail(); // Physical characters remaining
  49.  
  50. public:
  51.     static const int openprot;    // Default permissions = 0644(rw,r,r)
  52.  
  53.     void        fb_dbp(int __level);
  54.  
  55.             filebuf();    // Buffered, no handle, not open.
  56.  
  57.                     // Buffered, __handle already open.
  58.             filebuf(int __handle);
  59.  
  60.                     // Buffered, __handle already open.
  61.             filebuf(int __handle, char* __buf, int __len);
  62.  
  63.     int        is_open() const { return opened; }
  64.     int        fd() const { return xfd; }
  65.  
  66.                     // Open file.
  67.     filebuf*    _UNSAFE(open)(const char *__pathname, long __mode,
  68.                 long __prot=filebuf::openprot);
  69.  
  70.                     // Use __handle, fail if already open.
  71.     filebuf*    attach(int __handle);
  72.  
  73.                     // Sync and close file.
  74.     filebuf*     _UNSAFE(close)();
  75.  
  76.                     // Sync and close file.
  77.             ~filebuf();
  78.  
  79. // virtuals
  80.     virtual int    _UNSAFE(overflow)(int=EOF);
  81.     virtual int    _UNSAFE(underflow)();
  82.     virtual int    _UNSAFE(sync)();
  83.     virtual streampos
  84.             _UNSAFE(seekoff)(streamoff __offset,
  85.                 ios::seek_dir __dir, long __mode);
  86.     virtual streambuf*
  87.             setbuf(char* __buf, int __len);
  88. #if _MT
  89. public:
  90.     filebuf*    open(const char *__pathname, long __mode,
  91.                 long __prot=filebuf::openprot);
  92.     virtual int    overflow(int=EOF);
  93.     virtual int    underflow();
  94.     virtual int    sync();
  95.     filebuf*     close();
  96.     virtual streampos
  97.             seekoff(streamoff __offset, ios::seek_dir __dir,
  98.                 long __mode);
  99. #endif
  100.     };
  101.  
  102.  
  103. /*
  104.  *   Class fstreambase:
  105.  *
  106.  *      Base class for input/output functions on filebufs.
  107.  *
  108.  */
  109.  
  110. class fstreambase : virtual public ios { 
  111.  
  112. private:
  113.     void        check_stat();
  114.     filebuf        buf;
  115.  
  116. protected:
  117.     void        verify(int);
  118.  
  119. public:
  120.             fstreambase();
  121.     
  122.             fstreambase(const char* __name, long __mode,
  123.                     long __prot=filebuf::openprot);
  124.             fstreambase(int __handle);
  125.             fstreambase(int __handle, char* __buf, int __len);
  126.             ~fstreambase();
  127.     void        _UNSAFE(open)(const char* __name, long __mode, 
  128.                     long __prot=filebuf::openprot);
  129.     void        attach(int __handle);
  130.     void        close();
  131.     filebuf*    setbuf(char* __buf, int __len);
  132.     filebuf*    rdbuf() { return &buf; }
  133. #if _MT
  134. public:
  135.     void        open(const char* __name, long __mode, 
  136.                     long __prot=filebuf::openprot);
  137. #endif
  138.     };
  139.  
  140. /*
  141.  *   Class ifstream:
  142.  *
  143.  *      istream specialized for files.
  144.  *
  145.  */
  146.  
  147. class ifstream : public fstreambase, public istream {
  148. public:
  149.             ifstream();
  150.             ifstream(const char* __name, long __mode=ios::in,
  151.                     long __prot=filebuf::openprot);
  152.             ifstream(int __handle);
  153.             ifstream(int __handle, char* __buf, int __len);
  154.             ~ifstream();
  155.  
  156.     filebuf*    rdbuf() { return fstreambase::rdbuf(); }
  157.     void        open(const char* __fname, long __mode=ios::in, 
  158.                     long __prot=filebuf::openprot);
  159.     };
  160.  
  161. /*
  162.  *   Class ofstream:
  163.  *
  164.  *      ostream specialized for files.
  165.  *
  166.  */
  167.  
  168. class ofstream : public fstreambase, public ostream {
  169. public:
  170.             ofstream();
  171.             ofstream(const char* __name, long __mode=ios::out,
  172.                     long __prot=filebuf::openprot);
  173.             ofstream(int __handle);
  174.             ofstream(int __handle, char* __buf, int __len);
  175.             ~ofstream();
  176.  
  177.     filebuf*    rdbuf() { return fstreambase::rdbuf(); }
  178.     void        open(const char* __name, long __mode=ios::out, 
  179.                     long __prot=filebuf::openprot);
  180.     };
  181.  
  182. /*
  183.  *   Class fstream:
  184.  *
  185.  *      iostream specialized for files.
  186.  *
  187.  */
  188.  
  189. class fstream : public fstreambase, public iostream {
  190. public:
  191.             fstream();
  192.     
  193.             fstream(const char* __name, long __mode,
  194.                     long __prot=filebuf::openprot);
  195.             fstream(int __handle);
  196.             fstream(int __handle, char* __buf, int __len);
  197.             ~fstream();
  198.     filebuf*    rdbuf() { return fstreambase::rdbuf(); }
  199.     void        open(const char* __name, long __mode, 
  200.                 long __prot=filebuf::openprot);
  201.     };
  202. #pragma pop(nodebug)
  203.  
  204. #if _MSNT && _DLL && !_DLLBUILD
  205.     #pragma pop(dllimport);
  206. #endif
  207.  
  208. #if _MSDOS || _MSNT || _OS2
  209.     #pragma pop(exception_aware_class);
  210. #endif
  211.  
  212.  
  213. #pragma pop_align_members();
  214. #endif // __FSTREAM_H
  215.  
  216.  
  217. /**          Copyright (c) 1991-1993, MetaWare Incorporated             **/
  218.