31
|
1 |
/*
|
|
2 |
* Copyright (c) 2008 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 |
|
|
18 |
|
|
19 |
|
|
20 |
#ifndef _STLP_STRING_IO_C
|
|
21 |
#define _STLP_STRING_IO_C
|
|
22 |
|
|
23 |
#ifndef _STLP_STRING_IO_H
|
|
24 |
# include <stl/_string_io.h>
|
|
25 |
#endif
|
|
26 |
|
|
27 |
#ifndef _STLP_INTERNAL_CTYPE_H
|
|
28 |
# include <stl/_ctype.h>
|
|
29 |
#endif
|
|
30 |
|
|
31 |
# ifdef _STLP_DEBUG
|
|
32 |
# define basic_string _Nondebug_string
|
|
33 |
# endif
|
|
34 |
|
|
35 |
_STLP_BEGIN_NAMESPACE
|
|
36 |
|
|
37 |
# if defined (_STLP_OWN_IOSTREAMS)
|
|
38 |
# define _STLP_USING_IO
|
|
39 |
# else
|
|
40 |
# define _STLP_USING_IO _STLP_USING_VENDOR_STD
|
|
41 |
# endif
|
|
42 |
|
|
43 |
#if defined (_STLP_USE_NEW_IOSTREAMS)
|
|
44 |
|
|
45 |
template <class _CharT, class _Traits>
|
|
46 |
bool _STLP_CALL
|
|
47 |
__stlp_string_fill(basic_ostream<_CharT, _Traits>& __os,
|
|
48 |
basic_streambuf<_CharT, _Traits>* __buf,
|
|
49 |
size_t __n)
|
|
50 |
{
|
|
51 |
_CharT __f = __os.fill();
|
|
52 |
size_t __i;
|
|
53 |
bool __ok = true;
|
|
54 |
|
|
55 |
for (__i = 0; __i < __n; ++__i)
|
|
56 |
__ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
|
|
57 |
return __ok;
|
|
58 |
}
|
|
59 |
|
|
60 |
template <class _CharT, class _Traits, class _Alloc>
|
|
61 |
basic_ostream<_CharT, _Traits>& _STLP_CALL
|
|
62 |
operator<<(basic_ostream<_CharT, _Traits>& __os,
|
|
63 |
const basic_string<_CharT,_Traits,_Alloc>& __s)
|
|
64 |
{
|
|
65 |
|
|
66 |
_STLP_USING_IO
|
|
67 |
typedef basic_ostream<_CharT, _Traits> __ostream;
|
|
68 |
typename __ostream::sentry __sentry(__os);
|
|
69 |
bool __ok = false;
|
|
70 |
|
|
71 |
if (__sentry) {
|
|
72 |
__ok = true;
|
|
73 |
size_t __n = __s.size();
|
|
74 |
size_t __pad_len = 0;
|
|
75 |
const bool __left = (__os.flags() & __ostream::left) != 0;
|
|
76 |
const size_t __w = __os.width(0);
|
|
77 |
basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
|
|
78 |
|
|
79 |
if (__n < __w) {
|
|
80 |
__pad_len = __w - __n;
|
|
81 |
}
|
|
82 |
|
|
83 |
if (!__left)
|
|
84 |
__ok = __stlp_string_fill(__os, __buf, __pad_len);
|
|
85 |
|
|
86 |
__ok = __ok && (__buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n));
|
|
87 |
|
|
88 |
if (__left)
|
|
89 |
__ok = __ok && __stlp_string_fill(__os, __buf, __pad_len);
|
|
90 |
}
|
|
91 |
|
|
92 |
if (!__ok)
|
|
93 |
__os.setstate(__ostream::failbit);
|
|
94 |
|
|
95 |
return __os;
|
|
96 |
}
|
|
97 |
|
|
98 |
template <class _CharT, class _Traits, class _Alloc>
|
|
99 |
basic_istream<_CharT, _Traits>& _STLP_CALL
|
|
100 |
operator>>(basic_istream<_CharT, _Traits>& __is,
|
|
101 |
basic_string<_CharT,_Traits, _Alloc>& __s)
|
|
102 |
{
|
|
103 |
_STLP_USING_IO
|
|
104 |
typedef basic_istream<_CharT, _Traits> __istream;
|
|
105 |
typename __istream::sentry __sentry(__is);
|
|
106 |
|
|
107 |
if (__sentry) {
|
|
108 |
basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
|
|
109 |
typedef ctype<_CharT> _C_type;
|
|
110 |
|
|
111 |
#ifdef _STLP_OWN_IOSTREAMS
|
|
112 |
// const _C_type& _Ctype = use_facet<_C_type>(__loc);
|
|
113 |
const _C_type& _Ctype = *(const _C_type*)__is._M_ctype_facet();
|
|
114 |
#else
|
|
115 |
# if defined (_STLP_MSVC) && (_STLP_MSVC <= 1200 ) || defined (__ICL)
|
|
116 |
const locale& __loc = __is.getloc();
|
|
117 |
const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0, true);
|
|
118 |
# elif defined (__SUNPRO_CC)
|
|
119 |
const locale& __loc = __is.getloc();
|
|
120 |
const _C_type& _Ctype = use_facet(__loc , ( _C_type * ) 0);
|
|
121 |
# else
|
|
122 |
const locale& __loc = __is.getloc();
|
|
123 |
const _C_type& _Ctype = use_facet<_C_type>(__loc);
|
|
124 |
# endif
|
|
125 |
#endif
|
|
126 |
__s.clear();
|
|
127 |
size_t __n = __is.width(0);
|
|
128 |
if (__n == 0)
|
|
129 |
__n = __STATIC_CAST(size_t,-1);
|
|
130 |
else
|
|
131 |
__s.reserve(__n);
|
|
132 |
|
|
133 |
|
|
134 |
while (__n-- > 0) {
|
|
135 |
typename _Traits::int_type __c1 = __buf->sbumpc();
|
|
136 |
if (_Traits::eq_int_type(__c1, _Traits::eof())) {
|
|
137 |
__is.setstate(__istream::eofbit);
|
|
138 |
break;
|
|
139 |
}
|
|
140 |
else {
|
|
141 |
_CharT __c = _Traits::to_char_type(__c1);
|
|
142 |
|
|
143 |
if (_Ctype.is(_C_type::space, __c)) {
|
|
144 |
if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
|
|
145 |
__is.setstate(__istream::failbit);
|
|
146 |
break;
|
|
147 |
}
|
|
148 |
#ifdef __SYMBIAN32__
|
|
149 |
else if (__c == '\b') {
|
|
150 |
__s.pop_back();
|
|
151 |
}
|
|
152 |
#endif
|
|
153 |
else
|
|
154 |
__s.push_back(__c);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
// If we have read no characters, then set failbit.
|
|
159 |
if (__s.size() == 0)
|
|
160 |
__is.setstate(__istream::failbit);
|
|
161 |
}
|
|
162 |
else
|
|
163 |
__is.setstate(__istream::failbit);
|
|
164 |
|
|
165 |
return __is;
|
|
166 |
}
|
|
167 |
|
|
168 |
template <class _CharT, class _Traits, class _Alloc>
|
|
169 |
basic_istream<_CharT, _Traits>& _STLP_CALL
|
|
170 |
getline(basic_istream<_CharT, _Traits>& __is,
|
|
171 |
basic_string<_CharT,_Traits,_Alloc>& __s,
|
|
172 |
_CharT __delim)
|
|
173 |
{
|
|
174 |
_STLP_USING_IO
|
|
175 |
typedef basic_istream<_CharT, _Traits> __istream;
|
|
176 |
size_t __nread = 0;
|
|
177 |
typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
|
|
178 |
if (__sentry) {
|
|
179 |
basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
|
|
180 |
__s.clear();
|
|
181 |
|
|
182 |
while (__nread < __s.max_size()) {
|
|
183 |
int __c1 = __buf->sbumpc();
|
|
184 |
if (_Traits::eq_int_type(__c1, _Traits::eof())) {
|
|
185 |
__is.setstate(__istream::eofbit);
|
|
186 |
break;
|
|
187 |
}
|
|
188 |
else {
|
|
189 |
++__nread;
|
|
190 |
_CharT __c = _Traits::to_char_type(__c1);
|
|
191 |
if (!_Traits::eq(__c, __delim))
|
|
192 |
__s.push_back(__c);
|
|
193 |
else
|
|
194 |
break; // Character is extracted but not appended.
|
|
195 |
}
|
|
196 |
}
|
|
197 |
}
|
|
198 |
if (__nread == 0 || __nread >= __s.max_size())
|
|
199 |
__is.setstate(__istream::failbit);
|
|
200 |
|
|
201 |
return __is;
|
|
202 |
}
|
|
203 |
|
|
204 |
#elif ! defined ( _STLP_USE_NO_IOSTREAMS )
|
|
205 |
|
|
206 |
// (reg) For Watcom IO, _OSTREAM_DLL tells if ostream class is in .exe or in .dll
|
|
207 |
|
|
208 |
template <class _CharT, class _Traits, class _Alloc>
|
|
209 |
_OSTREAM_DLL& _STLP_CALL operator<<(_OSTREAM_DLL& __os,
|
|
210 |
const basic_string<_CharT,_Traits,_Alloc>& __s)
|
|
211 |
{
|
|
212 |
_STLP_USING_IO
|
|
213 |
streambuf* __buf = __os.rdbuf();
|
|
214 |
if (__buf) {
|
|
215 |
size_t __n = __s.size();
|
|
216 |
size_t __pad_len = 0;
|
|
217 |
const bool __left = (__os.flags() & ios::left) !=0;
|
|
218 |
const size_t __w = __os.width();
|
|
219 |
|
|
220 |
if (__n < __w) {
|
|
221 |
__pad_len = __w - __n;
|
|
222 |
}
|
|
223 |
|
|
224 |
if (!__left)
|
|
225 |
__stlp_string_fill(__os, __buf, __pad_len);
|
|
226 |
|
|
227 |
const size_t __nwritten = __buf->sputn(__s.data(), __n);
|
|
228 |
|
|
229 |
if (__left)
|
|
230 |
__stlp_string_fill(__os, __buf, __pad_len);
|
|
231 |
|
|
232 |
if (__nwritten != __n)
|
|
233 |
__os.clear(__os.rdstate() | ios::failbit);
|
|
234 |
|
|
235 |
__os.width(0);
|
|
236 |
}
|
|
237 |
else
|
|
238 |
__os.clear(__os.rdstate() | ios::badbit);
|
|
239 |
|
|
240 |
return __os;
|
|
241 |
}
|
|
242 |
|
|
243 |
template <class _CharT, class _Traits, class _Alloc>
|
|
244 |
_ISTREAM_DLL& _STLP_CALL operator>>(_ISTREAM_DLL& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
|
|
245 |
{
|
|
246 |
_STLP_USING_IO
|
|
247 |
if (!__is)
|
|
248 |
return __is;
|
|
249 |
|
|
250 |
streambuf* __buf = __is.rdbuf();
|
|
251 |
if (__buf) {
|
|
252 |
|
|
253 |
if (__is.flags() & ios::skipws) {
|
|
254 |
// _CharT __c;
|
|
255 |
int __c;
|
|
256 |
do {
|
|
257 |
__c = __buf->sbumpc();
|
|
258 |
}
|
|
259 |
while (__c != EOF && isspace((unsigned char)__c));
|
|
260 |
|
|
261 |
if (__c == EOF) {
|
|
262 |
__is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
|
|
263 |
}
|
|
264 |
else {
|
|
265 |
if (__buf->sputbackc(__c) == EOF)
|
|
266 |
__is.clear(__is.rdstate() | ios::failbit);
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
// If we arrive at end of file (or fail for some other reason) while
|
|
271 |
// still discarding whitespace, then we don't try to read the string.
|
|
272 |
if (__is) {
|
|
273 |
__s.clear();
|
|
274 |
|
|
275 |
size_t __n = __is.width();
|
|
276 |
if (__n == 0)
|
|
277 |
__n = __STATIC_CAST(size_t,-1);
|
|
278 |
else
|
|
279 |
__s.reserve(__n);
|
|
280 |
|
|
281 |
while (__n-- > 0) {
|
|
282 |
int __c1 = __buf->sbumpc();
|
|
283 |
if (__c1 == EOF) {
|
|
284 |
__is.clear(__is.rdstate() | ios::eofbit);
|
|
285 |
break;
|
|
286 |
}
|
|
287 |
else {
|
|
288 |
_CharT __c = _Traits::to_char_type(__c1);
|
|
289 |
|
|
290 |
if (isspace((unsigned char) __c)) {
|
|
291 |
if (__buf->sputbackc(__c) == EOF)
|
|
292 |
__is.clear(__is.rdstate() | ios::failbit);
|
|
293 |
break;
|
|
294 |
}
|
|
295 |
else
|
|
296 |
__s.push_back(__c);
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
// If we have read no characters, then set failbit.
|
|
301 |
if (__s.size() == 0)
|
|
302 |
__is.clear(__is.rdstate() | ios::failbit);
|
|
303 |
}
|
|
304 |
|
|
305 |
__is.width(0);
|
|
306 |
}
|
|
307 |
else // We have no streambuf.
|
|
308 |
__is.clear(__is.rdstate() | ios::badbit);
|
|
309 |
|
|
310 |
return __is;
|
|
311 |
}
|
|
312 |
|
|
313 |
template <class _CharT, class _Traits, class _Alloc>
|
|
314 |
_ISTREAM_DLL& _STLP_CALL getline(_ISTREAM_DLL& __is,
|
|
315 |
basic_string<_CharT,_Traits,_Alloc>& __s,
|
|
316 |
_CharT __delim)
|
|
317 |
{
|
|
318 |
_STLP_USING_IO
|
|
319 |
streambuf* __buf = __is.rdbuf();
|
|
320 |
if (__buf) {
|
|
321 |
size_t __nread = 0;
|
|
322 |
if (__is) {
|
|
323 |
__s.clear();
|
|
324 |
|
|
325 |
while (__nread < __s.max_size()) {
|
|
326 |
int __c1 = __buf->sbumpc();
|
|
327 |
if (__c1 == EOF) {
|
|
328 |
__is.clear(__is.rdstate() | ios::eofbit);
|
|
329 |
break;
|
|
330 |
}
|
|
331 |
else {
|
|
332 |
++__nread;
|
|
333 |
_CharT __c = _Traits::to_char_type(__c1);
|
|
334 |
if (!_Traits::eq(__c, __delim))
|
|
335 |
__s.push_back(__c);
|
|
336 |
else
|
|
337 |
break; // Character is extracted but not appended.
|
|
338 |
}
|
|
339 |
}
|
|
340 |
}
|
|
341 |
|
|
342 |
if (__nread == 0 || __nread >= __s.max_size())
|
|
343 |
__is.clear(__is.rdstate() | ios::failbit);
|
|
344 |
}
|
|
345 |
else
|
|
346 |
__is.clear(__is.rdstate() | ios::badbit);
|
|
347 |
|
|
348 |
return __is;
|
|
349 |
}
|
|
350 |
|
|
351 |
# endif /* _STLP_NEW_IOSTREAMS */
|
|
352 |
|
|
353 |
_STLP_END_NAMESPACE
|
|
354 |
|
|
355 |
// # undef _STLP_USING_IO
|
|
356 |
# undef basic_string
|
|
357 |
|
|
358 |
#endif
|