OSDN Git Service

2004-01-27 Jerry Quinn <jlquinn@optonline.net>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios.cc
index db69478..6f987c2 100644 (file)
@@ -1,6 +1,7 @@
 // Iostreams base classes -*- C++ -*-
 
-// Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
+// Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 // ISO C++ 14882: 27.4  Iostreams base classes
 //
 
-#include <bits/std_ios.h>
-#include <bits/std_ostream.h>
-#include <bits/std_istream.h>
-#include <bits/std_fstream.h>
+#include <ios>
+#include <limits>
+#include <bits/atomicity.h>
 
 namespace std 
 {
@@ -105,196 +105,108 @@ namespace std
   const ios_base::seekdir ios_base::cur;
   const ios_base::seekdir ios_base::end;
 
-  const int ios_base::_S_local_words;
-  int ios_base::Init::_S_ios_base_init = 0;
-  bool ios_base::Init::_S_synced_with_stdio = true;
-
-  istream cin(NULL);
-  ostream cout(NULL);
-  ostream cerr(NULL);
-  ostream clog(NULL);
-#ifdef _GLIBCPP_USE_WCHAR_T
-  wistream wcin(NULL);
-  wostream wcout(NULL);
-  wostream wcerr(NULL);
-  wostream wclog(NULL);
-#endif
-
-  ios_base::failure::failure(const string& __str)
-  {
-    strncpy(_M_name, __str.c_str(), _M_bufsize);
-    _M_name[_M_bufsize - 1] = '\0';
-  }
+  const int ios_base::_S_local_word_size;
 
-  ios_base::failure::~failure()
-  { }
+  _Atomic_word ios_base::Init::_S_refcount;
 
-  const char*
-  ios_base::failure::what() const throw()
-  { return _M_name; }
+  bool ios_base::Init::_S_synced_with_stdio = true;
 
-  ios_base::Init::Init()
+  ios_base::ios_base() 
+  : _M_callbacks(0), _M_word_size(_S_local_word_size), _M_word(_M_local_word)
   {
-    if (++_S_ios_base_init == 1)
-      {
-       // NB: std_iostream.h creates the four standard files with
-       // NULL buffers. At this point, we swap out these placeholder
-       // objects for the properly-constructed ones
-               _M_cout = new filebuf(1, "stdout", ios_base::out);
-       _M_cin = new filebuf(0, "stdin", ios_base::in);
-       _M_cerr = new filebuf(2, "stderr", ios_base::out);
-       new (&cout) ostream(_M_cout);
-       new (&cin) istream(_M_cin);
-       new (&cerr) ostream(_M_cerr);
-       new (&clog) ostream(_M_cerr);
-       cin.tie(&cout);
-       cerr.flags(ios_base::unitbuf);
-
-#ifdef _GLIBCPP_USE_WCHAR_T
-       _M_wcout = new wfilebuf(1, "stdout", ios_base::out);
-       _M_wcin = new wfilebuf(0, "stdin", ios_base::in);
-       _M_wcerr = new wfilebuf(2, "stderr", ios_base::out);
-       new (&wcout) wostream(_M_wcout);
-       new (&wcin) wistream(_M_wcin);
-       new (&wcerr) wostream(_M_wcerr);
-       new (&wclog) wostream(_M_wcerr);
-       wcin.tie(&wcout);
-       wcerr.flags(ios_base::unitbuf);
-#endif
-       ios_base::Init::_S_synced_with_stdio = true;
-      }
+    // Do nothing: basic_ios::init() does it.  
+    // NB: _M_callbacks and _M_word must be zero for non-initialized
+    // ios_base to go through ~ios_base gracefully.
   }
-
-  ios_base::Init::~Init()
+  
+  // 27.4.2.7  ios_base constructors/destructors
+  ios_base::~ios_base()
   {
-    if (--_S_ios_base_init == 0)
+    _M_call_callbacks(erase_event);
+    _M_dispose_callbacks();
+    if (_M_word != _M_local_word) 
       {
-       cout.flush();
-       cerr.flush();
-       clog.flush();
-       delete _M_cout;
-       delete _M_cin;
-       delete _M_cerr;
-       _M_cout = NULL;
-       _M_cin = NULL;
-       _M_cerr = NULL;
-#ifdef _GLIBCPP_USE_WCHAR_T
-       wcout.flush();
-       wcerr.flush();
-       wclog.flush();
-       delete _M_wcout;
-       delete _M_wcin;
-       delete _M_wcerr;
-       _M_wcout = NULL;
-       _M_wcin = NULL;
-       _M_wcerr = NULL;
-#endif
+       delete [] _M_word;
+       _M_word = 0;
       }
-  } 
+  }
 
   // 27.4.2.5  ios_base storage functions
   int 
   ios_base::xalloc() throw()
   {
-    // XXX MT
-    // XXX should be a symbol. (Reserve 0..3 for builtins.)
-    static int top = 4
-    return top++;
+    // Implementation note: Initialize top to zero to ensure that
+    // initialization occurs before main() is started.
+    static _Atomic_word _S_top = 0
+    return __exchange_and_add(&_S_top, 1) + 4;
   }
 
