⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iostream.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
//    This is part of the iostream library, providing input/output for C++.//    Copyright (C) 1991, 1992 Per Bothner.////    This library is free software; you can redistribute it and/or//    modify it under the terms of the GNU Library General Public//    License as published by the Free Software Foundation; either//    version 2 of the License, 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//    Library General Public License for more details.////    You should have received a copy of the GNU Library General Public//    License along with this library; if not, write to the Free//    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.#ifdef __GNUG__#pragma implementation#endif#define _STREAM_COMPAT#include "ioprivate.h"#include <iostream.h>#include <stdio.h>  /* Needed for sprintf */#include <ctype.h>#include <limits.h>#include "floatio.h"#define	BUF		(MAXEXP+MAXFRACT+1)	/* + decimal point *///#define isspace(ch) ((ch)==' ' || (ch)=='\t' || (ch)=='\n')istream::istream(streambuf *sb, ostream* tied) : ios(sb, tied){    _flags |= ios::dont_close;    _gcount = 0;}int skip_ws(streambuf* sb){    int ch;    for (;;) {	ch = sb->sbumpc();	if (ch == EOF || !isspace(ch))	    return ch;    }}istream& istream::get(char& c){    if (ipfx1()) {	int ch = _strbuf->sbumpc();	if (ch == EOF) set(ios::eofbit|ios::failbit);	else c = (char)ch;    }    return *this;}istream& istream::ignore(int n /* = 1 */, int delim /* = EOF */){    if (ipfx1()) {	register streambuf* sb = _strbuf;	if (delim == EOF) {	    _gcount = sb->ignore(n);	    return *this;	}	_gcount = 0;	for (;;) {#if 0	    if (n != MAXINT) // FIXME#endif	    if (--n < 0)		break;	    int ch = sb->sbumpc();	    if (ch == EOF) {		set(ios::eofbit|ios::failbit);		break;	    }	    _gcount++;	    if (ch == delim)		break;	}    }    return *this;}istream& istream::read(char *s, int n){    if (ipfx1()) {	_gcount = _strbuf->sgetn(s, n);	if (_gcount != n)	    set(ios::failbit);    }    return *this;}istream& istream::seekg(streampos pos){    pos = _strbuf->seekpos(pos, ios::in);    if (pos == streampos(EOF))	set(ios::badbit);    return *this;}istream& istream::seekg(streamoff off, _seek_dir dir){    streampos pos = _strbuf->seekoff(off, dir, ios::in);    if (pos == streampos(EOF))	set(ios::badbit);    return *this;}streampos istream::tellg(){    streampos pos = _strbuf->seekoff(0, ios::cur, ios::in);    if (pos == streampos(EOF))	set(ios::badbit);    return pos;}istream& istream::scan(const char *format ...){    if (ipfx0()) {	va_list ap;	va_start(ap, format);	_strbuf->vscan(format, ap, this);	va_end(ap);    }    return *this;}istream& istream::vscan(const char *format, _G_va_list args){    if (ipfx0())	_strbuf->vscan(format, args, this);    return *this;}istream& istream::operator>>(char& c){    if (ipfx0()) {	int ch = _strbuf->sbumpc();	if (ch == EOF)	    set(ios::eofbit|ios::failbit);	else	    c = (char)ch;    }    return *this;}istream& istream::operator>>(char* ptr){    if (ipfx0()) {	register streambuf* sb = _strbuf;	int ch = sb->sbumpc();	if (ch == EOF)	    set(ios::eofbit|ios::failbit);	else {	    int w = width(0);	    *ptr++ = ch;	    for (;;) {		ch = sb->sbumpc();		if (ch == EOF) {		    set(ios::eofbit|ios::failbit);		    break;		}		else if (isspace(ch)) {		    sb->sputbackc(ch);		    break;		}		else if (w == 1) {		    set(ios::failbit);		    sb->sputbackc(ch);		    break;		}		else *ptr++ = ch;		w--;	    }	}    }    *ptr = '\0';    return *this;}#ifdef __GNUC__#define LONGEST long long#else#define LONGEST long#endifstatic int read_int(istream& stream, unsigned LONGEST& val, int& neg){    if (!stream.ipfx0())	return 0;    register streambuf* sb = stream.rdbuf();    int base = 10;    int ndigits = 0;    register int ch = skip_ws(sb);    if (ch == EOF)	goto eof_fail;    neg = 0;    if (ch == '+') {	ch = skip_ws(sb);    }    else if (ch == '-') {	neg = 1;	ch = skip_ws(sb);    }    if (ch == EOF) goto eof_fail;    if (!(stream.flags() & ios::basefield)) {	if (ch == '0') {	    ch = sb->sbumpc();	    if (ch == EOF) {		val = 0;		return 1;	    }	    if (ch == 'x' || ch == 'X') {		base = 16;		ch = sb->sbumpc();		if (ch == EOF) goto eof_fail;	    }	    else {		sb->sputbackc(ch);		base = 8;		ch = '0';	    }	}    }    else if ((stream.flags() & ios::basefield) == ios::hex)	base = 16;    else if ((stream.flags() & ios::basefield) == ios::oct)	base = 8;    val = 0;    for (;;) {	if (ch == EOF)	    break;	int digit;	if (ch >= '0' && ch <= '9')	    digit = ch - '0';	else if (ch >= 'A' && ch <= 'F')	    digit = ch - 'A' + 10;	else if (ch >= 'a' && ch <= 'f')	    digit = ch - 'a' + 10;	else	    digit = 999;	if (digit >= base) {	    sb->sputbackc(ch);	    if (ndigits == 0)		goto fail;	    else		return 1;	}	ndigits++;	val = base * val + digit;	ch = sb->sbumpc();    }    return 1;  fail:    stream.set(ios::failbit);    return 0;  eof_fail:    stream.set(ios::failbit|ios::eofbit);    return 0;}#define READ_INT(TYPE) \istream& istream::operator>>(TYPE& i)\{\    unsigned LONGEST val; int neg;\    if (read_int(*this, val, neg)) {\	if (neg) val = -val;\	i = (TYPE)val;\    }\    return *this;\}READ_INT(short)READ_INT(unsigned short)READ_INT(int)READ_INT(unsigned int)READ_INT(long)READ_INT(unsigned long)#ifdef __GNUG__READ_INT(long long)READ_INT(unsigned long long)#endifistream& istream::operator>>(double& x){    if (ipfx0())	scan("%lg", &x);    return *this;}istream& istream::operator>>(float& x){    if (ipfx0())	scan("%g", &x);    return *this;}istream& istream::operator>>(register streambuf* sbuf){    if (ipfx0()) {	register streambuf* inbuf = rdbuf();	// FIXME: Should optimize!	for (;;) {	    register int ch = inbuf->sbumpc();	    if (ch == EOF) {		set(ios::eofbit);		break;	    }	    if (sbuf->sputc(ch) == EOF) {		set(ios::failbit);		break;	    }	}    }    return *this;}ostream& ostream::operator<<(char c){    if (opfx()) {	int w = width(0);	char fill_char = fill();	register int padding = w > 0 ? w - 1 : 0;	register streambuf *sb = _strbuf;	if (!(flags() & ios::left)) // Default adjustment.	    while (--padding >= 0) sb->sputc(fill_char);	sb->sputc(c);	if (flags() & ios::left) // Left adjustment.	    while (--padding >= 0) sb->sputc(fill_char);	osfx();    }    return *this;}static void write_int(ostream& stream, unsigned LONGEST val, int neg){#define WRITE_BUF_SIZE (10 + sizeof(unsigned LONGEST) * 3)    char buf[WRITE_BUF_SIZE];    register char *buf_ptr = buf+WRITE_BUF_SIZE; // End of buf.    char *show_base = "";    int show_base_len = 0;    // Now do the actual conversion, placing the result at the *end* of buf.    // Note that we use separate code for decimal, octal, and hex,    // so we can divide by optimizable constants.    if ((stream.flags() & ios::basefield) == ios::oct) { // Octal	if ((stream.flags() & ios::showbase) && (val != 0))	    show_base = "0", show_base_len = 1;	do {	    *--buf_ptr = (val & 7) + '0';	    val = val >> 3;	} while (val != 0);    }    else if ((stream.flags() & ios::basefield) == ios::hex) { // Hex	char *xdigs = (stream.flags() & ios::uppercase) ? "0123456789ABCDEF0X"	    : "0123456789abcdef0x";	do {	    *--buf_ptr = xdigs[val & 15];	    val = val >> 4;	} while (val != 0);	if (stream.flags() & ios::showbase) {	    show_base = xdigs + 16; // Either "0X" or "0x".	    show_base_len = 2;	}    }    else { // Decimal#ifdef __GNUC__	// Optimization:  Only use long long when we need to.	while (val > UINT_MAX) {	    *--buf_ptr = (val % 10) + '0';	    val /= 10;	}	// Use more efficient (int) arithmetic for the rest.	register unsigned int ival = (unsigned int)val;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -