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