1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
\r
2 Copyright (C) 2000 Free Software Foundation
\r
4 This file is part of the GNU IO Library. This library is free
\r
5 software; you can redistribute it and/or modify it under the
\r
6 terms of the GNU General Public License as published by the
\r
7 Free Software Foundation; either version 2, or (at your option)
\r
10 This library is distributed in the hope that it will be useful,
\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 GNU General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License
\r
16 along with this library; see the file COPYING. If not, write to the Free
\r
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
19 As a special exception, if you link this library with files
\r
20 compiled with a GNU compiler to produce an executable, this does not cause
\r
21 the resulting executable to be covered by the GNU General Public License.
\r
22 This exception does not however invalidate any other reasons why
\r
23 the executable file might be covered by the GNU General Public License. */
\r
25 /* Written by Magnus Fromreide (magfr@lysator.liu.se). */
\r
31 #include <iostream.h>
\r
32 #include <streambuf.h>
\r
36 class stringbuf : public streambuf
\r
39 typedef char char_type;
\r
40 typedef int int_type;
\r
41 typedef streampos pos_type;
\r
42 typedef streamoff off_type;
\r
44 explicit stringbuf(int which=ios::in|ios::out) :
\r
45 streambuf(which), buf(), mode(static_cast<ios::open_mode>(which)),
\r
49 explicit stringbuf(const std::string &s, int which=ios::in|ios::out) :
\r
50 streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
\r
55 setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
\r
59 setp(&defbuf, &defbuf + bufsize);
\r
61 rpos = (mode & ios::ate ? s.size() : 0);
\r
64 std::string str() const
\r
66 const_cast<stringbuf*>(this)->sync(); // Sigh, really ugly hack
\r
70 void str(const std::string& s)
\r
75 gbump(egptr() - gptr());
\r
79 pbump(pbase() - pptr());
\r
81 rpos = (mode & ios::ate ? s.size() : 0);
\r
85 inline virtual int sync();
\r
86 inline virtual int overflow(int = EOF);
\r
87 inline virtual int underflow();
\r
90 ios::open_mode mode;
\r
91 std::string::size_type rpos;
\r
96 class stringstreambase : virtual public ios {
\r
100 std::string str() const
\r
102 return dynamic_cast<stringbuf*>(_strbuf)->str();
\r
104 void str(const std::string& s)
\r
107 dynamic_cast<stringbuf*>(_strbuf)->str(s);
\r
115 stringstreambase(int which) :
\r
121 stringstreambase(const std::string& s, int which) :
\r
128 class istringstream : public stringstreambase, public istream {
\r
130 istringstream(int which=ios::in) :
\r
131 stringstreambase(which)
\r
134 istringstream(const std::string& s, int which=ios::in) :
\r
135 stringstreambase(s, which)
\r
139 class ostringstream : public stringstreambase, public ostream {
\r
141 ostringstream(int which=ios::out) :
\r
142 stringstreambase(which)
\r
145 ostringstream(const std::string& s, int which=ios::out) :
\r
146 stringstreambase(s, which)
\r
150 class stringstream : public stringstreambase, public iostream {
\r
152 stringstream(int which=ios::in|ios::out) :
\r
153 stringstreambase(which)
\r
156 stringstream(const std::string &s, int which=ios::in|ios::out) :
\r
157 stringstreambase(s, which)
\r
162 inline int std::stringbuf::sync()
\r
164 if((mode & ios::out) == 0)
\r
167 streamsize n = pptr() - pbase();
\r
170 buf.replace(rpos, std::string::npos, pbase(), n);
\r
171 if(buf.size() - rpos != n)
\r
175 gbump(egptr() - gptr());
\r
180 inline int std::stringbuf::overflow(int ch)
\r
182 if((mode & ios::out) == 0)
\r
185 streamsize n = pptr() - pbase();
\r
192 std::string::size_type oldSize = buf.size();
\r
194 buf.replace(rpos, std::string::npos, ch);
\r
195 if(buf.size() - oldSize != 1)
\r
202 inline int std::stringbuf::underflow()
\r
205 if((mode & ios::in) == 0)
\r
209 if(rpos >= buf.size())
\r
214 std::string::size_type n = egptr() - eback();
\r
215 std::string::size_type s;
\r
217 s = buf.copy(eback(), n, rpos);
\r
218 pbump(pbase() - pptr());
\r
219 gbump(eback() - gptr());
\r
220 int res = (0377 & buf[rpos]);
\r
225 #endif /* not __STRSTREAM__ */
\r