📄 std_streambuf.h
字号:
// Stream buffer classes -*- C++ -*-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003// Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING. If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License.//// ISO C++ 14882: 27.5 Stream buffers///** @file streambuf * This is a Standard C++ Library header. You should @c #include this header * in your programs, rather than any of the "st[dl]_*.h" implementation files. */#ifndef _CPP_STREAMBUF#define _CPP_STREAMBUF 1#pragma GCC system_header#include <bits/c++config.h>#include <iosfwd>#include <cstdio> // For SEEK_SET, SEEK_CUR, SEEK_END#include <bits/localefwd.h>#include <bits/ios_base.h>namespace std{ /** * @if maint * Does stuff. * @endif */ template<typename _CharT, typename _Traits> streamsize __copy_streambufs(basic_ios<_CharT, _Traits>& _ios, basic_streambuf<_CharT, _Traits>* __sbin, basic_streambuf<_CharT, _Traits>* __sbout); /** * @brief The actual work of input and output (interface). * * This is a base class. Derived stream buffers each control a * pair of character sequences: one for input, and one for output. * * Section [27.5.1] of the standard describes the requirements and * behavior of stream buffer classes. That section (three paragraphs) * is reproduced here, for simplicity and accuracy. * * -# Stream buffers can impose various constraints on the sequences * they control. Some constraints are: * - The controlled input sequence can be not readable. * - The controlled output sequence can be not writable. * - The controlled sequences can be associated with the contents of * other representations for character sequences, such as external * files. * - The controlled sequences can support operations @e directly to or * from associated sequences. * - The controlled sequences can impose limitations on how the * program can read characters from a sequence, write characters to * a sequence, put characters back into an input sequence, or alter * the stream position. * . * -# Each sequence is characterized by three pointers which, if non-null, * all point into the same @c charT array object. The array object * represents, at any moment, a (sub)sequence of characters from the * sequence. Operations performed on a sequence alter the values * stored in these pointers, perform reads and writes directly to or * from associated sequences, and alter "the stream position" and * conversion state as needed to maintain this subsequence relationship. * The three pointers are: * - the <em>beginning pointer</em>, or lowest element address in the * array (called @e xbeg here); * - the <em>next pointer</em>, or next element address that is a * current candidate for reading or writing (called @e xnext here); * - the <em>end pointer</em>, or first element address beyond the * end of the array (called @e xend here). * . * -# The following semantic constraints shall always apply for any set * of three pointers for a sequence, using the pointer names given * immediately above: * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall * also be non-null pointers into the same @c charT array, as * described above; otherwise, @e xbeg and @e xend shall also be null. * - If @e xnext is not a null pointer and @e xnext < @e xend for an * output sequence, then a <em>write position</em> is available. * In this case, @e *xnext shall be assignable as the next element * to write (to put, or to store a character value, into the sequence). * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an * input sequence, then a <em>putback position</em> is available. * In this case, @e xnext[-1] shall have a defined value and is the * next (preceding) element to store a character that is put back * into the input sequence. * - If @e xnext is not a null pointer and @e xnext< @e xend for an * input sequence, then a <em>read position</em> is available. * In this case, @e *xnext shall have a defined value and is the * next element to read (to get, or to obtain a character value, * from the sequence). */ template<typename _CharT, typename _Traits> class basic_streambuf { public: //@{ /** * These are standard types. They permit a standardized way of * referring to names of (or names dependant on) the template * parameters, which are specific to the implementation. */ typedef _CharT char_type; typedef _Traits traits_type; typedef typename traits_type::int_type int_type; typedef typename traits_type::pos_type pos_type; typedef typename traits_type::off_type off_type; //@} //@{ /** * @if maint * These are non-standard types. * @endif */ typedef ctype<char_type> __ctype_type; typedef basic_streambuf<char_type, traits_type> __streambuf_type; typedef typename traits_type::state_type __state_type; //@} friend class basic_ios<char_type, traits_type>; friend class basic_istream<char_type, traits_type>; friend class basic_ostream<char_type, traits_type>; friend class istreambuf_iterator<char_type, traits_type>; friend class ostreambuf_iterator<char_type, traits_type>; friend streamsize __copy_streambufs<>(basic_ios<char_type, traits_type>& __ios, __streambuf_type* __sbin,__streambuf_type* __sbout); protected: /** * @if maint * Pointer to the beginning of internally-allocated space. Filebuf * manually allocates/deallocates this, whereas stringstreams attempt * to use the built-in intelligence of the string class. If you are * managing memory, set this. If not, leave it NULL. * @endif */ char_type* _M_buf; /** * @if maint * Actual size of allocated internal buffer, in bytes. * @endif */ size_t _M_buf_size; /** * @if maint * Optimal or preferred size of internal buffer, in bytes. * @endif */ size_t _M_buf_size_opt; /** * @if maint * True iff _M_in_* and _M_out_* buffers should always point to * the same place. True for fstreams, false for sstreams. * @endif */ bool _M_buf_unified; //@{ /** * @if maint * This is based on _IO_FILE, just reordered to be more consistent, * and is intended to be the most minimal abstraction for an * internal buffer. * - get == input == read * - put == output == write * @endif */ char_type* _M_in_beg; // Start of get area. char_type* _M_in_cur; // Current read area. char_type* _M_in_end; // End of get area. char_type* _M_out_beg; // Start of put area. char_type* _M_out_cur; // Current put area. char_type* _M_out_end; // End of put area. //@} /** * @if maint * Place to stash in || out || in | out settings for current streambuf. * @endif */ ios_base::openmode _M_mode; /** * @if maint * Current locale setting. * @endif */ locale _M_buf_locale; /** * @if maint * True iff locale is initialized. * @endif */ bool _M_buf_locale_init; //@{ /** * @if maint * Necessary bits for putback buffer management. Only used in * the basic_filebuf class, as necessary for the standard * requirements. The only basic_streambuf member function that * needs access to these data members is in_avail... * * @note pbacks of over one character are not currently supported. * @endif */ static const size_t _S_pback_size = 1; char_type _M_pback[_S_pback_size]; char_type* _M_pback_cur_save; char_type* _M_pback_end_save; bool _M_pback_init; //@} /** * @if maint * Yet unused. * @endif */ fpos<__state_type> _M_pos; // Initializes pback buffers, and moves normal buffers to safety. // Assumptions: // _M_in_cur has already been moved back void _M_pback_create() { if (!_M_pback_init) { size_t __dist = _M_in_end - _M_in_cur; size_t __len = min(_S_pback_size, __dist); traits_type::copy(_M_pback, _M_in_cur, __len); _M_pback_cur_save = _M_in_cur; _M_pback_end_save = _M_in_end; this->setg(_M_pback, _M_pback, _M_pback + __len); _M_pback_init = true; } } // Deactivates pback buffer contents, and restores normal buffer. // Assumptions: // The pback buffer has only moved forward. void _M_pback_destroy() throw() { if (_M_pback_init) { // Length _M_in_cur moved in the pback buffer. size_t __off_cur = _M_in_cur - _M_pback; // For in | out buffers, the end can be pushed back... size_t __off_end = 0; size_t __pback_len = _M_in_end - _M_pback; size_t __save_len = _M_pback_end_save - _M_buf; if (__pback_len > __save_len) __off_end = __pback_len - __save_len; this->setg(_M_buf, _M_pback_cur_save + __off_cur, _M_pback_end_save + __off_end); _M_pback_cur_save = NULL; _M_pback_end_save = NULL; _M_pback_init = false; } } // Correctly sets the _M_in_cur pointer, and bumps the // _M_out_cur pointer as well if necessary. void _M_in_cur_move(off_type __n) // argument needs to be +- { bool __testout = _M_out_cur; _M_in_cur += __n; if (__testout && _M_buf_unified) _M_out_cur += __n; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -