home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / cxxinclude / strstream.h < prev   
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.4 KB  |  145 lines

  1. /* Copyright (c) 1993             by SAS Institute Inc., Cary NC     */
  2.  
  3. #ifndef __STRSTREAM_H
  4. #define __STRSTREAM_H
  5.  
  6. #ifndef __IOSTREAM_H
  7. #include <iostream.h>
  8. #endif
  9.  
  10. // class strstreambuf
  11. //     is created to allow reading and writing to and from
  12. //    strings in memory.
  13.  
  14.  
  15. class strstreambuf : public streambuf {
  16.     public:
  17.       strstreambuf() { _dynamic_init(); }
  18.  
  19.       strstreambuf( int _i )
  20.         _INLINE_FUNC(
  21.           {
  22.           _dynamic_init(); 
  23.           setbuf( 0, _i );
  24.           }
  25.         )
  26.  
  27.       strstreambuf( void* (*_a)(long), void (*_f)(void*) )
  28.         _INLINE_FUNC(
  29.           {
  30.           _dynamic_init();
  31.           _alloc_func = _a;
  32.           _free_func = _f;
  33.           }
  34.         )
  35.  
  36.       strstreambuf( char* _b, int _s, char* _pstart = 0 )
  37.         _INLINE_FUNC(
  38.           {
  39.           _fixed_init( _b, _s, _pstart );
  40.           }
  41.         )
  42.      
  43.      ~strstreambuf();
  44.  
  45.       void  freeze( int _n = 1 ) _INLINE_FUNC( { _frozen = (char)_n; })
  46.       char* str()
  47.          _INLINE_FUNC(
  48.            { 
  49.          this->sputc('\0');
  50.              freeze(); 
  51.              return base();
  52.             }
  53.           )
  54.  
  55.       int pcount() _INLINE_FUNC(
  56.         { return (base() == NULL ? 0 : pptr() - base()); })
  57.  
  58.       virtual int  sync() _INLINE_FUNC({ return 0; })
  59.       virtual streampos seekoff(streamoff, ios::seek_dir,
  60.                                 int =ios::in|ios::out);
  61.       virtual streampos seekpos(streampos, int =ios::in|ios::out);
  62.  
  63.       streambuf* setbuf( char* , size_t _i )
  64.        _INLINE_FUNC(
  65.           {
  66.             _upsize = ( _i <= 0) ? 512 : _i;
  67.             return this;
  68.           }
  69.         )
  70.  
  71.     private:
  72.       virtual int        underflow();
  73.       virtual int        overflow(int c=EOF);
  74.  
  75.     private:
  76.       unsigned int _upsize;
  77.       char _unlimited;
  78.       char _frozen;
  79.       char _dynamic;
  80.  
  81.       void _dynamic_init();
  82.       void _fixed_init( char*, int size, char* );
  83.       void* (*_alloc_func)(long);
  84.       void (*_free_func)(void*);
  85.     };
  86.  
  87.  
  88. class istrstream : public istream {
  89.     public:
  90.       istrstream( char* _p ) : ios( &buffer ), buffer( _p, 0, 0 ) {}
  91.       istrstream( char* _p, int _l ) 
  92.           : ios( &buffer ), buffer( _p, _l, 0 ) {}
  93.  
  94.      ~istrstream() {}
  95.       strstreambuf* rdbuf() _INLINE_FUNC({ return &buffer; })
  96.  
  97.     private:
  98.       strstreambuf buffer;
  99.     };
  100.  
  101. class ostrstream : public ostream {
  102.     public:
  103.       ostrstream( char* _p, int _l, int _mode = ios::out )
  104.           : ios( &buffer ), 
  105.             buffer( _p, _l,
  106.                     ( _mode & (ios::ate|ios::app) )
  107.                         ? _p + strlen( _p ) : _p )
  108.                 {}
  109.   
  110.       ostrstream() : ios( &buffer ) {}
  111.      ~ostrstream() {}
  112.  
  113.       strstreambuf* rdbuf() _INLINE_FUNC({ return &buffer; })
  114.  
  115.       char* str() _INLINE_FUNC({ return buffer.str(); })
  116.       int   pcount() _INLINE_FUNC({ return buffer.pcount(); })
  117.  
  118.     private:
  119.       strstreambuf buffer;
  120.     };
  121.  
  122. class strstream : public iostream {
  123.     public:
  124.       strstream( char* _p, int _l, int _mode )
  125.           : ios( &buffer ), 
  126.             buffer( _p, _l,
  127.                     ( _mode & (ios::ate|ios::app) )
  128.                         ? _p + strlen( _p ) : _p )
  129.                 {}
  130.   
  131.       strstream() : ios( &buffer ) {}
  132.      ~strstream() {}
  133.  
  134.       strstreambuf* rdbuf() _INLINE_FUNC({ return &buffer; })
  135.  
  136.       char* str() _INLINE_FUNC({ return buffer.str(); })
  137.       int   pcount() _INLINE_FUNC({ return buffer.pcount(); })
  138.  
  139.     private:
  140.       strstreambuf buffer;
  141.     };
  142.  
  143. #endif /* __STRSTREAM_H */
  144.  
  145.