home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MYIO.H < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  101 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. // Myio.h
  4. // Specialised I/O class to demonstrate use of C++ iostream
  5. // facilities in a customised environment
  6. // Written by David L Nugent, June 1993.
  7. //
  8.  
  9. # if !defined(_Myio_h)
  10. # define _Myio_h 1
  11.  
  12.     // Forward declare classes
  13.  
  14. class Myio;
  15. class Mystreambuf;
  16. class Mystreambase;
  17. class Mystream;
  18.  
  19.     // Forward declare iostream classes
  20.  
  21. class iostream;
  22.  
  23.     //
  24.     // class Myio
  25.     // This is a simplistic class which simply fields
  26.     // input and output to a simulated stream device.
  27.     //
  28.     // In fact, it doesn't really do much at all other
  29.     // than read input from and send output to a
  30.     // circular queue, as though talking via a loopback
  31.     // pipe to itself.
  32.     //
  33.  
  34.  
  35. class Myio
  36. {
  37.     friend class Mystreambuf;
  38.  
  39.   public:
  40.  
  41.     Myio (int sz =2048);                    // sz = buffer size to allocate
  42.     virtual ~Myio (void);
  43.  
  44.     iostream & stream (void);               // Return (or create) stream
  45.  
  46.     int readok (void) const;                // Underflow check
  47.     int writeok (void) const;               // Overflow check
  48.     int gcount (void) const;                // Get # of chrs last read
  49.     int pcount (void) const;                // Get # of chrs last written
  50.     int count (void) const;                 // Get # of chrs in buffer
  51.     int size (void) const;                  // Get size of buffer
  52.     int dump (void) const;                  // Debugging - dumps buffer
  53.  
  54.     int write (char const * buf, int len);  // Put data into 'pipe'
  55.     int read (char * buf, int max);         // Read data from our 'pipe'
  56.  
  57.   private:
  58.  
  59.     enum
  60.     {
  61.         overflow    = 0x0001,   // Last write only partial
  62.         underflow   = 0x0002    // Last read only partial
  63.     };
  64.  
  65.     unsigned stat;              // Last read/write status
  66.     int _pcount;                // Last write count
  67.     int _gcount;                // Last read count
  68.     int bufsize;                // Size of our buffer
  69.     int bufchars;               // Chrs in buffer now
  70.     int bufidx;                 // Index into buffer (next put)
  71.     char * bufaddr;             // Pointer to buffer
  72.     Mystream * mystream;        // Stream associated with this object
  73.  
  74. };
  75.  
  76. inline int
  77. Myio::readok (void) const
  78.     {   return ((stat & Myio::underflow) == 0); }
  79.  
  80. inline int
  81. Myio::writeok (void) const
  82.     {   return ((stat & Myio::overflow) == 0);  }
  83.  
  84. inline int
  85. Myio::gcount (void) const
  86.     {   return _gcount;     }
  87.  
  88. inline int
  89. Myio::pcount (void) const
  90.     {   return _pcount;     }
  91.  
  92. inline int
  93. Myio::count (void) const
  94.     {   return bufchars;    }
  95.  
  96. inline int
  97. Myio::size (void) const
  98.     {   return bufsize;     }
  99.  
  100. # endif     // _Myio_h
  101.