genericopenlibs/cppstdlib/stl/stlport/stl/_string_io.c
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #ifndef _STLP_STRING_IO_C
       
    18 #define _STLP_STRING_IO_C
       
    19 
       
    20 #ifndef _STLP_STRING_IO_H
       
    21 #  include <stl/_string_io.h>
       
    22 #endif
       
    23 
       
    24 #ifndef _STLP_INTERNAL_CTYPE_H
       
    25 #  include <stl/_ctype.h>
       
    26 #endif
       
    27 
       
    28 _STLP_BEGIN_NAMESPACE
       
    29 
       
    30 template <class _CharT, class _Traits>
       
    31 bool _STLP_CALL
       
    32 __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
       
    33                    basic_streambuf<_CharT, _Traits>* __buf,
       
    34                    streamsize __n) {
       
    35   _CharT __f = __os.fill();
       
    36   for (streamsize __i = 0; __i < __n; ++__i) {
       
    37     if (_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof()))
       
    38       return false;
       
    39   }
       
    40   return true;
       
    41 }
       
    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   typedef basic_ostream<_CharT, _Traits> __ostream;
       
    49   typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
       
    50 
       
    51   // The hypothesis of this implementation is that size_type is unsigned:
       
    52   _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0)
       
    53 
       
    54   typename __ostream::sentry __sentry(__os);
       
    55   bool __ok = false;
       
    56 
       
    57   if (__sentry) {
       
    58     __ok = true;
       
    59     size_type __n = __s.size();
       
    60     const bool __left = (__os.flags() & __ostream::left) != 0;
       
    61     const streamsize __w = __os.width(0);
       
    62     basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
       
    63 
       
    64     const bool __need_pad = (((sizeof(streamsize) > sizeof(size_t)) && (__STATIC_CAST(streamsize, __n) < __w)) ||
       
    65                              ((sizeof(streamsize) <= sizeof(size_t)) && (__n < __STATIC_CAST(size_t, __w))));
       
    66     streamsize __pad_len = __need_pad ? __w - __n : 0;
       
    67 
       
    68     if (!__left)
       
    69       __ok = __stlp_string_fill(__os, __buf, __pad_len);
       
    70 
       
    71     __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
       
    72 
       
    73     if (__left)
       
    74       __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
       
    75   }
       
    76 
       
    77   if (!__ok)
       
    78     __os.setstate(__ostream::failbit);
       
    79 
       
    80   return __os;
       
    81 }
       
    82 
       
    83 template <class _CharT, class _Traits, class _Alloc>
       
    84 basic_istream<_CharT, _Traits>& _STLP_CALL
       
    85 operator >> (basic_istream<_CharT, _Traits>& __is,
       
    86              basic_string<_CharT,_Traits, _Alloc>& __s) {
       
    87   typedef basic_istream<_CharT, _Traits> __istream;
       
    88   typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
       
    89 
       
    90   // The hypothesis of this implementation is that size_type is unsigned:
       
    91   _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0)
       
    92 
       
    93   typename __istream::sentry __sentry(__is);
       
    94 
       
    95   if (__sentry) {
       
    96     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
       
    97     typedef ctype<_CharT> _C_type;
       
    98 
       
    99     const locale& __loc = __is.getloc();
       
   100     const _C_type& _Ctype = use_facet<_C_type>(__loc);
       
   101     __s.clear();
       
   102     streamsize __width = __is.width(0);
       
   103     size_type __n;
       
   104     if (__width <= 0)
       
   105       __n = __s.max_size();
       
   106     /* __width can only overflow size_type if sizeof(streamsize) > sizeof(size_type)
       
   107      * because here we know that __width is positive and the stattic assertion check
       
   108      * that size_type is unsigned.
       
   109      */
       
   110     else if (sizeof(streamsize) > sizeof(size_type) &&
       
   111              (__width > __STATIC_CAST(streamsize, __s.max_size())))
       
   112       __n = 0;
       
   113     else {
       
   114       __n = __STATIC_CAST(size_type, __width);
       
   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         else
       
   133           __s.push_back(__c);
       
   134       }
       
   135     }
       
   136 
       
   137     // If we have read no characters, then set failbit.
       
   138     if (__s.empty())
       
   139       __is.setstate(__istream::failbit);
       
   140   }
       
   141   else
       
   142     __is.setstate(__istream::failbit);
       
   143 
       
   144   return __is;
       
   145 }
       
   146 
       
   147 template <class _CharT, class _Traits, class _Alloc>
       
   148 basic_istream<_CharT, _Traits>& _STLP_CALL
       
   149 getline(basic_istream<_CharT, _Traits>& __is,
       
   150         basic_string<_CharT,_Traits,_Alloc>& __s,
       
   151         _CharT __delim) {
       
   152   typedef basic_istream<_CharT, _Traits> __istream;
       
   153   typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
       
   154   size_type __nread = 0;
       
   155   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
       
   156   if (__sentry) {
       
   157     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
       
   158     __s.clear();
       
   159 
       
   160     while (__nread < __s.max_size()) {
       
   161       int __c1 = __buf->sbumpc();
       
   162       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
       
   163         __is.setstate(__istream::eofbit);
       
   164         break;
       
   165       }
       
   166       else {
       
   167         ++__nread;
       
   168         _CharT __c = _Traits::to_char_type(__c1);
       
   169         if (!_Traits::eq(__c, __delim))
       
   170           __s.push_back(__c);
       
   171         else
       
   172           break;              // Character is extracted but not appended.
       
   173       }
       
   174     }
       
   175   }
       
   176   if (__nread == 0 || __nread >= __s.max_size())
       
   177     __is.setstate(__istream::failbit);
       
   178 
       
   179   return __is;
       
   180 }
       
   181 
       
   182 _STLP_END_NAMESPACE
       
   183 
       
   184 #endif
       
   185 
       
   186 // Local Variables:
       
   187 // mode:C++
       
   188 // End: