home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / strstream.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  4KB  |  101 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_const() { xsetflags(_S_NO_WRITES); }
  34.     void init_static(char *ptr, int size, char *pstart);
  35.   protected:
  36.     virtual int overflow(int = EOF);
  37.     virtual int underflow();
  38.     virtual int pbackfail(int c);
  39.   public:
  40.     virtual ~strstreambuf();
  41.     strstreambuf() { init_dynamic(0, 0); }
  42.     strstreambuf(int initial_size) { init_dynamic(0, 0, initial_size); }
  43.     strstreambuf(void *(*alloc)(_G_size_t), void (*free)(void*))
  44.     { init_dynamic(alloc, free); }
  45.     strstreambuf(char *ptr, int size, char *pstart = NULL)
  46.     { init_static(ptr, size, pstart); }
  47.     strstreambuf(unsigned char *ptr, int size, unsigned char *pstart = NULL)
  48.     { init_static((char*)ptr, size, (char*)pstart); }
  49.     strstreambuf(const char *ptr, int size)
  50.     { init_static((char*)ptr, size, NULL); init_const(); }
  51.     strstreambuf(const unsigned char *ptr, int size)
  52.     { init_static((char*)ptr, size, NULL); init_const(); }
  53. #ifndef _G_BROKEN_SIGNED_CHAR
  54.     strstreambuf(signed char *ptr, int size, signed char *pstart = NULL)
  55.     { init_static((char*)ptr, size, (char*)pstart); }
  56.     strstreambuf(const signed char *ptr, int size)
  57.     { init_static((char*)ptr, size, NULL); init_const(); }
  58. #endif
  59.     int frozen() { return _flags & _S_USER_BUF ? 1 : 0; }
  60.     void freeze(int n=1)
  61.     { if (n) _flags |= _S_USER_BUF; else _flags &= ~_S_USER_BUF; }
  62.     _G_size_t pcount();
  63.     char *str();
  64.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  65. };
  66.  
  67. class strstreambase : virtual public ios {
  68.   public:
  69.     strstreambuf* rdbuf() { return (strstreambuf*)_strbuf; }
  70.   protected:
  71.     strstreambase() { }
  72.     strstreambase(char *cp, int n, int mode=ios::out);
  73. };
  74.  
  75. class istrstream : public strstreambase, public istream {
  76.   public:
  77.     istrstream(const char*, int=0);
  78. };
  79.  
  80. class ostrstream : public strstreambase, public ostream {
  81.   public:
  82.     ostrstream();
  83.     ostrstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
  84.     _G_size_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
  85.     char *str() { return ((strstreambuf*)_strbuf)->str(); }
  86.     void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
  87.     int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
  88. };
  89.  
  90. class strstream : public strstreambase, public iostream {
  91.   public:
  92.     strstream() : strstreambase() { init(new strstreambuf()); }
  93.     strstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
  94.     _G_size_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
  95.     char *str() { return ((strstreambuf*)_strbuf)->str(); }
  96.     void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
  97.     int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
  98. };
  99.  
  100. #endif /*!__STRSTREAM_H*/
  101.