epoc32/include/stdapis/stlport/stl/_string_io.c
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/stlport/stl/_string_io.c	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,342 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
     1.5 +
     1.6 +
     1.7 +#ifndef _STLP_STRING_IO_C
     1.8 +#define _STLP_STRING_IO_C
     1.9 +
    1.10 +#ifndef _STLP_STRING_IO_H
    1.11 +# include <stl/_string_io.h>
    1.12 +#endif
    1.13 +
    1.14 +#ifndef _STLP_INTERNAL_CTYPE_H
    1.15 +# include <stl/_ctype.h>
    1.16 +#endif
    1.17 +
    1.18 +# ifdef _STLP_DEBUG
    1.19 +#  define basic_string _Nondebug_string
    1.20 +# endif
    1.21 +
    1.22 +_STLP_BEGIN_NAMESPACE
    1.23 +
    1.24 +# if defined (_STLP_OWN_IOSTREAMS)
    1.25 +#  define _STLP_USING_IO
    1.26 +# else
    1.27 +#  define _STLP_USING_IO _STLP_USING_VENDOR_STD
    1.28 +# endif
    1.29 +
    1.30 +#if defined (_STLP_USE_NEW_IOSTREAMS)
    1.31 +
    1.32 +template <class _CharT, class _Traits>
    1.33 +bool _STLP_CALL
    1.34 +__stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
    1.35 +                  basic_streambuf<_CharT, _Traits>* __buf,
    1.36 +                  size_t __n)
    1.37 +{
    1.38 +  _CharT __f = __os.fill();
    1.39 +  size_t __i;
    1.40 +  bool __ok = true;
    1.41 +
    1.42 +  for (__i = 0; __i < __n; ++__i)
    1.43 +    __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
    1.44 +  return __ok;
    1.45 +}
    1.46 +
    1.47 +template <class _CharT, class _Traits, class _Alloc>
    1.48 +basic_ostream<_CharT, _Traits>& _STLP_CALL
    1.49 +operator<<(basic_ostream<_CharT, _Traits>& __os, 
    1.50 +           const basic_string<_CharT,_Traits,_Alloc>& __s)
    1.51 +{
    1.52 +
    1.53 +  _STLP_USING_IO
    1.54 +  typedef basic_ostream<_CharT, _Traits> __ostream;
    1.55 +  typename __ostream::sentry __sentry(__os);
    1.56 +  bool __ok = false;
    1.57 +
    1.58 +  if (__sentry) {
    1.59 +    __ok = true;
    1.60 +    size_t __n = __s.size();
    1.61 +    size_t __pad_len = 0;
    1.62 +    const bool __left = (__os.flags() & __ostream::left) != 0;
    1.63 +    const size_t __w = __os.width(0);
    1.64 +    basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
    1.65 +
    1.66 +    if (__n < __w) {
    1.67 +      __pad_len = __w - __n;
    1.68 +    }
    1.69 +    
    1.70 +    if (!__left)
    1.71 +      __ok = __stlp_string_fill(__os, __buf, __pad_len);    
    1.72 +
    1.73 +    __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
    1.74 +
    1.75 +    if (__left)
    1.76 +      __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
    1.77 +  }
    1.78 +
    1.79 +  if (!__ok)
    1.80 +    __os.setstate(__ostream::failbit);
    1.81 +
    1.82 +  return __os;
    1.83 +}
    1.84 + 
    1.85 +template <class _CharT, class _Traits, class _Alloc>
    1.86 +basic_istream<_CharT, _Traits>& _STLP_CALL 
    1.87 +operator>>(basic_istream<_CharT, _Traits>& __is,
    1.88 +           basic_string<_CharT,_Traits, _Alloc>& __s)
    1.89 +{
    1.90 +  _STLP_USING_IO
    1.91 +  typedef basic_istream<_CharT, _Traits> __istream;
    1.92 +  typename __istream::sentry __sentry(__is);
    1.93 +
    1.94 +  if (__sentry) {
    1.95 +    basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
    1.96 +    typedef ctype<_CharT> _C_type;
    1.97 +
    1.98 +#ifdef _STLP_OWN_IOSTREAMS
    1.99 +    //    const _C_type& _Ctype = use_facet<_C_type>(__loc);
   1.100 +    const _C_type& _Ctype = *(const _C_type*)__is._M_ctype_facet();
   1.101 +#else
   1.102 +# if defined (_STLP_MSVC) && (_STLP_MSVC <= 1200 ) || defined (__ICL)
   1.103 +    const locale& __loc = __is.getloc();
   1.104 +    const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0, true);
   1.105 +# elif defined (__SUNPRO_CC)
   1.106 +    const locale& __loc = __is.getloc();
   1.107 +    const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0);
   1.108 +# else
   1.109 +    const locale& __loc = __is.getloc();
   1.110 +    const _C_type& _Ctype = use_facet<_C_type>(__loc);
   1.111 +# endif
   1.112 +#endif
   1.113 +    __s.clear();
   1.114 +    size_t __n = __is.width(0);
   1.115 +    if (__n == 0)
   1.116 +      __n = __STATIC_CAST(size_t,-1);
   1.117 +    else
   1.118 +      __s.reserve(__n);
   1.119 +    
   1.120 +
   1.121 +    while (__n-- > 0) {
   1.122 +      typename _Traits::int_type __c1 = __buf->sbumpc();
   1.123 +      if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   1.124 +        __is.setstate(__istream::eofbit);
   1.125 +        break;
   1.126 +      }
   1.127 +      else {
   1.128 +        _CharT __c = _Traits::to_char_type(__c1);
   1.129 +
   1.130 +        if (_Ctype.is(_C_type::space, __c)) {
   1.131 +          if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
   1.132 +            __is.setstate(__istream::failbit);
   1.133 +          break;
   1.134 +        }
   1.135 +#ifdef __SYMBIAN32__
   1.136 +        else if (__c == '\b') {
   1.137 +          __s.pop_back();
   1.138 +        }
   1.139 +#endif
   1.140 +        else
   1.141 +          __s.push_back(__c);
   1.142 +      }
   1.143 +    }
   1.144 +    
   1.145 +    // If we have read no characters, then set failbit.
   1.146 +    if (__s.size() == 0)
   1.147 +      __is.setstate(__istream::failbit);
   1.148 +  }
   1.149 +  else
   1.150 +    __is.setstate(__istream::failbit);
   1.151 +
   1.152 +  return __is;
   1.153 +}
   1.154 +
   1.155 +template <class _CharT, class _Traits, class _Alloc>    
   1.156 +basic_istream<_CharT, _Traits>& _STLP_CALL 
   1.157 +getline(basic_istream<_CharT, _Traits>& __is,
   1.158 +        basic_string<_CharT,_Traits,_Alloc>& __s,
   1.159 +        _CharT __delim)
   1.160 +{
   1.161 +  _STLP_USING_IO
   1.162 +  typedef basic_istream<_CharT, _Traits> __istream;
   1.163 +  size_t __nread = 0;
   1.164 +  typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
   1.165 +  if (__sentry) {
   1.166 +    basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
   1.167 +    __s.clear();
   1.168 +
   1.169 +    while (__nread < __s.max_size()) {
   1.170 +      int __c1 = __buf->sbumpc();
   1.171 +      if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   1.172 +        __is.setstate(__istream::eofbit);
   1.173 +        break;
   1.174 +      }
   1.175 +      else {
   1.176 +        ++__nread;
   1.177 +        _CharT __c = _Traits::to_char_type(__c1);
   1.178 +        if (!_Traits::eq(__c, __delim)) 
   1.179 +          __s.push_back(__c);
   1.180 +        else
   1.181 +          break;              // Character is extracted but not appended.
   1.182 +      }
   1.183 +    }
   1.184 +  }
   1.185 +  if (__nread == 0 || __nread >= __s.max_size())
   1.186 +    __is.setstate(__istream::failbit);
   1.187 +
   1.188 +  return __is;
   1.189 +}
   1.190 +
   1.191 +#elif ! defined ( _STLP_USE_NO_IOSTREAMS )
   1.192 +
   1.193 +// (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll
   1.194 +
   1.195 +template <class _CharT, class _Traits, class _Alloc>
   1.196 +_OSTREAM_DLL&  _STLP_CALL operator<<(_OSTREAM_DLL& __os, 
   1.197 +                    const basic_string<_CharT,_Traits,_Alloc>& __s)
   1.198 +{
   1.199 +  _STLP_USING_IO
   1.200 +  streambuf* __buf = __os.rdbuf();
   1.201 +  if (__buf) {
   1.202 +    size_t __n = __s.size();
   1.203 +    size_t __pad_len = 0;
   1.204 +    const bool __left = (__os.flags() & ios::left) !=0;
   1.205 +    const size_t __w = __os.width();
   1.206 +
   1.207 +    if (__n < __w) { 
   1.208 +      __pad_len = __w - __n; 
   1.209 +    } 
   1.210 +    
   1.211 +    if (!__left)
   1.212 +      __stlp_string_fill(__os, __buf, __pad_len);
   1.213 +  
   1.214 +    const size_t __nwritten = __buf->sputn(__s.data(), __n);
   1.215 +
   1.216 +    if (__left)
   1.217 +      __stlp_string_fill(__os, __buf, __pad_len);
   1.218 +
   1.219 +    if (__nwritten != __n)
   1.220 +      __os.clear(__os.rdstate() | ios::failbit);
   1.221 +
   1.222 +    __os.width(0);
   1.223 +  }
   1.224 +  else
   1.225 +    __os.clear(__os.rdstate() | ios::badbit);
   1.226 +
   1.227 +  return __os;
   1.228 +}
   1.229 +
   1.230 +template <class _CharT, class _Traits, class _Alloc>
   1.231 +_ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
   1.232 +{
   1.233 +  _STLP_USING_IO
   1.234 +  if (!__is)
   1.235 +    return __is;
   1.236 +
   1.237 +  streambuf* __buf = __is.rdbuf();
   1.238 +  if (__buf) {
   1.239 +
   1.240 +    if (__is.flags() & ios::skipws) {
   1.241 +      //      _CharT __c;
   1.242 +      int __c;
   1.243 +      do {
   1.244 +        __c = __buf->sbumpc();
   1.245 +      }
   1.246 +      while (__c != EOF && isspace((unsigned char)__c));
   1.247 +
   1.248 +      if (__c == EOF) {
   1.249 +        __is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
   1.250 +      }
   1.251 +      else {
   1.252 +	if (__buf->sputbackc(__c) == EOF)
   1.253 +	  __is.clear(__is.rdstate() | ios::failbit);
   1.254 +      }
   1.255 +    }
   1.256 +
   1.257 +    // If we arrive at end of file (or fail for some other reason) while
   1.258 +    // still discarding whitespace, then we don't try to read the string.
   1.259 +    if (__is) {
   1.260 +      __s.clear();
   1.261 +
   1.262 +      size_t __n = __is.width();
   1.263 +      if (__n == 0)
   1.264 +        __n = __STATIC_CAST(size_t,-1);
   1.265 +      else
   1.266 +        __s.reserve(__n);
   1.267 +
   1.268 +      while (__n-- > 0) {
   1.269 +        int __c1 = __buf->sbumpc();
   1.270 +        if (__c1 == EOF) {
   1.271 +          __is.clear(__is.rdstate() | ios::eofbit);
   1.272 +          break;
   1.273 +        }
   1.274 +        else {
   1.275 +          _CharT __c = _Traits::to_char_type(__c1);
   1.276 +
   1.277 +          if (isspace((unsigned char) __c)) {
   1.278 +            if (__buf->sputbackc(__c) == EOF)
   1.279 +              __is.clear(__is.rdstate() | ios::failbit);
   1.280 +            break;
   1.281 +          }
   1.282 +          else
   1.283 +            __s.push_back(__c);
   1.284 +        }
   1.285 +      }
   1.286 +    
   1.287 +      // If we have read no characters, then set failbit.
   1.288 +      if (__s.size() == 0)
   1.289 +        __is.clear(__is.rdstate() | ios::failbit);
   1.290 +    }
   1.291 +
   1.292 +    __is.width(0);
   1.293 +  }
   1.294 +  else                          // We have no streambuf.
   1.295 +    __is.clear(__is.rdstate() | ios::badbit);
   1.296 +
   1.297 +  return __is;
   1.298 +}
   1.299 +
   1.300 +template <class _CharT, class _Traits, class _Alloc>    
   1.301 +_ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is,
   1.302 +                 basic_string<_CharT,_Traits,_Alloc>& __s,
   1.303 +                 _CharT __delim)
   1.304 +{
   1.305 +  _STLP_USING_IO
   1.306 +  streambuf* __buf = __is.rdbuf();
   1.307 +  if (__buf) {
   1.308 +    size_t __nread = 0;
   1.309 +    if (__is) {
   1.310 +      __s.clear();
   1.311 +
   1.312 +      while (__nread < __s.max_size()) {
   1.313 +        int __c1 = __buf->sbumpc();
   1.314 +        if (__c1 == EOF) {
   1.315 +          __is.clear(__is.rdstate() | ios::eofbit);
   1.316 +          break;
   1.317 +        }
   1.318 +        else {
   1.319 +          ++__nread;
   1.320 +          _CharT __c = _Traits::to_char_type(__c1);
   1.321 +          if (!_Traits::eq(__c, __delim)) 
   1.322 +            __s.push_back(__c);
   1.323 +          else
   1.324 +            break;              // Character is extracted but not appended.
   1.325 +        }
   1.326 +      }
   1.327 +    }
   1.328 +
   1.329 +    if (__nread == 0 || __nread >= __s.max_size())
   1.330 +      __is.clear(__is.rdstate() | ios::failbit);
   1.331 +  }
   1.332 +  else
   1.333 +    __is.clear(__is.rdstate() | ios::badbit);
   1.334 +
   1.335 +  return __is;
   1.336 +}
   1.337 +
   1.338 +# endif /* _STLP_NEW_IOSTREAMS */
   1.339 +
   1.340 +_STLP_END_NAMESPACE
   1.341 +
   1.342 +// # undef _STLP_USING_IO
   1.343 +# undef basic_string
   1.344 +
   1.345 +#endif