Annotation of researchv10no/cmd/cfront/libC/ooiostream/oldformat.c, revision 1.1.1.1

1.1       root        1: /*ident        "@(#)ctrans:lib/stream/oldformat.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: oldformat.c:
                     12:            file for obsolete stream stuff that isn't part of the
                     13:            official iostream package.
                     14: *****************************************************************************/
                     15: 
                     16: #include <stream.h>
                     17: #include "streamdefs.h"
                     18: #include <string.h>
                     19: #include <strstream.h>
                     20: #include <fstream.h>
                     21: #include <libc.h>
                     22: 
                     23: #ifdef VSPRINTF
                     24: #      include <stdarg.h>
                     25: #endif
                     26: 
                     27: static const int cb_size = 1024 ;
                     28: static const int fld_size = 256 ;
                     29: 
                     30: /* a circular formating buffer */
                     31: static char    formbuf[cb_size];       // some slob for form overflow
                     32: static char*   bfree=formbuf;
                     33: static char*   max = &formbuf[cb_size-1];
                     34: 
                     35: char* chr(register i, register int w)  /* note: chr(0) is "" */
                     36: {
                     37:        register char* buf = bfree;
                     38: 
                     39:        if (w<=0 || fld_size<w) w = 1;
                     40:        w++;                            /* space for trailing 0 */
                     41:        if (max < buf+w) buf = formbuf;
                     42:        bfree = buf+w;
                     43:        char * res = buf;
                     44: 
                     45:        w -= 2;                         /* pad */
                     46:        while (w--) *buf++ = ' ';
                     47:        if (i<0 || 127<i) i = ' ';
                     48:        *buf++ = i;
                     49:        *buf = 0;
                     50:        return res;
                     51: }
                     52: 
                     53: char* str(const char* s, register int w)
                     54: {
                     55:        register char* buf = bfree;
                     56:        int ll = strlen(s);
                     57:        if (w<=0 || fld_size<w) w = ll;
                     58:        if (w < ll) ll = w;
                     59:        w++;                            /* space for traling 0 */
                     60:        if (max < buf+w) buf = formbuf;
                     61:        bfree = buf+w;
                     62:        char* res = buf;
                     63: 
                     64:        w -= (ll+1);                    /* pad */
                     65:        while (w--) *buf++ = ' ';
                     66:        while (*s) *buf++ = *s++;
                     67:        *buf = 0;
                     68:        return res;
                     69: }
                     70: 
                     71: char* form(const char* format ...)
                     72: {
                     73:        register char* buf = bfree;
                     74:        if (max < buf+fld_size) buf = formbuf;
                     75: 
                     76: #      ifdef VSPRINTF
                     77:                va_list args ;
                     78:                va_start(args,format) ;
                     79:                VSPRINTF(buf,format,args) ;
                     80:                va_end(args) ;
                     81: #      else
                     82:                // not very portable
                     83:                register* ap = (int*)((char*)&format+sizeof(char*));
                     84:                sprintf(buf,format,ap[0],ap[1],ap[2],ap[3],ap[4],ap[5],ap[6],ap[7],ap[8],ap[9]);        
                     85: #      endif
                     86: 
                     87:        register ll = strlen(buf);                      // not all sprintf's return length
                     88:        if (0<ll && ll<cb_size)                         // length
                     89:                ;
                     90:        else if (buf<(char*)ll && (char*)ll<buf+cb_size)// pointer to trailing 0
                     91:                ll = (char*)ll - buf;
                     92:        else
                     93:                ll = strlen(buf);
                     94: 
                     95:        // If we have scribbled beyond the end of the buffer then
                     96:        // who knows what data we've destroyed.  Better to abort here
                     97:        // here there is some chance that somebody can associate
                     98:        // the location with the error than to continue and die
                     99:        // a mysterious death later.
                    100:        if (fld_size < ll) abort();
                    101: 
                    102:        bfree = buf+ll+1;
                    103:        return buf;
                    104: }
                    105: 
                    106: const char a10 = 'a'-10;
                    107: 
                    108: char* hex(long ii, register w)
                    109: {
                    110:        int m = sizeof(long)*2;         // maximum hex digits for a long
                    111:        if (w<0 || fld_size<w) w = 0;
                    112:        int sz = (w?w:m)+1;
                    113:        register char* buf = bfree;
                    114:        if (max < buf+sz) buf = formbuf;
                    115:        register char* p = buf+sz;
                    116:        bfree = p+1;
                    117:        *p-- = 0;                       // trailing 0
                    118:        register unsigned long i = ii;
                    119: 
                    120:        if (w) {
                    121:                do {
                    122:                        register h = (int)(i&0xf);
                    123:                        *p-- = (h < 10) ? h+'0' : h+a10;
                    124:                } while (--w && (i>>=4));
                    125:                while (0<w--) *p-- = ' ';
                    126:        }
                    127:        else {
                    128:                do {
                    129:                        register h = (int)(i&0xf);
                    130:                        *p-- = (h < 10) ? h+'0' : h+a10;
                    131:                } while (i>>=4);
                    132:        }
                    133:        return p+1;
                    134: }
                    135: 
                    136: char* oct(long ii, int w)
                    137: {
                    138:        int m = sizeof(long)*3;         // maximum oct digits for a long
                    139:        if (w<0 || fld_size<w) w = 0;
                    140:        int sz = (w?w:m)+1;
                    141:        register char* buf = bfree;
                    142:        if (max < buf+sz) buf = formbuf;
                    143:        register char* p = buf+sz;
                    144:        bfree = p+1;
                    145:        *p-- = 0;                       // trailing 0
                    146:        register unsigned long i = ii;
                    147: 
                    148:        if (w) {
                    149:                do {
                    150:                        register h = (int)(i&07);
                    151:                        *p-- = h + '0';
                    152:                } while (--w && (i>>=3));
                    153:                while (0<w--) *p-- = ' ';
                    154:        }
                    155:        else {
                    156:                do {
                    157:                        register h = (int)(i&07);
                    158:                        *p-- = h+'0';
                    159:                } while (i>>=3);
                    160:        }
                    161: 
                    162:        return p+1;
                    163: }
                    164: 
                    165: char* dec(long i, int w)
                    166: {
                    167:        int sign = 0;
                    168:        if (i < 0) {
                    169:                sign = 1;
                    170:                i = -i;
                    171:        }       
                    172:        int m = sizeof(long)*3;         /* maximum dec digits for a long */
                    173:        if (w<0 || fld_size<w) w = 0;
                    174:        int sz = (w?w:m)+1;
                    175:        register char* buf = bfree;
                    176:        if (max < buf+sz) buf = formbuf;
                    177:        register char* p = buf+sz;
                    178:        bfree = p+1;
                    179:        *p-- = 0;                       /* trailing 0 */
                    180: 
                    181:        if (w) {
                    182:                do {
                    183:                        register h = (int)(i%10);
                    184:                        *p-- = h + '0';
                    185:                } while (--w && (i/=10));
                    186:                if (sign && 0<w) {
                    187:                        w--;
                    188:                        *p-- = '-';
                    189:                }
                    190:                while (0<w--) *p-- = ' ';
                    191:        }
                    192:        else {
                    193:                do {
                    194:                        register h = (int)(i%10);
                    195:                        *p-- = h + '0';
                    196:                } while (i/=10);
                    197:                if (sign) *p-- = '-';
                    198:        }
                    199: 
                    200:        return p+1;
                    201: }
                    202: 
                    203: istream& WS(istream& i) { return i >> ws ; }
                    204: 
                    205: void eatwhite(istream& i) { i >> ws ; }
                    206: 
                    207: istream::istream(streambuf* b, int sk, ostream* t) 
                    208: {
                    209:        init(b) ;
                    210:        skip(sk) ;
                    211:        tie(t) ;
                    212:        }
                    213: 
                    214: istream::istream(int len, char* b, int sk) 
                    215: {
                    216:        init( new streambuf(b,len,-len) ) ;
                    217:        delbuf = 1 ;
                    218:        skip(sk) ;
                    219: }
                    220: 
                    221: istream::istream(int fd, int sk, ostream* t) 
                    222: {
                    223:        init( new filebuf(fd) ) ;
                    224:        delbuf = 1 ;
                    225:        skip(sk) ;
                    226:        tie(t) ;
                    227: }
                    228: 
                    229: ostream::ostream(int fd) 
                    230: {
                    231:        init( new filebuf(fd) ) ;
                    232:        delbuf = 1 ;
                    233: }
                    234: 
                    235: ostream::ostream(int len, char* b) 
                    236: {
                    237:        init( new streambuf(b,len,0) ) ;
                    238:        delbuf = 1 ;
                    239: }
                    240: 
                    241: streambuf::streambuf(char* p, int l, int c ) :
                    242:        x_unbuf(0), alloc(0)
                    243: {
                    244:        setb(0,0,0);
                    245:        setbuf(p,l,c) ;
                    246: }
                    247: 
                    248: streambuf* streambuf::setbuf(char* p, int len, int count)
                    249: {
                    250:        // Three argument setbuf for compatibility with old
                    251:        // stream package.
                    252:        if ( x_base ) return 0 ;
                    253:        if ( len <= 0 || p == 0 ) {
                    254:                // make it unbuffered
                    255:                setb(0,0,0) ;
                    256:                setg(0,0,0) ;
                    257:                setp(0,0);
                    258:                unbuffered(1) ;
                    259:        } else if ( count >= 0 ) {
                    260:                setb(p,p+len,0) ;
                    261:                setg(p,p,p+count) ;
                    262:                setp(p+count,p+len) ;
                    263:                unbuffered(0) ;
                    264:        } else  { // count < 0 
                    265:                // Special case,
                    266:                // Used by iostream::iostream(char*,int) 
                    267:                // For backwards compatibility with old streams
                    268:                setb(p,p+len,0) ;
                    269:                setg(p,p,p-count) ;
                    270:                setp(p,p+len) ;
                    271:                unbuffered(0) ;
                    272:                }
                    273:        return this;
                    274:        }
                    275: 
                    276: int ios::skip(int sk)
                    277: {
                    278:        long f ;
                    279:        if ( sk ) f = setf(skipws,skipws) ;
                    280:        else      f = setf(0,skipws) ;
                    281:        
                    282:        return (f&skipws) != 0 ;
                    283: }

unix.superglobalmegacorp.com

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