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 Some further changes might be needed to avoid use of namespaces.
34 Lars Gullik Bjønnes (larsbj@lyx.org)
42 #include <streambuf.h>
46 class stringbuf : public streambuf
49 typedef char char_type;
51 typedef streampos pos_type;
52 typedef streamoff off_type;
54 explicit stringbuf(int which=ios::in|ios::out) :
55 streambuf(which), buf(), mode(static_cast<ios::open_mode>(which)),
59 explicit stringbuf(const string &s, int which=ios::in|ios::out) :
60 streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
65 setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
69 setp(&defbuf, &defbuf + bufsize);
71 rpos = (mode & ios::ate ? s.size() : 0);
76 const_cast<stringbuf*>(this)->sync(); // Sigh, really ugly hack
80 void str(const string& s)
85 gbump(egptr() - gptr());
89 pbump(pbase() - pptr());
91 rpos = (mode & ios::ate ? s.size() : 0);
95 inline virtual int sync();
96 inline virtual int overflow(int = EOF);
97 inline virtual int underflow();
101 string::size_type rpos;
106 class stringstreambase : virtual public ios {
112 return static_cast<stringbuf*>(_strbuf)->str();
114 void str(const string& s)
117 static_cast<stringbuf*>(_strbuf)->str(s);
125 stringstreambase(int which) :
131 stringstreambase(const string& s, int which) :
138 class istringstream : public stringstreambase, public istream {
140 istringstream(int which=ios::in) :
141 stringstreambase(which)
144 istringstream(const string& s, int which=ios::in) :
145 stringstreambase(s, which)
149 class ostringstream : public stringstreambase, public ostream {
151 ostringstream(int which=ios::out) :
152 stringstreambase(which)
155 ostringstream(const string& s, int which=ios::out) :
156 stringstreambase(s, which)
160 class stringstream : public stringstreambase, public iostream {
162 stringstream(int which=ios::in|ios::out) :
163 stringstreambase(which)
166 stringstream(const string &s, int which=ios::in|ios::out) :
167 stringstreambase(s, which)
172 inline int stringbuf::sync()
174 if((mode & ios::out) == 0)
177 streamsize n = pptr() - pbase();
180 buf.replace(rpos, string::npos, pbase(), n);
181 //if(buf.size() - rpos != n)
182 if (buf.size() != n + rpos)
186 gbump(egptr() - gptr());
191 inline int stringbuf::overflow(int ch)
193 if((mode & ios::out) == 0)
196 streamsize n = pptr() - pbase();
203 string::size_type oldSize = buf.size();
205 buf.replace(rpos, string::npos, 1, ch);
206 if(buf.size() - oldSize != 1)
213 inline int stringbuf::underflow()
216 if((mode & ios::in) == 0)
220 if(rpos >= buf.size())
225 string::size_type n = egptr() - eback();
228 s = buf.copy(eback(), n, rpos);
229 pbump(pbase() - pptr());
230 gbump(eback() - gptr());
231 int res = (0377 & buf[rpos]);
236 #endif /* not __STRSTREAM__ */