+  void 
+  ios_base::register_callback(event_callback __fn, int __index)
+  { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
+
   // 27.4.2.5  iword/pword storage
   ios_base::_Words&
-  ios_base::_M_grow_words(int ix)
+  ios_base::_M_grow_words(int ix, bool iword)
   {
-    // Precondition: _M_word_limit <= ix
-    _Words zero = { 0, 0 };
-    int newlimit = _S_local_words;
-    _Words* words = _M_word_array;
-    int i = 0;
-    if (_S_local_words <= ix)
+    // Precondition: _M_word_size <= ix
+    int newsize = _S_local_word_size;
+    _Words* words = _M_local_word;
+    if (ix > _S_local_word_size - 1)
       {
-       newlimit = ix+1;
-       try
-         { words = new _Words[ix+1]; }
-       catch (...)
+       if (ix < numeric_limits<int>::max())
          {
-           _M_dummy = zero;  // XXX MT? Not on "normal" machines.
-           // XXX now in basic_ios
-           // _M_clear(_M_rdstate() | badbit);  // may throw
-           return _M_dummy;
+           newsize = ix + 1;
+           try
+             { words = new _Words[newsize]; }
+           catch (...)
+             {
+               _M_streambuf_state |= badbit;
+               if (_M_streambuf_state & _M_exception)
+                 __throw_ios_failure(__N("ios_base::_M_grow_words "
+                                     "allocation failed"));
+               if (iword)
+                 _M_word_zero._M_iword = 0;
+               else
+                 _M_word_zero._M_pword = 0;
+               return _M_word_zero;
+             }
+           for (int i = 0; i < _M_word_size; i++) 
+             words[i] = _M_word[i];
+           if (_M_word && _M_word != _M_local_word) 
+             {
+               delete [] _M_word;
+               _M_word = 0;
+             }
+         }
+       else
+         {
+           _M_streambuf_state |= badbit;
+           if (_M_streambuf_state & _M_exception)
+             __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
+           if (iword)
+             _M_word_zero._M_iword = 0;
+           else
+             _M_word_zero._M_pword = 0;
+           return _M_word_zero;
          }
-       for (; i < _M_word_limit; i++) 
-         words[i] = _M_words[i];
-       if (_M_words != _M_word_array) 
-         delete [] _M_words;
       }
-    
-    do { words[i] = zero; } while (++i < newlimit);
-    _M_words = words;
-    _M_word_limit = newlimit;
-    return words[ix];
-  }
-  
-  // Called only by basic_ios<>::init.
-  void 
-  ios_base::_M_init()   
-  {
-    // NB: May be called more than once
-    _M_precision = 6;
-    _M_width = 0;
-    _M_flags = skipws | dec;
-    _M_callbacks = 0;
-    _M_words = 0;
-    _M_word_limit = 0;
-    _M_ios_locale = locale();
-    // No init needed for _M_word_array or _M_dummy.
-  }  
-  
-  // 27.4.2.3  ios_base locale functions
-  locale
-  ios_base::imbue(const locale& __loc)
-  {
-    locale __old = _M_ios_locale;
-    _M_ios_locale = __loc;
-    // Make sure there's a callback for the format caches so they will be
-    // marked dirty.
-    _Format_cache<char>::_S_get(*this);
-#ifdef _GLIBCPP_USE_WCHAR_T
-    _Format_cache<wchar_t>::_S_get(*this);
-#endif
-    _M_call_callbacks(imbue_event);
-    return __old;
-  }
-
-  ios_base::ios_base()
-  {
-    // Do nothing; init() does it.  Static init to 0 makes everything sane.
-  }
-  
-  // 27.4.2.7  ios_base constructors/destructors
-  ios_base::~ios_base()
-  {
-    _M_call_callbacks(erase_event);
-    _M_dispose_callbacks();
-    if (_M_words != _M_word_array) 
-      delete [] _M_words;
-    // XXX done?
+    _M_word = words;
+    _M_word_size = newsize;
+    return _M_word[ix];
   }
 
   void 
-  ios_base::register_callback(event_callback __fn, int __index)
-  { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
-
-  void 
   ios_base::_M_call_callbacks(event __e) throw()
   {
-    for (_Callback_list* __p = _M_callbacks; __p; __p = __p->_M_next)
+    _Callback_list* __p = _M_callbacks;
+    while (__p)
       {
-       try 
-         (*__p->_M_fn) (__e, *this, __p->_M_index); 
-       } 
-       catch (...) { 
-       }
+       try 
+         { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
+       catch (...) 
+         { }
+       __p = __p->_M_next;
       }
   }
 
@@ -310,55 +222,4 @@ namespace std
       }
     _M_callbacks = 0;
   }
-
-  bool 
-  ios_base::sync_with_stdio(bool __sync)
-  { 
-#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
-    // 49.  Underspecification of ios_base::sync_with_stdio
-    bool __ret = ios_base::Init::_S_synced_with_stdio;
-#endif
-
-    // Turn off sync with C FILE* for cin, cout, cerr, clog iff
-    // currently synchronized.
-    if (!__sync && __ret)
-      {
-#if 0
-       // no longer need to do this
-       // Need to dispose of the buffers created at initialization.
-       __ioinit._M_cout->~filebuf();
-       __ioinit._M_cin->~filebuf();
-       __ioinit._M_cerr->~filebuf();
-       __ioinit._M_cout = new filebuf();
-       __ioinit._M_cin = new filebuf();
-       __ioinit._M_cerr = new filebuf();
-       __ioinit._M_cout->open("stdout", ios_base::out);
-       __ioinit._M_cin->open("stdin", ios_base::in);
-       __ioinit._M_cerr->open("stderr", ios_base::out);
-       cout.rdbuf(__ioinit._M_cout);
-       cin.rdbuf(__ioinit._M_cin);
-       cerr.rdbuf(__ioinit._M_cerr);
-       cerr.flags(ios_base::unitbuf);
-       clog.rdbuf(__ioinit._M_cerr);
-#endif
-#ifdef _GLIBCPP_USE_WCHAR_T
-#endif
-       ios_base::Init::_S_synced_with_stdio = false;
-      }
-    
-    return __ret; 
-  }
-
-}  // namespace std
-
-
-
-
-
-
-
-
-
-
-
-
+} // namespace std