epoc32/include/stdapis/stlport/stl/_string_io.c
branchSymbian2
changeset 2 2fe1408b6811
parent 0 061f57f2323e
     1.1 --- a/epoc32/include/stdapis/stlport/stl/_string_io.c	Tue Nov 24 13:55:44 2009 +0000
     1.2 +++ b/epoc32/include/stdapis/stlport/stl/_string_io.c	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -1,1 +1,369 @@
     1.4 -_string_io.c
     1.5 +/*
     1.6 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.7 +
     1.8 +* Redistribution and use in source and binary forms, with or without 
     1.9 +* modification, are permitted provided that the following conditions are met:
    1.10 +
    1.11 +* Redistributions of source code must retain the above copyright notice, this 
    1.12 +* list of conditions and the following disclaimer.
    1.13 +* Redistributions in binary form must reproduce the above copyright notice, 
    1.14 +* this list of conditions and the following disclaimer in the documentation 
    1.15 +* and/or other materials provided with the distribution.
    1.16 +* Neither the name of Nokia Corporation nor the names of its contributors 
    1.17 +* may be used to endorse or promote products derived from this software 
    1.18 +* without specific prior written permission.
    1.19 +
    1.20 +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    1.21 +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    1.22 +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    1.23 +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
    1.24 +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
    1.25 +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    1.26 +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
    1.27 +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
    1.28 +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.29 +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +*
    1.31 +* Description:
    1.32 +*
    1.33 +*/
    1.34 +
    1.35 +#ifndef _STLP_STRING_IO_C
    1.36 +#define _STLP_STRING_IO_C
    1.37 +
    1.38 +#ifndef _STLP_STRING_IO_H
    1.39 +# include <stl/_string_io.h>
    1.40 +#endif
    1.41 +
    1.42 +#ifndef _STLP_INTERNAL_CTYPE_H
    1.43 +# include <stl/_ctype.h>
    1.44 +#endif
    1.45 +
    1.46 +# ifdef _STLP_DEBUG
    1.47 +#  define basic_string _Nondebug_string
    1.48 +# endif
    1.49 +
    1.50 +_STLP_BEGIN_NAMESPACE
    1.51 +
    1.52 +# if defined (_STLP_OWN_IOSTREAMS)
    1.53 +#  define _STLP_USING_IO
    1.54 +# else
    1.55 +#  define _STLP_USING_IO _STLP_USING_VENDOR_STD
    1.56 +# endif
    1.57 +
    1.58 +#if defined (_STLP_USE_NEW_IOSTREAMS)
    1.59 +
    1.60 +template <class _CharT, class _Traits>
    1.61 +bool _STLP_CALL
    1.62 +__stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
    1.63 +                  basic_streambuf<_CharT, _Traits>* __buf,
    1.64 +                  size_t __n)
    1.65 +{
    1.66 +  _CharT __f = __os.fill();
    1.67 +  size_t __i;
    1.68 +  bool __ok = true;
    1.69 +
    1.70 +  for (__i = 0; __i < __n; ++__i)
    1.71 +    __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
    1.72 +  return __ok;
    1.73 +}
    1.74 +
    1.75 +template <class _CharT, class _Traits, class _Alloc>
    1.76 +basic_ostream<_CharT, _Traits>& _STLP_CALL
    1.77 +operator<<(basic_ostream<_CharT, _Traits>& __os, 
    1.78 +           const basic_string<_CharT,_Traits,_Alloc>& __s)
    1.79 +{
    1.80 +
    1.81 +  _STLP_USING_IO
    1.82 +  typedef basic_ostream<_CharT, _Traits> __ostream;
    1.83 +  typename __ostream::sentry __sentry(__os);
    1.84 +  bool __ok = false;
    1.85 +
    1.86 +  if (__sentry) {
    1.87 +    __ok = true;
    1.88 +    size_t __n = __s.size();
    1.89 +    size_t __pad_len = 0;
    1.90 +    const bool __left = (__os.flags() & __ostream::left) != 0;
    1.91 +    const size_t __w = __os.width(0);
    1.92 +    basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
    1.93 +
    1.94 +    if (__n < __w) {
    1.95 +      __pad_len = __w - __n;
    1.96 +    }
    1.97 +    
    1.98 +    if (!__left)
    1.99 +      __ok = __stlp_string_fill(__os, __buf, __pad_len);    
   1.100 +
   1.101 +    __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
   1.102 +
   1.103 +    if (__left)
   1.104 +      __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
   1.105 +  }
   1.106 +
   1.107 +  if (!__ok)
   1.108 +    __os.setstate(__ostream::failbit);
   1.109 +
   1.110 +  return __os;
   1.111 +}
   1.112 + 
   1.113 +template <class _CharT, class _Traits, class _Alloc>
   1.114 +basic_istream<_CharT, _Traits>& _STLP_CALL 
   1.115 +operator>>(basic_istream<_CharT, _Traits>& __is,
   1.116 +           basic_string<_CharT,_Traits, _Alloc>& __s)
   1.117 +{
   1.118 +  _STLP_USING_IO
   1.119 +  typedef basic_istream<_CharT, _Traits> __istream;
   1.120 +  typename __istream::sentry __sentry(__is);
   1.121 +
   1.122 +  if (__sentry) {
   1.123 +    basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
   1.124 +    typedef ctype<_CharT> _C_type;
   1.125 +
   1.126 +#ifdef _STLP_OWN_IOSTREAMS
   1.127 +    //    const _C_type& _Ctype = use_facet<_C_type>(__loc);
   1.128 +    const _C_type& _Ctype = *(const _C_type*)__is._M_ctype_facet();
   1.129 +#else
   1.130 +# if defined (_STLP_MSVC) && (_STLP_MSVC <= 1200 ) || defined (__ICL)
   1.131 +    const locale& __loc = __is.getloc();
   1.132 +    const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0, true);
   1.133 +# elif defined (__SUNPRO_CC)
   1.134 +    const locale& __loc = __is.getloc();
   1.135 +    const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0);
   1.136 +# else
   1.137 +    const locale& __loc = __is.getloc();
   1.138 +    const _C_type& _Ctype = use_facet<_C_type>(__loc);
   1.139 +# endif
   1.140 +#endif
   1.141 +    __s.clear();
   1.142 +    size_t __n = __is.width(0);
   1.143 +    if (__n == 0)
   1.144 +      __n = __STATIC_CAST(size_t,-1);
   1.145 +    else
   1.146 +      __s.reserve(__n);
   1.147 +    
   1.148 +
   1.149 +    while (__n-- > 0) {
   1.150 +      typename _Traits::int_type __c1 = __buf->sbumpc();
   1.151 +      if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   1.152 +        __is.setstate(__istream::eofbit);
   1.153 +        break;
   1.154 +      }
   1.155 +      else {
   1.156 +        _CharT __c = _Traits::to_char_type(__c1);
   1.157 +
   1.158 +        if (_Ctype.is(_C_type::space, __c)) {
   1.159 +          if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
   1.160 +            __is.setstate(__istream::failbit);
   1.161 +          break;
   1.162 +        }
   1.163 +#ifdef __SYMBIAN32__
   1.164 +        else if (__c == '\b') {
   1.165 +          __s.pop_back();
   1.166 +        }
   1.167 +#endif
   1.168 +        else
   1.169 +          __s.push_back(__c);
   1.170 +      }
   1.171 +    }
   1.172 +    
   1.173 +    // If we have read no characters, then set failbit.
   1.174 +    if (__s.size() == 0)
   1.175 +      __is.setstate(__istream::failbit);
   1.176 +  }
   1.177 +  else
   1.178 +    __is.setstate(__istream::failbit);
   1.179 +
   1.180 +  return __is;
   1.181 +}
   1.182 +
   1.183 +template <class _CharT, class _Traits, class _Alloc>    
   1.184 +basic_istream<_CharT, _Traits>& _STLP_CALL 
   1.185 +getline(basic_istream<_CharT, _Traits>& __is,
   1.186 +        basic_string<_CharT,_Traits,_Alloc>& __s,
   1.187 +        _CharT __delim)
   1.188 +{
   1.189 +  _STLP_USING_IO
   1.190 +  typedef basic_istream<_CharT, _Traits> __istream;
   1.191 +  size_t __nread = 0;
   1.192 +  typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
   1.193 +  if (__sentry) {
   1.194 +    basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
   1.195 +    __s.clear();
   1.196 +
   1.197 +    while (__nread < __s.max_size()) {
   1.198 +      int __c1 = __buf->sbumpc();
   1.199 +      if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   1.200 +        __is.setstate(__istream::eofbit);
   1.201 +        break;
   1.202 +      }
   1.203 +      else {
   1.204 +        ++__nread;
   1.205 +        _CharT __c = _Traits::to_char_type(__c1);
   1.206 +        if (!_Traits::eq(__c, __delim)) 
   1.207 +          __s.push_back(__c);
   1.208 +        else
   1.209 +          break;              // Character is extracted but not appended.
   1.210 +      }
   1.211 +    }
   1.212 +  }
   1.213 +  if (__nread == 0 || __nread >= __s.max_size())
   1.214 +    __is.setstate(__istream::failbit);
   1.215 +
   1.216 +  return __is;
   1.217 +}
   1.218 +
   1.219 +#elif ! defined ( _STLP_USE_NO_IOSTREAMS )
   1.220 +
   1.221 +// (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll
   1.222 +
   1.223 +template <class _CharT, class _Traits, class _Alloc>
   1.224 +_OSTREAM_DLL&  _STLP_CALL operator<<(_OSTREAM_DLL& __os, 
   1.225 +                    const basic_string<_CharT,_Traits,_Alloc>& __s)
   1.226 +{
   1.227 +  _STLP_USING_IO
   1.228 +  streambuf* __buf = __os.rdbuf();
   1.229 +  if (__buf) {
   1.230 +    size_t __n = __s.size();
   1.231 +    size_t __pad_len = 0;
   1.232 +    const bool __left = (__os.flags() & ios::left) !=0;
   1.233 +    const size_t __w = __os.width();
   1.234 +
   1.235 +    if (__n < __w) { 
   1.236 +      __pad_len = __w - __n; 
   1.237 +    } 
   1.238 +    
   1.239 +    if (!__left)
   1.240 +      __stlp_string_fill(__os, __buf, __pad_len);
   1.241 +  
   1.242 +    const size_t __nwritten = __buf->sputn(__s.data(), __n);
   1.243 +
   1.244 +    if (__left)
   1.245 +      __stlp_string_fill(__os, __buf, __pad_len);
   1.246 +
   1.247 +    if (__nwritten != __n)
   1.248 +      __os.clear(__os.rdstate() | ios::failbit);
   1.249 +
   1.250 +    __os.width(0);
   1.251 +  }
   1.252 +  else
   1.253 +    __os.clear(__os.rdstate() | ios::badbit);
   1.254 +
   1.255 +  return __os;
   1.256 +}
   1.257 +
   1.258 +template <class _CharT, class _Traits, class _Alloc>
   1.259 +_ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
   1.260 +{
   1.261 +  _STLP_USING_IO
   1.262 +  if (!__is)
   1.263 +    return __is;
   1.264 +
   1.265 +  streambuf* __buf = __is.rdbuf();
   1.266 +  if (__buf) {
   1.267 +
   1.268 +    if (__is.flags() & ios::skipws) {
   1.269 +      //      _CharT __c;
   1.270 +      int __c;
   1.271 +      do {
   1.272 +        __c = __buf->sbumpc();
   1.273 +      }
   1.274 +      while (__c != EOF && isspace((unsigned char)__c));
   1.275 +
   1.276 +      if (__c == EOF) {
   1.277 +        __is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
   1.278 +      }
   1.279 +      else {
   1.280 +	if (__buf->sputbackc(__c) == EOF)
   1.281 +	  __is.clear(__is.rdstate() | ios::failbit);
   1.282 +      }
   1.283 +    }
   1.284 +
   1.285 +    // If we arrive at end of file (or fail for some other reason) while
   1.286 +    // still discarding whitespace, then we don't try to read the string.
   1.287 +    if (__is) {
   1.288 +      __s.clear();
   1.289 +
   1.290 +      size_t __n = __is.width();
   1.291 +      if (__n == 0)
   1.292 +        __n = __STATIC_CAST(size_t,-1);
   1.293 +      else
   1.294 +        __s.reserve(__n);
   1.295 +
   1.296 +      while (__n-- > 0) {
   1.297 +        int __c1 = __buf->sbumpc();
   1.298 +        if (__c1 == EOF) {
   1.299 +          __is.clear(__is.rdstate() | ios::eofbit);
   1.300 +          break;
   1.301 +        }
   1.302 +        else {
   1.303 +          _CharT __c = _Traits::to_char_type(__c1);
   1.304 +
   1.305 +          if (isspace((unsigned char) __c)) {
   1.306 +            if (__buf->sputbackc(__c) == EOF)
   1.307 +              __is.clear(__is.rdstate() | ios::failbit);
   1.308 +            break;
   1.309 +          }
   1.310 +          else
   1.311 +            __s.push_back(__c);
   1.312 +        }
   1.313 +      }
   1.314 +    
   1.315 +      // If we have read no characters, then set failbit.
   1.316 +      if (__s.size() == 0)
   1.317 +        __is.clear(__is.rdstate() | ios::failbit);
   1.318 +    }
   1.319 +
   1.320 +    __is.width(0);
   1.321 +  }
   1.322 +  else                          // We have no streambuf.
   1.323 +    __is.clear(__is.rdstate() | ios::badbit);
   1.324 +
   1.325 +  return __is;
   1.326 +}
   1.327 +
   1.328 +template <class _CharT, class _Traits, class _Alloc>    
   1.329 +_ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is,
   1.330 +                 basic_string<_CharT,_Traits,_Alloc>& __s,
   1.331 +                 _CharT __delim)
   1.332 +{
   1.333 +  _STLP_USING_IO
   1.334 +  streambuf* __buf = __is.rdbuf();
   1.335 +  if (__buf) {
   1.336 +    size_t __nread = 0;
   1.337 +    if (__is) {
   1.338 +      __s.clear();
   1.339 +
   1.340 +      while (__nread < __s.max_size()) {
   1.341 +        int __c1 = __buf->sbumpc();
   1.342 +        if (__c1 == EOF) {
   1.343 +          __is.clear(__is.rdstate() | ios::eofbit);
   1.344 +          break;
   1.345 +        }
   1.346 +        else {
   1.347 +          ++__nread;
   1.348 +          _CharT __c = _Traits::to_char_type(__c1);
   1.349 +          if (!_Traits::eq(__c, __delim)) 
   1.350 +            __s.push_back(__c);
   1.351 +          else
   1.352 +            break;              // Character is extracted but not appended.
   1.353 +        }
   1.354 +      }
   1.355 +    }
   1.356 +
   1.357 +    if (__nread == 0 || __nread >= __s.max_size())
   1.358 +      __is.clear(__is.rdstate() | ios::failbit);
   1.359 +  }
   1.360 +  else
   1.361 +    __is.clear(__is.rdstate() | ios::badbit);
   1.362 +
   1.363 +  return __is;
   1.364 +}
   1.365 +
   1.366 +# endif /* _STLP_NEW_IOSTREAMS */
   1.367 +
   1.368 +_STLP_END_NAMESPACE
   1.369 +
   1.370 +// # undef _STLP_USING_IO
   1.371 +# undef basic_string
   1.372 +
   1.373 +#endif