diff -r e1b950c65cb4 -r 837f303aceeb epoc32/include/stdapis/stlportv5/stl/_ostream.c --- a/epoc32/include/stdapis/stlportv5/stl/_ostream.c Wed Mar 31 12:27:01 2010 +0100 +++ b/epoc32/include/stdapis/stlportv5/stl/_ostream.c Wed Mar 31 12:33:34 2010 +0100 @@ -1,75 +1,52 @@ /* - * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. - * * Copyright (c) 1999 * Silicon Graphics Computer Systems, Inc. * - * Copyright (c) 1999 + * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * - * Permission to use or copy this software for any purpose is hereby granted + * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * - */ + */ #ifndef _STLP_OSTREAM_C #define _STLP_OSTREAM_C - #ifndef _STLP_INTERNAL_OSTREAM_H -# include +# include #endif -#if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) - #if !defined (_STLP_INTERNAL_NUM_PUT_H) -# include // For basic_streambuf and iterators +# include // For basic_streambuf and iterators #endif _STLP_BEGIN_NAMESPACE -// Helper functions for istream<>::sentry constructor. -template -bool -_M_init(basic_ostream<_CharT, _Traits>& __str) { - if (__str.good()) { - // boris : check if this is needed ! - if (!__str.rdbuf()) - __str.setstate(ios_base::badbit); - if (__str.tie()) - __str.tie()->flush(); - return __str.good(); - } else - return false; -} - //---------------------------------------------------------------------- // Definitions of non-inline member functions. // Constructor, destructor template -_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits> - ::basic_ostream(basic_streambuf<_CharT, _Traits>* __buf) - : basic_ios<_CharT, _Traits>() -{ +basic_ostream<_CharT, _Traits>::basic_ostream(basic_streambuf<_CharT, _Traits>* __buf) + : basic_ios<_CharT, _Traits>() { this->init(__buf); } template -_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>::~basic_ostream() +basic_ostream<_CharT, _Traits>::~basic_ostream() {} // Output directly from a streambuf. template -_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>& -basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<_CharT, _Traits>* __from) -{ +basic_ostream<_CharT, _Traits>& +basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<_CharT, _Traits>* __from) { sentry __sentry(*this); if (__sentry) { if (__from) { @@ -94,8 +71,7 @@ template bool basic_ostream<_CharT, _Traits> ::_M_copy_buffered(basic_streambuf<_CharT, _Traits>* __from, - basic_streambuf<_CharT, _Traits>* __to) -{ + basic_streambuf<_CharT, _Traits>* __to) { bool __any_inserted = false; while (__from->egptr() != __from->gptr()) { @@ -123,10 +99,8 @@ return false; } } - else if (__nwritten != 0) return true; - else return __any_inserted; } @@ -136,107 +110,87 @@ return __any_inserted || this->_M_copy_unbuffered(__from, __to); } +/* + * Helper struct (guard) to put back a character in a streambuf + * whenever an exception or an eof occur. + */ +template +struct _SPutBackC { + typedef basic_streambuf<_CharT, _Traits> _StreamBuf; + typedef typename _StreamBuf::int_type int_type; + _SPutBackC(_StreamBuf *pfrom) + : __pfrom(pfrom), __c(0), __do_guard(false) {} + ~_SPutBackC() { + if (__do_guard) { + __pfrom->sputbackc(_Traits::to_char_type(__c)); + } + } + + void guard(int_type c) { + __c = c; + __do_guard = true; + } + void release() { + __do_guard = false; + } + +private: + _StreamBuf *__pfrom; + int_type __c; + bool __do_guard; +}; + template bool basic_ostream<_CharT, _Traits> ::_M_copy_unbuffered(basic_streambuf<_CharT, _Traits>* __from, - basic_streambuf<_CharT, _Traits>* __to) -{ + basic_streambuf<_CharT, _Traits>* __to) { + typedef _SPutBackC<_CharT, _Traits> _SPutBackCGuard; bool __any_inserted = false; + int_type __c; -#ifdef __SYMBIAN32__ - int_type __c; - _STLP_TRY { - __c = __from->sgetc();; - } - _STLP_CATCH_ALL { - this->_M_handle_exception(ios_base::failbit); - return __any_inserted; - } - for(;;){ - - if (this->_S_eof(__c)) - return __any_inserted; - - else { - int_type __tmp; + _STLP_TRY { + _SPutBackCGuard __cguard(__from); + for (;;) { _STLP_TRY { - __tmp = __to->sputc(__c); + __c = __from->sbumpc(); } _STLP_CATCH_ALL { - this->_M_handle_exception(ios_base::badbit); + this->_M_handle_exception(ios_base::failbit); return __any_inserted; } - if (this->_S_eof(__tmp)) { - break; - } - else - __any_inserted = true; - } - _STLP_TRY { - __c = __from->snextc(); - } - _STLP_CATCH_ALL { - this->_M_handle_exception(ios_base::failbit); - return __any_inserted; - } - } -#else - while (true) { - int_type __c; - _STLP_TRY { - __c = __from->sbumpc(); - } - _STLP_CATCH_ALL { - this->_M_handle_exception(ios_base::failbit); - return __any_inserted; - } + if ( this->_S_eof(__c) ) + return __any_inserted; - if (this->_S_eof(__c)) - return __any_inserted; - - else { - int_type __tmp; - _STLP_TRY { - __tmp = __to->sputc(__c); - } - _STLP_CATCH_ALL { - this->_M_handle_exception(ios_base::badbit); + __cguard.guard(__c); + if ( this->_S_eof( __to->sputc(_Traits::to_char_type(__c)) ) ) { return __any_inserted; } - if (this->_S_eof(__tmp)) { - _STLP_TRY { - /* __tmp = */ __from->sputbackc(__c); - } - _STLP_CATCH_ALL { - this->_M_handle_exception(ios_base::badbit); - return __any_inserted; - } - } - else - __any_inserted = true; + __cguard.release(); + __any_inserted = true; } } -#endif - return __any_inserted; + _STLP_CATCH_ALL { + this->_M_handle_exception(ios_base::badbit); + return __any_inserted; + } } +_STLP_MOVE_TO_PRIV_NAMESPACE + // Helper function for numeric output. - template basic_ostream<_CharT, _Traits>& _STLP_CALL -_M_put_num(basic_ostream<_CharT, _Traits>& __os, _Number __x) -{ +__put_num(basic_ostream<_CharT, _Traits>& __os, _Number __x) { typedef typename basic_ostream<_CharT, _Traits>::sentry _Sentry; _Sentry __sentry(__os); bool __failed = true; if (__sentry) { _STLP_TRY { - typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> > _NumPut; - __failed = (use_facet<_NumPut>(__os.getloc())).put( - ostreambuf_iterator<_CharT, _Traits>(__os.rdbuf()), + typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> > _NumPut; + __failed = (use_facet<_NumPut>(__os.getloc())).put(ostreambuf_iterator<_CharT, _Traits>(__os.rdbuf()), __os, __os.fill(), __x).failed(); } @@ -245,26 +199,96 @@ } } if (__failed) - __os.setstate(ios_base::badbit); + __os.setstate(ios_base::badbit); return __os; } -# if defined (_STLP_USE_TEMPLATE_EXPORT) && defined (__BUILDING_STLPORT) -_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream >& _STLP_CALL -_M_put_num(basic_ostream >&, unsigned long); -_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream >& _STLP_CALL -_M_put_num(basic_ostream >&, long); -# if defined (_STLP_LONG_LONG) -_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream >& _STLP_CALL -_M_put_num(basic_ostream >&, unsigned _STLP_LONG_LONG); -_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream >& _STLP_CALL -_M_put_num(basic_ostream >&, _STLP_LONG_LONG ); -# endif -# endif +_STLP_MOVE_TO_STD_NAMESPACE + +/* + * In the following operators we try to limit code bloat by limiting the + * number of __put_num instanciations. + */ +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __x) { + _STLP_STATIC_ASSERT( sizeof(short) <= sizeof(long) ) + long __tmp = ((this->flags() & _Basic_ios::basefield) != ios_base::dec) ? + __STATIC_CAST(long, __STATIC_CAST(unsigned short, __x)): __x; + return _STLP_PRIV __put_num(*this, __tmp); +} template -void basic_ostream<_CharT, _Traits>::_M_put_char(_CharT __c) -{ +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __x) { + _STLP_STATIC_ASSERT( sizeof(unsigned short) <= sizeof(unsigned long) ) + return _STLP_PRIV __put_num(*this, __STATIC_CAST(unsigned long,__x)); +} + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __x) { + _STLP_STATIC_ASSERT( sizeof(int) <= sizeof(long) ) + long __tmp = ((this->flags() & _Basic_ios::basefield) != ios_base::dec) ? + __STATIC_CAST(long, __STATIC_CAST(unsigned int, __x)): __x; + return _STLP_PRIV __put_num(*this, __tmp); +} + +template +#if defined (_WIN64) || !defined (_STLP_MSVC) || (_STLP_MSVC < 1300) +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __x) { + _STLP_STATIC_ASSERT( sizeof(unsigned int) <= sizeof(unsigned long) ) +#else +/* We define this operator with size_t rather than unsigned int to avoid + * 64 bits warning. + */ +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(size_t __x) { + _STLP_STATIC_ASSERT( sizeof(size_t) <= sizeof(unsigned long) ) +#endif + return _STLP_PRIV __put_num(*this, __STATIC_CAST(unsigned long,__x)); +} + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __x) +{ return _STLP_PRIV __put_num(*this, __x); } + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __x) +{ return _STLP_PRIV __put_num(*this, __x); } + +#ifdef _STLP_LONG_LONG +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<< (_STLP_LONG_LONG __x) +{ return _STLP_PRIV __put_num(*this, __x); } + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<< (unsigned _STLP_LONG_LONG __x) +{ return _STLP_PRIV __put_num(*this, __x); } +#endif + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __x) +{ return _STLP_PRIV __put_num(*this, __STATIC_CAST(double,__x)); } + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __x) +{ return _STLP_PRIV __put_num(*this, __x); } + +#ifndef _STLP_NO_LONG_DOUBLE +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __x) +{ return _STLP_PRIV __put_num(*this, __x); } +#endif + +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __x) +{ return _STLP_PRIV __put_num(*this, __x); } + +#ifndef _STLP_NO_BOOL +template +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __x) +{ return _STLP_PRIV __put_num(*this, __x); } +#endif + +template +void basic_ostream<_CharT, _Traits>::_M_put_char(_CharT __c) { sentry __sentry(*this); if (__sentry) { bool __failed = true; @@ -275,7 +299,7 @@ __failed = this->_S_eof(this->rdbuf()->sputc(__c)); else if ((this->flags() & ios_base::adjustfield) == ios_base::left) { __failed = this->_S_eof(this->rdbuf()->sputc(__c)); - __failed = __failed || + __failed = __failed || this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad; } else { @@ -295,8 +319,7 @@ } template -void basic_ostream<_CharT, _Traits>::_M_put_nowiden(const _CharT* __s) -{ +void basic_ostream<_CharT, _Traits>::_M_put_nowiden(const _CharT* __s) { sentry __sentry(*this); if (__sentry) { bool __failed = true; @@ -308,7 +331,7 @@ __failed = this->rdbuf()->sputn(__s, __n) != __n; else if ((this->flags() & ios_base::adjustfield) == ios_base::left) { __failed = this->rdbuf()->sputn(__s, __n) != __n; - __failed = __failed || + __failed = __failed || this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad; } else { @@ -328,8 +351,7 @@ } template -void basic_ostream<_CharT, _Traits>::_M_put_widen(const char* __s) -{ +void basic_ostream<_CharT, _Traits>::_M_put_widen(const char* __s) { sentry __sentry(*this); if (__sentry) { bool __failed = true; @@ -341,7 +363,7 @@ __failed = !this->_M_put_widen_aux(__s, __n); else if ((this->flags() & ios_base::adjustfield) == ios_base::left) { __failed = !this->_M_put_widen_aux(__s, __n); - __failed = __failed || + __failed = __failed || this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad; } else { @@ -362,8 +384,7 @@ template bool basic_ostream<_CharT, _Traits>::_M_put_widen_aux(const char* __s, - streamsize __n) -{ + streamsize __n) { basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf(); for ( ; __n > 0 ; --__n) @@ -374,9 +395,8 @@ // Unformatted output of a single character. template -_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>& -basic_ostream<_CharT, _Traits>::put(char_type __c) -{ +basic_ostream<_CharT, _Traits>& +basic_ostream<_CharT, _Traits>::put(char_type __c) { sentry __sentry(*this); bool __failed = true; @@ -397,9 +417,8 @@ // Unformatted output of a single character. template -_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>& -basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) -{ +basic_ostream<_CharT, _Traits>& +basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) { sentry __sentry(*this); bool __failed = true; @@ -420,6 +439,8 @@ _STLP_END_NAMESPACE -#endif /* defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) */ +#endif /* _STLP_OSTREAM_C */ -#endif /* _STLP_OSTREAM_C */ +// Local Variables: +// mode:C++ +// End: