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