]> git.lyx.org Git - lyx.git/blob - src/support/sstream.h
remove !NEW_INSETS cruft
[lyx.git] / src / support / sstream.h
1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 2000 Free Software Foundation
3
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.
9
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.
14
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.
18
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. */
24
25 /* Written by Magnus Fromreide (magfr@lysator.liu.se). */
26
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.
31
32    Lars Gullik Bjønnes (larsbj@lyx.org)
33 */
34
35 #ifndef SSTREAM_H
36 #define SSTREAM_H
37
38 #include "LString.h"
39 #include <iostream>
40 #include <streambuf.h>
41
42 namespace std
43 {
44   class stringbuf : public streambuf
45   {
46   public:
47     typedef char        char_type;
48     typedef int         int_type;
49     typedef streampos   pos_type;
50     typedef streamoff   off_type;
51
52     explicit stringbuf(int which=ios::in|ios::out) :
53       streambuf(which), buf(), mode(static_cast<ios::open_mode>(which)),
54       rpos(0), bufsize(1)
55     { }
56         
57     explicit stringbuf(const string &s, int which=ios::in|ios::out) :
58       streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
59       bufsize(1)
60     {
61       if(mode & ios::in)
62         {
63           setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
64         }
65       if(mode & ios::out)
66         {
67           setp(&defbuf, &defbuf + bufsize);
68         }
69       rpos = (mode & ios::ate ? s.size() : 0);
70     }
71         
72     string str() const
73     {
74       const_cast<stringbuf*>(this)->sync();  // Sigh, really ugly hack
75       return buf;
76     };
77
78     void str(const string& s)
79     {
80       buf = s;
81       if(mode & ios::in)
82         {
83           gbump(egptr() - gptr());
84         }
85       if(mode & ios::out)
86         {
87           pbump(pbase() - pptr());
88         }
89       rpos = (mode & ios::ate ? s.size() : 0);
90     }
91
92   protected:
93     inline virtual int sync();
94     inline virtual int overflow(int = EOF);
95     inline virtual int underflow();
96   private:
97     string                      buf;
98     ios::open_mode              mode;
99     string::size_type   rpos;
100     streamsize                  bufsize;
101     char                        defbuf;
102   };
103
104   class stringstreambase : virtual public ios {
105   protected:
106     stringbuf __my_sb;
107   public:
108     string str() const
109     {
110       return static_cast<stringbuf*>(_strbuf)->str();
111     }
112     void str(const string& s)
113     {
114       clear();
115       static_cast<stringbuf*>(_strbuf)->str(s);
116     }
117         
118     stringbuf* rdbuf()
119     {
120       return &__my_sb;
121     }
122   protected:
123     stringstreambase(int which) :
124       __my_sb(which)
125     {
126       init (&__my_sb);
127     }
128         
129     stringstreambase(const string& s, int which) :
130       __my_sb(s, which)
131     {
132       init (&__my_sb);
133     }
134   };
135     
136   class istringstream : public stringstreambase, public istream {
137   public:
138     istringstream(int which=ios::in) :
139       stringstreambase(which)
140     { }
141         
142     istringstream(const string& s, int which=ios::in) :
143       stringstreambase(s, which)
144     { }
145   };
146     
147   class ostringstream : public stringstreambase, public ostream {
148   public:
149     ostringstream(int which=ios::out) :
150       stringstreambase(which)
151     { }
152         
153     ostringstream(const string& s, int which=ios::out) :
154       stringstreambase(s, which)
155     { }
156   };
157     
158   class stringstream : public stringstreambase, public iostream {
159   public:
160     stringstream(int which=ios::in|ios::out) :
161       stringstreambase(which)
162     { }
163     
164     stringstream(const string &s, int which=ios::in|ios::out) :
165       stringstreambase(s, which)
166     { }
167   };
168
169 } // namespace std
170
171 inline int stringbuf::sync()
172 {
173   if((mode & ios::out) == 0)
174     return EOF;
175
176   streamsize n = pptr() - pbase();
177   if(n)
178     {
179       buf.replace(rpos, string::npos, pbase(), n);
180       //if(buf.size() - rpos != n)
181       if (buf.size() != n + rpos)
182         return EOF;
183       rpos += n;
184       pbump(-n);
185       gbump(egptr() - gptr());
186     }
187   return 0;
188 }
189
190 inline int stringbuf::overflow(int ch)
191 {
192   if((mode & ios::out) == 0)
193     return EOF;
194
195   streamsize n = pptr() - pbase();
196
197   if(n && sync())
198     return EOF;
199
200   if(ch != EOF)
201     {
202       string::size_type oldSize = buf.size();
203       
204       buf.replace(rpos, string::npos, 1, ch);
205       if(buf.size() - oldSize != 1)
206         return EOF;
207       ++rpos;
208     }
209   return 0;
210 }
211
212 inline int stringbuf::underflow()
213 {
214   sync();
215   if((mode & ios::in) == 0)
216     {
217       return EOF;
218     }
219   if(rpos >= buf.size())
220     {
221       return EOF;
222     }
223   
224   string::size_type n = egptr() - eback();
225   string::size_type s;
226
227   s = buf.copy(eback(), n, rpos);
228   pbump(pbase() - pptr());
229   gbump(eback() - gptr());
230   int res = (0377 & buf[rpos]);
231   rpos += s;
232   return res;
233 }
234
235 #endif /* not __STRSTREAM__ */