|
|
1.1 root 1: /*ident "@(#)ctrans:incl/iostream.h 1.1.5.2" */
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: *****************************************************************************/
12: #ifndef IOSTREAMH
13: #define IOSTREAMH
14:
15: #include <memory.h>
16: /* Some inlines use memcpy */
17:
18: /* If EOF is defined already verify that it is -1. Otherwise
19: * define it.
20: */
21:
22: #ifdef EOF
23: # if EOF!=-1
24: # define EOF (-1)
25: # endif
26: #else
27: # define EOF (-1)
28: #endif
29:
30: /* Don't worry about NULL not being 0 */
31: #ifndef NULL
32: # define NULL 0
33: #endif
34:
35: #define zapeof(c) ((c)&0377)
36: /* extracts char from c. The critical requirement is
37: * zapeof(EOF)!=EOF
38: * ((c)&0377) and ((unsigned char)(c)) are alternative definitions
39: * whose efficiency depends on compiler environment.
40: */
41:
42: typedef long streampos ;
43: typedef long streamoff ;
44:
45: class streambuf ;
46: class ostream ;
47:
48:
49: class ios {
50: public: /* Some enums are declared in ios to avoid pollution of
51: * global namespace
52: */
53: enum io_state { goodbit=0, eofbit=1, failbit=2, badbit=4,
54: hardfail=0200};
55: /* hard fail can be set and reset internally,
56: * but not via public function */
57: enum open_mode { in=1, out=2, ate=4, app=010, trunc=020,
58: nocreate=040, noreplace=0100} ;
59: enum seek_dir { beg=0, cur=1, end=2 } ;
60:
61: /* flags for controlling format */
62: enum { skipws=01,
63: /* skip whitespace on input */
64: left=02, right=04, internal=010,
65: /* padding location */
66: dec=020, oct=040, hex=0100,
67: /* conversion base */
68: showbase=0200, showpoint=0400, uppercase=01000,
69: showpos=02000,
70: /* modifiers */
71: scientific=04000, fixed=010000,
72: /* floating point notation */
73: unitbuf=020000, stdio=040000
74: /* stuff to control flushing */
75: } ;
76: static const long
77: basefield ; /* dec|oct|hex */
78: static const long
79: adjustfield ; /* left|right|internal */
80: static const long
81: floatfield ; /* scientific|fixed */
82: public:
83: ios(streambuf*) ;
84: virtual ~ios() ;
85:
86: long flags() { return x_flags ; }
87: long flags(long f);
88:
89: long setf(long setbits, long field);
90: long setf(long) ;
91: long unsetf(long) ;
92:
93: int width() { return x_width ; }
94: int width(int w)
95: {
96: int i = x_width ; x_width = w ; return i ;
97: }
98:
99: ostream* tie(ostream* s);
100: ostream* tie() { return x_tie ; }
101: char fill(char) ;
102: char fill() { return x_fill ; }
103: int precision(int) ;
104: int precision() { return x_precision ; }
105:
106: int rdstate() { return state ; }
107: operator void*()
108: {
109: if (state&(failbit|badbit|hardfail)) return 0 ;
110: else return this ;
111: }
112:
113: int operator!()
114: { return state&(failbit|badbit|hardfail); }
115: int eof() { return state&eofbit; }
116: int fail() { return state&(failbit|badbit|hardfail); }
117: int bad() { return state&badbit ; }
118: int good() { return state==0 ; }
119: void clear(int i =0)
120: {
121: state = (i&0377) | (state&hardfail) ;
122: ispecial = (ispecial&~0377) | state ;
123: ospecial = (ospecial&~0377) | state ;
124: }
125: streambuf* rdbuf() { return bp ;}
126:
127: public: /* Members related to user allocated bits and words */
128: long & iword(int) ;
129: void* & pword(int) ;
130: static long bitalloc() ;
131: static long xalloc() ;
132:
133: private: /*** privates for implemting allocated bits and words */
134: static long nextbit ;
135: static long nextword ;
136:
137: int nuser ;
138: union ios_user_union*
139: x_user ;
140: void uresize(int) ;
141: public: /* static member functions */
142: static void sync_with_stdio() ;
143: protected:
144: enum { skipping=01000, tied=02000 } ;
145: /*** bits 0377 are reserved for userbits ***/
146: streambuf* bp;
147: void setstate(int b)
148: { state |= (b&0377) ;
149: ispecial |= b&~skipping ;
150: ispecial |= b ;
151: }
152: int state;
153: int ispecial;
154: int ospecial;
155: int isfx_special;
156: int osfx_special;
157: int delbuf;
158: ostream* x_tie;
159: long x_flags;
160: short x_precision;
161: char x_fill;
162: short x_width;
163:
164: static void (*stdioflush)() ;
165:
166: void init(streambuf*) ;
167: /* Does the real work of a constructor */
168: ios() ; /* No initialization at all. Needed by
169: * multiple inheritance versions */
170: int assign_private ;
171: /* needed by with_assgn classes */
172: private:
173: ios(ios&) ; /* Declared but not defined */
174: void operator=(ios&) ; /* Declared but not defined */
175: public: /* old stream package compatibility */
176: int skip(int i) ;
177: };
178:
179: class streambuf {
180: short alloc;
181: short x_unbuf;
182: char* x_base;
183: char* x_pbase;
184: char* x_pptr;
185: char* x_epptr;
186: char* x_gptr;
187: char* x_egptr;
188: char* x_eback;
189: int x_blen;
190: private:
191: streambuf(streambuf&); /* Declared but not defined */
192: void operator=(streambuf&); /* Declared but not defined */
193: public:
194: void dbp();
195: protected:
196: char* base() { return x_base ; }
197: char* pbase() { return x_pbase ; }
198: char* pptr() { return x_pptr ; }
199: char* epptr() { return x_epptr ; }
200: char* gptr() { return x_gptr ; }
201: char* egptr() { return x_egptr ; }
202: char* eback() { return x_eback ; }
203: char* ebuf() { return x_base+x_blen ; }
204: int blen() { return x_blen; }
205: void setp(char* p, char* ep)
206: {
207: x_pbase=x_pptr=p ; x_epptr=ep ;
208: }
209: void setg(char* eb,char* g, char* eg)
210: {
211: x_eback=eb; x_gptr=g ; x_egptr=eg ;
212: }
213: void pbump(int n)
214: {
215: x_pptr+=n ;
216: }
217:
218: void gbump(int n)
219: {
220: x_gptr+=n ;
221: }
222:
223: void setb(char* b, char* eb, int a = 0 )
224: {
225: if ( alloc && x_base ) delete x_base ;
226: x_base = b ;
227: x_blen= (eb>b) ? (eb-b) : 0 ;
228: alloc = a ;
229: }
230: int unbuffered() { return x_unbuf; }
231: void unbuffered(int unb) { x_unbuf = (unb!=0) ; }
232: int allocate()
233: {
234: if ( x_base== 0 && !unbuffered() ) return doallocate() ;
235: else return 0 ;
236: }
237: virtual int doallocate();
238: public :
239: virtual int overflow(int c=EOF);
240: virtual int underflow();
241: virtual int pbackfail(int c);
242: virtual int sync();
243: virtual streampos
244: seekoff(streamoff,ios::seek_dir,int =ios::in|ios::out);
245: virtual streampos
246: seekpos(streampos, int =ios::in|ios::out) ;
247: virtual int xsputn(const char* s,int n);
248: virtual int xsgetn(char* s,int n);
249:
250: int in_avail()
251: {
252: return x_gptr<x_egptr ? x_egptr-x_gptr : 0 ;
253: }
254:
255: int out_waiting()
256: {
257: if ( x_pptr ) return x_pptr-x_pbase ;
258: else return 0 ;
259: }
260:
261: int sgetc()
262: {
263: /***WARNING: sgetc does not bump the pointer ***/
264: return (x_gptr>=x_egptr) ? underflow() : zapeof(*x_gptr);
265: }
266: int snextc()
267: {
268: return (++x_gptr>=x_egptr)
269: ? x_snextc()
270: : zapeof(*x_gptr);
271: }
272: int sbumpc()
273: {
274: return ( x_gptr>=x_egptr && underflow()==EOF )
275: ? EOF
276: : zapeof(*x_gptr++) ;
277: }
278: void stossc()
279: {
280: if ( x_gptr++ > x_egptr ) underflow() ;
281: }
282:
283: int sputbackc(char c)
284: {
285: if (x_gptr > x_eback ) {
286: if ( *--x_gptr == c ) return zapeof(c) ;
287: else return zapeof(*x_gptr=c) ;
288: } else {
289: return pbackfail(c) ;
290: }
291: }
292:
293: int sputc(int c)
294: {
295: return (x_pptr>=x_epptr) ? overflow(zapeof(c))
296: : zapeof(*x_pptr++=c);
297: }
298: int sputn(const char* s,int n)
299: {
300: if ( n <= (x_epptr-x_pptr) ) {
301: memcpy(x_pptr,s,n) ;
302: pbump(n);
303: return n ;
304: } else {
305: return xsputn(s,n) ;
306: }
307: }
308: int sgetn(char* s,int n)
309: {
310: if ( n <= (x_egptr-x_gptr) ) {
311: memcpy(s,x_gptr,n) ;
312: gbump(n);
313: return n ;
314: } else {
315: return xsgetn(s,n) ;
316: }
317: }
318: virtual streambuf*
319: setbuf(char* p, int len) ;
320: streambuf* setbuf(unsigned char* p, int len) ;
321:
322: streambuf* setbuf(char* p, int len, int count) ;
323: /* obsolete third argument */
324: /*** Constructors -- should be protected ***/
325: streambuf() ;
326: streambuf(char* p, int l) ;
327:
328: streambuf(char* p, int l,int c) ;
329: /* 3 argument form is obsolete.
330: * Use strstreambuf.
331: */
332: virtual ~streambuf() ;
333: private:
334: int x_snextc() ;
335: };
336:
337: class istream : virtual public ios {
338: public: /* Constructor */
339: istream(streambuf*) ;
340: virtual ~istream() ;
341: public:
342: int ipfx(int noskipws=0)
343: { if ( noskipws?(ispecial&~skipping):ispecial) {
344: return do_ipfx(noskipws) ;
345: } else return 1 ;
346: }
347: void isfx() { }
348: istream& seekg(streampos p) ;
349: istream& seekg(streamoff o, ios::seek_dir d) ;
350: streampos tellg() ;
351: istream& operator>> (istream& (*f)(istream&))
352: { return (*f)(*this) ; }
353: istream& operator>> (ios& (*f)(ios&) ) ;
354: istream& operator>>(char*);
355: istream& operator>>(unsigned char*);
356: istream& operator>>(unsigned char& c)
357: { if ( ipfx(0) ) {
358: if ( bp->in_avail() ) {
359: c = bp->sbumpc() ;
360: } else xget((char*)&c) ;
361: }
362: return *this ;
363: }
364: istream& operator>>(char& c)
365: { if ( ipfx(0) ) {
366: if ( bp->in_avail() ) {
367: c = bp->sbumpc() ;
368: } else xget((char*)&c) ;
369: }
370: return *this ;
371: }
372: istream& operator>>(short&);
373: istream& operator>>(int&);
374: istream& operator>>(long&);
375: istream& operator>>(unsigned short&);
376: istream& operator>>(unsigned int&);
377: istream& operator>>(unsigned long&);
378: istream& operator>>(float&);
379: istream& operator>>(double&);
380: istream& operator>>(streambuf*);
381: istream& get(char* , int lim, char delim='\n');
382: istream& get(unsigned char* b,int lim, char delim='\n');
383: istream& getline(char* b, int lim, char delim='\n');
384: istream& getline(unsigned char* b, int lim, char delim='\n');
385: istream& get(streambuf& sb, char delim ='\n');
386: istream& get(unsigned char& c)
387: {
388: if ( ipfx(1) && bp->in_avail()) {
389: x_gcount = 1 ;
390: c = bp->sbumpc() ;
391: } else {
392: xget((char*)&c) ;
393: }
394: return *this ;
395: }
396: istream& get(char& c)
397: {
398: if ( ipfx(1) && bp->in_avail()) {
399: x_gcount = 1 ;
400: c = bp->sbumpc() ;
401: } else {
402: xget(&c) ;
403: }
404: return *this ;
405: }
406: int get()
407: {
408: int c ;
409: if ( !ipfx(1) ) return EOF ;
410: else {
411: c = bp->sbumpc() ;
412: if ( c == EOF ) setstate(eofbit) ;
413: return c ;
414: }
415: }
416: int peek()
417: {
418: if ( ipfx(-1) ) return bp->sgetc() ;
419: else return EOF ;
420:
421: }
422: istream& ignore(int n=1,int delim=EOF) ;
423: istream& read(char* s,int n);
424: istream& read(unsigned char* s,int n)
425: {
426: return read((char*)s,n) ;
427: }
428: int gcount() ;
429: istream& putback(char c);
430: int sync() { return bp->sync() ; }
431: protected:
432: int do_ipfx(int noskipws) ;
433: void eatwhite() ;
434: istream() ;
435: private:
436: int x_gcount ;
437: void xget(char* c) ;
438: public: /*** Obsolete constructors, carried over from stream package ***/
439: istream(streambuf*, int sk, ostream* t=0) ;
440: /* obsolete, set sk and tie
441: * via format state variables */
442: istream(int size ,char*,int sk=1) ;
443: /* obsolete, use strstream */
444: istream(int fd,int sk=1, ostream* t=0) ;
445: /* obsolete use fstream */
446: };
447:
448: class ostream : virtual public ios {
449: public: /* Constructor */
450: ostream(streambuf*) ;
451: virtual ~ostream();
452: public:
453: int opfx() /* Output prefix */
454: { if ( ospecial ) return do_opfx() ;
455: else return 1 ;
456: }
457: void osfx()
458: { if ( osfx_special ) do_osfx() ; }
459:
460: ostream& flush() ;
461: ostream& seekp(streampos p) ;
462: ostream& seekp(streamoff o, ios::seek_dir d) ;
463: streampos tellp() ;
464: ostream& put(char c)
465: {
466: if ( opfx() ) {
467: if ( bp->sputc(c) == EOF ) {
468: setstate(eofbit|failbit) ;
469: }
470: osfx() ;
471: }
472: return *this ;
473: }
474: ostream& operator<<(char c) { put(c) ; osfx() ; return *this ; }
475:
476: ostream& operator<<(unsigned char c)
477: { put(c) ; osfx() ; return *this ; }
478:
479: ostream& operator<<(const char*);
480: ostream& operator<<(int a);
481: ostream& operator<<(long);
482: ostream& operator<<(double);
483: ostream& operator<<(float);
484: ostream& operator<<(unsigned int a);
485: ostream& operator<<(unsigned long);
486: ostream& operator<<(void*);
487: ostream& operator<<(streambuf*);
488: ostream& operator<<(short i) { return *this << (int)i ; }
489: ostream& operator<<(unsigned short i)
490: { return *this << (int)i ; }
491:
492: ostream& operator<< (ostream& (*f)(ostream&))
493: { return (*f)(*this) ; }
494: ostream& operator<< (ios& (*f)(ios&) ) ;
495:
496: ostream& write(const char* s,int n)
497: {
498: if ( !state ) {
499: if ( bp->sputn(s,n) != n ) setstate(eofbit|failbit);
500: }
501: return *this ;
502: }
503: ostream& write(const unsigned char* s, int n)
504: {
505: return write((const char*)s,n);
506: }
507: protected: /* More ostream members */
508: int do_opfx() ;
509: void do_osfx() ;
510: ostream() ;
511:
512: public: /*** Obsolete constructors, carried over from stream package ***/
513: ostream(int fd) ;
514: /* obsolete use fstream */
515: ostream(int size ,char*) ;
516: /* obsolete, use strstream */
517: } ;
518:
519: class iostream : public istream, public ostream {
520: public:
521: iostream(streambuf*) ;
522: virtual ~iostream() ;
523: protected:
524: iostream() ;
525: } ;
526:
527: class istream_withassign : public istream {
528: public:
529: istream_withassign() ;
530: virtual ~istream_withassign() ;
531: istream_withassign& operator=(istream&) ;
532: istream_withassign& operator=(streambuf*) ;
533: } ;
534:
535: class ostream_withassign : public ostream {
536: public:
537: ostream_withassign() ;
538: virtual ~ostream_withassign() ;
539: ostream_withassign& operator=(ostream&) ;
540: ostream_withassign& operator=(streambuf*) ;
541: } ;
542:
543: class iostream_withassign : public iostream {
544: public:
545: iostream_withassign() ;
546: virtual ~iostream_withassign() ;
547: iostream_withassign& operator=(ios&) ;
548: iostream_withassign& operator=(streambuf*) ;
549: } ;
550:
551: extern istream_withassign cin ;
552: extern ostream_withassign cout ;
553: extern ostream_withassign cerr ;
554: extern ostream_withassign clog ;
555:
556: ios& dec(ios&) ;
557: ostream& endl(ostream& i) ;
558: ostream& ends(ostream& i) ;
559: ostream& flush(ostream&) ;
560: ios& hex(ios&) ;
561: ios& oct(ios&) ;
562: istream& ws(istream&) ;
563:
564: static class Iostream_init {
565: static int stdstatus ; /* see cstreams.c */
566: static int initcount ;
567: friend ios ;
568: public:
569: Iostream_init() ;
570: ~Iostream_init() ;
571: } iostream_init ;
572:
573: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.