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>
42 #ifdef CXX_WORKING_NAMESPACES
47 class stringbuf : public streambuf
50 typedef char char_type;
52 typedef streampos pos_type;
53 typedef streamoff off_type;
55 explicit stringbuf(int which=ios::in|ios::out) :
56 streambuf(which), buf(), mode(static_cast<ios::open_mode>(which)),
60 explicit stringbuf(const string &s, int which=ios::in|ios::out) :
61 streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
66 setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
70 setp(&defbuf, &defbuf + bufsize);
72 rpos = (mode & ios::ate ? s.size() : 0);
77 const_cast<stringbuf*>(this)->sync(); // Sigh, really ugly hack
81 void str(const string& s)
86 gbump(egptr() - gptr());
90 pbump(pbase() - pptr());
92 rpos = (mode & ios::ate ? s.size() : 0);
96 inline virtual int sync();
97 inline virtual int overflow(int = EOF);
98 inline virtual int underflow();
102 string::size_type rpos;
107 class stringstreambase : virtual public ios {
113 return static_cast<stringbuf*>(_strbuf)->str();
115 void str(const string& s)
118 static_cast<stringbuf*>(_strbuf)->str(s);
126 stringstreambase(int which) :
132 stringstreambase(const string& s, int which) :
139 class istringstream : public stringstreambase, public istream {
141 istringstream(int which=ios::in) :
142 stringstreambase(which)
145 istringstream(const string& s, int which=ios::in) :
146 stringstreambase(s, which)
150 class ostringstream : public stringstreambase, public ostream {
152 ostringstream(int which=ios::out) :
153 stringstreambase(which)
156 ostringstream(const string& s, int which=ios::out) :
157 stringstreambase(s, which)
161 class stringstream : public stringstreambase, public iostream {
163 stringstream(int which=ios::in|ios::out) :
164 stringstreambase(which)
167 stringstream(const string &s, int which=ios::in|ios::out) :
168 stringstreambase(s, which)
172 #ifdef CXX_WORKING_NAMESPACES
176 inline int stringbuf::sync()
178 if((mode & ios::out) == 0)
181 streamsize n = pptr() - pbase();
184 buf.replace(rpos, string::npos, pbase(), n);
185 //if(buf.size() - rpos != n)
186 if (buf.size() != n + rpos)
190 gbump(egptr() - gptr());
195 inline int stringbuf::overflow(int ch)
197 if((mode & ios::out) == 0)
200 streamsize n = pptr() - pbase();
207 string::size_type oldSize = buf.size();
209 buf.replace(rpos, string::npos, 1, ch);
210 if(buf.size() - oldSize != 1)
217 inline int stringbuf::underflow()
220 if((mode & ios::in) == 0)
224 if(rpos >= buf.size())
229 string::size_type n = egptr() - eback();
232 s = buf.copy(eback(), n, rpos);
233 pbump(pbase() - pptr());
234 gbump(eback() - gptr());
235 int res = (0377 & buf[rpos]);
240 #endif /* not __STRSTREAM__ */