OSDN Git Service

2005-03-23 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 27.4  Iostreams base classes
33 //
34
35 #include <ios>
36 #include <limits>
37 #include <bits/atomicity.h>
38
39 namespace std 
40 {  
41   // XXX GLIBCXX_ABI Deprecated
42    // Definitions for static const data members of __ios_flags.
43   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
44   const __ios_flags::__int_type __ios_flags::_S_dec;
45   const __ios_flags::__int_type __ios_flags::_S_fixed;
46   const __ios_flags::__int_type __ios_flags::_S_hex;
47   const __ios_flags::__int_type __ios_flags::_S_internal;
48   const __ios_flags::__int_type __ios_flags::_S_left;
49   const __ios_flags::__int_type __ios_flags::_S_oct;
50   const __ios_flags::__int_type __ios_flags::_S_right;
51   const __ios_flags::__int_type __ios_flags::_S_scientific;
52   const __ios_flags::__int_type __ios_flags::_S_showbase;
53   const __ios_flags::__int_type __ios_flags::_S_showpoint;
54   const __ios_flags::__int_type __ios_flags::_S_showpos;
55   const __ios_flags::__int_type __ios_flags::_S_skipws;
56   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
57   const __ios_flags::__int_type __ios_flags::_S_uppercase;
58   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
59   const __ios_flags::__int_type __ios_flags::_S_basefield;
60   const __ios_flags::__int_type __ios_flags::_S_floatfield;
61
62   const __ios_flags::__int_type __ios_flags::_S_badbit;
63   const __ios_flags::__int_type __ios_flags::_S_eofbit;
64   const __ios_flags::__int_type __ios_flags::_S_failbit;
65
66   const __ios_flags::__int_type __ios_flags::_S_app;
67   const __ios_flags::__int_type __ios_flags::_S_ate;
68   const __ios_flags::__int_type __ios_flags::_S_bin;
69   const __ios_flags::__int_type __ios_flags::_S_in;
70   const __ios_flags::__int_type __ios_flags::_S_out;
71   const __ios_flags::__int_type __ios_flags::_S_trunc;
72
73   // Definitions for static const members of ios_base.
74   const ios_base::fmtflags ios_base::boolalpha;
75   const ios_base::fmtflags ios_base::dec;
76   const ios_base::fmtflags ios_base::fixed;
77   const ios_base::fmtflags ios_base::hex;
78   const ios_base::fmtflags ios_base::internal;
79   const ios_base::fmtflags ios_base::left;
80   const ios_base::fmtflags ios_base::oct;
81   const ios_base::fmtflags ios_base::right;
82   const ios_base::fmtflags ios_base::scientific;
83   const ios_base::fmtflags ios_base::showbase;
84   const ios_base::fmtflags ios_base::showpoint;
85   const ios_base::fmtflags ios_base::showpos;
86   const ios_base::fmtflags ios_base::skipws;
87   const ios_base::fmtflags ios_base::unitbuf;
88   const ios_base::fmtflags ios_base::uppercase;
89   const ios_base::fmtflags ios_base::adjustfield;
90   const ios_base::fmtflags ios_base::basefield;
91   const ios_base::fmtflags ios_base::floatfield;
92
93   const ios_base::iostate ios_base::badbit;
94   const ios_base::iostate ios_base::eofbit;
95   const ios_base::iostate ios_base::failbit;
96   const ios_base::iostate ios_base::goodbit;
97
98   const ios_base::openmode ios_base::app;
99   const ios_base::openmode ios_base::ate;
100   const ios_base::openmode ios_base::binary;
101   const ios_base::openmode ios_base::in;
102   const ios_base::openmode ios_base::out;
103   const ios_base::openmode ios_base::trunc;
104
105   const ios_base::seekdir ios_base::beg;
106   const ios_base::seekdir ios_base::cur;
107   const ios_base::seekdir ios_base::end;
108
109   const int ios_base::_S_local_word_size;
110
111   _Atomic_word ios_base::Init::_S_refcount;
112
113   bool ios_base::Init::_S_synced_with_stdio = true;
114
115   ios_base::ios_base() 
116   : _M_precision(), _M_width(), _M_flags(), _M_exception(), 
117   _M_streambuf_state(), _M_callbacks(0), _M_word_zero(), 
118   _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale()
119   {
120     // Do nothing: basic_ios::init() does it.  
121     // NB: _M_callbacks and _M_word must be zero for non-initialized
122     // ios_base to go through ~ios_base gracefully.
123   }
124   
125   // 27.4.2.7  ios_base constructors/destructors
126   ios_base::~ios_base()
127   {
128     _M_call_callbacks(erase_event);
129     _M_dispose_callbacks();
130     if (_M_word != _M_local_word) 
131       {
132         delete [] _M_word;
133         _M_word = 0;
134       }
135   }
136
137   // 27.4.2.5  ios_base storage functions
138   int 
139   ios_base::xalloc() throw()
140   {
141     // Implementation note: Initialize top to zero to ensure that
142     // initialization occurs before main() is started.
143     static _Atomic_word _S_top = 0; 
144     return __gnu_cxx::__exchange_and_add(&_S_top, 1) + 4;
145   }
146
147   void 
148   ios_base::register_callback(event_callback __fn, int __index)
149   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
150
151   // 27.4.2.5  iword/pword storage
152   ios_base::_Words&
153   ios_base::_M_grow_words(int __ix, bool __iword)
154   {
155     // Precondition: _M_word_size <= __ix
156     int __newsize = _S_local_word_size;
157     _Words* __words = _M_local_word;
158     if (__ix > _S_local_word_size - 1)
159       {
160         if (__ix < numeric_limits<int>::max())
161           {
162             __newsize = __ix + 1;
163             try
164               { __words = new _Words[__newsize]; }
165             catch (...)
166               {
167                 _M_streambuf_state |= badbit;
168                 if (_M_streambuf_state & _M_exception)
169                   __throw_ios_failure(__N("ios_base::_M_grow_words "
170                                       "allocation failed"));
171                 if (__iword)
172                   _M_word_zero._M_iword = 0;
173                 else
174                   _M_word_zero._M_pword = 0;
175                 return _M_word_zero;
176               }
177             for (int __i = 0; __i < _M_word_size; __i++) 
178               __words[__i] = _M_word[__i];
179             if (_M_word && _M_word != _M_local_word) 
180               {
181                 delete [] _M_word;
182                 _M_word = 0;
183               }
184           }
185         else
186           {
187             _M_streambuf_state |= badbit;
188             if (_M_streambuf_state & _M_exception)
189               __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
190             if (__iword)
191               _M_word_zero._M_iword = 0;
192             else
193               _M_word_zero._M_pword = 0;
194             return _M_word_zero;
195           }
196       }
197     _M_word = __words;
198     _M_word_size = __newsize;
199     return _M_word[__ix];
200   }
201
202   void 
203   ios_base::_M_call_callbacks(event __e) throw()
204   {
205     _Callback_list* __p = _M_callbacks;
206     while (__p)
207       {
208         try 
209           { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
210         catch (...) 
211           { }
212         __p = __p->_M_next;
213       }
214   }
215
216   void 
217   ios_base::_M_dispose_callbacks(void)
218   {
219     _Callback_list* __p = _M_callbacks;
220     while (__p && __p->_M_remove_reference() == 0)
221       {
222         _Callback_list* __next = __p->_M_next;
223         delete __p;
224         __p = __next;
225       }
226     _M_callbacks = 0;
227   }
228 } // namespace std