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