Update contrib.
2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
20 #include "stlport_prefix.h"
24 #if defined (_STLP_MSVC) || defined (__MWERKS__) || defined (__ICL) || defined (__ISCPP__) || defined(__SYMBIAN32__)
25 # define _STLP_USE_NOT_INIT_SEGMENT
29 #if defined(__SYMBIAN32__WSD__)
30 # include "libstdcppwsd.h"
33 #include "stdio_streambuf.h"
34 #include "aligned_buffer.h"
35 #include "_stdio_file.h"
38 // boris : note this is repeated in <iostream>
39 #ifndef _STLP_USE_NAMESPACES
40 // in case of SGI iostreams, we have to rename our streams not to clash with those
41 // provided in native lib
42 # define cin _STLP_cin
43 # define cout _STLP_cout
44 # define cerr _STLP_cerr
45 # define clog _STLP_clog
50 #if defined (__BORLANDC__) && ! defined (_STLP_USE_GLIBC)
51 using _STLP_VENDOR_CSTD::_streams;
54 // This file handles iostream initialization. It is inherently
55 // nonportable, since the C++ language definition provides no mechanism
56 // for controlling order of initialization of nonlocal objects.
57 // Initialization has three parts, which must be performed in the following
59 // (1) Initialize the locale system
60 // (2) Call the constructors for the eight global stream objects.
61 // (3) Create streambufs for the global stream objects, and initialize
62 // the stream objects by calling the init() member function.
65 #if defined (_STLP_USE_NOT_INIT_SEGMENT)
67 // Definitions of the eight global I/O objects that are declared in
68 // <iostream>. For some compilers we use pragmas to put the global I/O
69 // objects into an initialization segment that will not
70 // be executed. We then explicitly invoke the constructors
71 // with placement new in ios_base::_S_initialize()
73 # if defined (__MWERKS__)
74 # pragma suppress_init_code on
76 # pragma init_seg("STLPORT_NO_INIT")
79 #if defined(__SYMBIAN32__NO_STATIC_IMPORTS__)
82 # ifdef _STLP_REDIRECT_STDSTREAMS
92 #ifndef _STLP_NO_WCHAR_T
99 _STLP_DECLSPEC ostream& get_cerr()
104 _STLP_DECLSPEC ostream& get_cout()
109 _STLP_DECLSPEC ostream& get_clog()
114 _STLP_DECLSPEC istream& get_cin()
119 # ifndef _STLP_NO_WCHAR_T
120 _STLP_DECLSPEC wostream& get_wcerr()
125 _STLP_DECLSPEC wostream& get_wcout()
130 _STLP_DECLSPEC wostream& get_wclog()
135 _STLP_DECLSPEC wistream& get_wcin()
139 #endif //_STLP_NO_WCHAR_T
142 # elif defined (__SYMBIAN32__WSD__)
144 _STLP_DECLSPEC ostream& get_cerr()
146 return *get_libcpp_wsd().wsd_cerr;
149 _STLP_DECLSPEC ostream& get_cout()
151 return *get_libcpp_wsd().wsd_cout;
154 _STLP_DECLSPEC ostream& get_clog()
156 return *get_libcpp_wsd().wsd_clog;
159 _STLP_DECLSPEC istream& get_cin()
161 return *get_libcpp_wsd().wsd_cin;
164 # ifndef _STLP_NO_WCHAR_T
165 _STLP_DECLSPEC wostream& get_wcerr()
167 return *get_libcpp_wsd().wsd_wcerr;
170 _STLP_DECLSPEC wostream& get_wcout()
172 return *get_libcpp_wsd().wsd_wcout;
175 _STLP_DECLSPEC wostream& get_wclog()
177 return *get_libcpp_wsd().wsd_wclog;
180 _STLP_DECLSPEC wistream& get_wcin()
182 return *get_libcpp_wsd().wsd_wcin;
184 #endif //_STLP_NO_WCHAR_T
187 #define _S_count get_ios_base_Init_S_count()
188 #define _S_was_synced get_ios_base_S_was_synced()
191 _STLP_DECLSPEC istream cin(0);
193 # ifdef _STLP_REDIRECT_STDSTREAMS
194 _STLP_DECLSPEC ofstream cout;
195 _STLP_DECLSPEC ofstream cerr;
196 _STLP_DECLSPEC ofstream clog;
198 _STLP_DECLSPEC ostream cout(0);
199 _STLP_DECLSPEC ostream cerr(0);
200 _STLP_DECLSPEC ostream clog(0);
203 #ifndef _STLP_NO_WCHAR_T
204 _STLP_DECLSPEC wistream wcin(0);
205 _STLP_DECLSPEC wostream wcout(0);
206 _STLP_DECLSPEC wostream wcerr(0);
207 _STLP_DECLSPEC wostream wclog(0);
209 #endif //__SYMBIAN32__NO_STATIC_IMPORTS__
211 # if defined (__MWERKS__)
212 # pragma suppress_init_code off
217 // Definitions of the eight global I/O objects that are declared in
218 // <iostream>. Disgusting hack: we deliberately define them with the
219 // wrong types so that the constructors don't get run automatically.
220 // We need special tricks to make sure that these objects are struct-
221 // aligned rather than byte-aligned.
223 // This is not portable. Declaring a variable with different types in
224 // two translations units is "undefined", according to the C++ standard.
225 // Most compilers, however, silently accept this instead of diagnosing
229 _STLP_DECLSPEC _Stl_aligned_buffer<istream> cin;
230 _STLP_DECLSPEC _Stl_aligned_buffer<ostream> cout;
231 _STLP_DECLSPEC _Stl_aligned_buffer<ostream> cerr;
232 _STLP_DECLSPEC _Stl_aligned_buffer<ostream> clog;
234 _Stl_aligned_buffer<istream> cin;
235 _Stl_aligned_buffer<ostream> cout;
236 _Stl_aligned_buffer<ostream> cerr;
237 _Stl_aligned_buffer<ostream> clog;
239 # pragma alias("?cin@std@@3V?$basic_istream@std@DV?$char_traits@std@D@1@@1@A", "?cin@std@@3T?$_Stl_aligned_buffer@std@V?$basic_istream@std@DV?$char_traits@std@D@1@@1@@1@A")
240 # pragma alias("?cout@std@@3V?$basic_ostream@std@DV?$char_traits@std@D@1@@1@A", "?cout@std@@3T?$_Stl_aligned_buffer@std@V?$basic_ostream@std@DV?$char_traits@std@D@1@@1@@1@A")
241 # pragma alias("?cerr@std@@3V?$basic_ostream@std@DV?$char_traits@std@D@1@@1@A", "?cerr@std@@3T?$_Stl_aligned_buffer@std@V?$basic_ostream@std@DV?$char_traits@std@D@1@@1@@1@A")
242 # pragma alias("?clog@std@@3V?$basic_ostream@std@DV?$char_traits@std@D@1@@1@A", "?clog@std@@3T?$_Stl_aligned_buffer@std@V?$basic_ostream@std@DV?$char_traits@std@D@1@@1@@1@A")
245 # ifndef _STLP_NO_WCHAR_T
248 _STLP_DECLSPEC _Stl_aligned_buffer<wistream> wcin;
249 _STLP_DECLSPEC _Stl_aligned_buffer<wostream> wcout;
250 _STLP_DECLSPEC _Stl_aligned_buffer<wostream> wcerr;
251 _STLP_DECLSPEC _Stl_aligned_buffer<wostream> wclog;
253 _Stl_aligned_buffer<wistream> wcin;
254 _Stl_aligned_buffer<wostream> wcout;
255 _Stl_aligned_buffer<wostream> wcerr;
256 _Stl_aligned_buffer<wostream> wclog;
258 # pragma alias("?wcin@std@@3V?$basic_istream@std@_YV?$char_traits@std@_Y@1@@1@A", "?wcin@std@@3T?$_Stl_aligned_buffer@std@V?$basic_istream@std@_YV?$char_traits@std@_Y@1@@1@@1@A")
259 # pragma alias("?wcout@std@@3V?$basic_ostream@std@_YV?$char_traits@std@_Y@1@@1@A", "?wcout@std@@3T?$_Stl_aligned_buffer@std@V?$basic_ostream@std@_YV?$char_traits@std@_Y@1@@1@@1@A")
260 # pragma alias("?wcerr@std@@3V?$basic_ostream@std@_YV?$char_traits@std@_Y@1@@1@A", "?wcerr@std@@3T?$_Stl_aligned_buffer@std@V?$basic_ostream@std@_YV?$char_traits@std@_Y@1@@1@@1@A")
261 # pragma alias("?wclog@std@@3V?$basic_ostream@std@_YV?$char_traits@std@_Y@1@@1@A", "?wclog@std@@3T?$_Stl_aligned_buffer@std@V?$basic_ostream@std@_YV?$char_traits@std@_Y@1@@1@@1@A")
264 #endif /* STL_MSVC || __MWERKS__ */
266 // Member functions from class ios_base and ios_base::Init
267 #if !defined(__SYMBIAN32__WSD__)
268 long ios_base::Init::_S_count = 0;
269 // by default, those are synced
270 bool ios_base::_S_was_synced = true;
271 # endif //__SYMBIAN32__WSD__
273 _STLP_DECLSPEC ios_base::Init::Init() {
274 if (_S_count++ == 0) {
276 ios_base::_S_initialize();
277 _Filebuf_base::_S_initialize();
281 _STLP_DECLSPEC ios_base::Init::~Init() {
282 if (--_S_count == 0) {
283 ios_base::_S_uninitialize();
290 #define _FILE_fd fileno
294 _Stl_create_filebuf(FILE* f, ios_base::openmode mode ) {
295 basic_filebuf<char, char_traits<char> >* result =
296 new basic_filebuf<char, char_traits<char> >();
299 result->_M_open(_FILE_fd(f), mode);
303 if (!result->is_open()) {
310 #if !defined (_STLP_NO_WCHAR_T)
312 _Stl_create_wfilebuf(FILE* f, ios_base::openmode mode) {
313 basic_filebuf<wchar_t, char_traits<wchar_t> >* result =
314 new basic_filebuf<wchar_t, char_traits<wchar_t> >();
317 result->_M_open(_FILE_fd(f), mode);
321 if (!result->is_open()) {
329 void _STLP_CALL ios_base::_S_initialize() {
330 #if !defined (_STLP_HAS_NO_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE)
331 using _STLP_PRIV stdio_istreambuf;
332 using _STLP_PRIV stdio_ostreambuf;
335 istream* ptr_cin = new(static_cast<void*>(&cin)) istream(0);
336 # ifdef _STLP_REDIRECT_STDSTREAMS
337 ofstream* ptr_cout = new(static_cast<void*>(&cout)) ofstream;
338 ofstream* ptr_cerr = new(static_cast<void*>(&cerr)) ofstream;
339 ofstream* ptr_clog = new(static_cast<void*>(&clog)) ofstream;
341 // Initialize the four narrow stream objects.
343 ptr_cin->init(new stdio_istreambuf(stdin));
344 ptr_cout->open("/stdout.txt", ios::out);
345 ptr_cerr->open("/stderr.txt", ios::out);
346 ptr_clog->open("/stdlog.txt", ios::out);
348 ptr_cin->init(_Stl_create_filebuf(stdin, ios_base::in));
349 ptr_cin->init(_Stl_create_filebuf(stdout, ios_base::out));
350 ptr_cin->init(_Stl_create_filebuf(stderr, ios_base::out));
351 ptr_cin->init(_Stl_create_filebuf(stderr, ios_base::out));
353 ptr_cin->tie(ptr_cout);
354 ptr_cerr->setf(ios_base::unitbuf);
356 ostream* ptr_cout = new(static_cast<void*>(&cout)) ostream(0);
357 ostream* ptr_cerr = new(static_cast<void*>(&cerr)) ostream(0);
358 ostream* ptr_clog = new(static_cast<void*>(&clog)) ostream(0);
360 // Initialize the four narrow stream objects.
362 ptr_cin->init(new stdio_istreambuf(stdin));
363 ptr_cout->init(new stdio_ostreambuf(stdout));
364 ptr_cerr->init(new stdio_ostreambuf(stdout));
365 ptr_clog->init(new stdio_ostreambuf(stdout));
367 ptr_cin->init(_Stl_create_filebuf(stdin, ios_base::in));
368 ptr_cin->init(_Stl_create_filebuf(stdout, ios_base::out));
369 ptr_cin->init(_Stl_create_filebuf(stderr, ios_base::out));
370 ptr_cin->init(_Stl_create_filebuf(stderr, ios_base::out));
372 ptr_cin->tie(ptr_cout);
373 ptr_cerr->setf(ios_base::unitbuf);
374 # endif /* _STLP_REDIRECT_STDSTREAMS */
376 # ifndef _STLP_NO_WCHAR_T
377 // Run constructors for the four wide stream objects.
378 wistream* ptr_wcin = new(&wcin) wistream(0);
379 wostream* ptr_wcout = new(&wcout) wostream(0);
380 wostream* ptr_wcerr = new(&wcerr) wostream(0);
381 wostream* ptr_wclog = new(&wclog) wostream(0);
383 wfilebuf* win = _Stl_create_wfilebuf(stdin, ios_base::in);
384 wfilebuf* wout = _Stl_create_wfilebuf(stdout, ios_base::out);
385 wfilebuf* werr = _Stl_create_wfilebuf(stdout, ios_base::out);
386 wfilebuf* wlog = _Stl_create_wfilebuf(stdout, ios_base::out);
389 ptr_wcout->init(wout);
390 ptr_wcerr->init(werr);
391 ptr_wclog->init(wlog);
393 ptr_wcin->tie(ptr_wcout);
394 ptr_wcerr->setf(ios_base::unitbuf);
396 # endif /* _STLP_NO_WCHAR_T */
402 void _STLP_CALL ios_base::_S_uninitialize() {
403 // Note that destroying output streambufs flushes the buffers.
405 istream* ptr_cin = &cin;
406 ostream* ptr_cout = &cout;
407 ostream* ptr_cerr = &cerr;
408 ostream* ptr_clog = &clog;
410 #ifndef _STLP_NO_WCHAR_T
411 wistream* ptr_wcin = &wcin;
412 wostream* ptr_wcout = &wcout;
413 wostream* ptr_wcerr = &wcerr;
414 wostream* ptr_wclog = &wclog;
417 // we don't want any exceptions being thrown here
418 ptr_cin->exceptions(0);
419 ptr_cout->exceptions(0);
420 ptr_cerr->exceptions(0);
421 ptr_clog->exceptions(0);
423 delete ptr_cin->rdbuf(0);
424 delete ptr_cout->rdbuf(0);
425 delete ptr_cerr->rdbuf(0);
426 delete ptr_clog->rdbuf(0);
433 #ifndef _STLP_NO_WCHAR_T
434 // we don't want any exceptions being thrown here
435 ptr_wcin->exceptions(0);
436 ptr_wcout->exceptions(0);
437 ptr_wcerr->exceptions(0);
438 ptr_wclog->exceptions(0);
440 delete ptr_wcin->rdbuf(0);
441 delete ptr_wcout->rdbuf(0);
442 delete ptr_wcerr->rdbuf(0);
443 delete ptr_wclog->rdbuf(0);
453 _STLP_DECLSPEC bool _STLP_CALL ios_base::sync_with_stdio(bool sync) {
454 # if !defined (_STLP_HAS_NO_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE)
455 using _STLP_PRIV stdio_istreambuf;
456 using _STLP_PRIV stdio_ostreambuf;
459 bool was_synced = _S_was_synced;
461 // if by any chance we got there before std streams initialization,
462 // just set the sync flag and exit
463 if (Init::_S_count == 0) {
464 _S_was_synced = sync;
468 istream* ptr_cin = &cin;
469 ostream* ptr_cout = &cout;
470 ostream* ptr_cerr = &cerr;
471 ostream* ptr_clog = &clog;
473 streambuf* old_cin = ptr_cin->rdbuf();
474 streambuf* old_cout = ptr_cout->rdbuf();
475 streambuf* old_cerr = ptr_cerr->rdbuf();
476 streambuf* old_clog = ptr_clog->rdbuf();
478 streambuf* new_cin = 0;
479 streambuf* new_cout = 0;
480 streambuf* new_cerr = 0;
481 streambuf* new_clog = 0;
484 if (sync && !was_synced) {
485 new_cin = new stdio_istreambuf(stdin);
486 new_cout = new stdio_ostreambuf(stdout);
487 new_cerr = new stdio_ostreambuf(stdout);
488 new_clog = new stdio_ostreambuf(stdout);
490 else if (!sync && was_synced) {
491 new_cin = _Stl_create_filebuf(stdin, ios_base::in);
492 new_cout = _Stl_create_filebuf(stdout, ios_base::out);
493 new_cerr = _Stl_create_filebuf(stdout, ios_base::out);
494 new_clog = _Stl_create_filebuf(stdout, ios_base::out);
499 if (new_cin && new_cout && new_cerr && new_clog) {
500 ptr_cin->rdbuf(new_cin);
501 ptr_cout->rdbuf(new_cout);
502 ptr_cerr->rdbuf(new_cerr);
503 ptr_clog->rdbuf(new_clog);