1 #ifndef _STLP_STRING_IO_C
2 #define _STLP_STRING_IO_C
4 #ifndef _STLP_STRING_IO_H
5 # include <stl/_string_io.h>
8 #ifndef _STLP_INTERNAL_CTYPE_H
9 # include <stl/_ctype.h>
14 template <class _CharT, class _Traits>
16 __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
17 basic_streambuf<_CharT, _Traits>* __buf,
19 _CharT __f = __os.fill();
20 for (streamsize __i = 0; __i < __n; ++__i) {
21 if (_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof()))
28 template <class _CharT, class _Traits, class _Alloc>
29 basic_ostream<_CharT, _Traits>& _STLP_CALL
30 operator << (basic_ostream<_CharT, _Traits>& __os,
31 const basic_string<_CharT,_Traits,_Alloc>& __s) {
32 typedef basic_ostream<_CharT, _Traits> __ostream;
33 typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
35 // The hypothesis of this implementation is that size_type is unsigned:
36 _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0)
38 typename __ostream::sentry __sentry(__os);
43 size_type __n = __s.size();
44 const bool __left = (__os.flags() & __ostream::left) != 0;
45 const streamsize __w = __os.width(0);
46 basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
48 const bool __need_pad = (((sizeof(streamsize) > sizeof(size_t)) && (__STATIC_CAST(streamsize, __n) < __w)) ||
49 ((sizeof(streamsize) <= sizeof(size_t)) && (__n < __STATIC_CAST(size_t, __w))));
50 streamsize __pad_len = __need_pad ? __w - __n : 0;
53 __ok = __stlp_string_fill(__os, __buf, __pad_len);
55 __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
58 __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
62 __os.setstate(__ostream::failbit);
67 template <class _CharT, class _Traits, class _Alloc>
68 basic_istream<_CharT, _Traits>& _STLP_CALL
69 operator >> (basic_istream<_CharT, _Traits>& __is,
70 basic_string<_CharT,_Traits, _Alloc>& __s) {
71 typedef basic_istream<_CharT, _Traits> __istream;
72 typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
74 // The hypothesis of this implementation is that size_type is unsigned:
75 _STLP_STATIC_ASSERT(__STATIC_CAST(size_type, -1) > 0)
77 typename __istream::sentry __sentry(__is);
80 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
81 typedef ctype<_CharT> _C_type;
83 const locale& __loc = __is.getloc();
84 const _C_type& _Ctype = use_facet<_C_type>(__loc);
86 streamsize __width = __is.width(0);
90 /* __width can only overflow size_type if sizeof(streamsize) > sizeof(size_type)
91 * because here we know that __width is positive and the stattic assertion check
92 * that size_type is unsigned.
94 else if (sizeof(streamsize) > sizeof(size_type) &&
95 (__width > __STATIC_CAST(streamsize, __s.max_size())))
98 __n = __STATIC_CAST(size_type, __width);
103 typename _Traits::int_type __c1 = __buf->sbumpc();
104 if (_Traits::eq_int_type(__c1, _Traits::eof())) {
105 __is.setstate(__istream::eofbit);
109 _CharT __c = _Traits::to_char_type(__c1);
111 if (_Ctype.is(_C_type::space, __c)) {
112 if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
113 __is.setstate(__istream::failbit);
121 // If we have read no characters, then set failbit.
123 __is.setstate(__istream::failbit);
126 __is.setstate(__istream::failbit);
131 template <class _CharT, class _Traits, class _Alloc>
132 basic_istream<_CharT, _Traits>& _STLP_CALL
133 getline(basic_istream<_CharT, _Traits>& __is,
134 basic_string<_CharT,_Traits,_Alloc>& __s,
136 typedef basic_istream<_CharT, _Traits> __istream;
137 typedef typename basic_string<_CharT, _Traits, _Alloc>::size_type size_type;
138 size_type __nread = 0;
139 typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
141 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
144 while (__nread < __s.max_size()) {
145 int __c1 = __buf->sbumpc();
146 if (_Traits::eq_int_type(__c1, _Traits::eof())) {
147 __is.setstate(__istream::eofbit);
152 _CharT __c = _Traits::to_char_type(__c1);
153 if (!_Traits::eq(__c, __delim))
156 break; // Character is extracted but not appended.
160 if (__nread == 0 || __nread >= __s.max_size())
161 __is.setstate(__istream::failbit);