1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
4 #ifndef _STLP_STRING_IO_C
5 #define _STLP_STRING_IO_C
7 #ifndef _STLP_STRING_IO_H
8 # include <stl/_string_io.h>
11 #ifndef _STLP_INTERNAL_CTYPE_H
12 # include <stl/_ctype.h>
16 # define basic_string _Nondebug_string
21 # if defined (_STLP_OWN_IOSTREAMS)
22 # define _STLP_USING_IO
24 # define _STLP_USING_IO _STLP_USING_VENDOR_STD
27 #if defined (_STLP_USE_NEW_IOSTREAMS)
29 template <class _CharT, class _Traits>
31 __stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
32 basic_streambuf<_CharT, _Traits>* __buf,
35 _CharT __f = __os.fill();
39 for (__i = 0; __i < __n; ++__i)
40 __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
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)
51 typedef basic_ostream<_CharT, _Traits> __ostream;
52 typename __ostream::sentry __sentry(__os);
57 size_t __n = __s.size();
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();
64 __pad_len = __w - __n;
68 __ok = __stlp_string_fill(__os, __buf, __pad_len);
70 __ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
73 __ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
77 __os.setstate(__ostream::failbit);
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)
88 typedef basic_istream<_CharT, _Traits> __istream;
89 typename __istream::sentry __sentry(__is);
92 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
93 typedef ctype<_CharT> _C_type;
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();
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);
106 const locale& __loc = __is.getloc();
107 const _C_type& _Ctype = use_facet<_C_type>(__loc);
111 size_t __n = __is.width(0);
113 __n = __STATIC_CAST(size_t,-1);
119 typename _Traits::int_type __c1 = __buf->sbumpc();
120 if (_Traits::eq_int_type(__c1, _Traits::eof())) {
121 __is.setstate(__istream::eofbit);
125 _CharT __c = _Traits::to_char_type(__c1);
127 if (_Ctype.is(_C_type::space, __c)) {
128 if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
129 __is.setstate(__istream::failbit);
133 else if (__c == '\b') {
142 // If we have read no characters, then set failbit.
144 __is.setstate(__istream::failbit);
147 __is.setstate(__istream::failbit);
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,
159 typedef basic_istream<_CharT, _Traits> __istream;
161 typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
163 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
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);
174 _CharT __c = _Traits::to_char_type(__c1);
175 if (!_Traits::eq(__c, __delim))
178 break; // Character is extracted but not appended.
182 if (__nread == 0 || __nread >= __s.max_size())
183 __is.setstate(__istream::failbit);
188 #elif ! defined ( _STLP_USE_NO_IOSTREAMS )
190 // (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll
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)
197 streambuf* __buf = __os.rdbuf();
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();
205 __pad_len = __w - __n;
209 __stlp_string_fill(__os, __buf, __pad_len);
211 const size_t __nwritten = __buf->sputn(__s.data(), __n);
214 __stlp_string_fill(__os, __buf, __pad_len);
216 if (__nwritten != __n)
217 __os.clear(__os.rdstate() | ios::failbit);
222 __os.clear(__os.rdstate() | ios::badbit);
227 template <class _CharT, class _Traits, class _Alloc>
228 _ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
234 streambuf* __buf = __is.rdbuf();
237 if (__is.flags() & ios::skipws) {
241 __c = __buf->sbumpc();
243 while (__c != EOF && isspace((unsigned char)__c));
246 __is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
249 if (__buf->sputbackc(__c) == EOF)
250 __is.clear(__is.rdstate() | ios::failbit);
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.
259 size_t __n = __is.width();
261 __n = __STATIC_CAST(size_t,-1);
266 int __c1 = __buf->sbumpc();
268 __is.clear(__is.rdstate() | ios::eofbit);
272 _CharT __c = _Traits::to_char_type(__c1);
274 if (isspace((unsigned char) __c)) {
275 if (__buf->sputbackc(__c) == EOF)
276 __is.clear(__is.rdstate() | ios::failbit);
284 // If we have read no characters, then set failbit.
286 __is.clear(__is.rdstate() | ios::failbit);
291 else // We have no streambuf.
292 __is.clear(__is.rdstate() | ios::badbit);
297 template <class _CharT, class _Traits, class _Alloc>
298 _ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is,
299 basic_string<_CharT,_Traits,_Alloc>& __s,
303 streambuf* __buf = __is.rdbuf();
309 while (__nread < __s.max_size()) {
310 int __c1 = __buf->sbumpc();
312 __is.clear(__is.rdstate() | ios::eofbit);
317 _CharT __c = _Traits::to_char_type(__c1);
318 if (!_Traits::eq(__c, __delim))
321 break; // Character is extracted but not appended.
326 if (__nread == 0 || __nread >= __s.max_size())
327 __is.clear(__is.rdstate() | ios::failbit);
330 __is.clear(__is.rdstate() | ios::badbit);
335 # endif /* _STLP_NEW_IOSTREAMS */
339 // # undef _STLP_USING_IO