1 |
35 |
ultra_embe |
// Iostreams base classes -*- C++ -*-
|
2 |
|
|
|
3 |
|
|
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
4 |
|
|
// 2006, 2007, 2008, 2009, 2010, 2011, 2012
|
5 |
|
|
// Free Software Foundation, Inc.
|
6 |
|
|
//
|
7 |
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
8 |
|
|
// software; you can redistribute it and/or modify it under the
|
9 |
|
|
// terms of the GNU General Public License as published by the
|
10 |
|
|
// Free Software Foundation; either version 3, or (at your option)
|
11 |
|
|
// any later version.
|
12 |
|
|
|
13 |
|
|
// This library is distributed in the hope that it will be useful,
|
14 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
// GNU General Public License for more details.
|
17 |
|
|
|
18 |
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
19 |
|
|
// permissions described in the GCC Runtime Library Exception, version
|
20 |
|
|
// 3.1, as published by the Free Software Foundation.
|
21 |
|
|
|
22 |
|
|
// You should have received a copy of the GNU General Public License and
|
23 |
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
24 |
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
25 |
|
|
// <http://www.gnu.org/licenses/>.
|
26 |
|
|
|
27 |
|
|
/** @file bits/basic_ios.h
|
28 |
|
|
* This is an internal header file, included by other library headers.
|
29 |
|
|
* Do not attempt to use it directly. @headername{ios}
|
30 |
|
|
*/
|
31 |
|
|
|
32 |
|
|
#ifndef _BASIC_IOS_H
|
33 |
|
|
#define _BASIC_IOS_H 1
|
34 |
|
|
|
35 |
|
|
#pragma GCC system_header
|
36 |
|
|
|
37 |
|
|
#include <bits/localefwd.h>
|
38 |
|
|
#include <bits/locale_classes.h>
|
39 |
|
|
#include <bits/locale_facets.h>
|
40 |
|
|
#include <bits/streambuf_iterator.h>
|
41 |
|
|
|
42 |
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
43 |
|
|
{
|
44 |
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
45 |
|
|
|
46 |
|
|
template<typename _Facet>
|
47 |
|
|
inline const _Facet&
|
48 |
|
|
__check_facet(const _Facet* __f)
|
49 |
|
|
{
|
50 |
|
|
if (!__f)
|
51 |
|
|
__throw_bad_cast();
|
52 |
|
|
return *__f;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
/**
|
56 |
|
|
* @brief Template class basic_ios, virtual base class for all
|
57 |
|
|
* stream classes.
|
58 |
|
|
* @ingroup io
|
59 |
|
|
*
|
60 |
|
|
* @tparam _CharT Type of character stream.
|
61 |
|
|
* @tparam _Traits Traits for character type, defaults to
|
62 |
|
|
* char_traits<_CharT>.
|
63 |
|
|
*
|
64 |
|
|
* Most of the member functions called dispatched on stream objects
|
65 |
|
|
* (e.g., @c std::cout.foo(bar);) are consolidated in this class.
|
66 |
|
|
*/
|
67 |
|
|
template<typename _CharT, typename _Traits>
|
68 |
|
|
class basic_ios : public ios_base
|
69 |
|
|
{
|
70 |
|
|
public:
|
71 |
|
|
//@{
|
72 |
|
|
/**
|
73 |
|
|
* These are standard types. They permit a standardized way of
|
74 |
|
|
* referring to names of (or names dependant on) the template
|
75 |
|
|
* parameters, which are specific to the implementation.
|
76 |
|
|
*/
|
77 |
|
|
typedef _CharT char_type;
|
78 |
|
|
typedef typename _Traits::int_type int_type;
|
79 |
|
|
typedef typename _Traits::pos_type pos_type;
|
80 |
|
|
typedef typename _Traits::off_type off_type;
|
81 |
|
|
typedef _Traits traits_type;
|
82 |
|
|
//@}
|
83 |
|
|
|
84 |
|
|
//@{
|
85 |
|
|
/**
|
86 |
|
|
* These are non-standard types.
|
87 |
|
|
*/
|
88 |
|
|
typedef ctype<_CharT> __ctype_type;
|
89 |
|
|
typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
|
90 |
|
|
__num_put_type;
|
91 |
|
|
typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
|
92 |
|
|
__num_get_type;
|
93 |
|
|
//@}
|
94 |
|
|
|
95 |
|
|
// Data members:
|
96 |
|
|
protected:
|
97 |
|
|
basic_ostream<_CharT, _Traits>* _M_tie;
|
98 |
|
|
mutable char_type _M_fill;
|
99 |
|
|
mutable bool _M_fill_init;
|
100 |
|
|
basic_streambuf<_CharT, _Traits>* _M_streambuf;
|
101 |
|
|
|
102 |
|
|
// Cached use_facet<ctype>, which is based on the current locale info.
|
103 |
|
|
const __ctype_type* _M_ctype;
|
104 |
|
|
// For ostream.
|
105 |
|
|
const __num_put_type* _M_num_put;
|
106 |
|
|
// For istream.
|
107 |
|
|
const __num_get_type* _M_num_get;
|
108 |
|
|
|
109 |
|
|
public:
|
110 |
|
|
//@{
|
111 |
|
|
/**
|
112 |
|
|
* @brief The quick-and-easy status check.
|
113 |
|
|
*
|
114 |
|
|
* This allows you to write constructs such as
|
115 |
|
|
* <code>if (!a_stream) ...</code> and <code>while (a_stream) ...</code>
|
116 |
|
|
*/
|
117 |
|
|
operator void*() const
|
118 |
|
|
{ return this->fail() ? 0 : const_cast<basic_ios*>(this); }
|
119 |
|
|
|
120 |
|
|
bool
|
121 |
|
|
operator!() const
|
122 |
|
|
{ return this->fail(); }
|
123 |
|
|
//@}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* @brief Returns the error state of the stream buffer.
|
127 |
|
|
* @return A bit pattern (well, isn't everything?)
|
128 |
|
|
*
|
129 |
|
|
* See std::ios_base::iostate for the possible bit values. Most
|
130 |
|
|
* users will call one of the interpreting wrappers, e.g., good().
|
131 |
|
|
*/
|
132 |
|
|
iostate
|
133 |
|
|
rdstate() const
|
134 |
|
|
{ return _M_streambuf_state; }
|
135 |
|
|
|
136 |
|
|
/**
|
137 |
|
|
* @brief [Re]sets the error state.
|
138 |
|
|
* @param __state The new state flag(s) to set.
|
139 |
|
|
*
|
140 |
|
|
* See std::ios_base::iostate for the possible bit values. Most
|
141 |
|
|
* users will not need to pass an argument.
|
142 |
|
|
*/
|
143 |
|
|
void
|
144 |
|
|
clear(iostate __state = goodbit);
|
145 |
|
|
|
146 |
|
|
/**
|
147 |
|
|
* @brief Sets additional flags in the error state.
|
148 |
|
|
* @param __state The additional state flag(s) to set.
|
149 |
|
|
*
|
150 |
|
|
* See std::ios_base::iostate for the possible bit values.
|
151 |
|
|
*/
|
152 |
|
|
void
|
153 |
|
|
setstate(iostate __state)
|
154 |
|
|
{ this->clear(this->rdstate() | __state); }
|
155 |
|
|
|
156 |
|
|
// Flip the internal state on for the proper state bits, then re
|
157 |
|
|
// throws the propagated exception if bit also set in
|
158 |
|
|
// exceptions().
|
159 |
|
|
void
|
160 |
|
|
_M_setstate(iostate __state)
|
161 |
|
|
{
|
162 |
|
|
// 27.6.1.2.1 Common requirements.
|
163 |
|
|
// Turn this on without causing an ios::failure to be thrown.
|
164 |
|
|
_M_streambuf_state |= __state;
|
165 |
|
|
if (this->exceptions() & __state)
|
166 |
|
|
__throw_exception_again;
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* @brief Fast error checking.
|
171 |
|
|
* @return True if no error flags are set.
|
172 |
|
|
*
|
173 |
|
|
* A wrapper around rdstate.
|
174 |
|
|
*/
|
175 |
|
|
bool
|
176 |
|
|
good() const
|
177 |
|
|
{ return this->rdstate() == 0; }
|
178 |
|
|
|
179 |
|
|
/**
|
180 |
|
|
* @brief Fast error checking.
|
181 |
|
|
* @return True if the eofbit is set.
|
182 |
|
|
*
|
183 |
|
|
* Note that other iostate flags may also be set.
|
184 |
|
|
*/
|
185 |
|
|
bool
|
186 |
|
|
eof() const
|
187 |
|
|
{ return (this->rdstate() & eofbit) != 0; }
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* @brief Fast error checking.
|
191 |
|
|
* @return True if either the badbit or the failbit is set.
|
192 |
|
|
*
|
193 |
|
|
* Checking the badbit in fail() is historical practice.
|
194 |
|
|
* Note that other iostate flags may also be set.
|
195 |
|
|
*/
|
196 |
|
|
bool
|
197 |
|
|
fail() const
|
198 |
|
|
{ return (this->rdstate() & (badbit | failbit)) != 0; }
|
199 |
|
|
|
200 |
|
|
/**
|
201 |
|
|
* @brief Fast error checking.
|
202 |
|
|
* @return True if the badbit is set.
|
203 |
|
|
*
|
204 |
|
|
* Note that other iostate flags may also be set.
|
205 |
|
|
*/
|
206 |
|
|
bool
|
207 |
|
|
bad() const
|
208 |
|
|
{ return (this->rdstate() & badbit) != 0; }
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
|
|
* @brief Throwing exceptions on errors.
|
212 |
|
|
* @return The current exceptions mask.
|
213 |
|
|
*
|
214 |
|
|
* This changes nothing in the stream. See the one-argument version
|
215 |
|
|
* of exceptions(iostate) for the meaning of the return value.
|
216 |
|
|
*/
|
217 |
|
|
iostate
|
218 |
|
|
exceptions() const
|
219 |
|
|
{ return _M_exception; }
|
220 |
|
|
|
221 |
|
|
/**
|
222 |
|
|
* @brief Throwing exceptions on errors.
|
223 |
|
|
* @param __except The new exceptions mask.
|
224 |
|
|
*
|
225 |
|
|
* By default, error flags are set silently. You can set an
|
226 |
|
|
* exceptions mask for each stream; if a bit in the mask becomes set
|
227 |
|
|
* in the error flags, then an exception of type
|
228 |
|
|
* std::ios_base::failure is thrown.
|
229 |
|
|
*
|
230 |
|
|
* If the error flag is already set when the exceptions mask is
|
231 |
|
|
* added, the exception is immediately thrown. Try running the
|
232 |
|
|
* following under GCC 3.1 or later:
|
233 |
|
|
* @code
|
234 |
|
|
* #include <iostream>
|
235 |
|
|
* #include <fstream>
|
236 |
|
|
* #include <exception>
|
237 |
|
|
*
|
238 |
|
|
* int main()
|
239 |
|
|
* {
|
240 |
|
|
* std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
|
241 |
|
|
*
|
242 |
|
|
* std::ifstream f ("/etc/motd");
|
243 |
|
|
*
|
244 |
|
|
* std::cerr << "Setting badbit\n";
|
245 |
|
|
* f.setstate (std::ios_base::badbit);
|
246 |
|
|
*
|
247 |
|
|
* std::cerr << "Setting exception mask\n";
|
248 |
|
|
* f.exceptions (std::ios_base::badbit);
|
249 |
|
|
* }
|
250 |
|
|
* @endcode
|
251 |
|
|
*/
|
252 |
|
|
void
|
253 |
|
|
exceptions(iostate __except)
|
254 |
|
|
{
|
255 |
|
|
_M_exception = __except;
|
256 |
|
|
this->clear(_M_streambuf_state);
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
// Constructor/destructor:
|
260 |
|
|
/**
|
261 |
|
|
* @brief Constructor performs initialization.
|
262 |
|
|
*
|
263 |
|
|
* The parameter is passed by derived streams.
|
264 |
|
|
*/
|
265 |
|
|
explicit
|
266 |
|
|
basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
|
267 |
|
|
: ios_base(), _M_tie(0), _M_fill(), _M_fill_init(false), _M_streambuf(0),
|
268 |
|
|
_M_ctype(0), _M_num_put(0), _M_num_get(0)
|
269 |
|
|
{ this->init(__sb); }
|
270 |
|
|
|
271 |
|
|
/**
|
272 |
|
|
* @brief Empty.
|
273 |
|
|
*
|
274 |
|
|
* The destructor does nothing. More specifically, it does not
|
275 |
|
|
* destroy the streambuf held by rdbuf().
|
276 |
|
|
*/
|
277 |
|
|
virtual
|
278 |
|
|
~basic_ios() { }
|
279 |
|
|
|
280 |
|
|
// Members:
|
281 |
|
|
/**
|
282 |
|
|
* @brief Fetches the current @e tied stream.
|
283 |
|
|
* @return A pointer to the tied stream, or NULL if the stream is
|
284 |
|
|
* not tied.
|
285 |
|
|
*
|
286 |
|
|
* A stream may be @e tied (or synchronized) to a second output
|
287 |
|
|
* stream. When this stream performs any I/O, the tied stream is
|
288 |
|
|
* first flushed. For example, @c std::cin is tied to @c std::cout.
|
289 |
|
|
*/
|
290 |
|
|
basic_ostream<_CharT, _Traits>*
|
291 |
|
|
tie() const
|
292 |
|
|
{ return _M_tie; }
|
293 |
|
|
|
294 |
|
|
/**
|
295 |
|
|
* @brief Ties this stream to an output stream.
|
296 |
|
|
* @param __tiestr The output stream.
|
297 |
|
|
* @return The previously tied output stream, or NULL if the stream
|
298 |
|
|
* was not tied.
|
299 |
|
|
*
|
300 |
|
|
* This sets up a new tie; see tie() for more.
|
301 |
|
|
*/
|
302 |
|
|
basic_ostream<_CharT, _Traits>*
|
303 |
|
|
tie(basic_ostream<_CharT, _Traits>* __tiestr)
|
304 |
|
|
{
|
305 |
|
|
basic_ostream<_CharT, _Traits>* __old = _M_tie;
|
306 |
|
|
_M_tie = __tiestr;
|
307 |
|
|
return __old;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
/**
|
311 |
|
|
* @brief Accessing the underlying buffer.
|
312 |
|
|
* @return The current stream buffer.
|
313 |
|
|
*
|
314 |
|
|
* This does not change the state of the stream.
|
315 |
|
|
*/
|
316 |
|
|
basic_streambuf<_CharT, _Traits>*
|
317 |
|
|
rdbuf() const
|
318 |
|
|
{ return _M_streambuf; }
|
319 |
|
|
|
320 |
|
|
/**
|
321 |
|
|
* @brief Changing the underlying buffer.
|
322 |
|
|
* @param __sb The new stream buffer.
|
323 |
|
|
* @return The previous stream buffer.
|
324 |
|
|
*
|
325 |
|
|
* Associates a new buffer with the current stream, and clears the
|
326 |
|
|
* error state.
|
327 |
|
|
*
|
328 |
|
|
* Due to historical accidents which the LWG refuses to correct, the
|
329 |
|
|
* I/O library suffers from a design error: this function is hidden
|
330 |
|
|
* in derived classes by overrides of the zero-argument @c rdbuf(),
|
331 |
|
|
* which is non-virtual for hysterical raisins. As a result, you
|
332 |
|
|
* must use explicit qualifications to access this function via any
|
333 |
|
|
* derived class. For example:
|
334 |
|
|
*
|
335 |
|
|
* @code
|
336 |
|
|
* std::fstream foo; // or some other derived type
|
337 |
|
|
* std::streambuf* p = .....;
|
338 |
|
|
*
|
339 |
|
|
* foo.ios::rdbuf(p); // ios == basic_ios<char>
|
340 |
|
|
* @endcode
|
341 |
|
|
*/
|
342 |
|
|
basic_streambuf<_CharT, _Traits>*
|
343 |
|
|
rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
|
344 |
|
|
|
345 |
|
|
/**
|
346 |
|
|
* @brief Copies fields of __rhs into this.
|
347 |
|
|
* @param __rhs The source values for the copies.
|
348 |
|
|
* @return Reference to this object.
|
349 |
|
|
*
|
350 |
|
|
* All fields of __rhs are copied into this object except that rdbuf()
|
351 |
|
|
* and rdstate() remain unchanged. All values in the pword and iword
|
352 |
|
|
* arrays are copied. Before copying, each callback is invoked with
|
353 |
|
|
* erase_event. After copying, each (new) callback is invoked with
|
354 |
|
|
* copyfmt_event. The final step is to copy exceptions().
|
355 |
|
|
*/
|
356 |
|
|
basic_ios&
|
357 |
|
|
copyfmt(const basic_ios& __rhs);
|
358 |
|
|
|
359 |
|
|
/**
|
360 |
|
|
* @brief Retrieves the @a empty character.
|
361 |
|
|
* @return The current fill character.
|
362 |
|
|
*
|
363 |
|
|
* It defaults to a space (' ') in the current locale.
|
364 |
|
|
*/
|
365 |
|
|
char_type
|
366 |
|
|
fill() const
|
367 |
|
|
{
|
368 |
|
|
if (!_M_fill_init)
|
369 |
|
|
{
|
370 |
|
|
_M_fill = this->widen(' ');
|
371 |
|
|
_M_fill_init = true;
|
372 |
|
|
}
|
373 |
|
|
return _M_fill;
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
/**
|
377 |
|
|
* @brief Sets a new @a empty character.
|
378 |
|
|
* @param __ch The new character.
|
379 |
|
|
* @return The previous fill character.
|
380 |
|
|
*
|
381 |
|
|
* The fill character is used to fill out space when P+ characters
|
382 |
|
|
* have been requested (e.g., via setw), Q characters are actually
|
383 |
|
|
* used, and Q<P. It defaults to a space (' ') in the current locale.
|
384 |
|
|
*/
|
385 |
|
|
char_type
|
386 |
|
|
fill(char_type __ch)
|
387 |
|
|
{
|
388 |
|
|
char_type __old = this->fill();
|
389 |
|
|
_M_fill = __ch;
|
390 |
|
|
return __old;
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
// Locales:
|
394 |
|
|
/**
|
395 |
|
|
* @brief Moves to a new locale.
|
396 |
|
|
* @param __loc The new locale.
|
397 |
|
|
* @return The previous locale.
|
398 |
|
|
*
|
399 |
|
|
* Calls @c ios_base::imbue(loc), and if a stream buffer is associated
|
400 |
|
|
* with this stream, calls that buffer's @c pubimbue(loc).
|
401 |
|
|
*
|
402 |
|
|
* Additional l10n notes are at
|
403 |
|
|
* http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
|
404 |
|
|
*/
|
405 |
|
|
locale
|
406 |
|
|
imbue(const locale& __loc);
|
407 |
|
|
|
408 |
|
|
/**
|
409 |
|
|
* @brief Squeezes characters.
|
410 |
|
|
* @param __c The character to narrow.
|
411 |
|
|
* @param __dfault The character to narrow.
|
412 |
|
|
* @return The narrowed character.
|
413 |
|
|
*
|
414 |
|
|
* Maps a character of @c char_type to a character of @c char,
|
415 |
|
|
* if possible.
|
416 |
|
|
*
|
417 |
|
|
* Returns the result of
|
418 |
|
|
* @code
|
419 |
|
|
* std::use_facet<ctype<char_type> >(getloc()).narrow(c,dfault)
|
420 |
|
|
* @endcode
|
421 |
|
|
*
|
422 |
|
|
* Additional l10n notes are at
|
423 |
|
|
* http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
|
424 |
|
|
*/
|
425 |
|
|
char
|
426 |
|
|
narrow(char_type __c, char __dfault) const
|
427 |
|
|
{ return __check_facet(_M_ctype).narrow(__c, __dfault); }
|
428 |
|
|
|
429 |
|
|
/**
|
430 |
|
|
* @brief Widens characters.
|
431 |
|
|
* @param __c The character to widen.
|
432 |
|
|
* @return The widened character.
|
433 |
|
|
*
|
434 |
|
|
* Maps a character of @c char to a character of @c char_type.
|
435 |
|
|
*
|
436 |
|
|
* Returns the result of
|
437 |
|
|
* @code
|
438 |
|
|
* std::use_facet<ctype<char_type> >(getloc()).widen(c)
|
439 |
|
|
* @endcode
|
440 |
|
|
*
|
441 |
|
|
* Additional l10n notes are at
|
442 |
|
|
* http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
|
443 |
|
|
*/
|
444 |
|
|
char_type
|
445 |
|
|
widen(char __c) const
|
446 |
|
|
{ return __check_facet(_M_ctype).widen(__c); }
|
447 |
|
|
|
448 |
|
|
protected:
|
449 |
|
|
// 27.4.5.1 basic_ios constructors
|
450 |
|
|
/**
|
451 |
|
|
* @brief Empty.
|
452 |
|
|
*
|
453 |
|
|
* The default constructor does nothing and is not normally
|
454 |
|
|
* accessible to users.
|
455 |
|
|
*/
|
456 |
|
|
basic_ios()
|
457 |
|
|
: ios_base(), _M_tie(0), _M_fill(char_type()), _M_fill_init(false),
|
458 |
|
|
_M_streambuf(0), _M_ctype(0), _M_num_put(0), _M_num_get(0)
|
459 |
|
|
{ }
|
460 |
|
|
|
461 |
|
|
/**
|
462 |
|
|
* @brief All setup is performed here.
|
463 |
|
|
*
|
464 |
|
|
* This is called from the public constructor. It is not virtual and
|
465 |
|
|
* cannot be redefined.
|
466 |
|
|
*/
|
467 |
|
|
void
|
468 |
|
|
init(basic_streambuf<_CharT, _Traits>* __sb);
|
469 |
|
|
|
470 |
|
|
void
|
471 |
|
|
_M_cache_locale(const locale& __loc);
|
472 |
|
|
};
|
473 |
|
|
|
474 |
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
475 |
|
|
} // namespace
|
476 |
|
|
|
477 |
|
|
#include <bits/basic_ios.tcc>
|
478 |
|
|
|
479 |
|
|
#endif /* _BASIC_IOS_H */
|