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

unix.superglobalmegacorp.com

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