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). */
26 /* seekoff and ideas for overflow is largely borrowed from libstdc++-v3 */
32 #include <streambuf.h>
37 class stringbuf : public streambuf
40 typedef char char_type;
42 typedef streampos pos_type;
43 typedef streamoff off_type;
46 stringbuf(int which=ios::in|ios::out)
47 : streambuf(), mode(static_cast<ios::open_mode>(which)),
48 stream(NULL), stream_len(0)
54 stringbuf(const string &str, int which=ios::in|ios::out)
55 : streambuf(), mode(static_cast<ios::open_mode>(which)),
56 stream(NULL), stream_len(0)
58 if (mode & (ios::in|ios::out))
60 stream_len = str.size();
61 stream = new char_type[stream_len];
62 str.copy(stream, stream_len);
77 return string(stream, pptr()-pbase());
83 str(const string& str)
86 stream_len = str.size();
87 stream = new char_type[stream_len];
88 str.copy(stream, stream_len);
93 // The buffer is already in gptr, so if it ends then it is out of data.
101 overflow(int c = EOF)
108 streamsize old_stream_len = stream_len;
110 char_type* new_stream = new char_type[stream_len];
111 memcpy(new_stream, stream, old_stream_len);
114 stringbuf_sync(gptr()-eback(), pptr()-pbase());
127 setbuf(char_type* s, int n)
132 stream = new char_type[n];
133 memcpy(stream, s, n);
135 stringbuf_sync(0, 0);
141 seekoff(off_type off, ios::seek_dir way, int which = ios::in | ios::out)
143 pos_type ret = pos_type(off_type(-1));
144 bool testin = which & ios::in && mode & ios::in;
145 bool testout = which & ios::out && mode & ios::out;
146 bool testboth = testin && testout && way != ios::cur;
148 if (stream_len && ((testin != testout) || testboth))
150 char_type* beg = stream;
151 char_type* curi = NULL;
152 char_type* curo = NULL;
153 char_type* endi = NULL;
154 char_type* endo = NULL;
167 off_type newoffi = 0;
168 off_type newoffo = 0;
171 newoffi = beg - curi;
172 newoffo = beg - curo;
174 else if (way == ios::end)
176 newoffi = endi - curi;
177 newoffo = endo - curo;
180 if (testin && newoffi + off + curi - beg >= 0 &&
181 endi - beg >= newoffi + off + curi - beg)
183 gbump(newoffi + off);
184 ret = pos_type(newoffi + off + curi);
186 if (testout && newoffo + off + curo - beg >= 0 &&
187 endo - beg >= newoffo + off + curo - beg)
189 pbump(newoffo + off);
190 ret = pos_type(newoffo + off + curo);
197 seekpos(pos_type sp, int which = ios::in | ios::out)
199 pos_type ret = seekoff(sp, ios::beg, which);
205 stringbuf_sync(streamsize i, streamsize o)
208 setg(stream, stream + i, stream + stream_len);
211 setp(stream, stream + stream_len);
219 stringbuf_sync(0, stream_len);
221 stringbuf_sync(0, 0);
227 streamsize stream_len;
230 class istringstream : public istream {
232 typedef char char_type;
233 typedef int int_type;
234 typedef streampos pos_type;
235 typedef streamoff off_type;
238 istringstream(int which=ios::in)
239 : istream(&sb), sb(which | ios::in)
243 istringstream(const string& str, int which=ios::in)
244 : istream(&sb), sb(str, which | ios::in)
250 return const_cast<stringbuf*>(&sb);
256 return rdbuf()->str();
267 class ostringstream : public ostream {
269 typedef char char_type;
270 typedef int int_type;
271 typedef streampos pos_type;
272 typedef streamoff off_type;
275 ostringstream(int which=ios::out)
276 : ostream(&sb), sb(which | ios::out)
280 ostringstream(const string& str, int which=ios::out)
281 : ostream(&sb), sb(str, which | ios::out)
287 return const_cast<stringbuf*>(&sb);
293 return rdbuf()->str();
296 void str(const string& s)
304 class stringstream : public iostream {
306 typedef char char_type;
307 typedef int int_type;
308 typedef streampos pos_type;
309 typedef streamoff off_type;
312 stringstream(int which=ios::out|ios::in)
313 : iostream(&sb), sb(which)
317 stringstream(const string& str, int which=ios::out|ios::in)
318 : iostream(&sb), sb(str, which)
324 return const_cast<stringbuf*>(&sb);
330 return rdbuf()->str();
343 #endif /* not __STRSTREAM__ */