Imported vanilla contents from upstream
[construo.git] / compatlib / sstream
blob23efcd323fe41c532867c32a4d61c0c55e2488ec
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)
8 any later version.
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)
37 #ifndef SSTREAM_H
38 #define SSTREAM_H
40 #include <string>
41 #include <iostream>
42 #include <streambuf.h>
44 namespace std
46   class stringbuf : public streambuf
47   {
48   public:
49     typedef char        char_type;
50     typedef int         int_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)),
56       rpos(0), bufsize(1)
57     { }
58         
59     explicit stringbuf(const string &s, int which=ios::in|ios::out) :
60       streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
61       bufsize(1)
62     {
63       if(mode & ios::in)
64         {
65           setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
66         }
67       if(mode & ios::out)
68         {
69           setp(&defbuf, &defbuf + bufsize);
70         }
71       rpos = (mode & ios::ate ? s.size() : 0);
72     }
73         
74     string str() const
75     {
76       const_cast<stringbuf*>(this)->sync();  // Sigh, really ugly hack
77       return buf;
78     };
80     void str(const string& s)
81     {
82       buf = s;
83       if(mode & ios::in)
84         {
85           gbump(egptr() - gptr());
86         }
87       if(mode & ios::out)
88         {
89           pbump(pbase() - pptr());
90         }
91       rpos = (mode & ios::ate ? s.size() : 0);
92     }
94   protected:
95     inline virtual int sync();
96     inline virtual int overflow(int = EOF);
97     inline virtual int underflow();
98   private:
99     string                      buf;
100     ios::open_mode              mode;
101     string::size_type   rpos;
102     streamsize                  bufsize;
103     char                        defbuf;
104   };
106   class stringstreambase : virtual public ios {
107   protected:
108     stringbuf __my_sb;
109   public:
110     string str() const
111     {
112       return static_cast<stringbuf*>(_strbuf)->str();
113     }
114     void str(const string& s)
115     {
116       clear();
117       static_cast<stringbuf*>(_strbuf)->str(s);
118     }
119         
120     stringbuf* rdbuf()
121     {
122       return &__my_sb;
123     }
124   protected:
125     stringstreambase(int which) :
126       __my_sb(which)
127     {
128       init (&__my_sb);
129     }
130         
131     stringstreambase(const string& s, int which) :
132       __my_sb(s, which)
133     {
134       init (&__my_sb);
135     }
136   };
137     
138   class istringstream : public stringstreambase, public istream {
139   public:
140     istringstream(int which=ios::in) :
141       stringstreambase(which)
142     { }
143         
144     istringstream(const string& s, int which=ios::in) :
145       stringstreambase(s, which)
146     { }
147   };
148     
149   class ostringstream : public stringstreambase, public ostream {
150   public:
151     ostringstream(int which=ios::out) :
152       stringstreambase(which)
153     { }
154         
155     ostringstream(const string& s, int which=ios::out) :
156       stringstreambase(s, which)
157     { }
158   };
159     
160   class stringstream : public stringstreambase, public iostream {
161   public:
162     stringstream(int which=ios::in|ios::out) :
163       stringstreambase(which)
164     { }
165     
166     stringstream(const string &s, int which=ios::in|ios::out) :
167       stringstreambase(s, which)
168     { }
169   };
172 inline int stringbuf::sync()
174   if((mode & ios::out) == 0)
175     return EOF;
177   streamsize n = pptr() - pbase();
178   if(n)
179     {
180       buf.replace(rpos, string::npos, pbase(), n);
181       //if(buf.size() - rpos != n)
182       if (buf.size() != n + rpos)
183         return EOF;
184       rpos += n;
185       pbump(-n);
186       gbump(egptr() - gptr());
187     }
188   return 0;
191 inline int stringbuf::overflow(int ch)
193   if((mode & ios::out) == 0)
194     return EOF;
196   streamsize n = pptr() - pbase();
198   if(n && sync())
199     return EOF;
201   if(ch != EOF)
202     {
203       string::size_type oldSize = buf.size();
204       
205       buf.replace(rpos, string::npos, 1, ch);
206       if(buf.size() - oldSize != 1)
207         return EOF;
208       ++rpos;
209     }
210   return 0;
213 inline int stringbuf::underflow()
215   sync();
216   if((mode & ios::in) == 0)
217     {
218       return EOF;
219     }
220   if(rpos >= buf.size())
221     {
222       return EOF;
223     }
224   
225   string::size_type n = egptr() - eback();
226   string::size_type s;
228   s = buf.copy(eback(), n, rpos);
229   pbump(pbase() - pptr());
230   gbump(eback() - gptr());
231   int res = (0377 & buf[rpos]);
232   rpos += s;
233   return res;
236 #endif /* not __STRSTREAM__ */