/home/ntakagi/work/STLport-5.1.5/stlport/stl/_string_io.c

Go to the documentation of this file.
00001 #ifndef _STLP_STRING_IO_C
00002 #define _STLP_STRING_IO_C
00003 
00004 #ifndef _STLP_STRING_IO_H
00005 #  include <stl/_string_io.h>
00006 #endif
00007 
00008 #ifndef _STLP_INTERNAL_CTYPE_H
00009 #  include <stl/_ctype.h>
00010 #endif
00011 
00012 _STLP_BEGIN_NAMESPACE
00013 
00014 template <class _CharT, class _Traits>
00015 bool _STLP_CALL
00016 __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
00017                    basic_streambuf<_CharT, _Traits>* __buf,
00018                    streamsize __n) {
00019   _CharT __f = __os.fill();
00020   for (streamsize __i = 0; __i < __n; ++__i) {
00021     if (_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof()))
00022       return false;
00023   }
00024   return true;
00025 }
00026 
00027 
00028 template <class _CharT, class _Traits, class _Alloc>
00029 basic_ostream<_CharT, _Traits>& _STLP_CALL
00030 operator << (basic_ostream<_CharT, _Traits>& __os,
00031              const basic_string<_CharT,_Traits,_Alloc>& __s) {
00032   typedef basic_ostream<_CharT, _Traits> __ostream;
00033   typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
00034 
00035   // The hypothesis of this implementation is that size_type is unsigned:
00036   _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0)
00037 
00038   typename __ostream::sentry __sentry(__os);
00039   bool __ok = false;
00040 
00041   if (__sentry) {
00042     __ok = true;
00043     size_type __n = __s.size();
00044     const bool __left = (__os.flags() & __ostream::left) != 0;
00045     const streamsize __w = __os.width(0);
00046     basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
00047 
00048     const bool __need_pad = (((sizeof(streamsize) > sizeof(size_t)) && (__STATIC_CAST(streamsize, __n) < __w)) ||
00049                              ((sizeof(streamsize) <= sizeof(size_t)) && (__n < __STATIC_CAST(size_t, __w))));
00050     streamsize __pad_len = __need_pad ? __w - __n : 0;
00051 
00052     if (!__left)
00053       __ok = __stlp_string_fill(__os, __buf, __pad_len);
00054 
00055     __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
00056 
00057     if (__left)
00058       __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
00059   }
00060 
00061   if (!__ok)
00062     __os.setstate(__ostream::failbit);
00063 
00064   return __os;
00065 }
00066 
00067 template <class _CharT, class _Traits, class _Alloc>
00068 basic_istream<_CharT, _Traits>& _STLP_CALL
00069 operator >> (basic_istream<_CharT, _Traits>& __is,
00070              basic_string<_CharT,_Traits, _Alloc>& __s) {
00071   typedef basic_istream<_CharT, _Traits> __istream;
00072   typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
00073 
00074   // The hypothesis of this implementation is that size_type is unsigned:
00075   _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0)
00076 
00077   typename __istream::sentry __sentry(__is);
00078 
00079   if (__sentry) {
00080     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
00081     typedef ctype<_CharT> _C_type;
00082 
00083     const locale& __loc = __is.getloc();
00084     const _C_type& _Ctype = use_facet<_C_type>(__loc);
00085     __s.clear();
00086     streamsize __width = __is.width(0);
00087     size_type __n;
00088     if (__width <= 0)
00089       __n = __s.max_size();
00090     /* __width can only overflow size_type if sizeof(streamsize) > sizeof(size_type)
00091      * because here we know that __width is positive and the stattic assertion check
00092      * that size_type is unsigned.
00093      */
00094     else if (sizeof(streamsize) > sizeof(size_type) &&
00095              (__width > __STATIC_CAST(streamsize, __s.max_size())))
00096       __n = 0;
00097     else {
00098       __n = __STATIC_CAST(size_type, __width);
00099       __s.reserve(__n);
00100     }
00101 
00102     while (__n-- > 0) {
00103       typename _Traits::int_type __c1 = __buf->sbumpc();
00104       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
00105         __is.setstate(__istream::eofbit);
00106         break;
00107       }
00108       else {
00109         _CharT __c = _Traits::to_char_type(__c1);
00110 
00111         if (_Ctype.is(_C_type::space, __c)) {
00112           if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
00113             __is.setstate(__istream::failbit);
00114           break;
00115         }
00116         else
00117           __s.push_back(__c);
00118       }
00119     }
00120 
00121     // If we have read no characters, then set failbit.
00122     if (__s.empty())
00123       __is.setstate(__istream::failbit);
00124   }
00125   else
00126     __is.setstate(__istream::failbit);
00127 
00128   return __is;
00129 }
00130 
00131 template <class _CharT, class _Traits, class _Alloc>
00132 basic_istream<_CharT, _Traits>& _STLP_CALL
00133 getline(basic_istream<_CharT, _Traits>& __is,
00134         basic_string<_CharT,_Traits,_Alloc>& __s,
00135         _CharT __delim) {
00136   typedef basic_istream<_CharT, _Traits> __istream;
00137   typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
00138   size_type __nread = 0;
00139   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
00140   if (__sentry) {
00141     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
00142     __s.clear();
00143 
00144     while (__nread < __s.max_size()) {
00145       int __c1 = __buf->sbumpc();
00146       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
00147         __is.setstate(__istream::eofbit);
00148         break;
00149       }
00150       else {
00151         ++__nread;
00152         _CharT __c = _Traits::to_char_type(__c1);
00153         if (!_Traits::eq(__c, __delim))
00154           __s.push_back(__c);
00155         else
00156           break;              // Character is extracted but not appended.
00157       }
00158     }
00159   }
00160   if (__nread == 0 || __nread >= __s.max_size())
00161     __is.setstate(__istream::failbit);
00162 
00163   return __is;
00164 }
00165 
00166 _STLP_END_NAMESPACE
00167 
00168 #endif
00169 
00170 // Local Variables:
00171 // mode:C++
00172 // End:



Generated on Mon Mar 10 15:32:39 2008 by  doxygen 1.5.1