home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / MYIO.CPP < prev    next >
C/C++ Source or Header  |  1994-04-03  |  4KB  |  128 lines

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