31
|
1 |
/*
|
|
2 |
* Copyright (c) 1999
|
|
3 |
* Silicon Graphics Computer Systems, Inc.
|
|
4 |
*
|
|
5 |
* Copyright (c) 1999
|
|
6 |
* Boris Fomitchev
|
|
7 |
*
|
|
8 |
* This material is provided "as is", with absolutely no warranty expressed
|
|
9 |
* or implied. Any use is at your own risk.
|
|
10 |
*
|
|
11 |
* Permission to use or copy this software for any purpose is hereby granted
|
|
12 |
* without fee, provided the above notices are retained on all copies.
|
|
13 |
* Permission to modify the code and to distribute modified code is granted,
|
|
14 |
* provided the above notices are retained, and a notice that the code was
|
|
15 |
* modified is included with the above copyright notice.
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
#ifndef _STLP_INTERNAL_STREAMBUF
|
|
19 |
#define _STLP_INTERNAL_STREAMBUF
|
|
20 |
|
|
21 |
#ifndef _STLP_IOS_BASE_H
|
|
22 |
# include <stl/_ios_base.h> // Needed for ios_base bitfield members.
|
|
23 |
#endif // <ios_base> includes <iosfwd>.
|
|
24 |
|
|
25 |
_STLP_BEGIN_NAMESPACE
|
|
26 |
|
|
27 |
//----------------------------------------------------------------------
|
|
28 |
// Class basic_streambuf<>, the base class of the streambuf hierarchy.
|
|
29 |
|
|
30 |
// A basic_streambuf<> manages an input (get) area and an output (put)
|
|
31 |
// area. Each is described by three pointers: a beginning, an end, and a
|
|
32 |
// current position. basic_streambuf<> contains some very simple member
|
|
33 |
// functions that manipulate those six pointers, but almost all of the real
|
|
34 |
// functionality gets delegated to protected virtual member functions.
|
|
35 |
// All of the public member functions are inline, and most of the protected
|
|
36 |
// member functions are virtual.
|
|
37 |
|
|
38 |
// Although basic_streambuf<> is not abstract, it is useful only as a base
|
|
39 |
// class. Its virtual member functions have default definitions such that
|
|
40 |
// reading from a basic_streambuf<> will always yield EOF, and writing to a
|
|
41 |
// basic_streambuf<> will always fail.
|
|
42 |
|
|
43 |
// The second template parameter, _Traits, defaults to char_traits<_CharT>.
|
|
44 |
// The default is declared in header <iosfwd>, and it isn't declared here
|
|
45 |
// because C++ language rules do not allow it to be declared twice.
|
|
46 |
|
|
47 |
template <class _CharT, class _Traits>
|
|
48 |
class basic_streambuf {
|
|
49 |
friend class basic_istream<_CharT, _Traits>;
|
|
50 |
friend class basic_ostream<_CharT, _Traits>;
|
|
51 |
|
|
52 |
public: // Typedefs.
|
|
53 |
typedef _CharT char_type;
|
|
54 |
typedef typename _Traits::int_type int_type;
|
|
55 |
typedef typename _Traits::pos_type pos_type;
|
|
56 |
typedef typename _Traits::off_type off_type;
|
|
57 |
typedef _Traits traits_type;
|
|
58 |
|
|
59 |
private: // Data members.
|
|
60 |
|
|
61 |
char_type* _M_gbegin; // Beginning of get area
|
|
62 |
char_type* _M_gnext; // Current position within the get area
|
|
63 |
char_type* _M_gend; // End of get area
|
|
64 |
|
|
65 |
char_type* _M_pbegin; // Beginning of put area
|
|
66 |
char_type* _M_pnext; // Current position within the put area
|
|
67 |
char_type* _M_pend; // End of put area
|
|
68 |
|
|
69 |
locale _M_locale; // The streambuf's locale object
|
|
70 |
|
|
71 |
//public: // Extension: locking, for thread safety.
|
|
72 |
// _STLP_mutex _M_lock;
|
|
73 |
|
|
74 |
public: // Destructor.
|
|
75 |
virtual ~basic_streambuf(){}
|
|
76 |
|
|
77 |
protected: // The default constructor.
|
|
78 |
basic_streambuf()
|
|
79 |
#if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB)
|
|
80 |
//We make it inline to avoid unresolved symbol.
|
|
81 |
: _M_gbegin(0), _M_gnext(0), _M_gend(0),
|
|
82 |
_M_pbegin(0), _M_pnext(0), _M_pend(0),
|
|
83 |
_M_locale()
|
|
84 |
{}
|
|
85 |
#else
|
|
86 |
;
|
|
87 |
#endif
|
|
88 |
|
|
89 |
protected: // Protected interface to the get area.
|
|
90 |
char_type* eback() const { return _M_gbegin; } // Beginning
|
|
91 |
char_type* gptr() const { return _M_gnext; } // Current position
|
|
92 |
char_type* egptr() const { return _M_gend; } // End
|
|
93 |
|
|
94 |
void gbump(int __n) { _M_gnext += __n; }
|
|
95 |
void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
|
|
96 |
_M_gbegin = __gbegin;
|
|
97 |
_M_gnext = __gnext;
|
|
98 |
_M_gend = __gend;
|
|
99 |
}
|
|
100 |
|
|
101 |
public:
|
|
102 |
// An alternate public interface to the above functions
|
|
103 |
// which allows us to avoid using templated friends which
|
|
104 |
// are not supported on some compilers.
|
|
105 |
char_type* _M_eback() const { return eback(); }
|
|
106 |
char_type* _M_gptr() const { return gptr(); }
|
|
107 |
char_type* _M_egptr() const { return egptr(); }
|
|
108 |
void _M_gbump(int __n) { gbump(__n); }
|
|
109 |
void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
|
|
110 |
{ this->setg(__gbegin, __gnext, __gend); }
|
|
111 |
|
|
112 |
protected: // Protected interface to the put area
|
|
113 |
|
|
114 |
char_type* pbase() const { return _M_pbegin; } // Beginning
|
|
115 |
char_type* pptr() const { return _M_pnext; } // Current position
|
|
116 |
char_type* epptr() const { return _M_pend; } // End
|
|
117 |
|
|
118 |
void pbump(int __n) { _M_pnext += __n; }
|
|
119 |
void setp(char_type* __pbegin, char_type* __pend) {
|
|
120 |
_M_pbegin = __pbegin;
|
|
121 |
_M_pnext = __pbegin;
|
|
122 |
_M_pend = __pend;
|
|
123 |
}
|
|
124 |
|
|
125 |
protected: // Virtual buffer management functions.
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize) {
|
|
130 |
return this;
|
|
131 |
}
|
|
132 |
|
|
133 |
// Alters the stream position, using an integer offset. In this
|
|
134 |
// class seekoff does nothing; subclasses are expected to override it.
|
|
135 |
|
|
136 |
virtual pos_type seekoff(off_type, ios_base::seekdir,
|
|
137 |
ios_base::openmode = ios_base::in | ios_base::out) {
|
|
138 |
return pos_type(-1);
|
|
139 |
}
|
|
140 |
|
|
141 |
// Alters the stream position, using a previously obtained streampos. In
|
|
142 |
// this class seekpos does nothing; subclasses are expected to override it.
|
|
143 |
virtual pos_type
|
|
144 |
seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out) {
|
|
145 |
return pos_type(-1);
|
|
146 |
}
|
|
147 |
|
|
148 |
// Synchronizes (i.e. flushes) the buffer. All subclasses are expected to
|
|
149 |
// override this virtual member function.
|
|
150 |
virtual int sync() {
|
|
151 |
return 0;
|
|
152 |
}
|
|
153 |
|
|
154 |
public: // Buffer management.
|
|
155 |
basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
|
|
156 |
{ return this->setbuf(__s, __n); }
|
|
157 |
|
|
158 |
pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
|
|
159 |
ios_base::openmode __mod = ios_base::in | ios_base::out)
|
|
160 |
{ return this->seekoff(__offset, __way, __mod); }
|
|
161 |
|
|
162 |
pos_type pubseekpos(pos_type __sp,
|
|
163 |
ios_base::openmode __mod = ios_base::in | ios_base::out)
|
|
164 |
{ return this->seekpos(__sp, __mod); }
|
|
165 |
|
|
166 |
int pubsync() { return this->sync(); }
|
|
167 |
|
|
168 |
protected: // Virtual get area functions, as defined in
|
|
169 |
// 17.5.2.4.3 and 17.5.2.4.4 of the standard.
|
|
170 |
// Returns a lower bound on the number of characters that we can read,
|
|
171 |
// with underflow, before reaching end of file. (-1 is a special value:
|
|
172 |
// it means that underflow will fail.) Most subclasses should probably
|
|
173 |
// override this virtual member function.
|
|
174 |
|
|
175 |
virtual streamsize showmanyc() {
|
|
176 |
return 0;
|
|
177 |
}
|
|
178 |
|
|
179 |
// Reads up to __n characters. Return value is the number of
|
|
180 |
// characters read.
|
|
181 |
virtual streamsize xsgetn(char_type* __s, streamsize __n) {
|
|
182 |
streamsize __result = 0;
|
|
183 |
const int_type __eof = _Traits::eof();
|
|
184 |
|
|
185 |
while (__result < __n) {
|
|
186 |
if (_M_gnext < _M_gend) {
|
|
187 |
size_t __chunk = (min) (__STATIC_CAST(size_t,_M_gend - _M_gnext),
|
|
188 |
__STATIC_CAST(size_t,__n - __result));
|
|
189 |
_Traits::copy(__s, _M_gnext, __chunk);
|
|
190 |
__result += __chunk;
|
|
191 |
__s += __chunk;
|
|
192 |
_M_gnext += __chunk;
|
|
193 |
}
|
|
194 |
else {
|
|
195 |
int_type __c = this->sbumpc();
|
|
196 |
if (!_Traits::eq_int_type(__c, __eof)) {
|
|
197 |
*__s = _Traits::to_char_type(__c);
|
|
198 |
++__result;
|
|
199 |
++__s;
|
|
200 |
}
|
|
201 |
else
|
|
202 |
break;
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
return __result;
|
|
207 |
}
|
|
208 |
|
|
209 |
// Called when there is no read position, i.e. when gptr() is null
|
|
210 |
// or when gptr() >= egptr(). Subclasses are expected to override
|
|
211 |
// this virtual member function.
|
|
212 |
virtual int_type underflow() {
|
|
213 |
return _Traits::eof();
|
|
214 |
}
|
|
215 |
|
|
216 |
// Similar to underflow(), but used for unbuffered input. Most
|
|
217 |
// subclasses should probably override this virtual member function.
|
|
218 |
virtual int_type uflow() {
|
|
219 |
return ( _Traits::eq_int_type(this->underflow(),_Traits::eof()) ?
|
|
220 |
_Traits::eof() :
|
|
221 |
_Traits::to_int_type(*_M_gnext++));
|
|
222 |
}
|
|
223 |
|
|
224 |
// Called when there is no putback position, i.e. when gptr() is null
|
|
225 |
// or when gptr() == eback(). All subclasses are expected to override
|
|
226 |
// this virtual member function.
|
|
227 |
virtual int_type pbackfail(int_type = traits_type::eof()) {
|
|
228 |
return _Traits::eof();
|
|
229 |
}
|
|
230 |
|
|
231 |
|
|
232 |
protected: // Virtual put area functions, as defined in
|
|
233 |
// 27.5.2.4.5 of the standard.
|
|
234 |
|
|
235 |
// Writes up to __n characters. Return value is the number of characters
|
|
236 |
// written.
|
|
237 |
|
|
238 |
virtual streamsize xsputn(const char_type* __s, streamsize __n) {
|
|
239 |
streamsize __result = 0;
|
|
240 |
const int_type __eof = _Traits::eof();
|
|
241 |
|
|
242 |
while (__result < __n) {
|
|
243 |
if (_M_pnext < _M_pend) {
|
|
244 |
size_t __chunk = (min) (__STATIC_CAST(size_t,_M_pend - _M_pnext),
|
|
245 |
__STATIC_CAST(size_t,__n - __result));
|
|
246 |
_Traits::copy(_M_pnext, __s, __chunk);
|
|
247 |
__result += __chunk;
|
|
248 |
__s += __chunk;
|
|
249 |
_M_pnext += __chunk;
|
|
250 |
}
|
|
251 |
|
|
252 |
else if (!_Traits::eq_int_type(this->overflow(_Traits::to_int_type(*__s)),
|
|
253 |
__eof)) {
|
|
254 |
++__result;
|
|
255 |
++__s;
|
|
256 |
}
|
|
257 |
else
|
|
258 |
break;
|
|
259 |
}
|
|
260 |
return __result;
|
|
261 |
}
|
|
262 |
|
|
263 |
// Extension: writes up to __n copies of __c. Return value is the number
|
|
264 |
// of characters written.
|
|
265 |
|
|
266 |
virtual streamsize _M_xsputnc(char_type __c, streamsize __n) {
|
|
267 |
streamsize __result = 0;
|
|
268 |
const int_type __eof = _Traits::eof();
|
|
269 |
|
|
270 |
while (__result < __n) {
|
|
271 |
if (_M_pnext < _M_pend) {
|
|
272 |
size_t __chunk = (min) (__STATIC_CAST(size_t,_M_pend - _M_pnext),
|
|
273 |
__STATIC_CAST(size_t,__n - __result));
|
|
274 |
_Traits::assign(_M_pnext, __chunk, __c);
|
|
275 |
__result += __chunk;
|
|
276 |
_M_pnext += __chunk;
|
|
277 |
}
|
|
278 |
|
|
279 |
else if (!_Traits::eq_int_type(this->overflow(_Traits::to_int_type(__c)),
|
|
280 |
__eof))
|
|
281 |
++__result;
|
|
282 |
else
|
|
283 |
break;
|
|
284 |
}
|
|
285 |
return __result;
|
|
286 |
}
|
|
287 |
|
|
288 |
// Called when there is no write position. All subclasses are expected to
|
|
289 |
// override this virtual member function.
|
|
290 |
|
|
291 |
virtual int_type overflow(int_type = traits_type::eof()) {
|
|
292 |
return _Traits::eof();
|
|
293 |
}
|
|
294 |
|
|
295 |
|
|
296 |
public: // Public members for writing characters.
|
|
297 |
// Write a single character.
|
|
298 |
int_type sputc(char_type __c) {
|
|
299 |
return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
|
|
300 |
: this->overflow(_Traits::to_int_type(__c)));
|
|
301 |
}
|
|
302 |
|
|
303 |
// Write __n characters.
|
|
304 |
streamsize sputn(const char_type* __s, streamsize __n)
|
|
305 |
{ return this->xsputn(__s, __n); }
|
|
306 |
|
|
307 |
// Extension: write __n copies of __c.
|
|
308 |
streamsize _M_sputnc(char_type __c, streamsize __n)
|
|
309 |
{ return this->_M_xsputnc(__c, __n); }
|
|
310 |
|
|
311 |
private: // Helper functions.
|
|
312 |
int_type _M_snextc_aux();
|
|
313 |
|
|
314 |
public: // Public members for reading characters.
|
|
315 |
streamsize in_avail() {
|
|
316 |
return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
|
|
317 |
}
|
|
318 |
|
|
319 |
// Advance to the next character and return it.
|
|
320 |
int_type snextc() {
|
|
321 |
return ( _M_gend - _M_gnext > 1 ?
|
|
322 |
_Traits::to_int_type(*++_M_gnext) :
|
|
323 |
this->_M_snextc_aux());
|
|
324 |
}
|
|
325 |
|
|
326 |
// Return the current character and advance to the next.
|
|
327 |
int_type sbumpc() {
|
|
328 |
return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
|
|
329 |
: this->uflow();
|
|
330 |
}
|
|
331 |
|
|
332 |
// Return the current character without advancing to the next.
|
|
333 |
int_type sgetc() {
|
|
334 |
return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
|
|
335 |
: this->underflow();
|
|
336 |
}
|
|
337 |
|
|
338 |
streamsize sgetn(char_type* __s, streamsize __n)
|
|
339 |
{ return this->xsgetn(__s, __n); }
|
|
340 |
|
|
341 |
int_type sputbackc(char_type __c) {
|
|
342 |
return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
|
|
343 |
? _Traits::to_int_type(*--_M_gnext)
|
|
344 |
: this->pbackfail(_Traits::to_int_type(__c));
|
|
345 |
}
|
|
346 |
|
|
347 |
int_type sungetc() {
|
|
348 |
return (_M_gbegin < _M_gnext)
|
|
349 |
? _Traits::to_int_type(*--_M_gnext)
|
|
350 |
: this->pbackfail();
|
|
351 |
}
|
|
352 |
|
|
353 |
protected: // Virtual locale functions.
|
|
354 |
|
|
355 |
// This is a hook, called by pubimbue() just before pubimbue()
|
|
356 |
// sets the streambuf's locale to __loc. Note that imbue should
|
|
357 |
// not (and cannot, since it has no access to streambuf's private
|
|
358 |
// members) set the streambuf's locale itself.
|
|
359 |
|
|
360 |
virtual void imbue(const locale&) {}
|
|
361 |
|
|
362 |
public: // Locale-related functions.
|
|
363 |
locale pubimbue(const locale&);
|
|
364 |
locale getloc() const { return _M_locale; }
|
|
365 |
|
|
366 |
#if !defined (_STLP_NO_ANACHRONISMS)
|
|
367 |
void stossc() { this->sbumpc(); }
|
|
368 |
#endif
|
|
369 |
#if defined (__MVS__) || defined (__OS400__)
|
|
370 |
private: // Data members.
|
|
371 |
|
|
372 |
char_type* _M_gbegin; // Beginning of get area
|
|
373 |
char_type* _M_gnext; // Current position within the get area
|
|
374 |
char_type* _M_gend; // End of get area
|
|
375 |
|
|
376 |
char_type* _M_pbegin; // Beginning of put area
|
|
377 |
char_type* _M_pnext; // Current position within the put area
|
|
378 |
char_type* _M_pend; // End of put area
|
|
379 |
#endif
|
|
380 |
};
|
|
381 |
|
|
382 |
#if defined (_STLP_USE_TEMPLATE_EXPORT)
|
|
383 |
_STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<char, char_traits<char> >;
|
|
384 |
# if !defined (_STLP_NO_WCHAR_T)
|
|
385 |
_STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<wchar_t, char_traits<wchar_t> >;
|
|
386 |
# endif // _STLP_NO_WCHAR_T
|
|
387 |
#endif // _STLP_USE_TEMPLATE_EXPORT
|
|
388 |
|
|
389 |
_STLP_END_NAMESPACE
|
|
390 |
|
|
391 |
#if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
|
|
392 |
# include <stl/_streambuf.c>
|
|
393 |
#endif
|
|
394 |
|
|
395 |
#endif
|
|
396 |
|
|
397 |
// Local Variables:
|
|
398 |
// mode:C++
|
|
399 |
// End:
|