📄 iostream.c
字号:
#else register unsigned LONGEST ival = val;#endif do { *--buf_ptr = (ival % 10) + '0'; ival /= 10; } while (ival != 0); } int buf_len = buf+WRITE_BUF_SIZE - buf_ptr; int w = stream.width(0); int show_pos = 0; // Calculate padding. int len = buf_len; if (neg) len++; else if (val != 0 && (stream.flags() & ios::showpos)) len++, show_pos=1; len += show_base_len; int padding = len > w ? 0 : w - len; // Do actual output. register streambuf* sbuf = stream.rdbuf(); ios::fmtflags pad_kind = stream.flags() & (ios::left|ios::right|ios::internal); char fill_char = stream.fill(); if (padding > 0 && pad_kind != (ios::fmtflags)ios::left && pad_kind != (ios::fmtflags)ios::internal) // Default (right) adjust. sbuf->padn(fill_char, padding); if (neg) sbuf->sputc('-'); else if (show_pos) sbuf->sputc('+'); if (show_base_len) sbuf->sputn(show_base, show_base_len); if (pad_kind == (ios::fmtflags)ios::internal && padding > 0) sbuf->padn(fill_char, padding); sbuf->sputn(buf_ptr, buf_len); if (pad_kind == (ios::fmtflags)ios::left && padding > 0) // Left adjustment sbuf->padn(fill_char, padding); stream.osfx();}ostream& ostream::operator<<(int n){ if (opfx()) { int neg = 0; if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0) n = -n, neg = 1; write_int(*this, n, neg); } return *this;}ostream& ostream::operator<<(unsigned int n){ if (opfx()) write_int(*this, n, 0); return *this;}ostream& ostream::operator<<(long n){ if (opfx()) { int neg = 0; if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0) n = -n, neg = 1; write_int(*this, n, neg); } return *this;}ostream& ostream::operator<<(unsigned long n){ if (opfx()) write_int(*this, n, 0); return *this;}#ifdef __GNUG__ostream& ostream::operator<<(long long n){ if (opfx()) { int neg = 0; if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0) n = -n, neg = 1; write_int(*this, n, neg); } return *this;}ostream& ostream::operator<<(unsigned long long n){ if (opfx()) write_int(*this, n, 0); return *this;}#endif /*__GNUG__*/ostream& ostream::operator<<(double n){ if (opfx()) { // Uses __cvt_double (renamed from static cvt), in Chris Torek's // stdio implementation. The setup code uses the same logic // as in __vsbprintf.C (also based on Torek's code). int format_char;#if 0 if (flags() ios::showpos) sign = '+';#endif if ((flags() & ios::floatfield) == ios::fixed) format_char = 'f'; else if ((flags() & ios::floatfield) == ios::scientific) format_char = flags() & ios::uppercase ? 'E' : 'e'; else format_char = flags() & ios::uppercase ? 'G' : 'g'; int fpprec = 0; // 'Extra' (suppressed) floating precision. int prec = precision(); if (prec < 0) prec = 6; // default. else if (prec > MAXFRACT) { if (flags() & (ios::fixed|ios::scientific) & ios::showpos) fpprec = prec - MAXFRACT; prec = MAXFRACT; } // Do actual conversion.#ifdef USE_DTOA if (__outfloat(n, rdbuf(), format_char, width(0), precision(), flags(), 0, fill()) < 0) set(ios::badbit|ios::failbit); // ??#else int negative; char buf[BUF]; int sign = '\0'; char *cp = buf; *cp = 0; int size = __cvt_double(n, precision(), flags() & ios::showpoint ? 0x80 : 0, &negative, format_char, cp, buf + sizeof(buf)); if (negative) sign = '-'; if (*cp == 0) cp++; // Calculate padding. int fieldsize = size + fpprec; if (sign) fieldsize++; int padding = 0; int w = width(0); if (fieldsize < w) padding = w - fieldsize; // Do actual output. register streambuf* sbuf = rdbuf(); register i; char fill_char = fill(); ios::fmtflags pad_kind = flags() & (ios::left|ios::right|ios::internal); if (pad_kind != (ios::fmtflags)ios::left // Default (right) adjust. && pad_kind != (ios::fmtflags)ios::internal) for (i = padding; --i >= 0; ) sbuf->sputc(fill_char); if (sign) sbuf->sputc(sign); if (pad_kind == (ios::fmtflags)ios::internal) for (i = padding; --i >= 0; ) sbuf->sputc(fill_char); // Emit the actual concented field, followed by extra zeros. sbuf->sputn(cp, size); for (i = fpprec; --i >= 0; ) sbuf->sputc('0'); if (pad_kind == (ios::fmtflags)ios::left) // Left adjustment for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);#endif osfx(); } return *this;}ostream& ostream::operator<<(const char *s){ if (opfx()) { if (s == NULL) s = "(null)"; int len = strlen(s); int w = width(0); char fill_char = fill(); register streambuf *sbuf = rdbuf(); register int padding = w > len ? w - len : 0; if (!(flags() & ios::left)) // Default adjustment. while (--padding >= 0) sbuf->sputc(fill_char); sbuf->sputn(s, len); if (flags() & ios::left) // Left adjustment. while (--padding >= 0) sbuf->sputc(fill_char); osfx(); } return *this;}ostream& ostream::operator<<(void *p){ if (opfx()) { form("%p", p); osfx(); } return *this;}ostream& ostream::operator<<(register streambuf* sbuf){ if (opfx()) { register streambuf* outbuf = rdbuf(); // FIXME: Should optimize! for (;;) { register int ch = sbuf->sbumpc(); if (ch == EOF) break; if (outbuf->sputc(ch) == EOF) { set(ios::badbit); break; } } osfx(); } return *this;}ostream::ostream(streambuf* sb, ostream* tied) : ios(sb, tied){ _flags |= ios::dont_close;}ostream& ostream::seekp(streampos pos){ pos = _strbuf->seekpos(pos, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return *this;}ostream& ostream::seekp(streamoff off, _seek_dir dir){ streampos pos = _strbuf->seekoff(off, dir, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return *this;}streampos ostream::tellp(){ streampos pos = _strbuf->seekoff(0, ios::cur, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return pos;}ostream& ostream::form(const char *format ...){ if (opfx()) { va_list ap; va_start(ap, format); _strbuf->vform(format, ap); va_end(ap); } return *this;}ostream& ostream::vform(const char *format, _G_va_list args){ if (opfx()) _strbuf->vform(format, args); return *this;}ostream& ostream::flush(){ if (_strbuf->sync()) set(ios::badbit); return *this;}ostream& flush(ostream& outs){ outs.rdbuf()->overflow(EOF); return outs;}istream& ws(istream& ins){ if (ins.ipfx1()) { int ch = skip_ws(ins._strbuf); if (ch == EOF) ins.set(ios::eofbit); else ins._strbuf->sputbackc(ch); } return ins;}// Skip white-space. Return 0 on failure (EOF), or 1 on success.// Differs from ws() manipulator in that failbit is set on EOF.// Called by ipfx() and ipfx0() if needed.int istream::_skip_ws(){ int ch = skip_ws(_strbuf); if (ch == EOF) { set(ios::eofbit|ios::failbit); return 0; } else { _strbuf->sputbackc(ch); return 1; }}ostream& ends(ostream& outs){ outs.put(0); return outs;}ostream& endl(ostream& outs){ return flush(outs.put('\n'));}ostream& ostream::write(const char *s, int n){ if (opfx()) { if (_strbuf->sputn(s, n) != n) set(ios::failbit); } return *this;}void ostream::do_osfx(){ if (flags() & ios::unitbuf) flush(); if (flags() & ios::stdio) { fflush(stdout); fflush(stderr); }}iostream::iostream(streambuf* sb, ostream* tied) : ios(sb, tied){ _flags |= ios::dont_close; _gcount = 0;}// NOTE: extension for compatibility with old libg++.// Not really compatible with fistream::close().#ifdef _STREAM_COMPATvoid ios::close(){ if (!(_flags & (unsigned int)ios::dont_close)) delete _strbuf; else if (_strbuf->_flags & _S_IS_FILEBUF) ((struct filebuf*)_strbuf)->close(); else if (_strbuf != NULL) _strbuf->sync(); _flags |= ios::dont_close; _strbuf = NULL; _state = badbit;}int istream::skip(int i){ int old = (_flags & ios::skipws) != 0; if (i) _flags |= ios::skipws; else _flags &= ~ios::skipws; return old;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -