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