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