📄 streambuf.h
字号:
#else friend int __underflow(streambuf*);#endif struct streammarker *_next; // Only if saving() streambuf *_sbuf; // Only valid if saving(). streampos _spos; // -2: means that _pos is valid. void set_streampos(streampos sp) { _spos = sp; } void set_offset(int offset) { _pos = offset; _spos = (streampos)(-2); } // If _pos >= 0, it points to _buf->Gbase()+_pos. // if _pos < 0, it points to _buf->eBptr()+_pos. int _pos; public: streammarker(streambuf *sb); ~streammarker(); int saving() { return _spos == -2; } int delta(streammarker&); int delta();};struct __streambuf { // NOTE: If this is changed, also change __FILE in stdio/stdio.h! int _flags; /* High-order word is _IO_MAGIC; rest is flags. */ char* _gptr; /* Current get pointer */ char* _egptr; /* End of get area. */ char* _eback; /* Start of putback+get area. */ char* _pbase; /* Start of put area. */ char* _pptr; /* Current put pointer. */ char* _epptr; /* End of put area. */ char* _base; /* Start of reserve area. */ char* _ebuf; /* End of reserve area. */ struct streambuf *_chain; // The following fields are used to support backing up and undo. friend class streammarker; char *_other_gbase; // Pointer to start of non-current get area. char *_aux_limit; // Pointer to first valid character of backup area, char *_other_egptr; // Pointer to end of non-current get area. streammarker *_markers;#define __HAVE_COLUMN /* temporary */ // 1+column number of pbase(); 0 is unknown. unsigned short _cur_column; char _unused; char _shortbuf[1];};extern unsigned __adjust_column(unsigned start, const char *line, int count);struct streambuf : private __streambuf { friend class ios; friend class istream; friend class ostream; friend class streammarker;#ifdef _G_FRIEND_BUG friend int __UNDERFLOW(streambuf*);#else friend int __underflow(streambuf*);#endif protected: static streambuf* _list_all; /* List of open streambufs. */ streambuf*& xchain() { return _chain; } void _un_link(); void _link_in(); char* gptr() const { return _gptr; } char* pptr() const { return _pptr; } char* egptr() const { return _egptr; } char* epptr() const { return _epptr; } char* pbase() const { return _pbase; } char* eback() const { return _eback; } char* base() const { return _base; } char* ebuf() const { return _ebuf; } int blen() const { return _ebuf - _base; } void xput_char(char c) { *_pptr++ = c; } int xflags() { return _flags; } int xflags(int f) { int fl = _flags; _flags = f; return fl; } void xsetflags(int f) { _flags |= f; } void xsetflags(int f, int mask) { _flags = (_flags & ~mask) | (f & mask); } void gbump(int n) { _gptr += n; } void pbump(int n) { _pptr += n; } void setb(char* b, char* eb, int a=0); void setp(char* p, char* ep) { _pbase=_pptr=p; _epptr=ep; } void setg(char* eb, char* g, char *eg) { _eback=eb; _gptr=g; _egptr=eg; } char *shortbuf() { return _shortbuf; } int in_backup() { return _flags & _S_IN_BACKUP; } // The start of the main get area: FIXME: wrong for write-mode filebuf? char *Gbase() { return in_backup() ? _other_gbase : _eback; } // The end of the main get area: char *eGptr() { return in_backup() ? _other_egptr : _egptr; } // The start of the backup area: char *Bbase() { return in_backup() ? _eback : _other_gbase; } char *Bptr() { return _aux_limit; } // The end of the backup area: char *eBptr() { return in_backup() ? _egptr : _other_egptr; } char *Nbase() { return _other_gbase; } char *eNptr() { return _other_egptr; } int have_backup() { return _other_gbase != NULL; } int have_markers() { return _markers != NULL; } int _least_marker(); void switch_to_main_get_area(); void switch_to_backup_area(); void free_backup_area(); void unsave_markers(); // Make all streammarkers !saving(). int put_mode() { return _flags & _S_CURRENTLY_PUTTING; } int switch_to_get_mode(); streambuf(int flags=0); public: static int flush_all(); static void flush_all_linebuffered(); // Flush all line buffered files. virtual int underflow() = 0; // Leave public for now virtual int overflow(int c = EOF) = 0; // Leave public for now virtual int doallocate(); virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out); virtual streampos seekpos(streampos pos, int mode = ios::in|ios::out); int seekmark(streammarker& mark, int delta = 0); int sputbackc(char c); int sungetc(); virtual ~streambuf(); int unbuffered() { return _flags & _S_UNBUFFERED ? 1 : 0; } int linebuffered() { return _flags & _S_LINE_BUF ? 1 : 0; } void unbuffered(int i) { if (i) _flags |= _S_UNBUFFERED; else _flags &= ~_S_UNBUFFERED; } void linebuffered(int i) { if (i) _flags |= _S_LINE_BUF; else _flags &= ~_S_LINE_BUF; } int allocate() { // For AT&T compatibility if (base() || unbuffered()) return 0; else return doallocate(); } // Allocate a buffer if needed; use _shortbuf if appropriate. void allocbuf() { if (base() == NULL) doallocbuf(); } void doallocbuf(); virtual int sync(); virtual int pbackfail(int c); virtual streambuf* setbuf(char* p, int len); int in_avail() { return _egptr - _gptr; } int out_waiting() { return _pptr - _pbase; } virtual int xsputn(const char* s, int n); int sputn(const char* s, int n) { return xsputn(s, n); } int padn(char pad, int n); // Emit 'n' copies of 'pad'. virtual int xsgetn(char* s, int n); int sgetn(char* s, int n) { return xsgetn(s, n); } int ignore(int); virtual int get_column(); virtual int set_column(int); long sgetline(char* buf, _G_size_t n, char delim, int putback_delim); int sbumpc() { if (_gptr >= _egptr && __underflow(this) == EOF) return EOF; else return *(unsigned char*)_gptr++; } int sgetc() { if (_gptr >= _egptr && __underflow(this) == EOF) return EOF; else return *(unsigned char*)_gptr; } int snextc() { if (_gptr >= _egptr && __underflow(this) == EOF) return EOF; return _gptr++, sgetc(); } int sputc(int c) { if (_pptr >= _epptr) return __overflow(this, (unsigned char)c); else return *_pptr++ = c, (unsigned char)c; } void stossc() { if (_gptr < _egptr) _gptr++; } int vscan(char const *fmt0, _G_va_list ap, ios* stream = NULL); int scan(char const *fmt0 ...); int vform(char const *fmt0, _G_va_list ap); int form(char const *fmt0 ...);#if 0 /* Work in progress */ int collumn(); // Current collumn number (of put pointer). -1 is unknown. void collumn(int c); // Set collumn number of put pointer to c.#endif};// A backupbuf is a streambuf with full backup and savepoints on reading.// All standard streambufs in the GNU iostream library are backupbufs.// A backupbuf may have two get area:// - The main get area, and (sometimes) the putback area.// Whichever one of these contains the gptr is the current get area;// the other one is the non-current get area.class backupbuf : public streambuf { friend class streammarker; protected: backupbuf(int flags=0) : streambuf(flags|_S_IS_BACKUPBUF) { } public: virtual int pbackfail(int c); virtual int underflow(); virtual int overflow(int c = EOF);};struct __file_fields { short _fileno; int _blksize; fpos_t _offset;// char* _save_gptr; char* _save_egptr;};class filebuf : public backupbuf { protected: struct __file_fields _fb; void init(); public: static const int openprot; // Non-ANSI AT&T-ism: Default open protection. filebuf(); filebuf(int fd); filebuf(int fd, char* p, int len); ~filebuf(); filebuf* attach(int fd); filebuf* open(const char *filename, const char *mode); filebuf* open(const char *filename, ios::openmode mode, int prot = 0664); virtual int underflow(); virtual int overflow(int c = EOF); int is_open() const { return _fb._fileno >= 0; } int fd() const { return is_open() ? _fb._fileno : EOF; } filebuf* close(); virtual int doallocate(); virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out); virtual streambuf* setbuf(char* p, int len); int xsputn(const char* s, int n); int xsgetn(char* s, int n); virtual int sync(); protected: // See documentation in filebuf.C.// virtual int pbackfail(int c); int is_reading() { return eback() != egptr(); } char* cur_ptr() { return is_reading() ? gptr() : pptr(); } /* System's idea of pointer */ char* file_ptr() { return eGptr(); } int do_write(const char *data, int to_do); int do_flush() { return do_write(_pbase, _pptr-_pbase); } // Low-level operations (Usually invoke system calls.) virtual _G_ssize_t sys_read(char* buf, _G_size_t size); virtual fpos_t sys_seek(fpos_t, _seek_dir); virtual _G_ssize_t sys_write(const void*, long); virtual int sys_stat(void*); // Actually, a (struct stat*) virtual int sys_close();};inline ios::ios(streambuf* sb /* = 0 */, ostream* tie /* = 0 */) { _state = sb ? ios::goodbit : ios::badbit; _exceptions=0; _strbuf=sb; _tie = tie; _width=0; _fill=' '; _flags=ios::skipws|ios::dec; _precision=6; }inline ios::~ios() { if (!(_flags & (unsigned int)ios::dont_close)) delete _strbuf; }#endif /* _STREAMBUF_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -