home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stl453up.zip / stl453fx / src / ios.cpp < prev    next >
C/C++ Source or Header  |  2002-04-29  |  10KB  |  316 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18.  
  19. # include "stlport_prefix.h"
  20. # include <algorithm>
  21. # include <stl/_ios.h>
  22.  
  23. _STLP_BEGIN_NAMESPACE
  24.  
  25. char* _STLP_CALL
  26. __write_integer(char* buf, ios_base::fmtflags flags, long x);
  27.  
  28. //----------------------------------------------------------------------
  29. // ios_base members
  30.  
  31. // class ios_base::failure, a subclass of exception.  It's used solely
  32. // for reporting errors.
  33.  
  34. ios_base::failure::failure(const string& s) 
  35.   : __Named_exception(s)
  36. {}
  37.  
  38. ios_base::failure::~failure() _STLP_NOTHROW_INHERENTLY {}
  39.  
  40. #if !defined (_STLP_STATIC_CONST_INIT_BUG)
  41.  
  42. // Definitions of ios_base's formatting flags.
  43. const ios_base::fmtflags ios_base::left;
  44. const ios_base::fmtflags ios_base::right;
  45. const ios_base::fmtflags ios_base::internal;
  46. const ios_base::fmtflags ios_base::dec;
  47. const ios_base::fmtflags ios_base::hex;
  48. const ios_base::fmtflags ios_base::oct;
  49. const ios_base::fmtflags ios_base::fixed;
  50. const ios_base::fmtflags ios_base::scientific;
  51. const ios_base::fmtflags ios_base::boolalpha;
  52. const ios_base::fmtflags ios_base::showbase;
  53. const ios_base::fmtflags ios_base::showpoint;
  54. const ios_base::fmtflags ios_base::showpos;
  55. const ios_base::fmtflags ios_base::skipws;
  56. const ios_base::fmtflags ios_base::unitbuf;
  57. const ios_base::fmtflags ios_base::uppercase;
  58. const ios_base::fmtflags ios_base::adjustfield;
  59. const ios_base::fmtflags ios_base::basefield;
  60. const ios_base::fmtflags ios_base::floatfield;
  61.  
  62. // Definitions of ios_base's state flags.
  63. const ios_base::iostate ios_base::goodbit;
  64. const ios_base::iostate ios_base::badbit;
  65. const ios_base::iostate ios_base::eofbit;
  66. const ios_base::iostate ios_base::failbit;
  67.  
  68. // Definitions of ios_base's openmode flags.
  69. const ios_base::openmode ios_base::app;
  70. const ios_base::openmode ios_base::ate;
  71. const ios_base::openmode ios_base::binary;
  72. const ios_base::openmode ios_base::in;
  73. const ios_base::openmode ios_base::out;
  74. const ios_base::openmode ios_base::trunc;
  75.  
  76. // Definitions of ios_base's seekdir flags.
  77. const ios_base::seekdir ios_base::beg;
  78. const ios_base::seekdir ios_base::cur;
  79. const ios_base::seekdir ios_base::end;
  80.  
  81. # endif /*  _STLP_STATIC_CONST_INIT_BUG */
  82.  
  83. // Internal functions used for managing exponentially-growing arrays of
  84. // POD types.
  85.  
  86. // array is a pointer to N elements of type PODType.  Expands the array,
  87. // if necessary, so that array[index] is meaningful.  All new elements are
  88. // initialized to zero.  Returns a pointer to the new array, and the new
  89. // size.
  90. template <class PODType>
  91. pair<PODType*, size_t> 
  92. _Stl_expand_array(PODType* array, size_t N, int index)
  93. {
  94.   if ((int)N < index + 1) {
  95.     size_t new_N = (max)(2 * N, size_t(index + 1));
  96.     PODType* new_array
  97.       = __STATIC_CAST(PODType*,realloc(array, new_N * sizeof(PODType)));
  98.     if (new_array) {
  99.       fill(new_array + N, new_array + new_N, PODType());
  100.       return pair<PODType*, size_t>(new_array, new_N);
  101.     }
  102.     else 
  103.       return pair<PODType*, size_t>(__STATIC_CAST(PODType*,0), 0);
  104.   }
  105.   else
  106.     return pair<PODType*, size_t>(array, N);
  107. }
  108.  
  109. // array is a pointer to N elements of type PODType.  Allocate a new
  110. // array of N elements, copying the values from the old array to the new.
  111. // Return a pointer to the new array.  It is assumed that array is non-null
  112. // and N is nonzero.
  113. template <class PODType>
  114. PODType* _Stl_copy_array(const PODType* array, size_t N) {
  115.   PODType* result = __STATIC_CAST(PODType*,malloc(N * sizeof(PODType)));
  116.   if (result)
  117.     copy(array, array + N, result);
  118.   return result;
  119. }
  120.  
  121. locale ios_base::imbue(const locale& loc) {
  122.     locale previous = _M_locale;
  123.     _M_locale = loc;
  124.     _M_invoke_callbacks(imbue_event);
  125.     return previous;
  126. }
  127.  
  128. int ios_base::_S_index = 0;
  129.  
  130. int _STLP_CALL ios_base::xalloc()
  131. {
  132.   static _STLP_STATIC_MUTEX L _STLP_MUTEX_INITIALIZER;
  133.   _STLP_auto_lock sentry(L);
  134.   return _S_index++;
  135. }
  136.  
  137. long& ios_base::iword(int index) {
  138.   static long dummy = 0;
  139.  
  140.   pair<long*, size_t> tmp = _Stl_expand_array(_M_iwords, _M_num_iwords, index);
  141.   if (tmp.first) {              // The allocation, if any, succeeded.
  142.     _M_iwords = tmp.first;
  143.     _M_num_iwords = tmp.second;
  144.     return _M_iwords[index];
  145.   }
  146.   else {
  147.     _M_setstate_nothrow(badbit);
  148.     _M_check_exception_mask();
  149.     return dummy;
  150.   }
  151. }
  152.  
  153.  
  154. void*& ios_base::pword(int index) {
  155.   static void* dummy = 0;
  156.  
  157.   pair<void**, size_t> tmp = _Stl_expand_array(_M_pwords, _M_num_pwords, index);
  158.   if (tmp.first) {              // The allocation, if any, succeeded.
  159.     _M_pwords = tmp.first;
  160.     _M_num_pwords = tmp.second;
  161.     return _M_pwords[index];
  162.   }
  163.   else {
  164.     _M_setstate_nothrow(badbit);
  165.     _M_check_exception_mask();
  166.     return dummy;
  167.   }
  168. }
  169.  
  170. void ios_base::register_callback(event_callback __fn, int index) {
  171.   pair<pair<event_callback, int>*, size_t> tmp
  172.     = _Stl_expand_array(_M_callbacks, _M_num_callbacks, (int)_M_callback_index /* fbp: index ??? */ );
  173.   if (tmp.first) {
  174.     _M_callbacks = tmp.first;
  175.     _M_num_callbacks = tmp.second;
  176.     _M_callbacks[_M_callback_index++] = make_pair(__fn, index);
  177.   }
  178.   else {
  179.     _M_setstate_nothrow(badbit);
  180.     _M_check_exception_mask();
  181.   }
  182. }
  183.  
  184. // Invokes all currently registered callbacks for a particular event.
  185. // Behaves correctly even if one of the callbacks adds a new callback.
  186. void ios_base::_M_invoke_callbacks(event E) {
  187.   for (size_t i = _M_callback_index; i > 0; --i) {
  188.     event_callback f = _M_callbacks[i-1].first;
  189.     int n = _M_callbacks[i-1].second;
  190.     f(E, *this, n);
  191.   }
  192. }
  193.  
  194. // This function is called if the state, rdstate(), has a bit set
  195. // that is also set in the exception mask exceptions().
  196. //TORLAB DEBUG original void ios_base::_M_throw_failure() {
  197. void _Export ios_base::_M_throw_failure() { //TORLABD DEBUG added _Export keyword
  198.   const char* arg ;
  199. # if 0
  200.   char buffer[256];
  201.   char* ptr;
  202.   strcpy(buffer, "ios failure: rdstate = 0x");
  203.   ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_iostate)); 
  204.   strcpy(ptr, " mask = 0x");
  205.   ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_exception_mask)); 
  206.   *ptr = 0;
  207.   arg = buffer;
  208. # else
  209.   arg = "ios failure";
  210. # endif
  211.  
  212. # ifndef _STLP_USE_EXCEPTIONS
  213.   fputs(arg, stderr);
  214. # else
  215.   throw failure(arg);
  216. # endif
  217. }
  218.  
  219. // Copy x's state to *this.  This member function is used in the 
  220. // implementation of basic_ios::copyfmt.  Does not copy _M_exception_mask
  221. // or _M_iostate.  
  222. void ios_base::_M_copy_state(const ios_base& x) {
  223.   _M_fmtflags  = x._M_fmtflags; // Copy the flags, except for _M_iostate
  224.   _M_openmode  = x._M_openmode; // and _M_exception_mask.
  225.   _M_seekdir   = x._M_seekdir;
  226.   _M_precision = x._M_precision;
  227.   _M_width     = x._M_width;
  228.   
  229.   if (_M_locale != x._M_locale) {
  230.     _M_locale = x._M_locale;
  231.     _M_cached_ctype = x._M_cached_ctype;
  232.     _M_cached_numpunct = x._M_cached_numpunct;
  233.   }
  234.  
  235.   if (x._M_callbacks) {
  236.     pair<event_callback, int>* tmp = _Stl_copy_array(x._M_callbacks, x._M_callback_index);
  237.     if (tmp) {
  238.       free(_M_callbacks);
  239.       _M_callbacks = tmp;
  240.       _M_num_callbacks = _M_callback_index = x._M_callback_index;
  241.     }
  242.     else {
  243.       _M_setstate_nothrow(badbit);
  244.       _M_check_exception_mask();
  245.     }
  246.   }
  247.  
  248.   if (x._M_iwords) {
  249.     long* tmp = _Stl_copy_array(x._M_iwords, x._M_num_iwords);
  250.     if (tmp) {
  251.       free(_M_iwords);
  252.       _M_iwords = tmp;
  253.       _M_num_iwords = x._M_num_iwords;
  254.     }
  255.     else {
  256.       _M_setstate_nothrow(badbit);
  257.       _M_check_exception_mask();
  258.     }
  259.   }
  260.  
  261.   if (x._M_pwords) {
  262.     void** tmp = _Stl_copy_array(x._M_pwords, x._M_num_pwords);
  263.     if (tmp) {
  264.       free(_M_pwords);
  265.       _M_pwords = tmp;
  266.       _M_num_pwords = x._M_num_pwords;
  267.     }
  268.     else {
  269.       _M_setstate_nothrow(badbit);
  270.       _M_check_exception_mask();
  271.     }
  272.   }
  273. }
  274.  
  275.  
  276. // ios's (protected) default constructor.  The standard says that all 
  277. // fields have indeterminate values; we initialize them to zero for
  278. // simplicity.  The only thing that really matters is that the arrays
  279. // are all initially null pointers, and the array element counts are all
  280. // initially zero.
  281. ios_base::ios_base()
  282.   : _M_fmtflags(0), _M_iostate(0), _M_openmode(0), _M_seekdir(0),
  283.     _M_exception_mask(0),
  284.     _M_precision(0), _M_width(0),
  285.     _M_locale(),
  286.     _M_callbacks(0), _M_num_callbacks(0), _M_callback_index(0),
  287.     _M_iwords(0), _M_num_iwords(0),
  288.     _M_pwords(0),
  289.     _M_num_pwords(0) , _M_cached_ctype(0), _M_cached_numpunct(0)
  290. { }
  291.  
  292. // ios's destructor.
  293. //TORLAB DEBUG original ios_base::~ios_base() {
  294. _Export ios_base::~ios_base() { //TORLAB DEBUG added _Export keyword
  295.   _M_invoke_callbacks(erase_event);
  296.   free(_M_callbacks);
  297.   free(_M_iwords);
  298.   free(_M_pwords);
  299. }
  300.  
  301. //----------------------------------------------------------------------
  302. // Force instantiation of basic_ios
  303. // For DLL exports, they are already instantiated.
  304. #  if !defined(_STLP_NO_FORCE_INSTANTIATE)
  305. template class _STLP_CLASS_DECLSPEC basic_ios<char, char_traits<char> >;
  306. #   ifndef _STLP_NO_WCHAR_T
  307. template class _STLP_CLASS_DECLSPEC basic_ios<wchar_t, char_traits<wchar_t> >;
  308. #   endif /* _STLP_NO_WCHAR_T */
  309. #  endif
  310.  
  311. _STLP_END_NAMESPACE
  312.  
  313. // Local Variables:
  314. // mode:C++
  315. // End:
  316.