home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / strstream.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  5KB  |  124 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this library; see the file COPYING.  If not, write to the Free
  17. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /* Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #ifndef __STRSTREAM_H
  28. #define __STRSTREAM_H
  29. #ifdef __GNUG__
  30. #pragma interface
  31. #endif
  32. #include <iostream.h>
  33. #include <strfile.h>
  34.  
  35. extern "C++" {
  36. class strstreambuf : public streambuf
  37. {
  38.   struct _IO_str_fields _s;
  39.   friend class istrstream;
  40.  
  41.     void init_dynamic(_IO_alloc_type alloc, _IO_free_type free,
  42.               int initial_size = 128);
  43.     void init_static(char *ptr, int size, char *pstart);
  44.     void init_readonly(const char *ptr, int size);
  45.   protected:
  46.     int is_static() const { return _s._allocate_buffer == (_IO_alloc_type)0; }
  47.     virtual int overflow(int = EOF);
  48.     virtual int underflow();
  49.     virtual int pbackfail(int c);
  50.   public:
  51.     virtual ~strstreambuf();
  52.     strstreambuf() { init_dynamic(0, 0); }
  53.     strstreambuf(int initial_size) { init_dynamic(0, 0, initial_size); }
  54.     strstreambuf(void *(*alloc)(_IO_size_t), void (*free)(void*))
  55.     { init_dynamic(alloc, free); }
  56.     strstreambuf(char *ptr, int size, char *pstart = NULL)
  57.     { init_static(ptr, size, pstart); }
  58.     strstreambuf(unsigned char *ptr, int size, unsigned char *pstart = NULL)
  59.     { init_static((char*)ptr, size, (char*)pstart); }
  60.     strstreambuf(const char *ptr, int size)
  61.     { init_readonly(ptr, size); }
  62.     strstreambuf(const unsigned char *ptr, int size)
  63.     { init_readonly((const char*)ptr, size); }
  64.     strstreambuf(signed char *ptr, int size, signed char *pstart = NULL)
  65.     { init_static((char*)ptr, size, (char*)pstart); }
  66.     strstreambuf(const signed char *ptr, int size)
  67.     { init_readonly((const char*)ptr, size); }
  68.     // Note: frozen() is always true if is_static().
  69.     int frozen() { return _flags & _IO_USER_BUF ? 1 : 0; }
  70.     void freeze(int n=1)
  71.     { if (!is_static())
  72.         { if (n) _flags |= _IO_USER_BUF; else _flags &= ~_IO_USER_BUF; } }
  73.     _IO_ssize_t pcount();
  74.     char *str();
  75.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  76. };
  77.  
  78. class strstreambase : virtual public ios {
  79. #ifdef _IO_NEW_STREAMS
  80.   protected:
  81.     strstreambuf __my_sb;
  82. #endif
  83.   public:
  84. #ifdef _IO_NEW_STREAMS
  85.     strstreambuf* rdbuf() { return &__my_sb; }
  86. #else
  87.     strstreambuf* rdbuf() { return (strstreambuf*)ios::rdbuf(); }
  88. #endif
  89.   protected:
  90.     strstreambase() { }
  91. #ifdef _IO_NEW_STREAMS
  92.     strstreambase(char *cp, int n);
  93. #endif
  94.     strstreambase(char *cp, int n, int mode=ios::out);
  95. };
  96.  
  97. class istrstream : public strstreambase, public istream {
  98.   public:
  99.     istrstream(const char*, int=0);
  100. };
  101.  
  102. class ostrstream : public strstreambase, public ostream {
  103.   public:
  104.     ostrstream();
  105.     ostrstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
  106.     _IO_ssize_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
  107.     char *str() { return ((strstreambuf*)_strbuf)->str(); }
  108.     void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
  109.     int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
  110. };
  111.  
  112. class strstream : public strstreambase, public iostream {
  113.   public:
  114.     strstream();
  115.     strstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
  116.     _IO_ssize_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
  117.     char *str() { return ((strstreambuf*)_strbuf)->str(); }
  118.     void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
  119.     int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
  120. };
  121. } // extern "C++"
  122.  
  123. #endif /*!__STRSTREAM_H*/
  124.