home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / support / g++include / strstream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  4.0 KB  |  104 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef __STRSTREAM_H
  19. #define __STRSTREAM_H
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23. #include <iostream.h>
  24.  
  25. class strstreambuf : public backupbuf {
  26.     _G_size_t _len; // The current length is max(_len, _pptr-_pbase).
  27.     typedef void *(*_alloc_type)(_G_size_t);
  28.     typedef void (*_free_type)(void*);
  29.     _alloc_type _allocate_buffer;
  30.     _free_type _free_buffer;
  31.     void init_dynamic(_alloc_type alloc, _free_type free,
  32.               int initial_size = 128);
  33.     void init_static(char *ptr, int size, char *pstart);
  34.     void init_static(const char *ptr, int size);
  35.   protected:
  36.     int is_static() const { return _allocate_buffer == (_alloc_type)0; }
  37.     virtual int overflow(int = EOF);
  38.     virtual int underflow();
  39.     virtual int pbackfail(int c);
  40.   public:
  41.     virtual ~strstreambuf();
  42.     strstreambuf() { init_dynamic(0, 0); }
  43.     strstreambuf(int initial_size) { init_dynamic(0, 0, initial_size); }
  44.     strstreambuf(void *(*alloc)(_G_size_t), void (*free)(void*))
  45.     { init_dynamic(alloc, free); }
  46.     strstreambuf(char *ptr, int size, char *pstart = NULL)
  47.     { init_static(ptr, size, pstart); }
  48.     strstreambuf(unsigned char *ptr, int size, unsigned char *pstart = NULL)
  49.     { init_static((char*)ptr, size, (char*)pstart); }
  50.     strstreambuf(const char *ptr, int size)
  51.     { init_static(ptr, size); }
  52.     strstreambuf(const unsigned char *ptr, int size)
  53.     { init_static((const char*)ptr, size); }
  54. #ifndef _G_BROKEN_SIGNED_CHAR
  55.     strstreambuf(signed char *ptr, int size, signed char *pstart = NULL)
  56.     { init_static((char*)ptr, size, (char*)pstart); }
  57.     strstreambuf(const signed char *ptr, int size)
  58.     { init_static((const char*)ptr, size); }
  59. #endif
  60.     // Note: frozen() is always true if is_static().
  61.     int frozen() { return _flags & _S_USER_BUF ? 1 : 0; }
  62.     void freeze(int n=1)
  63.     { if (!is_static())
  64.         { if (n) _flags |= _S_USER_BUF; else _flags &= ~_S_USER_BUF; } }
  65.     _G_size_t pcount();
  66.     char *str();
  67.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  68. };
  69.  
  70. class strstreambase : virtual public ios {
  71.   public:
  72.     strstreambuf* rdbuf() { return (strstreambuf*)_strbuf; }
  73.   protected:
  74.     strstreambase() { }
  75.     strstreambase(char *cp, int n, int mode=ios::out);
  76. };
  77.  
  78. class istrstream : public strstreambase, public istream {
  79.   public:
  80.     istrstream(const char*, int=0);
  81. };
  82.  
  83. class ostrstream : public strstreambase, public ostream {
  84.   public:
  85.     ostrstream();
  86.     ostrstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
  87.     _G_size_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
  88.     char *str() { return ((strstreambuf*)_strbuf)->str(); }
  89.     void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
  90.     int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
  91. };
  92.  
  93. class strstream : public strstreambase, public iostream {
  94.   public:
  95.     strstream() : strstreambase() { init(new strstreambuf()); }
  96.     strstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
  97.     _G_size_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
  98.     char *str() { return ((strstreambuf*)_strbuf)->str(); }
  99.     void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
  100.     int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
  101. };
  102.  
  103. #endif /*!__STRSTREAM_H*/
  104.