1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 2000 Free Software Foundation
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)
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.
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.
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. */
25 /* Written by Magnus Fromreide (magfr@lysator.liu.se). */
27 /* Sligtly modified for use in The LyX Project.
28 Made to be usable with both std::string (as supplied with Gcc 2.95.2),
29 and with lyxstring. Dynamic casts have been replaced by static_casts.
30 One fix to avoid unsigned/signed warnings.
32 Lars Gullik Bjønnes (larsbj@lyx.org)
40 #include <streambuf.h>
44 class stringbuf : public streambuf
47 typedef char char_type;
49 typedef streampos pos_type;
50 typedef streamoff off_type;
52 explicit stringbuf(int which=ios::in|ios::out) :
53 streambuf(which), buf(), mode(static_cast<ios::open_mode>(which)),
57 explicit stringbuf(const string &s, int which=ios::in|ios::out) :
58 streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
63 setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
67 setp(&defbuf, &defbuf + bufsize);
69 rpos = (mode & ios::ate ? s.size() : 0);
74 const_cast<stringbuf*>(this)->sync(); // Sigh, really ugly hack
78 void str(const string& s)
83 gbump(egptr() - gptr());
87 pbump(pbase() - pptr());
89 rpos = (mode & ios::ate ? s.size() : 0);
93 inline virtual int sync();
94 inline virtual int overflow(int = EOF);
95 inline virtual int underflow();
99 string::size_type rpos;
104 class stringstreambase : virtual public ios {
110 return static_cast<stringbuf*>(_strbuf)->str();
112 void str(const string& s)
115 static_cast<stringbuf*>(_strbuf)->str(s);
123 stringstreambase(int which) :
129 stringstreambase(const string& s, int which) :
136 class istringstream : public stringstreambase, public istream {
138 istringstream(int which=ios::in) :
139 stringstreambase(which)
142 istringstream(const string& s, int which=ios::in) :
143 stringstreambase(s, which)
147 class ostringstream : public stringstreambase, public ostream {
149 ostringstream(int which=ios::out) :
150 stringstreambase(which)
153 ostringstream(const string& s, int which=ios::out) :
154 stringstreambase(s, which)
158 class stringstream : public stringstreambase, public iostream {
160 stringstream(int which=ios::in|ios::out) :
161 stringstreambase(which)
164 stringstream(const string &s, int which=ios::in|ios::out) :
165 stringstreambase(s, which)
171 inline int stringbuf::sync()
173 if((mode & ios::out) == 0)
176 streamsize n = pptr() - pbase();
179 buf.replace(rpos, string::npos, pbase(), n);
180 //if(buf.size() - rpos != n)
181 if (buf.size() != n + rpos)
185 gbump(egptr() - gptr());
190 inline int stringbuf::overflow(int ch)
192 if((mode & ios::out) == 0)
195 streamsize n = pptr() - pbase();
202 string::size_type oldSize = buf.size();
204 buf.replace(rpos, string::npos, 1, ch);
205 if(buf.size() - oldSize != 1)
212 inline int stringbuf::underflow()
215 if((mode & ios::in) == 0)
219 if(rpos >= buf.size())
224 string::size_type n = egptr() - eback();
227 s = buf.copy(eback(), n, rpos);
228 pbump(pbase() - pptr());
229 gbump(eback() - gptr());
230 int res = (0377 & buf[rpos]);
235 #endif /* not __STRSTREAM__ */