OSDN Git Service

2004-01-27 Jerry Quinn <jlquinn@optonline.net>
[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
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   // Definitions for static const data members of __ios_flags.
42   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
43   const __ios_flags::__int_type __ios_flags::_S_dec;
44   const __ios_flags::__int_type __ios_flags::_S_fixed;
45   const __ios_flags::__int_type __ios_flags::_S_hex;
46   const __ios_flags::__int_type __ios_flags::_S_internal;
47   const __ios_flags::__int_type __ios_flags::_S_left;
48   const __ios_flags::__int_type __ios_flags::_S_oct;
49   const __ios_flags::__int_type __ios_flags::_S_right;
50   const __ios_flags::__int_type __ios_flags::_S_scientific;
51   const __ios_flags::__int_type __ios_flags::_S_showbase;
52   const __ios_flags::__int_type __ios_flags::_S_showpoint;
53   const __ios_flags::__int_type __ios_flags::_S_showpos;
54   const __ios_flags::__int_type __ios_flags::_S_skipws;
55   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
56   const __ios_flags::__int_type __ios_flags::_S_uppercase;
57   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
58   const __ios_flags::__int_type __ios_flags::_S_basefield;
59   const __ios_flags::__int_type __ios_flags::_S_floatfield;
60
61   const __ios_flags::__int_type __ios_flags::_S_badbit;
62   const __ios_flags::__int_type __ios_flags::_S_eofbit;
63   const __ios_flags::__int_type __ios_flags::_S_failbit;
64
65   const __ios_flags::__int_type __ios_flags::_S_app;
66   const __ios_flags::__int_type __ios_flags::_S_ate;
67   const __ios_flags::__int_type __ios_flags::_S_bin;
68   const __ios_flags::__int_type __ios_flags::_S_in;
69   const __ios_flags::__int_type __ios_flags::_S_out;
70   const __ios_flags::__int_type __ios_flags::_S_trunc;
71
72   // Definitions for static const members of ios_base.
73   const ios_base::fmtflags ios_base::boolalpha;
74   const ios_base::fmtflags ios_base::dec;
75   const ios_base::fmtflags ios_base::fixed;
76   const ios_base::fmtflags ios_base::hex;
77   const ios_base::fmtflags ios_base::internal;
78   const ios_base::fmtflags ios_base::left;
79   const ios_base::fmtflags ios_base::oct;
80   const ios_base::fmtflags ios_base::right;
81   const ios_base::fmtflags ios_base::scientific;
82   const ios_base::fmtflags ios_base::showbase;
83   const ios_base::fmtflags ios_base::showpoint;
84   const ios_base::fmtflags ios_base::showpos;
85   const ios_base::fmtflags ios_base::skipws;
86   const ios_base::fmtflags ios_base::unitbuf;
87   const ios_base::fmtflags ios_base::uppercase;
88   const ios_base::fmtflags ios_base::adjustfield;
89   const ios_base::fmtflags ios_base::basefield;
90   const ios_base::fmtflags ios_base::floatfield;
91
92   const ios_base::iostate ios_base::badbit;
93   const ios_base::iostate ios_base::eofbit;
94   const ios_base::iostate ios_base::failbit;
95   const ios_base::iostate ios_base::goodbit;
96
97   const ios_base::openmode ios_base::app;
98   const ios_base::openmode ios_base::ate;
99   const ios_base::openmode ios_base::binary;
100   const ios_base::openmode ios_base::in;
101   const ios_base::openmode ios_base::out;
102   const ios_base::openmode ios_base::trunc;
103
104   const ios_base::seekdir ios_base::beg;
105   const ios_base::seekdir ios_base::cur;
106   const ios_base::seekdir ios_base::end;
107
108   const int ios_base::_S_local_word_size;
109
110   _Atomic_word ios_base::Init::_S_refcount;
111
112   bool ios_base::Init::_S_synced_with_stdio = true;
113
114   ios_base::ios_base() 
115   : _M_callbacks(0), _M_word_size(_S_local_word_size), _M_word(_M_local_word)
116   {
117     // Do nothing: basic_ios::init() does it.  
118     // NB: _M_callbacks and _M_word must be zero for non-initialized
119     // ios_base to go through ~ios_base gracefully.
120   }
121   
122   // 27.4.2.7  ios_base constructors/destructors
123   ios_base::~ios_base()
124   {
125     _M_call_callbacks(erase_event);
126     _M_dispose_callbacks();
127     if (_M_word != _M_local_word) 
128       {
129         delete [] _M_word;
130         _M_word = 0;
131       }
132   }
133
134   // 27.4.2.5  ios_base storage functions
135   int 
136   ios_base::xalloc() throw()
137   {
138     // Implementation note: Initialize top to zero to ensure that
139     // initialization occurs before main() is started.
140     static _Atomic_word _S_top = 0; 
141     return __exchange_and_add(&_S_top, 1) + 4;
142   }
143
144   void 
145   ios_base::register_callback(event_callback __fn, int __index)
146   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
147
148   // 27.4.2.5  iword/pword storage
149   ios_base::_Words&
150   ios_base::_M_grow_words(int ix, bool iword)
151   {
152     // Precondition: _M_word_size <= ix
153     int newsize = _S_local_word_size;
154     _Words* words = _M_local_word;
155     if (ix > _S_local_word_size - 1)
156       {
157         if (ix < numeric_limits<int>::max())
158           {
159             newsize = ix + 1;
160             try
161               { words = new _Words[newsize]; }
162             catch (...)
163               {
164                 _M_streambuf_state |= badbit;
165                 if (_M_streambuf_state & _M_exception)
166                   __throw_ios_failure(__N("ios_base::_M_grow_words "
167                                       "allocation failed"));
168                 if (iword)
169                   _M_word_zero._M_iword = 0;
170                 else
171                   _M_word_zero._M_pword = 0;
172                 return _M_word_zero;
173               }
174             for (int i = 0; i < _M_word_size; i++) 
175               words[i] = _M_word[i];
176             if (_M_word && _M_word != _M_local_word) 
177               {
178                 delete [] _M_word;
179                 _M_word = 0;
180               }
181           }
182         else
183           {
184             _M_streambuf_state |= badbit;
185             if (_M_streambuf_state & _M_exception)
186               __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
187             if (iword)
188               _M_word_zero._M_iword = 0;
189             else
190               _M_word_zero._M_pword = 0;
191             return _M_word_zero;
192           }
193       }
194     _M_word = words;
195     _M_word_size = newsize;
196     return _M_word[ix];
197   }
198
199   void 
200   ios_base::_M_call_callbacks(event __e) throw()
201   {
202     _Callback_list* __p = _M_callbacks;
203     while (__p)
204       {
205         try 
206           { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
207         catch (...) 
208           { }
209         __p = __p->_M_next;
210       }
211   }
212
213   void 
214   ios_base::_M_dispose_callbacks(void)
215   {
216     _Callback_list* __p = _M_callbacks;
217     while (__p && __p->_M_remove_reference() == 0)
218       {
219         _Callback_list* __next = __p->_M_next;
220         delete __p;
221         __p = __next;
222       }
223     _M_callbacks = 0;
224   }
225 } // namespace std