fstream.tcc
来自「ARM Linux Tool 各种代码包括MTD」· TCC 代码 · 共 606 行 · 第 1/2 页
TCC
606 行
template<typename _CharT, typename _Traits> basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>:: pbackfail(int_type __i) { int_type __ret = traits_type::eof(); bool __testin = _M_mode & ios_base::in; if (__testin) { bool __testpb = _M_in_beg < _M_in_cur; char_type __c = traits_type::to_char_type(__i); bool __testeof = traits_type::eq_int_type(__i, __ret); if (__testpb) { bool __testout = _M_mode & ios_base::out; bool __testeq = traits_type::eq(__c, this->gptr()[-1]); // Try to put back __c into input sequence in one of three ways. // Order these tests done in is unspecified by the standard. if (!__testeof && __testeq) { --_M_in_cur; if (__testout) --_M_out_cur; __ret = __i; } else if (__testeof) { --_M_in_cur; if (__testout) --_M_out_cur; __ret = traits_type::not_eof(__i); } else if (!__testeof) { --_M_in_cur; if (__testout) --_M_out_cur; _M_pback_create(); *_M_in_cur = __c; __ret = __i; } } else { // At the beginning of the buffer, need to make a // putback position available. this->seekoff(-1, ios_base::cur); this->underflow(); if (!__testeof) { if (!traits_type::eq(__c, *_M_in_cur)) { _M_pback_create(); *_M_in_cur = __c; } __ret = __i; } else __ret = traits_type::not_eof(__i); } } _M_last_overflowed = false; return __ret; } template<typename _CharT, typename _Traits> basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>:: overflow(int_type __c) { int_type __ret = traits_type::eof(); bool __testput = _M_out_cur && _M_out_cur < _M_buf + _M_buf_size; bool __testout = _M_mode & ios_base::out; if (__testout) { if (__testput) { *_M_out_cur = traits_type::to_char_type(__c); _M_out_cur_move(1); __ret = traits_type::not_eof(__c); } else __ret = this->_M_really_overflow(__c); } _M_last_overflowed = false; // Set in _M_really_overflow, below. return __ret; } template<typename _CharT, typename _Traits> basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>:: _M_really_overflow(int_type __c) { int_type __ret = traits_type::eof(); bool __testput = _M_out_cur && _M_out_beg < _M_out_end; bool __testunbuffered = _M_file && !_M_buf_size; if (__testput || __testunbuffered) {#if 1 int __plen = _M_out_end - _M_out_beg; streamsize __len = 0; if (__plen) __len = _M_file->xsputn(_M_out_beg, __plen); if (__c !=traits_type::eof()) { char_type __pending = traits_type::to_char_type(__c); __len += _M_file->xsputn(&__pending, 1); ++__plen; } // NB: Need this so that external byte sequence reflects // internal buffer. _M_file->sync(); if (__len == __plen) { _M_set_indeterminate(); __ret = traits_type::not_eof(__c); }#else // Part one: Allocate temporary conversion buffer on // stack. Convert internal buffer plus __c (ie, // "pending sequence") to temporary conversion buffer. int __plen = _M_out_end - _M_out_beg; char_type* __pbuf = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen + 1)); traits_type::copy(__pbuf, this->pbase(), __plen); if (!__testeof) { __pbuf[__plen] = traits_type::to_char_type(__c); ++__plen; } char_type* __pend; char* __conv_buf = static_cast<char*>(__builtin_alloca(__plen)); char* __conv_end; _M_state_beg = _M_state_cur; __res_type __r = _M_fcvt->out(_M_state_cur, __pbuf, __pbuf + __plen, const_cast<const char_type*&>(__pend), __conv_buf, __conv_buf + __plen, __conv_end); // Part two: (Re)spill converted "pending sequence" // contents (now in temporary conversion buffer) to // external buffer (_M_file->_IO_*) using // _M_file->sys_write(), and do error (minimal) checking. if (__r != codecvt_base::error) { streamsize __len = _M_file->xsputn(__conv_buf, __plen); // NB: Need this so that external byte sequence reflects // internal buffer. _M_file->sync(); if (__len == __plen) { _M_set_indeterminate(); __ret = traits_type::not_eof(__c); } }#endif } _M_last_overflowed = true; return __ret; } template<typename _CharT, typename _Traits> basic_filebuf<_CharT, _Traits>::__streambuf_type* basic_filebuf<_CharT, _Traits>:: setbuf(char_type* __s, streamsize __n) { if (!this->is_open() && __s == 0 && __n == 0) _M_buf_size_opt = 0; else if (__s && __n) { // This is implementation-defined behavior, and assumes // that an external char_type array of length (__s + __n) // exists and has been pre-allocated. If this is not the // case, things will quickly blow up. // Step 1: Destroy the current internal array. _M_destroy_internal_buffer(); // Step 2: Use the external array. _M_buf = __s; _M_buf_size_opt = _M_buf_size = __n; _M_set_indeterminate(); // Step 3: Make sure a pback buffer is allocated. _M_allocate_pback_buffer(); } _M_last_overflowed = false; return this; } template<typename _CharT, typename _Traits> basic_filebuf<_CharT, _Traits>::pos_type basic_filebuf<_CharT, _Traits>:: seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode) { pos_type __ret = pos_type(off_type(-1)); bool __testopen = this->is_open(); bool __testin = __mode & ios_base::in && _M_mode & ios_base::in; bool __testout = __mode & ios_base::out && _M_mode & ios_base::out; // Should probably do has_facet checks here. int __width = use_facet<__codecvt_type>(_M_buf_locale).encoding(); if (__width < 0) __width = 0; bool __testfail = __off != 0 && __width <= 0; if (__testopen && !__testfail && (__testin || __testout)) { // Ditch any pback buffers to avoid confusion. _M_pback_destroy(); if (__way != ios_base::cur || __off != 0) { off_type __computed_off = __width * __off; bool __testget = _M_in_cur && _M_in_beg < _M_in_end; bool __testput = _M_out_cur && _M_out_beg < _M_out_end; // Sync the internal and external streams. // out if (__testput || _M_last_overflowed) { // Part one: update the output sequence. this->sync(); // Part two: output unshift sequence. _M_output_unshift(); } //in // NB: underflow() rewinds the external buffer. else if (__testget && __way == ios_base::cur) __computed_off += _M_in_cur - _M_in_beg; __ret = _M_file->seekoff(__computed_off, __way, __mode); _M_set_indeterminate(); } // NB: Need to do this in case _M_file in indeterminate // state, ie _M_file->_offset == -1 else { __ret = _M_file->seekoff(__off, ios_base::cur, __mode); __ret += max(_M_out_cur, _M_in_cur) - _M_buf; } } _M_last_overflowed = false; return __ret; } template<typename _CharT, typename _Traits> basic_filebuf<_CharT, _Traits>::pos_type basic_filebuf<_CharT, _Traits>:: seekpos(pos_type __pos, ios_base::openmode __mode) { pos_type __ret; off_type __off = __pos; __ret = this->seekoff(__off, ios_base::beg, __mode); _M_last_overflowed = false; return __ret; } template<typename _CharT, typename _Traits> void basic_filebuf<_CharT, _Traits>:: _M_output_unshift() { } template<typename _CharT, typename _Traits> void basic_filebuf<_CharT, _Traits>:: imbue(const locale& __loc) { bool __testbeg = gptr() == eback() && pptr() == pbase(); if (__testbeg && _M_buf_locale != __loc) { _M_buf_locale = __loc; _M_buf_locale_init = true; } // NB this may require the reconversion of previously // converted chars. This in turn may cause the reconstruction // of the original file. YIKES!! // XXX The part in the above comment is not done. _M_last_overflowed = false; } } // namespace std#endif // _CPP_BITS_FSTREAM_TCC
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?