home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_12 / taylor2 / gstream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-06  |  1.3 KB  |  65 lines

  1. // gstream.cpp
  2. #include <stdio.h>
  3. #include <iostream.h>
  4. #include <string.h>
  5. #include "gpibio.h"
  6.  
  7. // GENERIC STREAM Definitions
  8. gstream::gstream()   // constructor
  9. {
  10.     base_ = ebuf_ = pbase_ = epptr_ = pptr_= gptr_ = NULL;
  11.     gleng_ = 0;
  12. }
  13.  
  14. // Destructor
  15. gstream::~gstream()
  16. {
  17.    if(base_)
  18.       delete base_;     // Free up areas if allocated
  19.    if(pbase_)
  20.       delete pbase_;
  21. }
  22.  
  23. // Implementation of sput function
  24. void gstream::sput(char *s)
  25. {
  26.     int len = strlen(s);
  27.  
  28.     // If it's too long, flush and reallocate more room
  29.     if(pptr_+len+1 >= epptr_)
  30.     {
  31.        overflow();
  32.  
  33.        // See if it will fit at all
  34.       if(len > (int)(epptr_ - pbase_))
  35.       {
  36.          char *newarea = new char [len+1];
  37.  
  38.          if(newarea == NULL)
  39.          {
  40.             cerr << "Not enough memory to output " << s << endl;
  41.             return;
  42.          }
  43.          else
  44.            delete pbase_;
  45.          pbase_ = newarea;
  46.          pptr_  = pbase_;
  47.          epptr_ = base_ + len + 1;
  48.       }
  49.     }
  50.     strcpy(pptr_,s);   // Copy string into output stream
  51.     pptr_ += len;
  52. }
  53.  
  54. ... [full source code is on code disk -mb]
  55.  
  56. // Implementation of gets
  57. void gstream::gets(char *s)
  58. {
  59.    if(gptr_ >= egptr_)
  60.      underflow();
  61.    strcpy(s,gptr_);    // copy the remaining stream into the string
  62.    gptr_ = egptr_;     // update get pointer
  63. }
  64.  
  65.