libstdc++
|
00001 // File based streams -*- C++ -*- 00002 00003 // Copyright (C) 1997-2018 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/fstream.tcc 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{fstream} 00028 */ 00029 00030 // 00031 // ISO C++ 14882: 27.8 File-based streams 00032 // 00033 00034 #ifndef _FSTREAM_TCC 00035 #define _FSTREAM_TCC 1 00036 00037 #pragma GCC system_header 00038 00039 #include <bits/cxxabi_forced.h> 00040 #include <bits/move.h> // for swap 00041 00042 namespace std _GLIBCXX_VISIBILITY(default) 00043 { 00044 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00045 00046 template<typename _CharT, typename _Traits> 00047 void 00048 basic_filebuf<_CharT, _Traits>:: 00049 _M_allocate_internal_buffer() 00050 { 00051 // Allocate internal buffer only if one doesn't already exist 00052 // (either allocated or provided by the user via setbuf). 00053 if (!_M_buf_allocated && !_M_buf) 00054 { 00055 _M_buf = new char_type[_M_buf_size]; 00056 _M_buf_allocated = true; 00057 } 00058 } 00059 00060 template<typename _CharT, typename _Traits> 00061 void 00062 basic_filebuf<_CharT, _Traits>:: 00063 _M_destroy_internal_buffer() throw() 00064 { 00065 if (_M_buf_allocated) 00066 { 00067 delete [] _M_buf; 00068 _M_buf = 0; 00069 _M_buf_allocated = false; 00070 } 00071 delete [] _M_ext_buf; 00072 _M_ext_buf = 0; 00073 _M_ext_buf_size = 0; 00074 _M_ext_next = 0; 00075 _M_ext_end = 0; 00076 } 00077 00078 template<typename _CharT, typename _Traits> 00079 basic_filebuf<_CharT, _Traits>:: 00080 basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock), 00081 _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(), 00082 _M_state_last(), _M_buf(0), _M_buf_size(BUFSIZ), 00083 _M_buf_allocated(false), _M_reading(false), _M_writing(false), _M_pback(), 00084 _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false), 00085 _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0), 00086 _M_ext_end(0) 00087 { 00088 if (has_facet<__codecvt_type>(this->_M_buf_locale)) 00089 _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale); 00090 } 00091 00092 #if __cplusplus >= 201103L 00093 template<typename _CharT, typename _Traits> 00094 basic_filebuf<_CharT, _Traits>:: 00095 basic_filebuf(basic_filebuf&& __rhs) 00096 : __streambuf_type(__rhs), 00097 _M_lock(), _M_file(std::move(__rhs._M_file), &_M_lock), 00098 _M_mode(std::__exchange(__rhs._M_mode, ios_base::openmode(0))), 00099 _M_state_beg(std::move(__rhs._M_state_beg)), 00100 _M_state_cur(std::move(__rhs._M_state_cur)), 00101 _M_state_last(std::move(__rhs._M_state_last)), 00102 _M_buf(std::__exchange(__rhs._M_buf, nullptr)), 00103 _M_buf_size(std::__exchange(__rhs._M_buf_size, 1)), 00104 _M_buf_allocated(std::__exchange(__rhs._M_buf_allocated, false)), 00105 _M_reading(std::__exchange(__rhs._M_reading, false)), 00106 _M_writing(std::__exchange(__rhs._M_writing, false)), 00107 _M_pback(__rhs._M_pback), 00108 _M_pback_cur_save(std::__exchange(__rhs._M_pback_cur_save, nullptr)), 00109 _M_pback_end_save(std::__exchange(__rhs._M_pback_end_save, nullptr)), 00110 _M_pback_init(std::__exchange(__rhs._M_pback_init, false)), 00111 _M_codecvt(__rhs._M_codecvt), 00112 _M_ext_buf(std::__exchange(__rhs._M_ext_buf, nullptr)), 00113 _M_ext_buf_size(std::__exchange(__rhs._M_ext_buf_size, 0)), 00114 _M_ext_next(std::__exchange(__rhs._M_ext_next, nullptr)), 00115 _M_ext_end(std::__exchange(__rhs._M_ext_end, nullptr)) 00116 { 00117 __rhs._M_set_buffer(-1); 00118 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg; 00119 } 00120 00121 template<typename _CharT, typename _Traits> 00122 basic_filebuf<_CharT, _Traits>& 00123 basic_filebuf<_CharT, _Traits>:: 00124 operator=(basic_filebuf&& __rhs) 00125 { 00126 this->close(); 00127 __streambuf_type::operator=(__rhs); 00128 _M_file.swap(__rhs._M_file); 00129 _M_mode = std::__exchange(__rhs._M_mode, ios_base::openmode(0)); 00130 _M_state_beg = std::move(__rhs._M_state_beg); 00131 _M_state_cur = std::move(__rhs._M_state_cur); 00132 _M_state_last = std::move(__rhs._M_state_last); 00133 _M_buf = std::__exchange(__rhs._M_buf, nullptr); 00134 _M_buf_size = std::__exchange(__rhs._M_buf_size, 1); 00135 _M_buf_allocated = std::__exchange(__rhs._M_buf_allocated, false); 00136 _M_ext_buf = std::__exchange(__rhs._M_ext_buf, nullptr); 00137 _M_ext_buf_size = std::__exchange(__rhs._M_ext_buf_size, 0); 00138 _M_ext_next = std::__exchange(__rhs._M_ext_next, nullptr); 00139 _M_ext_end = std::__exchange(__rhs._M_ext_end, nullptr); 00140 _M_reading = std::__exchange(__rhs._M_reading, false); 00141 _M_writing = std::__exchange(__rhs._M_writing, false); 00142 _M_pback_cur_save = std::__exchange(__rhs._M_pback_cur_save, nullptr); 00143 _M_pback_end_save = std::__exchange(__rhs._M_pback_end_save, nullptr); 00144 _M_pback_init = std::__exchange(__rhs._M_pback_init, false); 00145 __rhs._M_set_buffer(-1); 00146 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg; 00147 return *this; 00148 } 00149 00150 template<typename _CharT, typename _Traits> 00151 void 00152 basic_filebuf<_CharT, _Traits>:: 00153 swap(basic_filebuf& __rhs) 00154 { 00155 __streambuf_type::swap(__rhs); 00156 _M_file.swap(__rhs._M_file); 00157 std::swap(_M_mode, __rhs._M_mode); 00158 std::swap(_M_state_beg, __rhs._M_state_beg); 00159 std::swap(_M_state_cur, __rhs._M_state_cur); 00160 std::swap(_M_state_last, __rhs._M_state_last); 00161 std::swap(_M_buf, __rhs._M_buf); 00162 std::swap(_M_buf_size, __rhs._M_buf_size); 00163 std::swap(_M_buf_allocated, __rhs._M_buf_allocated); 00164 std::swap(_M_ext_buf, __rhs._M_ext_buf); 00165 std::swap(_M_ext_buf_size, __rhs._M_ext_buf_size); 00166 std::swap(_M_ext_next, __rhs._M_ext_next); 00167 std::swap(_M_ext_end, __rhs._M_ext_end); 00168 std::swap(_M_reading, __rhs._M_reading); 00169 std::swap(_M_writing, __rhs._M_writing); 00170 std::swap(_M_pback_cur_save, __rhs._M_pback_cur_save); 00171 std::swap(_M_pback_end_save, __rhs._M_pback_end_save); 00172 std::swap(_M_pback_init, __rhs._M_pback_init); 00173 } 00174 #endif 00175 00176 template<typename _CharT, typename _Traits> 00177 typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 00178 basic_filebuf<_CharT, _Traits>:: 00179 open(const char* __s, ios_base::openmode __mode) 00180 { 00181 __filebuf_type *__ret = 0; 00182 if (!this->is_open()) 00183 { 00184 _M_file.open(__s, __mode); 00185 if (this->is_open()) 00186 { 00187 _M_allocate_internal_buffer(); 00188 _M_mode = __mode; 00189 00190 // Setup initial buffer to 'uncommitted' mode. 00191 _M_reading = false; 00192 _M_writing = false; 00193 _M_set_buffer(-1); 00194 00195 // Reset to initial state. 00196 _M_state_last = _M_state_cur = _M_state_beg; 00197 00198 // 27.8.1.3,4 00199 if ((__mode & ios_base::ate) 00200 && this->seekoff(0, ios_base::end, __mode) 00201 == pos_type(off_type(-1))) 00202 this->close(); 00203 else 00204 __ret = this; 00205 } 00206 } 00207 return __ret; 00208 } 00209 00210 template<typename _CharT, typename _Traits> 00211 typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 00212 basic_filebuf<_CharT, _Traits>:: 00213 close() 00214 { 00215 if (!this->is_open()) 00216 return 0; 00217 00218 bool __testfail = false; 00219 { 00220 // NB: Do this here so that re-opened filebufs will be cool... 00221 struct __close_sentry 00222 { 00223 basic_filebuf *__fb; 00224 __close_sentry (basic_filebuf *__fbi): __fb(__fbi) { } 00225 ~__close_sentry () 00226 { 00227 __fb->_M_mode = ios_base::openmode(0); 00228 __fb->_M_pback_init = false; 00229 __fb->_M_destroy_internal_buffer(); 00230 __fb->_M_reading = false; 00231 __fb->_M_writing = false; 00232 __fb->_M_set_buffer(-1); 00233 __fb->_M_state_last = __fb->_M_state_cur = __fb->_M_state_beg; 00234 } 00235 } __cs (this); 00236 00237 __try 00238 { 00239 if (!_M_terminate_output()) 00240 __testfail = true; 00241 } 00242 __catch(__cxxabiv1::__forced_unwind&) 00243 { 00244 _M_file.close(); 00245 __throw_exception_again; 00246 } 00247 __catch(...) 00248 { __testfail = true; } 00249 } 00250 00251 if (!_M_file.close()) 00252 __testfail = true; 00253 00254 if (__testfail) 00255 return 0; 00256 else 00257 return this; 00258 } 00259 00260 template<typename _CharT, typename _Traits> 00261 streamsize 00262 basic_filebuf<_CharT, _Traits>:: 00263 showmanyc() 00264 { 00265 streamsize __ret = -1; 00266 const bool __testin = _M_mode & ios_base::in; 00267 if (__testin && this->is_open()) 00268 { 00269 // For a stateful encoding (-1) the pending sequence might be just 00270 // shift and unshift prefixes with no actual character. 00271 __ret = this->egptr() - this->gptr(); 00272 00273 #if _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM 00274 // About this workaround, see libstdc++/20806. 00275 const bool __testbinary = _M_mode & ios_base::binary; 00276 if (__check_facet(_M_codecvt).encoding() >= 0 00277 && __testbinary) 00278 #else 00279 if (__check_facet(_M_codecvt).encoding() >= 0) 00280 #endif 00281 __ret += _M_file.showmanyc() / _M_codecvt->max_length(); 00282 } 00283 return __ret; 00284 } 00285 00286 template<typename _CharT, typename _Traits> 00287 typename basic_filebuf<_CharT, _Traits>::int_type 00288 basic_filebuf<_CharT, _Traits>:: 00289 underflow() 00290 { 00291 int_type __ret = traits_type::eof(); 00292 const bool __testin = _M_mode & ios_base::in; 00293 if (__testin) 00294 { 00295 if (_M_writing) 00296 { 00297 if (overflow() == traits_type::eof()) 00298 return __ret; 00299 _M_set_buffer(-1); 00300 _M_writing = false; 00301 } 00302 // Check for pback madness, and if so switch back to the 00303 // normal buffers and jet outta here before expensive 00304 // fileops happen... 00305 _M_destroy_pback(); 00306 00307 if (this->gptr() < this->egptr()) 00308 return traits_type::to_int_type(*this->gptr()); 00309 00310 // Get and convert input sequence. 00311 const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1; 00312 00313 // Will be set to true if ::read() returns 0 indicating EOF. 00314 bool __got_eof = false; 00315 // Number of internal characters produced. 00316 streamsize __ilen = 0; 00317 codecvt_base::result __r = codecvt_base::ok; 00318 if (__check_facet(_M_codecvt).always_noconv()) 00319 { 00320 __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()), 00321 __buflen); 00322 if (__ilen == 0) 00323 __got_eof = true; 00324 } 00325 else 00326 { 00327 // Worst-case number of external bytes. 00328 // XXX Not done encoding() == -1. 00329 const int __enc = _M_codecvt->encoding(); 00330 streamsize __blen; // Minimum buffer size. 00331 streamsize __rlen; // Number of chars to read. 00332 if (__enc > 0) 00333 __blen = __rlen = __buflen * __enc; 00334 else 00335 { 00336 __blen = __buflen + _M_codecvt->max_length() - 1; 00337 __rlen = __buflen; 00338 } 00339 const streamsize __remainder = _M_ext_end - _M_ext_next; 00340 __rlen = __rlen > __remainder ? __rlen - __remainder : 0; 00341 00342 // An imbue in 'read' mode implies first converting the external 00343 // chars already present. 00344 if (_M_reading && this->egptr() == this->eback() && __remainder) 00345 __rlen = 0; 00346 00347 // Allocate buffer if necessary and move unconverted 00348 // bytes to front. 00349 if (_M_ext_buf_size < __blen) 00350 { 00351 char* __buf = new char[__blen]; 00352 if (__remainder) 00353 __builtin_memcpy(__buf, _M_ext_next, __remainder); 00354 00355 delete [] _M_ext_buf; 00356 _M_ext_buf = __buf; 00357 _M_ext_buf_size = __blen; 00358 } 00359 else if (__remainder) 00360 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder); 00361 00362 _M_ext_next = _M_ext_buf; 00363 _M_ext_end = _M_ext_buf + __remainder; 00364 _M_state_last = _M_state_cur; 00365 00366 do 00367 { 00368 if (__rlen > 0) 00369 { 00370 // Sanity check! 00371 // This may fail if the return value of 00372 // codecvt::max_length() is bogus. 00373 if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size) 00374 { 00375 __throw_ios_failure(__N("basic_filebuf::underflow " 00376 "codecvt::max_length() " 00377 "is not valid")); 00378 } 00379 streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen); 00380 if (__elen == 0) 00381 __got_eof = true; 00382 else if (__elen == -1) 00383 break; 00384 _M_ext_end += __elen; 00385 } 00386 00387 char_type* __iend = this->eback(); 00388 if (_M_ext_next < _M_ext_end) 00389 __r = _M_codecvt->in(_M_state_cur, _M_ext_next, 00390 _M_ext_end, _M_ext_next, 00391 this->eback(), 00392 this->eback() + __buflen, __iend); 00393 if (__r == codecvt_base::noconv) 00394 { 00395 size_t __avail = _M_ext_end - _M_ext_buf; 00396 __ilen = std::min(__avail, __buflen); 00397 traits_type::copy(this->eback(), 00398 reinterpret_cast<char_type*> 00399 (_M_ext_buf), __ilen); 00400 _M_ext_next = _M_ext_buf + __ilen; 00401 } 00402 else 00403 __ilen = __iend - this->eback(); 00404 00405 // _M_codecvt->in may return error while __ilen > 0: this is 00406 // ok, and actually occurs in case of mixed encodings (e.g., 00407 // XML files). 00408 if (__r == codecvt_base::error) 00409 break; 00410 00411 __rlen = 1; 00412 } 00413 while (__ilen == 0 && !__got_eof); 00414 } 00415 00416 if (__ilen > 0) 00417 { 00418 _M_set_buffer(__ilen); 00419 _M_reading = true; 00420 __ret = traits_type::to_int_type(*this->gptr()); 00421 } 00422 else if (__got_eof) 00423 { 00424 // If the actual end of file is reached, set 'uncommitted' 00425 // mode, thus allowing an immediate write without an 00426 // intervening seek. 00427 _M_set_buffer(-1); 00428 _M_reading = false; 00429 // However, reaching it while looping on partial means that 00430 // the file has got an incomplete character. 00431 if (__r == codecvt_base::partial) 00432 __throw_ios_failure(__N("basic_filebuf::underflow " 00433 "incomplete character in file")); 00434 } 00435 else if (__r == codecvt_base::error) 00436 __throw_ios_failure(__N("basic_filebuf::underflow " 00437 "invalid byte sequence in file")); 00438 else 00439 __throw_ios_failure(__N("basic_filebuf::underflow " 00440 "error reading the file")); 00441 } 00442 return __ret; 00443 } 00444 00445 template<typename _CharT, typename _Traits> 00446 typename basic_filebuf<_CharT, _Traits>::int_type 00447 basic_filebuf<_CharT, _Traits>:: 00448 pbackfail(int_type __i) 00449 { 00450 int_type __ret = traits_type::eof(); 00451 const bool __testin = _M_mode & ios_base::in; 00452 if (__testin) 00453 { 00454 if (_M_writing) 00455 { 00456 if (overflow() == traits_type::eof()) 00457 return __ret; 00458 _M_set_buffer(-1); 00459 _M_writing = false; 00460 } 00461 // Remember whether the pback buffer is active, otherwise below 00462 // we may try to store in it a second char (libstdc++/9761). 00463 const bool __testpb = _M_pback_init; 00464 const bool __testeof = traits_type::eq_int_type(__i, __ret); 00465 int_type __tmp; 00466 if (this->eback() < this->gptr()) 00467 { 00468 this->gbump(-1); 00469 __tmp = traits_type::to_int_type(*this->gptr()); 00470 } 00471 else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1))) 00472 { 00473 __tmp = this->underflow(); 00474 if (traits_type::eq_int_type(__tmp, __ret)) 00475 return __ret; 00476 } 00477 else 00478 { 00479 // At the beginning of the buffer, need to make a 00480 // putback position available. But the seek may fail 00481 // (f.i., at the beginning of a file, see 00482 // libstdc++/9439) and in that case we return 00483 // traits_type::eof(). 00484 return __ret; 00485 } 00486 00487 // Try to put back __i into input sequence in one of three ways. 00488 // Order these tests done in is unspecified by the standard. 00489 if (!__testeof && traits_type::eq_int_type(__i, __tmp)) 00490 __ret = __i; 00491 else if (__testeof) 00492 __ret = traits_type::not_eof(__i); 00493 else if (!__testpb) 00494 { 00495 _M_create_pback(); 00496 _M_reading = true; 00497 *this->gptr() = traits_type::to_char_type(__i); 00498 __ret = __i; 00499 } 00500 } 00501 return __ret; 00502 } 00503 00504 template<typename _CharT, typename _Traits> 00505 typename basic_filebuf<_CharT, _Traits>::int_type 00506 basic_filebuf<_CharT, _Traits>:: 00507 overflow(int_type __c) 00508 { 00509 int_type __ret = traits_type::eof(); 00510 const bool __testeof = traits_type::eq_int_type(__c, __ret); 00511 const bool __testout = (_M_mode & ios_base::out 00512 || _M_mode & ios_base::app); 00513 if (__testout) 00514 { 00515 if (_M_reading) 00516 { 00517 _M_destroy_pback(); 00518 const int __gptr_off = _M_get_ext_pos(_M_state_last); 00519 if (_M_seek(__gptr_off, ios_base::cur, _M_state_last) 00520 == pos_type(off_type(-1))) 00521 return __ret; 00522 } 00523 if (this->pbase() < this->pptr()) 00524 { 00525 // If appropriate, append the overflow char. 00526 if (!__testeof) 00527 { 00528 *this->pptr() = traits_type::to_char_type(__c); 00529 this->pbump(1); 00530 } 00531 00532 // Convert pending sequence to external representation, 00533 // and output. 00534 if (_M_convert_to_external(this->pbase(), 00535 this->pptr() - this->pbase())) 00536 { 00537 _M_set_buffer(0); 00538 __ret = traits_type::not_eof(__c); 00539 } 00540 } 00541 else if (_M_buf_size > 1) 00542 { 00543 // Overflow in 'uncommitted' mode: set _M_writing, set 00544 // the buffer to the initial 'write' mode, and put __c 00545 // into the buffer. 00546 _M_set_buffer(0); 00547 _M_writing = true; 00548 if (!__testeof) 00549 { 00550 *this->pptr() = traits_type::to_char_type(__c); 00551 this->pbump(1); 00552 } 00553 __ret = traits_type::not_eof(__c); 00554 } 00555 else 00556 { 00557 // Unbuffered. 00558 char_type __conv = traits_type::to_char_type(__c); 00559 if (__testeof || _M_convert_to_external(&__conv, 1)) 00560 { 00561 _M_writing = true; 00562 __ret = traits_type::not_eof(__c); 00563 } 00564 } 00565 } 00566 return __ret; 00567 } 00568 00569 template<typename _CharT, typename _Traits> 00570 bool 00571 basic_filebuf<_CharT, _Traits>:: 00572 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen) 00573 { 00574 // Sizes of external and pending output. 00575 streamsize __elen; 00576 streamsize __plen; 00577 if (__check_facet(_M_codecvt).always_noconv()) 00578 { 00579 __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen); 00580 __plen = __ilen; 00581 } 00582 else 00583 { 00584 // Worst-case number of external bytes needed. 00585 // XXX Not done encoding() == -1. 00586 streamsize __blen = __ilen * _M_codecvt->max_length(); 00587 char* __buf = static_cast<char*>(__builtin_alloca(__blen)); 00588 00589 char* __bend; 00590 const char_type* __iend; 00591 codecvt_base::result __r; 00592 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen, 00593 __iend, __buf, __buf + __blen, __bend); 00594 00595 if (__r == codecvt_base::ok || __r == codecvt_base::partial) 00596 __blen = __bend - __buf; 00597 else if (__r == codecvt_base::noconv) 00598 { 00599 // Same as the always_noconv case above. 00600 __buf = reinterpret_cast<char*>(__ibuf); 00601 __blen = __ilen; 00602 } 00603 else 00604 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external " 00605 "conversion error")); 00606 00607 __elen = _M_file.xsputn(__buf, __blen); 00608 __plen = __blen; 00609 00610 // Try once more for partial conversions. 00611 if (__r == codecvt_base::partial && __elen == __plen) 00612 { 00613 const char_type* __iresume = __iend; 00614 streamsize __rlen = this->pptr() - __iend; 00615 __r = _M_codecvt->out(_M_state_cur, __iresume, 00616 __iresume + __rlen, __iend, __buf, 00617 __buf + __blen, __bend); 00618 if (__r != codecvt_base::error) 00619 { 00620 __rlen = __bend - __buf; 00621 __elen = _M_file.xsputn(__buf, __rlen); 00622 __plen = __rlen; 00623 } 00624 else 00625 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external " 00626 "conversion error")); 00627 } 00628 } 00629 return __elen == __plen; 00630 } 00631 00632 template<typename _CharT, typename _Traits> 00633 streamsize 00634 basic_filebuf<_CharT, _Traits>:: 00635 xsgetn(_CharT* __s, streamsize __n) 00636 { 00637 // Clear out pback buffer before going on to the real deal... 00638 streamsize __ret = 0; 00639 if (_M_pback_init) 00640 { 00641 if (__n > 0 && this->gptr() == this->eback()) 00642 { 00643 *__s++ = *this->gptr(); // emulate non-underflowing sbumpc 00644 this->gbump(1); 00645 __ret = 1; 00646 --__n; 00647 } 00648 _M_destroy_pback(); 00649 } 00650 else if (_M_writing) 00651 { 00652 if (overflow() == traits_type::eof()) 00653 return __ret; 00654 _M_set_buffer(-1); 00655 _M_writing = false; 00656 } 00657 00658 // Optimization in the always_noconv() case, to be generalized in the 00659 // future: when __n > __buflen we read directly instead of using the 00660 // buffer repeatedly. 00661 const bool __testin = _M_mode & ios_base::in; 00662 const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1; 00663 00664 if (__n > __buflen && __check_facet(_M_codecvt).always_noconv() 00665 && __testin) 00666 { 00667 // First, copy the chars already present in the buffer. 00668 const streamsize __avail = this->egptr() - this->gptr(); 00669 if (__avail != 0) 00670 { 00671 traits_type::copy(__s, this->gptr(), __avail); 00672 __s += __avail; 00673 this->setg(this->eback(), this->gptr() + __avail, this->egptr()); 00674 __ret += __avail; 00675 __n -= __avail; 00676 } 00677 00678 // Need to loop in case of short reads (relatively common 00679 // with pipes). 00680 streamsize __len; 00681 for (;;) 00682 { 00683 __len = _M_file.xsgetn(reinterpret_cast<char*>(__s), __n); 00684 if (__len == -1) 00685 __throw_ios_failure(__N("basic_filebuf::xsgetn " 00686 "error reading the file")); 00687 if (__len == 0) 00688 break; 00689 00690 __n -= __len; 00691 __ret += __len; 00692 if (__n == 0) 00693 break; 00694 00695 __s += __len; 00696 } 00697 00698 if (__n == 0) 00699 { 00700 // Set _M_reading. Buffer is already in initial 'read' mode. 00701 _M_reading = true; 00702 } 00703 else if (__len == 0) 00704 { 00705 // If end of file is reached, set 'uncommitted' 00706 // mode, thus allowing an immediate write without 00707 // an intervening seek. 00708 _M_set_buffer(-1); 00709 _M_reading = false; 00710 } 00711 } 00712 else 00713 __ret += __streambuf_type::xsgetn(__s, __n); 00714 00715 return __ret; 00716 } 00717 00718 template<typename _CharT, typename _Traits> 00719 streamsize 00720 basic_filebuf<_CharT, _Traits>:: 00721 xsputn(const _CharT* __s, streamsize __n) 00722 { 00723 streamsize __ret = 0; 00724 // Optimization in the always_noconv() case, to be generalized in the 00725 // future: when __n is sufficiently large we write directly instead of 00726 // using the buffer. 00727 const bool __testout = (_M_mode & ios_base::out 00728 || _M_mode & ios_base::app); 00729 if (__check_facet(_M_codecvt).always_noconv() 00730 && __testout && !_M_reading) 00731 { 00732 // Measurement would reveal the best choice. 00733 const streamsize __chunk = 1ul << 10; 00734 streamsize __bufavail = this->epptr() - this->pptr(); 00735 00736 // Don't mistake 'uncommitted' mode buffered with unbuffered. 00737 if (!_M_writing && _M_buf_size > 1) 00738 __bufavail = _M_buf_size - 1; 00739 00740 const streamsize __limit = std::min(__chunk, __bufavail); 00741 if (__n >= __limit) 00742 { 00743 const streamsize __buffill = this->pptr() - this->pbase(); 00744 const char* __buf = reinterpret_cast<const char*>(this->pbase()); 00745 __ret = _M_file.xsputn_2(__buf, __buffill, 00746 reinterpret_cast<const char*>(__s), 00747 __n); 00748 if (__ret == __buffill + __n) 00749 { 00750 _M_set_buffer(0); 00751 _M_writing = true; 00752 } 00753 if (__ret > __buffill) 00754 __ret -= __buffill; 00755 else 00756 __ret = 0; 00757 } 00758 else 00759 __ret = __streambuf_type::xsputn(__s, __n); 00760 } 00761 else 00762 __ret = __streambuf_type::xsputn(__s, __n); 00763 return __ret; 00764 } 00765 00766 template<typename _CharT, typename _Traits> 00767 typename basic_filebuf<_CharT, _Traits>::__streambuf_type* 00768 basic_filebuf<_CharT, _Traits>:: 00769 setbuf(char_type* __s, streamsize __n) 00770 { 00771 if (!this->is_open()) 00772 { 00773 if (__s == 0 && __n == 0) 00774 _M_buf_size = 1; 00775 else if (__s && __n > 0) 00776 { 00777 // This is implementation-defined behavior, and assumes that 00778 // an external char_type array of length __n exists and has 00779 // been pre-allocated. If this is not the case, things will 00780 // quickly blow up. When __n > 1, __n - 1 positions will be 00781 // used for the get area, __n - 1 for the put area and 1 00782 // position to host the overflow char of a full put area. 00783 // When __n == 1, 1 position will be used for the get area 00784 // and 0 for the put area, as in the unbuffered case above. 00785 _M_buf = __s; 00786 _M_buf_size = __n; 00787 } 00788 } 00789 return this; 00790 } 00791 00792 00793 // According to 27.8.1.4 p11 - 13, seekoff should ignore the last 00794 // argument (of type openmode). 00795 template<typename _CharT, typename _Traits> 00796 typename basic_filebuf<_CharT, _Traits>::pos_type 00797 basic_filebuf<_CharT, _Traits>:: 00798 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) 00799 { 00800 int __width = 0; 00801 if (_M_codecvt) 00802 __width = _M_codecvt->encoding(); 00803 if (__width < 0) 00804 __width = 0; 00805 00806 pos_type __ret = pos_type(off_type(-1)); 00807 const bool __testfail = __off != 0 && __width <= 0; 00808 if (this->is_open() && !__testfail) 00809 { 00810 // tellg and tellp queries do not affect any state, unless 00811 // ! always_noconv and the put sequence is not empty. 00812 // In that case, determining the position requires converting the 00813 // put sequence. That doesn't use ext_buf, so requires a flush. 00814 bool __no_movement = __way == ios_base::cur && __off == 0 00815 && (!_M_writing || _M_codecvt->always_noconv()); 00816 00817 // Ditch any pback buffers to avoid confusion. 00818 if (!__no_movement) 00819 _M_destroy_pback(); 00820 00821 // Correct state at destination. Note that this is the correct 00822 // state for the current position during output, because 00823 // codecvt::unshift() returns the state to the initial state. 00824 // This is also the correct state at the end of the file because 00825 // an unshift sequence should have been written at the end. 00826 __state_type __state = _M_state_beg; 00827 off_type __computed_off = __off * __width; 00828 if (_M_reading && __way == ios_base::cur) 00829 { 00830 __state = _M_state_last; 00831 __computed_off += _M_get_ext_pos(__state); 00832 } 00833 if (!__no_movement) 00834 __ret = _M_seek(__computed_off, __way, __state); 00835 else 00836 { 00837 if (_M_writing) 00838 __computed_off = this->pptr() - this->pbase(); 00839 00840 off_type __file_off = _M_file.seekoff(0, ios_base::cur); 00841 if (__file_off != off_type(-1)) 00842 { 00843 __ret = __file_off + __computed_off; 00844 __ret.state(__state); 00845 } 00846 } 00847 } 00848 return __ret; 00849 } 00850 00851 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00852 // 171. Strange seekpos() semantics due to joint position 00853 // According to the resolution of DR 171, seekpos should ignore the last 00854 // argument (of type openmode). 00855 template<typename _CharT, typename _Traits> 00856 typename basic_filebuf<_CharT, _Traits>::pos_type 00857 basic_filebuf<_CharT, _Traits>:: 00858 seekpos(pos_type __pos, ios_base::openmode) 00859 { 00860 pos_type __ret = pos_type(off_type(-1)); 00861 if (this->is_open()) 00862 { 00863 // Ditch any pback buffers to avoid confusion. 00864 _M_destroy_pback(); 00865 __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state()); 00866 } 00867 return __ret; 00868 } 00869 00870 template<typename _CharT, typename _Traits> 00871 typename basic_filebuf<_CharT, _Traits>::pos_type 00872 basic_filebuf<_CharT, _Traits>:: 00873 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state) 00874 { 00875 pos_type __ret = pos_type(off_type(-1)); 00876 if (_M_terminate_output()) 00877 { 00878 off_type __file_off = _M_file.seekoff(__off, __way); 00879 if (__file_off != off_type(-1)) 00880 { 00881 _M_reading = false; 00882 _M_writing = false; 00883 _M_ext_next = _M_ext_end = _M_ext_buf; 00884 _M_set_buffer(-1); 00885 _M_state_cur = __state; 00886 __ret = __file_off; 00887 __ret.state(_M_state_cur); 00888 } 00889 } 00890 return __ret; 00891 } 00892 00893 // Returns the distance from the end of the ext buffer to the point 00894 // corresponding to gptr(). This is a negative value. Updates __state 00895 // from eback() correspondence to gptr(). 00896 template<typename _CharT, typename _Traits> 00897 int basic_filebuf<_CharT, _Traits>:: 00898 _M_get_ext_pos(__state_type& __state) 00899 { 00900 if (_M_codecvt->always_noconv()) 00901 return this->gptr() - this->egptr(); 00902 else 00903 { 00904 // Calculate offset from _M_ext_buf that corresponds to 00905 // gptr(). Precondition: __state == _M_state_last, which 00906 // corresponds to eback(). 00907 const int __gptr_off = 00908 _M_codecvt->length(__state, _M_ext_buf, _M_ext_next, 00909 this->gptr() - this->eback()); 00910 return _M_ext_buf + __gptr_off - _M_ext_end; 00911 } 00912 } 00913 00914 template<typename _CharT, typename _Traits> 00915 bool 00916 basic_filebuf<_CharT, _Traits>:: 00917 _M_terminate_output() 00918 { 00919 // Part one: update the output sequence. 00920 bool __testvalid = true; 00921 if (this->pbase() < this->pptr()) 00922 { 00923 const int_type __tmp = this->overflow(); 00924 if (traits_type::eq_int_type(__tmp, traits_type::eof())) 00925 __testvalid = false; 00926 } 00927 00928 // Part two: output unshift sequence. 00929 if (_M_writing && !__check_facet(_M_codecvt).always_noconv() 00930 && __testvalid) 00931 { 00932 // Note: this value is arbitrary, since there is no way to 00933 // get the length of the unshift sequence from codecvt, 00934 // without calling unshift. 00935 const size_t __blen = 128; 00936 char __buf[__blen]; 00937 codecvt_base::result __r; 00938 streamsize __ilen = 0; 00939 00940 do 00941 { 00942 char* __next; 00943 __r = _M_codecvt->unshift(_M_state_cur, __buf, 00944 __buf + __blen, __next); 00945 if (__r == codecvt_base::error) 00946 __testvalid = false; 00947 else if (__r == codecvt_base::ok || 00948 __r == codecvt_base::partial) 00949 { 00950 __ilen = __next - __buf; 00951 if (__ilen > 0) 00952 { 00953 const streamsize __elen = _M_file.xsputn(__buf, __ilen); 00954 if (__elen != __ilen) 00955 __testvalid = false; 00956 } 00957 } 00958 } 00959 while (__r == codecvt_base::partial && __ilen > 0 && __testvalid); 00960 00961 if (__testvalid) 00962 { 00963 // This second call to overflow() is required by the standard, 00964 // but it's not clear why it's needed, since the output buffer 00965 // should be empty by this point (it should have been emptied 00966 // in the first call to overflow()). 00967 const int_type __tmp = this->overflow(); 00968 if (traits_type::eq_int_type(__tmp, traits_type::eof())) 00969 __testvalid = false; 00970 } 00971 } 00972 return __testvalid; 00973 } 00974 00975 template<typename _CharT, typename _Traits> 00976 int 00977 basic_filebuf<_CharT, _Traits>:: 00978 sync() 00979 { 00980 // Make sure that the internal buffer resyncs its idea of 00981 // the file position with the external file. 00982 int __ret = 0; 00983 if (this->pbase() < this->pptr()) 00984 { 00985 const int_type __tmp = this->overflow(); 00986 if (traits_type::eq_int_type(__tmp, traits_type::eof())) 00987 __ret = -1; 00988 } 00989 return __ret; 00990 } 00991 00992 template<typename _CharT, typename _Traits> 00993 void 00994 basic_filebuf<_CharT, _Traits>:: 00995 imbue(const locale& __loc) 00996 { 00997 bool __testvalid = true; 00998 00999 const __codecvt_type* _M_codecvt_tmp = 0; 01000 if (__builtin_expect(has_facet<__codecvt_type>(__loc), true)) 01001 _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc); 01002 01003 if (this->is_open()) 01004 { 01005 // encoding() == -1 is ok only at the beginning. 01006 if ((_M_reading || _M_writing) 01007 && __check_facet(_M_codecvt).encoding() == -1) 01008 __testvalid = false; 01009 else 01010 { 01011 if (_M_reading) 01012 { 01013 if (__check_facet(_M_codecvt).always_noconv()) 01014 { 01015 if (_M_codecvt_tmp 01016 && !__check_facet(_M_codecvt_tmp).always_noconv()) 01017 __testvalid = this->seekoff(0, ios_base::cur, _M_mode) 01018 != pos_type(off_type(-1)); 01019 } 01020 else 01021 { 01022 // External position corresponding to gptr(). 01023 _M_ext_next = _M_ext_buf 01024 + _M_codecvt->length(_M_state_last, _M_ext_buf, 01025 _M_ext_next, 01026 this->gptr() - this->eback()); 01027 const streamsize __remainder = _M_ext_end - _M_ext_next; 01028 if (__remainder) 01029 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder); 01030 01031 _M_ext_next = _M_ext_buf; 01032 _M_ext_end = _M_ext_buf + __remainder; 01033 _M_set_buffer(-1); 01034 _M_state_last = _M_state_cur = _M_state_beg; 01035 } 01036 } 01037 else if (_M_writing && (__testvalid = _M_terminate_output())) 01038 _M_set_buffer(-1); 01039 } 01040 } 01041 01042 if (__testvalid) 01043 _M_codecvt = _M_codecvt_tmp; 01044 else 01045 _M_codecvt = 0; 01046 } 01047 01048 // Inhibit implicit instantiations for required instantiations, 01049 // which are defined via explicit instantiations elsewhere. 01050 #if _GLIBCXX_EXTERN_TEMPLATE 01051 extern template class basic_filebuf<char>; 01052 extern template class basic_ifstream<char>; 01053 extern template class basic_ofstream<char>; 01054 extern template class basic_fstream<char>; 01055 01056 #ifdef _GLIBCXX_USE_WCHAR_T 01057 extern template class basic_filebuf<wchar_t>; 01058 extern template class basic_ifstream<wchar_t>; 01059 extern template class basic_ofstream<wchar_t>; 01060 extern template class basic_fstream<wchar_t>; 01061 #endif 01062 #endif 01063 01064 _GLIBCXX_END_NAMESPACE_VERSION 01065 } // namespace std 01066 01067 #endif