1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/stlport/src/iostream.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,354 @@ 1.4 +/* 1.5 + * Copyright (c) 1999 1.6 + * Silicon Graphics Computer Systems, Inc. 1.7 + * 1.8 + * Copyright (c) 1999 1.9 + * Boris Fomitchev 1.10 + * 1.11 + * This material is provided "as is", with absolutely no warranty expressed 1.12 + * or implied. Any use is at your own risk. 1.13 + * 1.14 + * Permission to use or copy this software for any purpose is hereby granted 1.15 + * without fee, provided the above notices are retained on all copies. 1.16 + * Permission to modify the code and to distribute modified code is granted, 1.17 + * provided the above notices are retained, and a notice that the code was 1.18 + * modified is included with the above copyright notice. 1.19 + * 1.20 + */ 1.21 +#include "stlport_prefix.h" 1.22 + 1.23 +#include <memory> 1.24 +#include <istream> 1.25 +#include <fstream> 1.26 +#if defined (_STLP_MSVC) || defined (__MWERKS__) || defined (__ICL) || defined (__ISCPP__) 1.27 +# define _STLP_USE_NOT_INIT_SEGMENT 1.28 +# include <iostream> 1.29 +#endif 1.30 + 1.31 +#include "stdio_streambuf.h" 1.32 +#include "aligned_buffer.h" 1.33 +#include "_stdio_file.h" 1.34 +#include "c_locale.h" 1.35 + 1.36 +// boris : note this is repeated in <iostream> 1.37 +#ifndef _STLP_USE_NAMESPACES 1.38 +// in case of SGI iostreams, we have to rename our streams not to clash with those 1.39 +// provided in native lib 1.40 +# define cin _STLP_cin 1.41 +# define cout _STLP_cout 1.42 +# define cerr _STLP_cerr 1.43 +# define clog _STLP_clog 1.44 +#endif 1.45 + 1.46 +_STLP_BEGIN_NAMESPACE 1.47 + 1.48 +// This file handles iostream initialization. It is inherently 1.49 +// nonportable, since the C++ language definition provides no mechanism 1.50 +// for controlling order of initialization of nonlocal objects. 1.51 +// Initialization has three parts, which must be performed in the following 1.52 +// order: 1.53 +// (1) Initialize the locale system 1.54 +// (2) Call the constructors for the eight global stream objects. 1.55 +// (3) Create streambufs for the global stream objects, and initialize 1.56 +// the stream objects by calling the init() member function. 1.57 + 1.58 + 1.59 +#if defined (_STLP_USE_NOT_INIT_SEGMENT) 1.60 + 1.61 +// Definitions of the eight global I/O objects that are declared in 1.62 +// <iostream>. For some compilers we use pragmas to put the global I/O 1.63 +// objects into an initialization segment that will not 1.64 +// be executed. We then explicitly invoke the constructors 1.65 +// with placement new in ios_base::_S_initialize() 1.66 + 1.67 +# if defined (__MWERKS__) 1.68 +# pragma suppress_init_code on 1.69 +# else 1.70 +# pragma init_seg("STLPORT_NO_INIT") 1.71 +# endif 1.72 + 1.73 +_STLP_DECLSPEC istream cin(0); 1.74 +_STLP_DECLSPEC ostream cout(0); 1.75 +_STLP_DECLSPEC ostream cerr(0); 1.76 +_STLP_DECLSPEC ostream clog(0); 1.77 + 1.78 +# ifndef _STLP_NO_WCHAR_T 1.79 +_STLP_DECLSPEC wistream wcin(0); 1.80 +_STLP_DECLSPEC wostream wcout(0); 1.81 +_STLP_DECLSPEC wostream wcerr(0); 1.82 +_STLP_DECLSPEC wostream wclog(0); 1.83 +# endif 1.84 + 1.85 +# if defined (__MWERKS__) 1.86 +# pragma suppress_init_code off 1.87 +# endif 1.88 + 1.89 +#else 1.90 + 1.91 +// Definitions of the eight global I/O objects that are declared in 1.92 +// <iostream>. Disgusting hack: we deliberately define them with the 1.93 +// wrong types so that the constructors don't get run automatically. 1.94 +// We need special tricks to make sure that these objects are struct- 1.95 +// aligned rather than byte-aligned. 1.96 + 1.97 +// This is not portable. Declaring a variable with different types in 1.98 +// two translations units is "undefined", according to the C++ standard. 1.99 +// Most compilers, however, silently accept this instead of diagnosing 1.100 +// it as an error. 1.101 + 1.102 +# ifndef __DMC__ 1.103 +_STLP_DECLSPEC _Stl_aligned_buffer<istream> cin; 1.104 +_STLP_DECLSPEC _Stl_aligned_buffer<ostream> cout; 1.105 +_STLP_DECLSPEC _Stl_aligned_buffer<ostream> cerr; 1.106 +_STLP_DECLSPEC _Stl_aligned_buffer<ostream> clog; 1.107 +# else 1.108 +_Stl_aligned_buffer<istream> cin; 1.109 +_Stl_aligned_buffer<ostream> cout; 1.110 +_Stl_aligned_buffer<ostream> cerr; 1.111 +_Stl_aligned_buffer<ostream> clog; 1.112 + 1.113 +# 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") 1.114 +# 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") 1.115 +# 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") 1.116 +# 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") 1.117 +# endif 1.118 + 1.119 +# ifndef _STLP_NO_WCHAR_T 1.120 + 1.121 +# ifndef __DMC__ 1.122 +_STLP_DECLSPEC _Stl_aligned_buffer<wistream> wcin; 1.123 +_STLP_DECLSPEC _Stl_aligned_buffer<wostream> wcout; 1.124 +_STLP_DECLSPEC _Stl_aligned_buffer<wostream> wcerr; 1.125 +_STLP_DECLSPEC _Stl_aligned_buffer<wostream> wclog; 1.126 +# else 1.127 +_Stl_aligned_buffer<wistream> wcin; 1.128 +_Stl_aligned_buffer<wostream> wcout; 1.129 +_Stl_aligned_buffer<wostream> wcerr; 1.130 +_Stl_aligned_buffer<wostream> wclog; 1.131 + 1.132 +# 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") 1.133 +# 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") 1.134 +# 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") 1.135 +# 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") 1.136 +# endif 1.137 +# endif 1.138 +#endif /* STL_MSVC || __MWERKS__ */ 1.139 + 1.140 +// Member functions from class ios_base and ios_base::Init 1.141 + 1.142 +long ios_base::Init::_S_count = 0; 1.143 +// by default, those are synced 1.144 +bool ios_base::_S_is_synced = true; 1.145 + 1.146 +ios_base::Init::Init() { 1.147 + if (_S_count++ == 0) { 1.148 + _Locale_init(); 1.149 + ios_base::_S_initialize(); 1.150 + _Filebuf_base::_S_initialize(); 1.151 + } 1.152 +} 1.153 + 1.154 +ios_base::Init::~Init() { 1.155 + if (--_S_count == 0) { 1.156 + ios_base::_S_uninitialize(); 1.157 + _Locale_final(); 1.158 + } 1.159 +} 1.160 + 1.161 +static int _Stl_extract_open_param(FILE* f) 1.162 +{ return _FILE_fd(f); } 1.163 + 1.164 +#ifdef _STLP_REDIRECT_STDSTREAMS 1.165 +static const char* _Stl_extract_open_param(const char* name) 1.166 +{ return name; } 1.167 +#endif 1.168 + 1.169 +template <class _Tp> 1.170 +static filebuf* 1.171 +_Stl_create_filebuf(_Tp x, ios_base::openmode mode ) { 1.172 + auto_ptr<filebuf> result(new basic_filebuf<char, char_traits<char> >()); 1.173 + result->open(_Stl_extract_open_param(x), mode); 1.174 + 1.175 + if (result->is_open()) 1.176 + return result.release(); 1.177 + 1.178 + return 0; 1.179 +} 1.180 + 1.181 +#if !defined (_STLP_NO_WCHAR_T) 1.182 +static wfilebuf* 1.183 +_Stl_create_wfilebuf(FILE* f, ios_base::openmode mode) { 1.184 + auto_ptr<wfilebuf> result(new basic_filebuf<wchar_t, char_traits<wchar_t> >()); 1.185 + result->_M_open(_FILE_fd(f), mode); 1.186 + 1.187 + if (result->is_open()) 1.188 + return result.release(); 1.189 + 1.190 + return 0; 1.191 +} 1.192 +#endif 1.193 + 1.194 +void _STLP_CALL ios_base::_S_initialize() { 1.195 +#if !defined (_STLP_HAS_NO_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE) 1.196 + using _STLP_PRIV stdio_istreambuf; 1.197 + using _STLP_PRIV stdio_ostreambuf; 1.198 +#endif 1.199 + 1.200 + auto_ptr<streambuf> cin_buf; 1.201 + auto_ptr<streambuf> cout_buf; 1.202 + auto_ptr<streambuf> cerr_buf; 1.203 + auto_ptr<streambuf> clog_buf; 1.204 + 1.205 + if (_S_is_synced) 1.206 + cin_buf.reset(new stdio_istreambuf(stdin)); 1.207 + else 1.208 + cin_buf.reset(_Stl_create_filebuf(stdin, ios_base::in)); 1.209 + 1.210 + if (_S_is_synced) { 1.211 +#ifdef _STLP_REDIRECT_STDSTREAMS 1.212 + cout_buf.reset(_Stl_create_filebuf("/stdout.txt", ios::out)); 1.213 + cerr_buf.reset(_Stl_create_filebuf("/stderr.txt", ios::out)); 1.214 + clog_buf.reset(_Stl_create_filebuf("/stdlog.txt", ios::out)); 1.215 +#else 1.216 + cout_buf.reset(new stdio_ostreambuf(stdout)); 1.217 + cerr_buf.reset(new stdio_ostreambuf(stderr)); 1.218 + clog_buf.reset(new stdio_ostreambuf(stderr)); 1.219 +#endif 1.220 + } 1.221 + else { 1.222 + cout_buf.reset(_Stl_create_filebuf(stdout, ios_base::out)); 1.223 + cerr_buf.reset(_Stl_create_filebuf(stderr, ios_base::out)); 1.224 + clog_buf.reset(_Stl_create_filebuf(stderr, ios_base::out)); 1.225 + } 1.226 + 1.227 + istream* ptr_cin = new(&cin) istream(cin_buf.get()); cin_buf.release(); 1.228 + ostream* ptr_cout = new(&cout) ostream(cout_buf.get()); cout_buf.release(); 1.229 + ostream* ptr_cerr = new(&cerr) ostream(cerr_buf.get()); cerr_buf.release(); 1.230 + /*ostream* ptr_clog = */ new(&clog) ostream(clog_buf.get()); clog_buf.release(); 1.231 + ptr_cin->tie(ptr_cout); 1.232 + ptr_cerr->setf(ios_base::unitbuf); 1.233 + 1.234 +#ifndef _STLP_NO_WCHAR_T 1.235 + auto_ptr<wfilebuf> win(_Stl_create_wfilebuf(stdin, ios_base::in)); 1.236 + auto_ptr<wfilebuf> wout(_Stl_create_wfilebuf(stdout, ios_base::out)); 1.237 + auto_ptr<wfilebuf> werr(_Stl_create_wfilebuf(stderr, ios_base::out)); 1.238 + auto_ptr<wfilebuf> wlog(_Stl_create_wfilebuf(stderr, ios_base::out)); 1.239 + 1.240 + // Run constructors for the four wide stream objects. 1.241 + wistream* ptr_wcin = new(&wcin) wistream(win.get()); win.release(); 1.242 + wostream* ptr_wcout = new(&wcout) wostream(wout.get()); wout.release(); 1.243 + wostream* ptr_wcerr = new(&wcerr) wostream(werr.get()); werr.release(); 1.244 + /*wostream* ptr_wclog = */ new(&wclog) wostream(wlog.get()); wlog.release(); 1.245 + 1.246 + ptr_wcin->tie(ptr_wcout); 1.247 + ptr_wcerr->setf(ios_base::unitbuf); 1.248 +#endif 1.249 +} 1.250 + 1.251 +void _STLP_CALL ios_base::_S_uninitialize() { 1.252 + // Note that destroying output streambufs flushes the buffers. 1.253 + istream* ptr_cin = &cin; 1.254 + ostream* ptr_cout = &cout; 1.255 + ostream* ptr_cerr = &cerr; 1.256 + ostream* ptr_clog = &clog; 1.257 + 1.258 + // We don't want any exceptions being thrown here 1.259 + ptr_cin->exceptions(0); 1.260 + ptr_cout->exceptions(0); 1.261 + ptr_cerr->exceptions(0); 1.262 + ptr_clog->exceptions(0); 1.263 + 1.264 + delete ptr_cin->rdbuf(0); 1.265 + delete ptr_cout->rdbuf(0); 1.266 + delete ptr_cerr->rdbuf(0); 1.267 + delete ptr_clog->rdbuf(0); 1.268 + 1.269 + _Destroy(ptr_cin); 1.270 + _Destroy(ptr_cout); 1.271 + _Destroy(ptr_cerr); 1.272 + _Destroy(ptr_clog); 1.273 + 1.274 +#ifndef _STLP_NO_WCHAR_T 1.275 + wistream* ptr_wcin = &wcin; 1.276 + wostream* ptr_wcout = &wcout; 1.277 + wostream* ptr_wcerr = &wcerr; 1.278 + wostream* ptr_wclog = &wclog; 1.279 + 1.280 + // We don't want any exceptions being thrown here 1.281 + ptr_wcin->exceptions(0); 1.282 + ptr_wcout->exceptions(0); 1.283 + ptr_wcerr->exceptions(0); 1.284 + ptr_wclog->exceptions(0); 1.285 + 1.286 + delete ptr_wcin->rdbuf(0); 1.287 + delete ptr_wcout->rdbuf(0); 1.288 + delete ptr_wcerr->rdbuf(0); 1.289 + delete ptr_wclog->rdbuf(0); 1.290 + 1.291 + _Destroy(ptr_wcin); 1.292 + _Destroy(ptr_wcout); 1.293 + _Destroy(ptr_wcerr); 1.294 + _Destroy(ptr_wclog); 1.295 +#endif 1.296 +} 1.297 + 1.298 + 1.299 +bool _STLP_CALL ios_base::sync_with_stdio(bool sync) { 1.300 +# if !defined (_STLP_HAS_NO_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE) 1.301 + using _STLP_PRIV stdio_istreambuf; 1.302 + using _STLP_PRIV stdio_ostreambuf; 1.303 +# endif 1.304 + 1.305 + if (sync == _S_is_synced) return sync; 1.306 + 1.307 + // if by any chance we got there before std streams initialization, 1.308 + // just set the sync flag and exit 1.309 + if (Init::_S_count == 0) { 1.310 + _S_is_synced = sync; 1.311 + return sync; 1.312 + } 1.313 + 1.314 + auto_ptr<streambuf> cin_buf; 1.315 + auto_ptr<streambuf> cout_buf; 1.316 + auto_ptr<streambuf> cerr_buf; 1.317 + auto_ptr<streambuf> clog_buf; 1.318 + 1.319 + if (sync) 1.320 + cin_buf.reset(new stdio_istreambuf(stdin)); 1.321 + else 1.322 + cin_buf.reset(_Stl_create_filebuf(stdin, ios_base::in)); 1.323 + 1.324 + if (sync) { 1.325 +#ifdef _STLP_REDIRECT_STDSTREAMS 1.326 + cout_buf.reset(_Stl_create_filebuf("/stdout.txt", ios::out)); 1.327 + cerr_buf.reset(_Stl_create_filebuf("/stderr.txt", ios::out)); 1.328 + clog_buf.reset(_Stl_create_filebuf("/stdlog.txt", ios::out)); 1.329 +#else 1.330 + cout_buf.reset(new stdio_ostreambuf(stdout)); 1.331 + cerr_buf.reset(new stdio_ostreambuf(stderr)); 1.332 + clog_buf.reset(new stdio_ostreambuf(stderr)); 1.333 +#endif 1.334 + } 1.335 + else { 1.336 + cout_buf.reset(_Stl_create_filebuf(stdout, ios_base::out)); 1.337 + cerr_buf.reset(_Stl_create_filebuf(stderr, ios_base::out)); 1.338 + clog_buf.reset(_Stl_create_filebuf(stderr, ios_base::out)); 1.339 + } 1.340 + 1.341 + if (cin_buf.get() != 0 && cout_buf.get() != 0 && cerr_buf.get() != 0 && clog_buf.get() != 0) { 1.342 + // When streambuf passed to rdbuf is not null, rdbuf is exception safe: 1.343 + delete (&cin)->rdbuf(cin_buf.release()); 1.344 + delete (&cout)->rdbuf(cout_buf.release()); 1.345 + delete (&cerr)->rdbuf(cerr_buf.release()); 1.346 + delete (&clog)->rdbuf(clog_buf.release()); 1.347 + _S_is_synced = sync; 1.348 + } 1.349 + 1.350 + return _S_is_synced; 1.351 +} 1.352 + 1.353 +_STLP_END_NAMESPACE 1.354 + 1.355 +// Local Variables: 1.356 +// mode:C++ 1.357 +// End: