epoc32/include/stdapis/stlport/stl/_string_io.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
     2 
     3 
     4 #ifndef _STLP_STRING_IO_C
     5 #define _STLP_STRING_IO_C
     6 
     7 #ifndef _STLP_STRING_IO_H
     8 # include <stl/_string_io.h>
     9 #endif
    10 
    11 #ifndef _STLP_INTERNAL_CTYPE_H
    12 # include <stl/_ctype.h>
    13 #endif
    14 
    15 # ifdef _STLP_DEBUG
    16 #  define basic_string _Nondebug_string
    17 # endif
    18 
    19 _STLP_BEGIN_NAMESPACE
    20 
    21 # if defined (_STLP_OWN_IOSTREAMS)
    22 #  define _STLP_USING_IO
    23 # else
    24 #  define _STLP_USING_IO _STLP_USING_VENDOR_STD
    25 # endif
    26 
    27 #if defined (_STLP_USE_NEW_IOSTREAMS)
    28 
    29 template <class _CharT, class _Traits>
    30 bool _STLP_CALL
    31 __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
    32                   basic_streambuf<_CharT, _Traits>* __buf,
    33                   size_t __n)
    34 {
    35   _CharT __f = __os.fill();
    36   size_t __i;
    37   bool __ok = true;
    38 
    39   for (__i = 0; __i < __n; ++__i)
    40     __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
    41   return __ok;
    42 }
    43 
    44 template <class _CharT, class _Traits, class _Alloc>
    45 basic_ostream<_CharT, _Traits>& _STLP_CALL
    46 operator<<(basic_ostream<_CharT, _Traits>& __os, 
    47            const basic_string<_CharT,_Traits,_Alloc>& __s)
    48 {
    49 
    50   _STLP_USING_IO
    51   typedef basic_ostream<_CharT, _Traits> __ostream;
    52   typename __ostream::sentry __sentry(__os);
    53   bool __ok = false;
    54 
    55   if (__sentry) {
    56     __ok = true;
    57     size_t __n = __s.size();
    58     size_t __pad_len = 0;
    59     const bool __left = (__os.flags() & __ostream::left) != 0;
    60     const size_t __w = __os.width(0);
    61     basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
    62 
    63     if (__n < __w) {
    64       __pad_len = __w - __n;
    65     }
    66     
    67     if (!__left)
    68       __ok = __stlp_string_fill(__os, __buf, __pad_len);    
    69 
    70     __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
    71 
    72     if (__left)
    73       __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
    74   }
    75 
    76   if (!__ok)
    77     __os.setstate(__ostream::failbit);
    78 
    79   return __os;
    80 }
    81  
    82 template <class _CharT, class _Traits, class _Alloc>
    83 basic_istream<_CharT, _Traits>& _STLP_CALL 
    84 operator>>(basic_istream<_CharT, _Traits>& __is,
    85            basic_string<_CharT,_Traits, _Alloc>& __s)
    86 {
    87   _STLP_USING_IO
    88   typedef basic_istream<_CharT, _Traits> __istream;
    89   typename __istream::sentry __sentry(__is);
    90 
    91   if (__sentry) {
    92     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
    93     typedef ctype<_CharT> _C_type;
    94 
    95 #ifdef _STLP_OWN_IOSTREAMS
    96     //    const _C_type& _Ctype = use_facet<_C_type>(__loc);
    97     const _C_type& _Ctype = *(const _C_type*)__is._M_ctype_facet();
    98 #else
    99 # if defined (_STLP_MSVC) && (_STLP_MSVC <= 1200 ) || defined (__ICL)
   100     const locale& __loc = __is.getloc();
   101     const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0, true);
   102 # elif defined (__SUNPRO_CC)
   103     const locale& __loc = __is.getloc();
   104     const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0);
   105 # else
   106     const locale& __loc = __is.getloc();
   107     const _C_type& _Ctype = use_facet<_C_type>(__loc);
   108 # endif
   109 #endif
   110     __s.clear();
   111     size_t __n = __is.width(0);
   112     if (__n == 0)
   113       __n = __STATIC_CAST(size_t,-1);
   114     else
   115       __s.reserve(__n);
   116     
   117 
   118     while (__n-- > 0) {
   119       typename _Traits::int_type __c1 = __buf->sbumpc();
   120       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   121         __is.setstate(__istream::eofbit);
   122         break;
   123       }
   124       else {
   125         _CharT __c = _Traits::to_char_type(__c1);
   126 
   127         if (_Ctype.is(_C_type::space, __c)) {
   128           if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
   129             __is.setstate(__istream::failbit);
   130           break;
   131         }
   132 #ifdef __SYMBIAN32__
   133         else if (__c == '\b') {
   134           __s.pop_back();
   135         }
   136 #endif
   137         else
   138           __s.push_back(__c);
   139       }
   140     }
   141     
   142     // If we have read no characters, then set failbit.
   143     if (__s.size() == 0)
   144       __is.setstate(__istream::failbit);
   145   }
   146   else
   147     __is.setstate(__istream::failbit);
   148 
   149   return __is;
   150 }
   151 
   152 template <class _CharT, class _Traits, class _Alloc>    
   153 basic_istream<_CharT, _Traits>& _STLP_CALL 
   154 getline(basic_istream<_CharT, _Traits>& __is,
   155         basic_string<_CharT,_Traits,_Alloc>& __s,
   156         _CharT __delim)
   157 {
   158   _STLP_USING_IO
   159   typedef basic_istream<_CharT, _Traits> __istream;
   160   size_t __nread = 0;
   161   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
   162   if (__sentry) {
   163     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
   164     __s.clear();
   165 
   166     while (__nread < __s.max_size()) {
   167       int __c1 = __buf->sbumpc();
   168       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   169         __is.setstate(__istream::eofbit);
   170         break;
   171       }
   172       else {
   173         ++__nread;
   174         _CharT __c = _Traits::to_char_type(__c1);
   175         if (!_Traits::eq(__c, __delim)) 
   176           __s.push_back(__c);
   177         else
   178           break;              // Character is extracted but not appended.
   179       }
   180     }
   181   }
   182   if (__nread == 0 || __nread >= __s.max_size())
   183     __is.setstate(__istream::failbit);
   184 
   185   return __is;
   186 }
   187 
   188 #elif ! defined ( _STLP_USE_NO_IOSTREAMS )
   189 
   190 // (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll
   191 
   192 template <class _CharT, class _Traits, class _Alloc>
   193 _OSTREAM_DLL&  _STLP_CALL operator<<(_OSTREAM_DLL& __os, 
   194                     const basic_string<_CharT,_Traits,_Alloc>& __s)
   195 {
   196   _STLP_USING_IO
   197   streambuf* __buf = __os.rdbuf();
   198   if (__buf) {
   199     size_t __n = __s.size();
   200     size_t __pad_len = 0;
   201     const bool __left = (__os.flags() & ios::left) !=0;
   202     const size_t __w = __os.width();
   203 
   204     if (__n < __w) { 
   205       __pad_len = __w - __n; 
   206     } 
   207     
   208     if (!__left)
   209       __stlp_string_fill(__os, __buf, __pad_len);
   210   
   211     const size_t __nwritten = __buf->sputn(__s.data(), __n);
   212 
   213     if (__left)
   214       __stlp_string_fill(__os, __buf, __pad_len);
   215 
   216     if (__nwritten != __n)
   217       __os.clear(__os.rdstate() | ios::failbit);
   218 
   219     __os.width(0);
   220   }
   221   else
   222     __os.clear(__os.rdstate() | ios::badbit);
   223 
   224   return __os;
   225 }
   226 
   227 template <class _CharT, class _Traits, class _Alloc>
   228 _ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
   229 {
   230   _STLP_USING_IO
   231   if (!__is)
   232     return __is;
   233 
   234   streambuf* __buf = __is.rdbuf();
   235   if (__buf) {
   236 
   237     if (__is.flags() & ios::skipws) {
   238       //      _CharT __c;
   239       int __c;
   240       do {
   241         __c = __buf->sbumpc();
   242       }
   243       while (__c != EOF && isspace((unsigned char)__c));
   244 
   245       if (__c == EOF) {
   246         __is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
   247       }
   248       else {
   249 	if (__buf->sputbackc(__c) == EOF)
   250 	  __is.clear(__is.rdstate() | ios::failbit);
   251       }
   252     }
   253 
   254     // If we arrive at end of file (or fail for some other reason) while
   255     // still discarding whitespace, then we don't try to read the string.
   256     if (__is) {
   257       __s.clear();
   258 
   259       size_t __n = __is.width();
   260       if (__n == 0)
   261         __n = __STATIC_CAST(size_t,-1);
   262       else
   263         __s.reserve(__n);
   264 
   265       while (__n-- > 0) {
   266         int __c1 = __buf->sbumpc();
   267         if (__c1 == EOF) {
   268           __is.clear(__is.rdstate() | ios::eofbit);
   269           break;
   270         }
   271         else {
   272           _CharT __c = _Traits::to_char_type(__c1);
   273 
   274           if (isspace((unsigned char) __c)) {
   275             if (__buf->sputbackc(__c) == EOF)
   276               __is.clear(__is.rdstate() | ios::failbit);
   277             break;
   278           }
   279           else
   280             __s.push_back(__c);
   281         }
   282       }
   283     
   284       // If we have read no characters, then set failbit.
   285       if (__s.size() == 0)
   286         __is.clear(__is.rdstate() | ios::failbit);
   287     }
   288 
   289     __is.width(0);
   290   }
   291   else                          // We have no streambuf.
   292     __is.clear(__is.rdstate() | ios::badbit);
   293 
   294   return __is;
   295 }
   296 
   297 template <class _CharT, class _Traits, class _Alloc>    
   298 _ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is,
   299                  basic_string<_CharT,_Traits,_Alloc>& __s,
   300                  _CharT __delim)
   301 {
   302   _STLP_USING_IO
   303   streambuf* __buf = __is.rdbuf();
   304   if (__buf) {
   305     size_t __nread = 0;
   306     if (__is) {
   307       __s.clear();
   308 
   309       while (__nread < __s.max_size()) {
   310         int __c1 = __buf->sbumpc();
   311         if (__c1 == EOF) {
   312           __is.clear(__is.rdstate() | ios::eofbit);
   313           break;
   314         }
   315         else {
   316           ++__nread;
   317           _CharT __c = _Traits::to_char_type(__c1);
   318           if (!_Traits::eq(__c, __delim)) 
   319             __s.push_back(__c);
   320           else
   321             break;              // Character is extracted but not appended.
   322         }
   323       }
   324     }
   325 
   326     if (__nread == 0 || __nread >= __s.max_size())
   327       __is.clear(__is.rdstate() | ios::failbit);
   328   }
   329   else
   330     __is.clear(__is.rdstate() | ios::badbit);
   331 
   332   return __is;
   333 }
   334 
   335 # endif /* _STLP_NEW_IOSTREAMS */
   336 
   337 _STLP_END_NAMESPACE
   338 
   339 // # undef _STLP_USING_IO
   340 # undef basic_string
   341 
   342 #endif