Annotation of researchv10no/cmd/cfront/libC/iostream/flt.c, revision 1.1

1.1     ! root        1: /*ident        "@(#)ctrans:lib/stream/flt.c    1.1.4.1" */
        !             2: /**************************************************************************
        !             3:                         Copyright (c) 1984 AT&T
        !             4:                           All Rights Reserved   
        !             5: 
        !             6:         THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
        !             7:         
        !             8:         The copyright notice above does not evidence any        
        !             9:         actual or intended publication of such source code.
        !            10: 
        !            11: flt.c:
        !            12: 
        !            13: *****************************************************************************/
        !            14: 
        !            15: #include <iostream.h>
        !            16: #include <ctype.h>
        !            17: #include <stdio.h>
        !            18: #include <libc.h>
        !            19: #include <string.h>
        !            20: 
        !            21: #define OSTREAM ostream
        !            22: #define ISTREAM istream
        !            23: 
        !            24: // This file contains all the functions having to do with i/o of
        !            25: // floats and doubles.  It drags in lots of stuff from stdio, which
        !            26: // is why I made it separate.
        !            27: 
        !            28: istream& ISTREAM::operator>>(double& d)
        !            29: /*
        !            30:        {+|-} d* {.} d* { e|E {+|-} d+ } 
        !            31:        except that
        !            32:                - a dot must be pre- or succeeded by at least one digit
        !            33:                - an exponent must be preceded by at least one digit
        !            34: */
        !            35: {
        !            36:        register c = 0;
        !            37:        register anydigits = 0 ;
        !            38:        char buf[256];
        !            39:        register char* p = buf;
        !            40:        register streambuf* nbp = bp;
        !            41:        
        !            42:        if (!ipfx() ) return *this ;
        !            43: 
        !            44:        /* get the sign */
        !            45:        switch (c = nbp->sgetc()) {
        !            46:        case EOF:
        !            47:                setstate(eofbit|failbit) ;
        !            48:                return *this;
        !            49:        case '-':
        !            50:        case '+':
        !            51:                *p++ = c;
        !            52:                c = bp->snextc();
        !            53:        }
        !            54: 
        !            55:        /* get integral part */
        !            56:        while (isdigit(c)) {
        !            57:                *p++ = c;
        !            58:                c = bp->snextc();
        !            59:                anydigits = 1 ;
        !            60:        }
        !            61: 
        !            62:        /* get fraction */
        !            63:        if (c == '.') {
        !            64:                do {
        !            65:                        *p++ = c;
        !            66:                        c = bp->snextc();
        !            67:                        anydigits = 1 ;
        !            68:                } while (isdigit(c));
        !            69:        }
        !            70: 
        !            71:        /* get exponent */
        !            72:        if ( anydigits && (c == 'e' || c == 'E')) {
        !            73:                *p++ = c;
        !            74:                switch (c = nbp->snextc()) {
        !            75:                case EOF:
        !            76:                        setstate(eofbit|failbit) ;
        !            77:                        return *this;
        !            78:                case '-':
        !            79:                case '+':
        !            80:                        *p++ = c;
        !            81:                        c = bp->snextc();
        !            82:                }
        !            83:                while (isdigit(c)) {
        !            84:                        *p++ = c;
        !            85:                        c = bp->snextc();
        !            86:                }
        !            87:        }
        !            88: 
        !            89:        *p = 0;
        !            90:        d = atof(buf);
        !            91: 
        !            92:        if (c == EOF) setstate(eofbit) ;
        !            93:        if (!anydigits) setstate(badbit) ;
        !            94:        return *this;
        !            95: }
        !            96: 
        !            97: 
        !            98: istream& ISTREAM::operator>>(float& f)
        !            99: {
        !           100:        double d;
        !           101: 
        !           102: 
        !           103:        if (!ipfx() ) return *this ;
        !           104: 
        !           105:        *this >> d ;
        !           106:        if ( good() ) f = d;
        !           107: 
        !           108:        return *this;
        !           109: }
        !           110: 
        !           111: ostream& OSTREAM::operator<<(float f)  { return *this << (double)f ; }
        !           112: 
        !           113: static const int fltbits =
        !           114:                ios::scientific|ios::fixed|ios::uppercase ;
        !           115: 
        !           116: ostream& OSTREAM::operator<<(double d)
        !           117: {
        !           118:        char buf[32];
        !           119:        int w = width(0) ;
        !           120:        int p = precision() ;
        !           121:        int explicitp = ( p > 0 ) || (flags()&ios::fixed) ;
        !           122:        if (!opfx()) return *this;
        !           123:        long bits = flags() & fltbits ;
        !           124:        char fmt[20] ;
        !           125:        strcpy(fmt,"%") ;
        !           126:        if ( flags() & ios::showpoint ) {
        !           127:                strcat(fmt,"#") ;
        !           128:                }
        !           129:        if ( flags() & ios::showpos ) {
        !           130:                strcat(fmt,"+") ;
        !           131:                }
        !           132:        if ( explicitp ) {
        !           133:                strcat(fmt,".*") ;
        !           134:                }
        !           135: 
        !           136:        long mode = flags()
        !           137:                    & (ios::scientific|ios::fixed|ios::uppercase);
        !           138:        
        !           139:        switch(mode) {
        !           140:        case ios::scientific :          strcat(fmt,"e") ; break ;
        !           141:        case ios::scientific|ios::uppercase :
        !           142:                                                strcat(fmt,"E") ; break;
        !           143:        case ios::fixed :                       strcat(fmt,"f") ; break ;
        !           144:        case ios::fixed|ios::uppercase: strcat(fmt,"F") ; break ;
        !           145:        case ios::uppercase :           strcat(fmt,"G") ; break ;
        !           146:        default :                               strcat(fmt,"g") ; break ;
        !           147:        }
        !           148: 
        !           149:        if ( explicitp ) {
        !           150:                sprintf(buf,fmt,p,d) ;
        !           151:                }
        !           152:        else {
        !           153:                sprintf(buf,fmt,d) ;
        !           154:                }
        !           155: 
        !           156:        int padf = w-strlen(buf) ;
        !           157:        if ( padf < 0 ) padf = 0 ;
        !           158:        if ( padf && !(flags()&ios::left) ) {
        !           159:                while ( padf-- > 0 ) put(fill()) ;
        !           160:                }
        !           161:        *this << buf ;
        !           162:        while ( padf-- > 0 ) put(fill()) ;
        !           163:        osfx() ;
        !           164:        return *this ;
        !           165:        }

unix.superglobalmegacorp.com

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