build/stlport/src/iostream.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright (c) 1999
michael@0 3 * Silicon Graphics Computer Systems, Inc.
michael@0 4 *
michael@0 5 * Copyright (c) 1999
michael@0 6 * Boris Fomitchev
michael@0 7 *
michael@0 8 * This material is provided "as is", with absolutely no warranty expressed
michael@0 9 * or implied. Any use is at your own risk.
michael@0 10 *
michael@0 11 * Permission to use or copy this software for any purpose is hereby granted
michael@0 12 * without fee, provided the above notices are retained on all copies.
michael@0 13 * Permission to modify the code and to distribute modified code is granted,
michael@0 14 * provided the above notices are retained, and a notice that the code was
michael@0 15 * modified is included with the above copyright notice.
michael@0 16 *
michael@0 17 */
michael@0 18 #include "stlport_prefix.h"
michael@0 19
michael@0 20 #include <memory>
michael@0 21 #include <istream>
michael@0 22 #include <fstream>
michael@0 23 #if defined (_STLP_MSVC) || defined (__MWERKS__) || defined (__ICL) || defined (__ISCPP__)
michael@0 24 # define _STLP_USE_NOT_INIT_SEGMENT
michael@0 25 # include <iostream>
michael@0 26 #endif
michael@0 27
michael@0 28 #include "stdio_streambuf.h"
michael@0 29 #include "aligned_buffer.h"
michael@0 30 #include "_stdio_file.h"
michael@0 31 #include "c_locale.h"
michael@0 32
michael@0 33 // boris : note this is repeated in <iostream>
michael@0 34 #ifndef _STLP_USE_NAMESPACES
michael@0 35 // in case of SGI iostreams, we have to rename our streams not to clash with those
michael@0 36 // provided in native lib
michael@0 37 # define cin _STLP_cin
michael@0 38 # define cout _STLP_cout
michael@0 39 # define cerr _STLP_cerr
michael@0 40 # define clog _STLP_clog
michael@0 41 #endif
michael@0 42
michael@0 43 _STLP_BEGIN_NAMESPACE
michael@0 44
michael@0 45 // This file handles iostream initialization. It is inherently
michael@0 46 // nonportable, since the C++ language definition provides no mechanism
michael@0 47 // for controlling order of initialization of nonlocal objects.
michael@0 48 // Initialization has three parts, which must be performed in the following
michael@0 49 // order:
michael@0 50 // (1) Initialize the locale system
michael@0 51 // (2) Call the constructors for the eight global stream objects.
michael@0 52 // (3) Create streambufs for the global stream objects, and initialize
michael@0 53 // the stream objects by calling the init() member function.
michael@0 54
michael@0 55
michael@0 56 #if defined (_STLP_USE_NOT_INIT_SEGMENT)
michael@0 57
michael@0 58 // Definitions of the eight global I/O objects that are declared in
michael@0 59 // <iostream>. For some compilers we use pragmas to put the global I/O
michael@0 60 // objects into an initialization segment that will not
michael@0 61 // be executed. We then explicitly invoke the constructors
michael@0 62 // with placement new in ios_base::_S_initialize()
michael@0 63
michael@0 64 # if defined (__MWERKS__)
michael@0 65 # pragma suppress_init_code on
michael@0 66 # else
michael@0 67 # pragma init_seg("STLPORT_NO_INIT")
michael@0 68 # endif
michael@0 69
michael@0 70 _STLP_DECLSPEC istream cin(0);
michael@0 71 _STLP_DECLSPEC ostream cout(0);
michael@0 72 _STLP_DECLSPEC ostream cerr(0);
michael@0 73 _STLP_DECLSPEC ostream clog(0);
michael@0 74
michael@0 75 # ifndef _STLP_NO_WCHAR_T
michael@0 76 _STLP_DECLSPEC wistream wcin(0);
michael@0 77 _STLP_DECLSPEC wostream wcout(0);
michael@0 78 _STLP_DECLSPEC wostream wcerr(0);
michael@0 79 _STLP_DECLSPEC wostream wclog(0);
michael@0 80 # endif
michael@0 81
michael@0 82 # if defined (__MWERKS__)
michael@0 83 # pragma suppress_init_code off
michael@0 84 # endif
michael@0 85
michael@0 86 #else
michael@0 87
michael@0 88 // Definitions of the eight global I/O objects that are declared in
michael@0 89 // <iostream>. Disgusting hack: we deliberately define them with the
michael@0 90 // wrong types so that the constructors don't get run automatically.
michael@0 91 // We need special tricks to make sure that these objects are struct-
michael@0 92 // aligned rather than byte-aligned.
michael@0 93
michael@0 94 // This is not portable. Declaring a variable with different types in
michael@0 95 // two translations units is "undefined", according to the C++ standard.
michael@0 96 // Most compilers, however, silently accept this instead of diagnosing
michael@0 97 // it as an error.
michael@0 98
michael@0 99 # ifndef __DMC__
michael@0 100 _STLP_DECLSPEC _Stl_aligned_buffer<istream> cin;
michael@0 101 _STLP_DECLSPEC _Stl_aligned_buffer<ostream> cout;
michael@0 102 _STLP_DECLSPEC _Stl_aligned_buffer<ostream> cerr;
michael@0 103 _STLP_DECLSPEC _Stl_aligned_buffer<ostream> clog;
michael@0 104 # else
michael@0 105 _Stl_aligned_buffer<istream> cin;
michael@0 106 _Stl_aligned_buffer<ostream> cout;
michael@0 107 _Stl_aligned_buffer<ostream> cerr;
michael@0 108 _Stl_aligned_buffer<ostream> clog;
michael@0 109
michael@0 110 # 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")
michael@0 111 # 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")
michael@0 112 # 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")
michael@0 113 # 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")
michael@0 114 # endif
michael@0 115
michael@0 116 # ifndef _STLP_NO_WCHAR_T
michael@0 117
michael@0 118 # ifndef __DMC__
michael@0 119 _STLP_DECLSPEC _Stl_aligned_buffer<wistream> wcin;
michael@0 120 _STLP_DECLSPEC _Stl_aligned_buffer<wostream> wcout;
michael@0 121 _STLP_DECLSPEC _Stl_aligned_buffer<wostream> wcerr;
michael@0 122 _STLP_DECLSPEC _Stl_aligned_buffer<wostream> wclog;
michael@0 123 # else
michael@0 124 _Stl_aligned_buffer<wistream> wcin;
michael@0 125 _Stl_aligned_buffer<wostream> wcout;
michael@0 126 _Stl_aligned_buffer<wostream> wcerr;
michael@0 127 _Stl_aligned_buffer<wostream> wclog;
michael@0 128
michael@0 129 # 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")
michael@0 130 # 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")
michael@0 131 # 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")
michael@0 132 # 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")
michael@0 133 # endif
michael@0 134 # endif
michael@0 135 #endif /* STL_MSVC || __MWERKS__ */
michael@0 136
michael@0 137 // Member functions from class ios_base and ios_base::Init
michael@0 138
michael@0 139 long ios_base::Init::_S_count = 0;
michael@0 140 // by default, those are synced
michael@0 141 bool ios_base::_S_is_synced = true;
michael@0 142
michael@0 143 ios_base::Init::Init() {
michael@0 144 if (_S_count++ == 0) {
michael@0 145 _Locale_init();
michael@0 146 ios_base::_S_initialize();
michael@0 147 _Filebuf_base::_S_initialize();
michael@0 148 }
michael@0 149 }
michael@0 150
michael@0 151 ios_base::Init::~Init() {
michael@0 152 if (--_S_count == 0) {
michael@0 153 ios_base::_S_uninitialize();
michael@0 154 _Locale_final();
michael@0 155 }
michael@0 156 }
michael@0 157
michael@0 158 static int _Stl_extract_open_param(FILE* f)
michael@0 159 { return _FILE_fd(f); }
michael@0 160
michael@0 161 #ifdef _STLP_REDIRECT_STDSTREAMS
michael@0 162 static const char* _Stl_extract_open_param(const char* name)
michael@0 163 { return name; }
michael@0 164 #endif
michael@0 165
michael@0 166 template <class _Tp>
michael@0 167 static filebuf*
michael@0 168 _Stl_create_filebuf(_Tp x, ios_base::openmode mode ) {
michael@0 169 auto_ptr<filebuf> result(new basic_filebuf<char, char_traits<char> >());
michael@0 170 result->open(_Stl_extract_open_param(x), mode);
michael@0 171
michael@0 172 if (result->is_open())
michael@0 173 return result.release();
michael@0 174
michael@0 175 return 0;
michael@0 176 }
michael@0 177
michael@0 178 #if !defined (_STLP_NO_WCHAR_T)
michael@0 179 static wfilebuf*
michael@0 180 _Stl_create_wfilebuf(FILE* f, ios_base::openmode mode) {
michael@0 181 auto_ptr<wfilebuf> result(new basic_filebuf<wchar_t, char_traits<wchar_t> >());
michael@0 182 result->_M_open(_FILE_fd(f), mode);
michael@0 183
michael@0 184 if (result->is_open())
michael@0 185 return result.release();
michael@0 186
michael@0 187 return 0;
michael@0 188 }
michael@0 189 #endif
michael@0 190
michael@0 191 void _STLP_CALL ios_base::_S_initialize() {
michael@0 192 #if !defined (_STLP_HAS_NO_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE)
michael@0 193 using _STLP_PRIV stdio_istreambuf;
michael@0 194 using _STLP_PRIV stdio_ostreambuf;
michael@0 195 #endif
michael@0 196
michael@0 197 auto_ptr<streambuf> cin_buf;
michael@0 198 auto_ptr<streambuf> cout_buf;
michael@0 199 auto_ptr<streambuf> cerr_buf;
michael@0 200 auto_ptr<streambuf> clog_buf;
michael@0 201
michael@0 202 if (_S_is_synced)
michael@0 203 cin_buf.reset(new stdio_istreambuf(stdin));
michael@0 204 else
michael@0 205 cin_buf.reset(_Stl_create_filebuf(stdin, ios_base::in));
michael@0 206
michael@0 207 if (_S_is_synced) {
michael@0 208 #ifdef _STLP_REDIRECT_STDSTREAMS
michael@0 209 cout_buf.reset(_Stl_create_filebuf("/stdout.txt", ios::out));
michael@0 210 cerr_buf.reset(_Stl_create_filebuf("/stderr.txt", ios::out));
michael@0 211 clog_buf.reset(_Stl_create_filebuf("/stdlog.txt", ios::out));
michael@0 212 #else
michael@0 213 cout_buf.reset(new stdio_ostreambuf(stdout));
michael@0 214 cerr_buf.reset(new stdio_ostreambuf(stderr));
michael@0 215 clog_buf.reset(new stdio_ostreambuf(stderr));
michael@0 216 #endif
michael@0 217 }
michael@0 218 else {
michael@0 219 cout_buf.reset(_Stl_create_filebuf(stdout, ios_base::out));
michael@0 220 cerr_buf.reset(_Stl_create_filebuf(stderr, ios_base::out));
michael@0 221 clog_buf.reset(_Stl_create_filebuf(stderr, ios_base::out));
michael@0 222 }
michael@0 223
michael@0 224 istream* ptr_cin = new(&cin) istream(cin_buf.get()); cin_buf.release();
michael@0 225 ostream* ptr_cout = new(&cout) ostream(cout_buf.get()); cout_buf.release();
michael@0 226 ostream* ptr_cerr = new(&cerr) ostream(cerr_buf.get()); cerr_buf.release();
michael@0 227 /*ostream* ptr_clog = */ new(&clog) ostream(clog_buf.get()); clog_buf.release();
michael@0 228 ptr_cin->tie(ptr_cout);
michael@0 229 ptr_cerr->setf(ios_base::unitbuf);
michael@0 230
michael@0 231 #ifndef _STLP_NO_WCHAR_T
michael@0 232 auto_ptr<wfilebuf> win(_Stl_create_wfilebuf(stdin, ios_base::in));
michael@0 233 auto_ptr<wfilebuf> wout(_Stl_create_wfilebuf(stdout, ios_base::out));
michael@0 234 auto_ptr<wfilebuf> werr(_Stl_create_wfilebuf(stderr, ios_base::out));
michael@0 235 auto_ptr<wfilebuf> wlog(_Stl_create_wfilebuf(stderr, ios_base::out));
michael@0 236
michael@0 237 // Run constructors for the four wide stream objects.
michael@0 238 wistream* ptr_wcin = new(&wcin) wistream(win.get()); win.release();
michael@0 239 wostream* ptr_wcout = new(&wcout) wostream(wout.get()); wout.release();
michael@0 240 wostream* ptr_wcerr = new(&wcerr) wostream(werr.get()); werr.release();
michael@0 241 /*wostream* ptr_wclog = */ new(&wclog) wostream(wlog.get()); wlog.release();
michael@0 242
michael@0 243 ptr_wcin->tie(ptr_wcout);
michael@0 244 ptr_wcerr->setf(ios_base::unitbuf);
michael@0 245 #endif
michael@0 246 }
michael@0 247
michael@0 248 void _STLP_CALL ios_base::_S_uninitialize() {
michael@0 249 // Note that destroying output streambufs flushes the buffers.
michael@0 250 istream* ptr_cin = &cin;
michael@0 251 ostream* ptr_cout = &cout;
michael@0 252 ostream* ptr_cerr = &cerr;
michael@0 253 ostream* ptr_clog = &clog;
michael@0 254
michael@0 255 // We don't want any exceptions being thrown here
michael@0 256 ptr_cin->exceptions(0);
michael@0 257 ptr_cout->exceptions(0);
michael@0 258 ptr_cerr->exceptions(0);
michael@0 259 ptr_clog->exceptions(0);
michael@0 260
michael@0 261 delete ptr_cin->rdbuf(0);
michael@0 262 delete ptr_cout->rdbuf(0);
michael@0 263 delete ptr_cerr->rdbuf(0);
michael@0 264 delete ptr_clog->rdbuf(0);
michael@0 265
michael@0 266 _Destroy(ptr_cin);
michael@0 267 _Destroy(ptr_cout);
michael@0 268 _Destroy(ptr_cerr);
michael@0 269 _Destroy(ptr_clog);
michael@0 270
michael@0 271 #ifndef _STLP_NO_WCHAR_T
michael@0 272 wistream* ptr_wcin = &wcin;
michael@0 273 wostream* ptr_wcout = &wcout;
michael@0 274 wostream* ptr_wcerr = &wcerr;
michael@0 275 wostream* ptr_wclog = &wclog;
michael@0 276
michael@0 277 // We don't want any exceptions being thrown here
michael@0 278 ptr_wcin->exceptions(0);
michael@0 279 ptr_wcout->exceptions(0);
michael@0 280 ptr_wcerr->exceptions(0);
michael@0 281 ptr_wclog->exceptions(0);
michael@0 282
michael@0 283 delete ptr_wcin->rdbuf(0);
michael@0 284 delete ptr_wcout->rdbuf(0);
michael@0 285 delete ptr_wcerr->rdbuf(0);
michael@0 286 delete ptr_wclog->rdbuf(0);
michael@0 287
michael@0 288 _Destroy(ptr_wcin);
michael@0 289 _Destroy(ptr_wcout);
michael@0 290 _Destroy(ptr_wcerr);
michael@0 291 _Destroy(ptr_wclog);
michael@0 292 #endif
michael@0 293 }
michael@0 294
michael@0 295
michael@0 296 bool _STLP_CALL ios_base::sync_with_stdio(bool sync) {
michael@0 297 # if !defined (_STLP_HAS_NO_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE)
michael@0 298 using _STLP_PRIV stdio_istreambuf;
michael@0 299 using _STLP_PRIV stdio_ostreambuf;
michael@0 300 # endif
michael@0 301
michael@0 302 if (sync == _S_is_synced) return sync;
michael@0 303
michael@0 304 // if by any chance we got there before std streams initialization,
michael@0 305 // just set the sync flag and exit
michael@0 306 if (Init::_S_count == 0) {
michael@0 307 _S_is_synced = sync;
michael@0 308 return sync;
michael@0 309 }
michael@0 310
michael@0 311 auto_ptr<streambuf> cin_buf;
michael@0 312 auto_ptr<streambuf> cout_buf;
michael@0 313 auto_ptr<streambuf> cerr_buf;
michael@0 314 auto_ptr<streambuf> clog_buf;
michael@0 315
michael@0 316 if (sync)
michael@0 317 cin_buf.reset(new stdio_istreambuf(stdin));
michael@0 318 else
michael@0 319 cin_buf.reset(_Stl_create_filebuf(stdin, ios_base::in));
michael@0 320
michael@0 321 if (sync) {
michael@0 322 #ifdef _STLP_REDIRECT_STDSTREAMS
michael@0 323 cout_buf.reset(_Stl_create_filebuf("/stdout.txt", ios::out));
michael@0 324 cerr_buf.reset(_Stl_create_filebuf("/stderr.txt", ios::out));
michael@0 325 clog_buf.reset(_Stl_create_filebuf("/stdlog.txt", ios::out));
michael@0 326 #else
michael@0 327 cout_buf.reset(new stdio_ostreambuf(stdout));
michael@0 328 cerr_buf.reset(new stdio_ostreambuf(stderr));
michael@0 329 clog_buf.reset(new stdio_ostreambuf(stderr));
michael@0 330 #endif
michael@0 331 }
michael@0 332 else {
michael@0 333 cout_buf.reset(_Stl_create_filebuf(stdout, ios_base::out));
michael@0 334 cerr_buf.reset(_Stl_create_filebuf(stderr, ios_base::out));
michael@0 335 clog_buf.reset(_Stl_create_filebuf(stderr, ios_base::out));
michael@0 336 }
michael@0 337
michael@0 338 if (cin_buf.get() != 0 && cout_buf.get() != 0 && cerr_buf.get() != 0 && clog_buf.get() != 0) {
michael@0 339 // When streambuf passed to rdbuf is not null, rdbuf is exception safe:
michael@0 340 delete (&cin)->rdbuf(cin_buf.release());
michael@0 341 delete (&cout)->rdbuf(cout_buf.release());
michael@0 342 delete (&cerr)->rdbuf(cerr_buf.release());
michael@0 343 delete (&clog)->rdbuf(clog_buf.release());
michael@0 344 _S_is_synced = sync;
michael@0 345 }
michael@0 346
michael@0 347 return _S_is_synced;
michael@0 348 }
michael@0 349
michael@0 350 _STLP_END_NAMESPACE
michael@0 351
michael@0 352 // Local Variables:
michael@0 353 // mode:C++
michael@0 354 // End:

mercurial