Annotation of researchv8dc/cmd/cfront/libC/stream/in.c, revision 1.1.1.1

1.1       root        1: /*
                      2:        C++ stream i/o source
                      3: 
                      4:        in.c
                      5: */
                      6: #include <ctype.h>
                      7: #include "stream.h"
                      8: #include <common.h>
                      9: 
                     10: filebuf cin_file(0);                   // UNIX input stream 1
                     11: istream cin(&cin_file,1,&cout);                // cin tied to cout
                     12: 
                     13: /* predefined whitespace */
                     14: whitespace WS;
                     15: 
                     16: /*inline */void eatwhite (istream& is)
                     17: {
                     18:        if (is.tied_to) is.tied_to->flush();
                     19:        register streambuf *nbp = is.bp;
                     20:        register char c = nbp->sgetc();
                     21:        while (isspace(c)) c = nbp->snextc();
                     22: }
                     23: 
                     24: istream& istream.operator>>(whitespace&)
                     25: {
                     26:        register streambuf *nbp = bp;
                     27: 
                     28:        if (state) return *this;
                     29:        if (tied_to) tied_to->flush();
                     30:        register c = nbp->sgetc();
                     31:        while (isspace(c)) c = nbp->snextc();
                     32:        if (c == EOF) state |= _eof;
                     33:        return *this;
                     34: }
                     35: 
                     36: istream& istream.operator>>(register char& s)
                     37: /*
                     38:        reads characters NOT very small integers
                     39:  */
                     40: {
                     41:        if (skipws) eatwhite(*this);
                     42: 
                     43:        if (state) {
                     44:                state |= _fail;
                     45:                return *this;
                     46:        }
                     47: 
                     48:        register c = bp->sgetc();
                     49:        if (c == EOF) {
                     50:                state |= _fail|_eof;
                     51:                return *this;
                     52:        }
                     53: 
                     54:        if (bp->snextc() == EOF) state |= _eof;
                     55:        s = c;
                     56:        return *this;
                     57: }
                     58: 
                     59: istream& istream.operator>>(register char* s)
                     60: {
                     61:        register streambuf *nbp = bp;
                     62: 
                     63:        if (skipws) eatwhite(*this);
                     64: 
                     65:        if (state) {
                     66:                state |= _fail;
                     67:                return *this;
                     68:        }
                     69: 
                     70:        /* get string */
                     71:        register c = nbp->sgetc();
                     72:        if (c == EOF) state |= _fail;
                     73:        while (!isspace(c) && c != EOF) {
                     74:                *s++ = c;
                     75:                c = nbp->snextc();
                     76:        }
                     77:        *s = '\0';
                     78: 
                     79:        if (c == EOF) state |= _eof;
                     80: 
                     81:        return *this;
                     82: }
                     83: 
                     84: istream&
                     85: istream.operator>>(long& i)
                     86: {
                     87:        register c;
                     88:        register ii = 0;
                     89:        register streambuf *nbp = bp;
                     90:        int neg = 0;
                     91: 
                     92:        if (skipws) eatwhite(*this);
                     93: 
                     94:        if (state) {
                     95:                state |= _fail;
                     96:                return *this;
                     97:        }
                     98: 
                     99:        switch (c = nbp->sgetc()) {
                    100:        case '-':
                    101:        case '+':
                    102:                neg = c;
                    103:                c = nbp->snextc();
                    104:                break;
                    105:        case EOF:
                    106:                state |= _fail;
                    107:        }
                    108: 
                    109:        if (isdigit(c)) {
                    110:                do {
                    111:                        ii = ii*10+c-'0';
                    112:                } while (isdigit(c=nbp->snextc()));
                    113:                i = (neg=='-') ? -ii : ii;
                    114:        } else
                    115:                state |= _fail;
                    116: 
                    117:        if (c == EOF) state |= _eof;
                    118:        return *this;
                    119: }
                    120: 
                    121: istream&
                    122: istream.operator>>(int& i)
                    123: {
                    124:        long l;
                    125: 
                    126:        if (skipws) eatwhite(*this);
                    127: 
                    128:        if (state) {
                    129:                state |= _fail;
                    130:                return *this;
                    131:        }
                    132: 
                    133:        if ( *this>>l ) {
                    134:                i = l;
                    135:        }
                    136:        return *this;
                    137: }
                    138: 
                    139: istream&
                    140: istream.operator>>(short& i)
                    141: {
                    142:        long l;
                    143: 
                    144:        if (skipws) eatwhite(*this);
                    145: 
                    146:        if (state) {
                    147:                state |= _fail;
                    148:                return *this;
                    149:        }
                    150: 
                    151:        if ( *this>>l ) {
                    152:                i = l;
                    153:        }
                    154:        return *this;
                    155: }
                    156: 
                    157: istream&
                    158: istream.operator>>(double& d)
                    159: /*
                    160:        {+|-} d* {.} d* { e|E {+|-} d+ } 
                    161:        except that
                    162:                - a dot must be pre- or succeded by at least one digit
                    163:                - an exponent must be preseded by at least one digit
                    164: */
                    165: {
                    166:        register c = 0;
                    167:        char buf[256];
                    168:        register char* p = buf;
                    169:        register streambuf* nbp = bp;
                    170:        extern double atof(char*);
                    171: 
                    172:        if (skipws) eatwhite(*this);
                    173: 
                    174:        if (state) {
                    175:                state |= _fail;
                    176:                return *this;
                    177:        }
                    178: 
                    179:        /* get the sign */
                    180:        switch (c = nbp->sgetc()) {
                    181:        case EOF:
                    182:                state = _eof|_fail;
                    183:                return *this;
                    184:        case '-':
                    185:        case '+':
                    186:                *p++ = c;
                    187:                c = bp->snextc();
                    188:        }
                    189: 
                    190:        /* get integral part */
                    191:        while (isdigit(c)) {
                    192:                *p++ = c;
                    193:                c = bp->snextc();
                    194:        }
                    195: 
                    196:        /* get fraction */
                    197:        if (c == '.') {
                    198:                do {
                    199:                        *p++ = c;
                    200:                        c = bp->snextc();
                    201:                } while (isdigit(c));
                    202:        }
                    203: 
                    204:        /* get exponent */
                    205:        if (c == 'e' || c == 'E') {
                    206:                *p++ = c;
                    207:                switch (c = nbp->snextc()) {
                    208:                case EOF:
                    209:                        state = _eof|_fail;
                    210:                        return *this;
                    211:                case '-':
                    212:                case '+':
                    213:                        *p++ = c;
                    214:                        c = bp->snextc();
                    215:                }
                    216:                while (isdigit(c)) {
                    217:                        *p++ = c;
                    218:                        c = bp->snextc();
                    219:                }
                    220:        }
                    221: 
                    222:        *p = 0;
                    223:        d = atof(buf);
                    224: 
                    225:        if (c == EOF) state |= _eof;
                    226:        return *this;
                    227: }
                    228: 
                    229: istream&
                    230: istream.operator>>(float& f)
                    231: {
                    232:        double d;
                    233: 
                    234:        if (skipws) eatwhite(*this);
                    235: 
                    236:        if (state) {
                    237:                state |= _fail;
                    238:                return *this;
                    239:        }
                    240: 
                    241:        if ( *this>>d ) {
                    242:                f = d;
                    243:        }
                    244:        return *this;
                    245: }
                    246: 
                    247: istream&
                    248: istream.get(
                    249:        register char* s,       /* character array to read into */
                    250:        register int len,       /* size of character array */
                    251:        register char term      /* character that terminates input */
                    252: ) {
                    253:        register c;
                    254:        register streambuf *nbp = bp;
                    255: 
                    256:        if (state) {
                    257:                state |= _fail;
                    258:                return *this;
                    259:        }
                    260: 
                    261:        if ((c = bp->sgetc()) == EOF) {
                    262:                state |= _fail | _eof;
                    263:                return *this;
                    264:        }
                    265: 
                    266:        while (c != term && c != EOF && len > 1) {
                    267:                *s++ = c;
                    268:                c = nbp->snextc();
                    269:                len--;
                    270:        }
                    271:        *s = '\0';
                    272:        if (c == EOF) state |= _eof;
                    273:        return *this;
                    274: }
                    275: 
                    276: istream& istream.putback(register char c)
                    277: {
                    278:        bp->sputbackc(c);
                    279:        return *this;
                    280: }
                    281: 
                    282: 
                    283: istream& istream.get(
                    284:        register streambuf &s,  /* streambuf to input to */
                    285:        register char term      /* termination character */
                    286: ){
                    287:        register c;
                    288:        register streambuf *nbp = bp;
                    289: 
                    290:        if (state) {
                    291:                state |= _fail;
                    292:                return *this;
                    293:        }
                    294: 
                    295:        if ((c = bp->sgetc()) == EOF) {
                    296:                state |= _fail | _eof;
                    297:                return *this;
                    298:        }
                    299: 
                    300:        while (c != term && c != EOF) {
                    301:                if (s.sputc(c) == EOF) break;
                    302:                c = nbp->snextc();
                    303:        }
                    304:        if (c == EOF) state |= _eof;
                    305:        return *this;
                    306: }
                    307: 
                    308: istream&
                    309: istream.operator>>(register streambuf &s) {
                    310:        register c;
                    311:        register streambuf *nbp = bp;
                    312: 
                    313:        if (state) {
                    314:                state |= _fail;
                    315:                return *this;
                    316:        }
                    317: 
                    318:        if ((c = bp->sgetc()) == EOF) {
                    319:                state |= _fail | _eof;
                    320:                return *this;
                    321:        }
                    322: 
                    323:        while (c != EOF) {
                    324:                if (s.sputc(c) == EOF) break;
                    325:                c = nbp->snextc();
                    326:        }
                    327:        if (c == EOF) state |= _eof;
                    328:        return *this;
                    329: }
                    330: 
                    331: istream& istream.operator>>(common& p)
                    332: {
                    333:        return p.read(*this);
                    334: }
                    335: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.