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

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. // Myio.cpp
  4. // Simple 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. # include <iostream.h>
  9. # include <string.h>
  10. # include "Myio.h"
  11. # include "Mystream.h"
  12.  
  13. Myio::Myio (int sz)
  14.     : bufsize(sz), bufchars(0), bufidx(0),
  15.       bufaddr(new char[bufsize]), mystream(0)
  16. {}
  17.  
  18. Myio::~Myio (void)
  19. {
  20.     delete bufaddr;
  21.     delete mystream;
  22. }
  23.  
  24. iostream &
  25. Myio::stream (void)
  26. {
  27.     if (!mystream)      // Create a stream if required
  28.         mystream = new Mystream(this);
  29.     return *mystream;
  30. }
  31.  
  32. int         // Simple write function into a circular buffer
  33. Myio::write (char const * buf, int len)
  34. {
  35.     int avail = (bufsize - bufchars);   // See how many fit
  36.     if (len > avail)
  37.     {
  38.         len = avail;
  39.         stat |= Myio::overflow;         // Only partial write
  40.     }
  41.     else
  42.         stat &= ~Myio::overflow;
  43.     avail = bufsize - bufidx;           // Calculate room at end
  44.     if (avail > len)
  45.         avail = len;
  46.     if (avail)
  47.     {
  48.         memcpy (bufaddr + bufidx, buf, avail);
  49.         bufidx += avail;                // Update the put index
  50.         buf += avail;                   // And the input pointer
  51.     }
  52.     if (bufidx >= bufsize)              // Wrap buffer to start
  53.         bufidx = 0;
  54.     avail = len - avail;                // See if there is any more to go
  55.     if (avail)
  56.     {
  57.         memcpy (bufaddr + bufidx, buf, avail);
  58.         bufidx += avail;                // Update the put index
  59.     }
  60.     bufchars += len;
  61.     return (_pcount = len);
  62. }
  63.  
  64. int         // Simple read function from a circular buffer
  65. Myio::read (char * buf, int len)
  66. {
  67.     if (len > bufchars)                 // Adjust for available bytes
  68.     {
  69.         len = bufchars;
  70.         stat |= Myio::underflow;        // Got an underflow (partial read)
  71.     }
  72.     else
  73.         stat &= ~Myio::underflow;       // Clear underflow flag
  74.     int startidx = bufidx - bufchars;   // Determine start get position
  75.     if (startidx < 0)
  76.         startidx += bufsize;            // Adjust for wrap
  77.     int avail = bufsize - startidx;     // Check room at end of buffer
  78.     if (avail > len)                    // Adjust down if necessary
  79.         avail = len;
  80.     if (avail)                          // Copy first section
  81.     {
  82.         memcpy (buf, bufaddr + startidx, avail);
  83.         startidx += avail;              // Adjust start index
  84.         buf += avail;                   // Adjust output pointer
  85.     }
  86.     if (startidx >= bufsize)            // Wrap buffer to start
  87.         startidx = 0;
  88.     avail = len - avail;                // See if there is any more to go
  89.     if (avail)                          // If so, copy the rest
  90.         memcpy (buf, bufaddr + startidx, avail);
  91.     bufchars -= len;                    // Adjust character count
  92.     return (_gcount = len);
  93. }
  94.  
  95. Myio &
  96. operator<< (Myio & m, char const * ptr)
  97. {
  98.     m.write (ptr, strlen (ptr));
  99.     return m;
  100. }
  101.  
  102. int
  103. Myio::dump (void) const
  104. {
  105.     if (bufchars)
  106.     {
  107.         char * tmp = new char[bufchars + 2];
  108.         int idx = bufidx - bufchars;
  109.       int i;
  110.  
  111.       if (idx < 0)
  112.             idx += bufsize;
  113.         for (i = 0; i < bufchars; )
  114.         {
  115.             if (idx >= bufsize)
  116.                 idx = 0;
  117.             tmp[i++] = bufaddr[idx++];
  118.         }
  119.         if (i)
  120.         {
  121.             if (tmp[i-1] != '\n')   // Terminate with NL
  122.                 tmp[i++] = '\n';
  123.             tmp[i] = 0;
  124.             cout << "---\n"
  125.                  << tmp
  126.                  << "---\n";
  127.         }
  128.         delete tmp;
  129.     }
  130.     return bufchars;
  131. }
  132.