📄 std_streambuf.h
字号:
// Correctly sets the _M_out_cur pointer, and bumps the // appropriate _M_*_end pointers as well. Necessary for the // un-tied stringbufs, in in|out mode. // Invariant: // __n + _M_out_[cur, end] <= _M_buf + _M_buf_size // Assuming all _M_*_[beg, cur, end] pointers are operating on // the same range: // _M_buf <= _M_*_ <= _M_buf + _M_buf_size void _M_out_cur_move(off_type __n) // argument needs to be +- { bool __testin = _M_in_cur; _M_out_cur += __n; if (__testin && _M_buf_unified) _M_in_cur += __n; if (_M_out_cur > _M_out_end) { _M_out_end = _M_out_cur; // NB: in | out buffers drag the _M_in_end pointer along... if (__testin) _M_in_end += __n; } } // Return the size of the output buffer. This depends on the // buffer in use: allocated buffers have a stored size in // _M_buf_size and setbuf() buffers don't. off_type _M_out_buf_size() { off_type __ret = 0; if (_M_out_cur) { // Using allocated buffer. if (_M_out_beg == _M_buf) __ret = _M_out_beg + _M_buf_size - _M_out_cur; // Using non-allocated buffer. else __ret = _M_out_end - _M_out_cur; } return __ret; } public: /// Destructor deallocates no buffer space. virtual ~basic_streambuf() { _M_buf_unified = false; _M_buf_size = 0; _M_buf_size_opt = 0; _M_mode = ios_base::openmode(0); } // [27.5.2.2.1] locales /** * @brief Entry point for imbue(). * @param loc The new locale. * @return The previous locale. * * Calls the derived imbue(loc). */ locale pubimbue(const locale &__loc) { locale __tmp(this->getloc()); this->imbue(__loc); _M_buf_locale = __loc; return __tmp; } /** * @brief Locale access. * @return The current locale in effect. * * If pubimbue(loc) has been called, then the most recent @c loc * is returned. Otherwise the global locale in effect at the time * of construction is returned. */ locale getloc() const { return _M_buf_locale; } // [27.5.2.2.2] buffer management and positioning //@{ /** * @brief Entry points for derived buffer functions. * * The public versions of @c pubfoo dispatch to the protected * derived @c foo member functions, passing the arguments (if any) * and returning the result unchanged. */ __streambuf_type* pubsetbuf(char_type* __s, streamsize __n) { return this->setbuf(__s, __n); } pos_type pubseekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode = ios_base::in | ios_base::out) { return this->seekoff(__off, __way, __mode); } pos_type pubseekpos(pos_type __sp, ios_base::openmode __mode = ios_base::in | ios_base::out) { return this->seekpos(__sp, __mode); } int pubsync() { return this->sync(); } //@} // [27.5.2.2.3] get area /** * @brief Looking ahead into the stream. * @return The number of characters available. * * If a read position is available, returns the number of characters * available for reading before the buffer must be refilled. * Otherwise returns the derived @c showmanyc(). */ streamsize in_avail() { streamsize __ret; if (_M_in_cur && _M_in_cur < _M_in_end) { if (_M_pback_init) { size_t __save_len = _M_pback_end_save - _M_pback_cur_save; size_t __pback_len = _M_in_cur - _M_pback; __ret = __save_len - __pback_len; } else __ret = this->egptr() - this->gptr(); } else __ret = this->showmanyc(); return __ret; } /** * @brief Getting the next character. * @return The next character, or eof. * * Calls @c sbumpc(), and if that function returns * @c traits::eof(), so does this function. Otherwise, @c sgetc(). */ int_type snextc() { int_type __eof = traits_type::eof(); return (traits_type::eq_int_type(this->sbumpc(), __eof) ? __eof : this->sgetc()); } /** * @brief Getting the next character. * @return The next character, or eof. * * If the input read position is available, returns that character * and increments the read pointer, otherwise calls and returns * @c uflow(). */ int_type sbumpc(); /** * @brief Getting the next character. * @return The next character, or eof. * * If the input read position is available, returns that character, * otherwise calls and returns @c underflow(). Does not move the * read position after fetching the character. */ int_type sgetc() { int_type __ret; if (_M_in_cur && _M_in_cur < _M_in_end) __ret = traits_type::to_int_type(*(this->gptr())); else __ret = this->underflow(); return __ret; } /** * @brief Entry point for xsgetn. * @param s A buffer area. * @param n A count. * * Returns xsgetn(s,n). The effect is to fill @a s[0] through * @a s[n-1] with characters from the input sequence, if possible. */ streamsize sgetn(char_type* __s, streamsize __n) { return this->xsgetn(__s, __n); } // [27.5.2.2.4] putback /** * @brief Pushing characters back into the input stream. * @param c The character to push back. * @return The previous character, if possible. * * Similar to sungetc(), but @a c is pushed onto the stream instead * of "the previous character". If successful, the next character * fetched from the input stream will be @a c. */ int_type sputbackc(char_type __c); /** * @brief Moving backwards in the input stream. * @return The previous character, if possible. * * If a putback position is available, this function decrements the * input pointer and returns that character. Otherwise, calls and * returns pbackfail(). The effect is to "unget" the last character * "gotten". */ int_type sungetc(); // [27.5.2.2.5] put area /** * @brief Entry point for all single-character output functions. * @param c A character to output. * @return @a c, if possible. * * One of two public output functions. * * If a write position is available for the output sequence (i.e., * the buffer is not full), stores @a c in that position, increments * the position, and returns @c traits::to_int_type(c). If a write * position is not available, returns @c overflow(c). */ int_type sputc(char_type __c); /** * @brief Entry point for all single-character output functions. * @param s A buffer read area. * @param n A count. * * One of two public output functions. * * * Returns xsputn(s,n). The effect is to write @a s[0] through * @a s[n-1] to the output sequence, if possible. */ streamsize sputn(const char_type* __s, streamsize __n) { return this->xsputn(__s, __n); } protected: /** * @brief Base constructor. * * Only called from derived constructors, and sets up all the * buffer data to zero, including the pointers described in the * basic_streambuf class description. Note that, as a result, * - the class starts with no read nor write positions available, * - this is not an error */ basic_streambuf() : _M_buf(NULL), _M_buf_size(0), _M_buf_size_opt(BUFSIZ), _M_buf_unified(false), _M_in_beg(0), _M_in_cur(0), _M_in_end(0), _M_out_beg(0), _M_out_cur(0), _M_out_end(0), _M_mode(ios_base::openmode(0)), _M_buf_locale(locale()), _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false) { } // [27.5.2.3.1] get area access //@{ /** * @brief Access to the get area. * * These functions are only available to other protected functions, * including derived classes. * * - eback() returns the beginning pointer for the input sequence * - gptr() returns the next pointer for the input sequence * - egptr() returns the end pointer for the input sequence */ char_type* eback() const { return _M_in_beg; } char_type* gptr() const { return _M_in_cur; } char_type* egptr() const { return _M_in_end; } //@} /** * @brief Moving the read position. * @param n The delta by which to move. * * This just advances the read position without returning any data. */ void gbump(int __n) { _M_in_cur += __n; } /** * @brief Setting the three read area pointers. * @param gbeg A pointer. * @param gnext A pointer. * @param gend A pointer. * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and * @a gend == @c egptr() */ void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -