epoc32/include/stdapis/stlportv5/stl/_string_io.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 2 epoc32/include/stdapis/stlport/stl/_string_io.c@2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     3 
     4 * Redistribution and use in source and binary forms, with or without 
     5 * modification, are permitted provided that the following conditions are met:
     6 
     7 * Redistributions of source code must retain the above copyright notice, this 
     8 * list of conditions and the following disclaimer.
     9 * Redistributions in binary form must reproduce the above copyright notice, 
    10 * this list of conditions and the following disclaimer in the documentation 
    11 * and/or other materials provided with the distribution.
    12 * Neither the name of Nokia Corporation nor the names of its contributors 
    13 * may be used to endorse or promote products derived from this software 
    14 * without specific prior written permission.
    15 
    16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
    20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
    21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
    23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
    24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    26 *
    27 * Description:
    28 *
    29 */
    30 
    31 #ifndef _STLP_STRING_IO_C
    32 #define _STLP_STRING_IO_C
    33 
    34 #ifndef _STLP_STRING_IO_H
    35 # include <stl/_string_io.h>
    36 #endif
    37 
    38 #ifndef _STLP_INTERNAL_CTYPE_H
    39 # include <stl/_ctype.h>
    40 #endif
    41 
    42 # ifdef _STLP_DEBUG
    43 #  define basic_string _Nondebug_string
    44 # endif
    45 
    46 _STLP_BEGIN_NAMESPACE
    47 
    48 # if defined (_STLP_OWN_IOSTREAMS)
    49 #  define _STLP_USING_IO
    50 # else
    51 #  define _STLP_USING_IO _STLP_USING_VENDOR_STD
    52 # endif
    53 
    54 #if defined (_STLP_USE_NEW_IOSTREAMS)
    55 
    56 template <class _CharT, class _Traits>
    57 bool _STLP_CALL
    58 __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
    59                   basic_streambuf<_CharT, _Traits>* __buf,
    60                   size_t __n)
    61 {
    62   _CharT __f = __os.fill();
    63   size_t __i;
    64   bool __ok = true;
    65 
    66   for (__i = 0; __i < __n; ++__i)
    67     __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
    68   return __ok;
    69 }
    70 
    71 template <class _CharT, class _Traits, class _Alloc>
    72 basic_ostream<_CharT, _Traits>& _STLP_CALL
    73 operator<<(basic_ostream<_CharT, _Traits>& __os, 
    74            const basic_string<_CharT,_Traits,_Alloc>& __s)
    75 {
    76 
    77   _STLP_USING_IO
    78   typedef basic_ostream<_CharT, _Traits> __ostream;
    79   typename __ostream::sentry __sentry(__os);
    80   bool __ok = false;
    81 
    82   if (__sentry) {
    83     __ok = true;
    84     size_t __n = __s.size();
    85     size_t __pad_len = 0;
    86     const bool __left = (__os.flags() & __ostream::left) != 0;
    87     const size_t __w = __os.width(0);
    88     basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
    89 
    90     if (__n < __w) {
    91       __pad_len = __w - __n;
    92     }
    93     
    94     if (!__left)
    95       __ok = __stlp_string_fill(__os, __buf, __pad_len);    
    96 
    97     __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
    98 
    99     if (__left)
   100       __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
   101   }
   102 
   103   if (!__ok)
   104     __os.setstate(__ostream::failbit);
   105 
   106   return __os;
   107 }
   108  
   109 template <class _CharT, class _Traits, class _Alloc>
   110 basic_istream<_CharT, _Traits>& _STLP_CALL 
   111 operator>>(basic_istream<_CharT, _Traits>& __is,
   112            basic_string<_CharT,_Traits, _Alloc>& __s)
   113 {
   114   _STLP_USING_IO
   115   typedef basic_istream<_CharT, _Traits> __istream;
   116   typename __istream::sentry __sentry(__is);
   117 
   118   if (__sentry) {
   119     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
   120     typedef ctype<_CharT> _C_type;
   121 
   122 #ifdef _STLP_OWN_IOSTREAMS
   123     //    const _C_type& _Ctype = use_facet<_C_type>(__loc);
   124     const _C_type& _Ctype = *(const _C_type*)__is._M_ctype_facet();
   125 #else
   126 # if defined (_STLP_MSVC) && (_STLP_MSVC <= 1200 ) || defined (__ICL)
   127     const locale& __loc = __is.getloc();
   128     const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0, true);
   129 # elif defined (__SUNPRO_CC)
   130     const locale& __loc = __is.getloc();
   131     const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0);
   132 # else
   133     const locale& __loc = __is.getloc();
   134     const _C_type& _Ctype = use_facet<_C_type>(__loc);
   135 # endif
   136 #endif
   137     __s.clear();
   138     size_t __n = __is.width(0);
   139     if (__n == 0)
   140       __n = __STATIC_CAST(size_t,-1);
   141     else
   142       __s.reserve(__n);
   143     
   144 
   145     while (__n-- > 0) {
   146       typename _Traits::int_type __c1 = __buf->sbumpc();
   147       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   148         __is.setstate(__istream::eofbit);
   149         break;
   150       }
   151       else {
   152         _CharT __c = _Traits::to_char_type(__c1);
   153 
   154         if (_Ctype.is(_C_type::space, __c)) {
   155           if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
   156             __is.setstate(__istream::failbit);
   157           break;
   158         }
   159 #ifdef __SYMBIAN32__
   160         else if (__c == '\b') {
   161           __s.pop_back();
   162         }
   163 #endif
   164         else
   165           __s.push_back(__c);
   166       }
   167     }
   168     
   169     // If we have read no characters, then set failbit.
   170     if (__s.size() == 0)
   171       __is.setstate(__istream::failbit);
   172   }
   173   else
   174     __is.setstate(__istream::failbit);
   175 
   176   return __is;
   177 }
   178 
   179 template <class _CharT, class _Traits, class _Alloc>    
   180 basic_istream<_CharT, _Traits>& _STLP_CALL 
   181 getline(basic_istream<_CharT, _Traits>& __is,
   182         basic_string<_CharT,_Traits,_Alloc>& __s,
   183         _CharT __delim)
   184 {
   185   _STLP_USING_IO
   186   typedef basic_istream<_CharT, _Traits> __istream;
   187   size_t __nread = 0;
   188   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
   189   if (__sentry) {
   190     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
   191     __s.clear();
   192 
   193     while (__nread < __s.max_size()) {
   194       int __c1 = __buf->sbumpc();
   195       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
   196         __is.setstate(__istream::eofbit);
   197         break;
   198       }
   199       else {
   200         ++__nread;
   201         _CharT __c = _Traits::to_char_type(__c1);
   202         if (!_Traits::eq(__c, __delim)) 
   203           __s.push_back(__c);
   204         else
   205           break;              // Character is extracted but not appended.
   206       }
   207     }
   208   }
   209   if (__nread == 0 || __nread >= __s.max_size())
   210     __is.setstate(__istream::failbit);
   211 
   212   return __is;
   213 }
   214 
   215 #elif ! defined ( _STLP_USE_NO_IOSTREAMS )
   216 
   217 // (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll
   218 
   219 template <class _CharT, class _Traits, class _Alloc>
   220 _OSTREAM_DLL&  _STLP_CALL operator<<(_OSTREAM_DLL& __os, 
   221                     const basic_string<_CharT,_Traits,_Alloc>& __s)
   222 {
   223   _STLP_USING_IO
   224   streambuf* __buf = __os.rdbuf();
   225   if (__buf) {
   226     size_t __n = __s.size();
   227     size_t __pad_len = 0;
   228     const bool __left = (__os.flags() & ios::left) !=0;
   229     const size_t __w = __os.width();
   230 
   231     if (__n < __w) { 
   232       __pad_len = __w - __n; 
   233     } 
   234     
   235     if (!__left)
   236       __stlp_string_fill(__os, __buf, __pad_len);
   237   
   238     const size_t __nwritten = __buf->sputn(__s.data(), __n);
   239 
   240     if (__left)
   241       __stlp_string_fill(__os, __buf, __pad_len);
   242 
   243     if (__nwritten != __n)
   244       __os.clear(__os.rdstate() | ios::failbit);
   245 
   246     __os.width(0);
   247   }
   248   else
   249     __os.clear(__os.rdstate() | ios::badbit);
   250 
   251   return __os;
   252 }
   253 
   254 template <class _CharT, class _Traits, class _Alloc>
   255 _ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
   256 {
   257   _STLP_USING_IO
   258   if (!__is)
   259     return __is;
   260 
   261   streambuf* __buf = __is.rdbuf();
   262   if (__buf) {
   263 
   264     if (__is.flags() & ios::skipws) {
   265       //      _CharT __c;
   266       int __c;
   267       do {
   268         __c = __buf->sbumpc();
   269       }
   270       while (__c != EOF && isspace((unsigned char)__c));
   271 
   272       if (__c == EOF) {
   273         __is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
   274       }
   275       else {
   276 	if (__buf->sputbackc(__c) == EOF)
   277 	  __is.clear(__is.rdstate() | ios::failbit);
   278       }
   279     }
   280 
   281     // If we arrive at end of file (or fail for some other reason) while
   282     // still discarding whitespace, then we don't try to read the string.
   283     if (__is) {
   284       __s.clear();
   285 
   286       size_t __n = __is.width();
   287       if (__n == 0)
   288         __n = __STATIC_CAST(size_t,-1);
   289       else
   290         __s.reserve(__n);
   291 
   292       while (__n-- > 0) {
   293         int __c1 = __buf->sbumpc();
   294         if (__c1 == EOF) {
   295           __is.clear(__is.rdstate() | ios::eofbit);
   296           break;
   297         }
   298         else {
   299           _CharT __c = _Traits::to_char_type(__c1);
   300 
   301           if (isspace((unsigned char) __c)) {
   302             if (__buf->sputbackc(__c) == EOF)
   303               __is.clear(__is.rdstate() | ios::failbit);
   304             break;
   305           }
   306           else
   307             __s.push_back(__c);
   308         }
   309       }
   310     
   311       // If we have read no characters, then set failbit.
   312       if (__s.size() == 0)
   313         __is.clear(__is.rdstate() | ios::failbit);
   314     }
   315 
   316     __is.width(0);
   317   }
   318   else                          // We have no streambuf.
   319     __is.clear(__is.rdstate() | ios::badbit);
   320 
   321   return __is;
   322 }
   323 
   324 template <class _CharT, class _Traits, class _Alloc>    
   325 _ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is,
   326                  basic_string<_CharT,_Traits,_Alloc>& __s,
   327                  _CharT __delim)
   328 {
   329   _STLP_USING_IO
   330   streambuf* __buf = __is.rdbuf();
   331   if (__buf) {
   332     size_t __nread = 0;
   333     if (__is) {
   334       __s.clear();
   335 
   336       while (__nread < __s.max_size()) {
   337         int __c1 = __buf->sbumpc();
   338         if (__c1 == EOF) {
   339           __is.clear(__is.rdstate() | ios::eofbit);
   340           break;
   341         }
   342         else {
   343           ++__nread;
   344           _CharT __c = _Traits::to_char_type(__c1);
   345           if (!_Traits::eq(__c, __delim)) 
   346             __s.push_back(__c);
   347           else
   348             break;              // Character is extracted but not appended.
   349         }
   350       }
   351     }
   352 
   353     if (__nread == 0 || __nread >= __s.max_size())
   354       __is.clear(__is.rdstate() | ios::failbit);
   355   }
   356   else
   357     __is.clear(__is.rdstate() | ios::badbit);
   358 
   359   return __is;
   360 }
   361 
   362 # endif /* _STLP_NEW_IOSTREAMS */
   363 
   364 _STLP_END_NAMESPACE
   365 
   366 // # undef _STLP_USING_IO
   367 # undef basic_string
   368 
   369 #endif