diff -r e1b950c65cb4 -r 837f303aceeb epoc32/include/stdapis/stlportv5/stl/_string_io.c --- a/epoc32/include/stdapis/stlportv5/stl/_string_io.c Wed Mar 31 12:27:01 2010 +0100 +++ b/epoc32/include/stdapis/stlportv5/stl/_string_io.c Wed Mar 31 12:33:34 2010 +0100 @@ -1,98 +1,56 @@ -/* -* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. - -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* Neither the name of Nokia Corporation nor the names of its contributors -* may be used to endorse or promote products derived from this software -* without specific prior written permission. - -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -* -* Description: -* -*/ - #ifndef _STLP_STRING_IO_C #define _STLP_STRING_IO_C #ifndef _STLP_STRING_IO_H -# include +# include #endif #ifndef _STLP_INTERNAL_CTYPE_H -# include +# include #endif -# ifdef _STLP_DEBUG -# define basic_string _Nondebug_string -# endif - _STLP_BEGIN_NAMESPACE -# if defined (_STLP_OWN_IOSTREAMS) -# define _STLP_USING_IO -# else -# define _STLP_USING_IO _STLP_USING_VENDOR_STD -# endif - -#if defined (_STLP_USE_NEW_IOSTREAMS) - template bool _STLP_CALL __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os, - basic_streambuf<_CharT, _Traits>* __buf, - size_t __n) -{ + basic_streambuf<_CharT, _Traits>* __buf, + streamsize __n) { _CharT __f = __os.fill(); - size_t __i; - bool __ok = true; + for (streamsize __i = 0; __i < __n; ++__i) { + if (_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof())) + return false; + } + return true; +} - for (__i = 0; __i < __n; ++__i) - __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof()); - return __ok; -} template basic_ostream<_CharT, _Traits>& _STLP_CALL -operator<<(basic_ostream<_CharT, _Traits>& __os, - const basic_string<_CharT,_Traits,_Alloc>& __s) -{ +operator << (basic_ostream<_CharT, _Traits>& __os, + const basic_string<_CharT,_Traits,_Alloc>& __s) { + typedef basic_ostream<_CharT, _Traits> __ostream; + typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type; - _STLP_USING_IO - typedef basic_ostream<_CharT, _Traits> __ostream; + // The hypothesis of this implementation is that size_type is unsigned: + _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0) + typename __ostream::sentry __sentry(__os); bool __ok = false; if (__sentry) { __ok = true; - size_t __n = __s.size(); - size_t __pad_len = 0; + size_type __n = __s.size(); const bool __left = (__os.flags() & __ostream::left) != 0; - const size_t __w = __os.width(0); + const streamsize __w = __os.width(0); basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf(); - if (__n < __w) { - __pad_len = __w - __n; - } - + const bool __need_pad = (((sizeof(streamsize) > sizeof(size_t)) && (__STATIC_CAST(streamsize, __n) < __w)) || + ((sizeof(streamsize) <= sizeof(size_t)) && (__n < __STATIC_CAST(size_t, __w)))); + streamsize __pad_len = __need_pad ? __w - __n : 0; + if (!__left) - __ok = __stlp_string_fill(__os, __buf, __pad_len); + __ok = __stlp_string_fill(__os, __buf, __pad_len); __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n)); @@ -105,42 +63,41 @@ return __os; } - + template -basic_istream<_CharT, _Traits>& _STLP_CALL -operator>>(basic_istream<_CharT, _Traits>& __is, - basic_string<_CharT,_Traits, _Alloc>& __s) -{ - _STLP_USING_IO +basic_istream<_CharT, _Traits>& _STLP_CALL +operator >> (basic_istream<_CharT, _Traits>& __is, + basic_string<_CharT,_Traits, _Alloc>& __s) { typedef basic_istream<_CharT, _Traits> __istream; + typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type; + + // The hypothesis of this implementation is that size_type is unsigned: + _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0) + typename __istream::sentry __sentry(__is); if (__sentry) { basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf(); typedef ctype<_CharT> _C_type; -#ifdef _STLP_OWN_IOSTREAMS - // const _C_type& _Ctype = use_facet<_C_type>(__loc); - const _C_type& _Ctype = *(const _C_type*)__is._M_ctype_facet(); -#else -# if defined (_STLP_MSVC) && (_STLP_MSVC <= 1200 ) || defined (__ICL) - const locale& __loc = __is.getloc(); - const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0, true); -# elif defined (__SUNPRO_CC) - const locale& __loc = __is.getloc(); - const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0); -# else const locale& __loc = __is.getloc(); const _C_type& _Ctype = use_facet<_C_type>(__loc); -# endif -#endif __s.clear(); - size_t __n = __is.width(0); - if (__n == 0) - __n = __STATIC_CAST(size_t,-1); - else + streamsize __width = __is.width(0); + size_type __n; + if (__width <= 0) + __n = __s.max_size(); + /* __width can only overflow size_type if sizeof(streamsize) > sizeof(size_type) + * because here we know that __width is positive and the stattic assertion check + * that size_type is unsigned. + */ + else if (sizeof(streamsize) > sizeof(size_type) && + (__width > __STATIC_CAST(streamsize, __s.max_size()))) + __n = 0; + else { + __n = __STATIC_CAST(size_type, __width); __s.reserve(__n); - + } while (__n-- > 0) { typename _Traits::int_type __c1 = __buf->sbumpc(); @@ -156,18 +113,13 @@ __is.setstate(__istream::failbit); break; } -#ifdef __SYMBIAN32__ - else if (__c == '\b') { - __s.pop_back(); - } -#endif else __s.push_back(__c); } } - + // If we have read no characters, then set failbit. - if (__s.size() == 0) + if (__s.empty()) __is.setstate(__istream::failbit); } else @@ -176,15 +128,14 @@ return __is; } -template -basic_istream<_CharT, _Traits>& _STLP_CALL +template +basic_istream<_CharT, _Traits>& _STLP_CALL getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT,_Traits,_Alloc>& __s, - _CharT __delim) -{ - _STLP_USING_IO + _CharT __delim) { typedef basic_istream<_CharT, _Traits> __istream; - size_t __nread = 0; + typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type; + size_type __nread = 0; typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true); if (__sentry) { basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf(); @@ -199,7 +150,7 @@ else { ++__nread; _CharT __c = _Traits::to_char_type(__c1); - if (!_Traits::eq(__c, __delim)) + if (!_Traits::eq(__c, __delim)) __s.push_back(__c); else break; // Character is extracted but not appended. @@ -212,158 +163,10 @@ return __is; } -#elif ! defined ( _STLP_USE_NO_IOSTREAMS ) - -// (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll - -template -_OSTREAM_DLL& _STLP_CALL operator<<(_OSTREAM_DLL& __os, - const basic_string<_CharT,_Traits,_Alloc>& __s) -{ - _STLP_USING_IO - streambuf* __buf = __os.rdbuf(); - if (__buf) { - size_t __n = __s.size(); - size_t __pad_len = 0; - const bool __left = (__os.flags() & ios::left) !=0; - const size_t __w = __os.width(); - - if (__n < __w) { - __pad_len = __w - __n; - } - - if (!__left) - __stlp_string_fill(__os, __buf, __pad_len); - - const size_t __nwritten = __buf->sputn(__s.data(), __n); - - if (__left) - __stlp_string_fill(__os, __buf, __pad_len); - - if (__nwritten != __n) - __os.clear(__os.rdstate() | ios::failbit); - - __os.width(0); - } - else - __os.clear(__os.rdstate() | ios::badbit); - - return __os; -} - -template -_ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s) -{ - _STLP_USING_IO - if (!__is) - return __is; - - streambuf* __buf = __is.rdbuf(); - if (__buf) { - - if (__is.flags() & ios::skipws) { - // _CharT __c; - int __c; - do { - __c = __buf->sbumpc(); - } - while (__c != EOF && isspace((unsigned char)__c)); - - if (__c == EOF) { - __is.clear(__is.rdstate() | ios::eofbit | ios::failbit); - } - else { - if (__buf->sputbackc(__c) == EOF) - __is.clear(__is.rdstate() | ios::failbit); - } - } - - // If we arrive at end of file (or fail for some other reason) while - // still discarding whitespace, then we don't try to read the string. - if (__is) { - __s.clear(); - - size_t __n = __is.width(); - if (__n == 0) - __n = __STATIC_CAST(size_t,-1); - else - __s.reserve(__n); - - while (__n-- > 0) { - int __c1 = __buf->sbumpc(); - if (__c1 == EOF) { - __is.clear(__is.rdstate() | ios::eofbit); - break; - } - else { - _CharT __c = _Traits::to_char_type(__c1); - - if (isspace((unsigned char) __c)) { - if (__buf->sputbackc(__c) == EOF) - __is.clear(__is.rdstate() | ios::failbit); - break; - } - else - __s.push_back(__c); - } - } - - // If we have read no characters, then set failbit. - if (__s.size() == 0) - __is.clear(__is.rdstate() | ios::failbit); - } - - __is.width(0); - } - else // We have no streambuf. - __is.clear(__is.rdstate() | ios::badbit); - - return __is; -} - -template -_ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is, - basic_string<_CharT,_Traits,_Alloc>& __s, - _CharT __delim) -{ - _STLP_USING_IO - streambuf* __buf = __is.rdbuf(); - if (__buf) { - size_t __nread = 0; - if (__is) { - __s.clear(); - - while (__nread < __s.max_size()) { - int __c1 = __buf->sbumpc(); - if (__c1 == EOF) { - __is.clear(__is.rdstate() | ios::eofbit); - break; - } - else { - ++__nread; - _CharT __c = _Traits::to_char_type(__c1); - if (!_Traits::eq(__c, __delim)) - __s.push_back(__c); - else - break; // Character is extracted but not appended. - } - } - } - - if (__nread == 0 || __nread >= __s.max_size()) - __is.clear(__is.rdstate() | ios::failbit); - } - else - __is.clear(__is.rdstate() | ios::badbit); - - return __is; -} - -# endif /* _STLP_NEW_IOSTREAMS */ - _STLP_END_NAMESPACE -// # undef _STLP_USING_IO -# undef basic_string +#endif -#endif +// Local Variables: +// mode:C++ +// End: