|
|
1.1 root 1: /*ident "@(#)ctrans:lib/stream/stream.c 1.1.3.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: stream.c:
12:
13: *****************************************************************************/
14:
15: #include <iostream.h>
16:
17: void (*ios::stdioflush)() = 0 ;
18:
19: long ios::nextbit = 1L<<16 ;
20: long ios::nextword = 0 ;
21:
22: const long ios::adjustfield = ios::left|ios::right|ios::internal ;
23: const long ios::floatfield = ios::fixed|ios::scientific ;
24: const long ios::basefield = ios::dec|ios::hex|ios::oct ;
25:
26: #define ISTREAM istream
27: #define OSTREAM ostream
28: #define IOSTREAM iostream
29:
30: union ios_user_union { long i ; void* p ; } ;
31:
32: void ios::init(streambuf* b)
33: {
34: // Must be called by other constructors. */
35: bp=b ;
36: state = 0 ;
37: ispecial = 0 ;
38: ospecial = 0 ;
39: osfx_special = 0 ;
40: isfx_special = 0 ;
41: delbuf = 0 ;
42: if (!bp ) setstate(hardfail|failbit) ;
43: flags(skipws|dec) ;
44: width(0);
45: precision(6);
46: fill(' ');
47: tie(0);
48:
49: x_user = 0 ;
50: nuser = 0 ;
51: }
52:
53: ios::ios(streambuf* b) { init(b) ; }
54:
55: ios::~ios()
56: {
57: if (bp) bp->sync() ;
58: if (delbuf) {
59: delete bp ;
60: bp = 0 ;
61: }
62: if (x_user) {
63: delete x_user ;
64: x_user = 0 ;
65: }
66: }
67:
68: iostream::iostream(streambuf* b) { init(b) ; }
69: iostream::~iostream() { }
70: istream::istream(streambuf* b) { init(b) ; }
71: istream::~istream() { }
72: ostream::ostream(streambuf* b) { init(b) ; }
73: ostream::~ostream() { }
74:
75: IOSTREAM::IOSTREAM() { }
76: ISTREAM::ISTREAM() { }
77: OSTREAM::OSTREAM() { }
78:
79: ostream& OSTREAM::flush()
80: {
81: if ( bp->out_waiting() ) bp->overflow() ;
82: else if ( bp->in_avail() ) bp->sync() ;
83:
84: return *this ;
85: }
86:
87: streampos OSTREAM::tellp()
88: {
89: return bp->seekoff(0,cur,out) ;
90: }
91:
92: streampos ISTREAM::tellg()
93: {
94: return bp->seekoff(0,cur,in) ;
95: }
96:
97: ostream& OSTREAM::seekp(streampos p)
98: {
99: if ( bp->seekpos(p,out) == EOF ) setstate(badbit) ;
100: return *this ;
101: }
102:
103: typedef ios::seek_dir Sdir ; /** Gets around a bug in release 2.0 beta 5 **/
104:
105: ostream& OSTREAM::seekp(streamoff o, Sdir d)
106: {
107: if ( bp->seekoff(o,d,out) == EOF ) setstate(badbit) ;
108: return *this ;
109: }
110:
111: istream& ISTREAM::seekg(streampos p)
112: {
113: if ( bp->seekpos(p,in) == EOF ) setstate(badbit) ;
114: return *this ;
115: }
116:
117: istream& ISTREAM::seekg(streamoff o, Sdir d)
118: {
119: if ( bp->seekoff(o,d,in) == EOF ) setstate(badbit) ;
120: return *this ;
121: }
122:
123: ostream* ios::tie(ostream* s)
124: {
125: ostream* t = x_tie ;
126: x_tie = s ;
127:
128: if ( s ) {
129: ispecial |= tied ;
130: ospecial |= tied ;
131: }
132: else {
133: ispecial &= ~tied ;
134: ospecial &= ~tied ;
135: }
136: return t ;
137: }
138:
139: char ios::fill(char c)
140: {
141: char oldf = x_fill ;
142: x_fill = c ;
143: return oldf ;
144: }
145:
146: int ios::precision(int p)
147: {
148: register int oldp = x_precision ;
149: x_precision = p;
150: return oldp ;
151: }
152:
153: long ios::setf(long b, long f)
154: {
155: long oldf = x_flags ;
156: x_flags = (b&f) | (x_flags&~f) ;
157:
158: if (x_flags&skipws ) ispecial |= skipping ;
159: else ispecial &= ~skipping ;
160:
161: osfx_special = (x_flags&(unitbuf|stdio)) != 0 ;
162:
163: return oldf ;
164: }
165:
166: long ios::setf(long b)
167: {
168: long oldf = x_flags ;
169: x_flags |= b ;
170:
171: if (x_flags&skipws ) ispecial |= skipping ;
172: else ispecial &= ~skipping ;
173:
174: osfx_special = (x_flags&(unitbuf|stdio)) != 0 ;
175: return oldf ;
176: }
177:
178: long ios::unsetf(long b)
179: {
180: long oldf = x_flags ;
181: x_flags &= ~b ;
182:
183: if (x_flags&skipws ) ispecial |= skipping ;
184: else ispecial &= ~skipping ;
185:
186: osfx_special = (x_flags&(unitbuf|stdio)) != 0 ;
187: return oldf ;
188: }
189:
190: long ios::flags(long f)
191: {
192: long oldf = x_flags ;
193: x_flags = f ;
194:
195: if (x_flags&skipws ) ispecial |= skipping ;
196: else ispecial &= ~skipping ;
197:
198: osfx_special = (x_flags&(unitbuf|stdio)) != 0 ;
199: return oldf ;
200: }
201:
202: int ISTREAM::do_ipfx(int noskipws)
203: {
204: if ( state&hardfail) return 0 ;
205: // note that we flush tied stream even when !this->good().
206: if ( x_tie && x_tie->rdbuf()->out_waiting()
207: && (noskipws==0 || rdbuf()->in_avail()<noskipws) ){
208: x_tie->flush() ;
209: }
210: if ( good() && !noskipws && (ispecial&skipping) ) eatwhite() ;
211: if ( eof() ) {
212: // if we were only skipping this wouldn't be a failure.
213: // but the presumption is that this is a prefix operation
214: // prior to inputting something else.
215: setstate(ios::failbit) ;
216: return 0 ;
217: }
218: return good() ;
219: }
220:
221: int OSTREAM::do_opfx()
222: {
223: if ( state&hardfail) return 0 ;
224: if ( x_tie && x_tie->rdbuf()->out_waiting()) {
225: x_tie->flush() ;
226: }
227: return good() ;
228: }
229:
230: void OSTREAM::do_osfx()
231: {
232: if ( (x_flags & stdio) && stdioflush ) { (*stdioflush)() ; }
233: if ( x_flags & unitbuf ) flush() ;
234: }
235:
236: void ios::operator=(ios& rhs) { bp = rhs.bp ; }
237:
238: ios::ios() { assign_private = state ; state = hardfail ; }
239:
240: istream_withassign::istream_withassign()
241: {
242: // In order for the standard streams to be properly initialized
243: // it is essential that nothing is done by the combination
244: // of this constructor and ios::ios(). So we undo the effect of
245: // ios::ios()
246: state = assign_private ;
247: }
248:
249: istream_withassign::~istream_withassign() { }
250:
251: istream_withassign& istream_withassign::operator=(istream& s)
252: {
253: init(s.rdbuf()) ;
254: return *this ;
255: }
256:
257: ostream_withassign::~ostream_withassign() { }
258:
259: istream_withassign& istream_withassign::operator=(streambuf* sb)
260: {
261: init(sb) ;
262: return *this ;
263: }
264:
265: ostream_withassign::ostream_withassign()
266: {
267: // In order for the standard streams to be properly initialized
268: // it is essential that nothing is done by the combination
269: // of this constructor and ios::ios(). So we undo the effect of
270: // ios::ios()
271: state = assign_private ;
272: }
273:
274: ostream_withassign& ostream_withassign::operator=(ostream& s)
275: {
276: init(s.rdbuf()) ;
277: return *this ;
278: }
279:
280: ostream_withassign& ostream_withassign::operator=(streambuf* sb)
281: {
282: init(sb) ;
283: return *this ;
284: }
285:
286: iostream_withassign::iostream_withassign()
287: {
288: // In order for the standard streams to be properly initialized
289: // it is essential that nothing is done by the combination
290: // of this constructor and ios::ios(). So we undo the effect of
291: // ios::ios()
292: state = assign_private ;
293: }
294:
295: iostream_withassign::~iostream_withassign() { }
296:
297:
298: iostream_withassign& iostream_withassign::operator=(ios& s)
299: {
300: init(s.rdbuf()) ;
301: return *this ;
302: }
303:
304: iostream_withassign& iostream_withassign::operator=(streambuf* sb)
305: {
306: init(sb) ;
307: return *this ;
308: }
309:
310: void ios::uresize(int n)
311: {
312: if ( n < nuser ) return ;
313: ios_user_union* newu = new ios_user_union[n+1] ;
314: for ( int x = 0 ; x < nuser ; ++x ) {
315: newu[x] = x_user[x] ;
316: } ;
317: delete [nuser] x_user ;
318: nuser = n+1 ;
319: x_user = newu ;
320: }
321:
322: long & ios::iword(int x)
323: {
324: if ( x < 0 ) x = 0 ;
325: if ( x >= nuser ) uresize(x) ;
326: return x_user[x].i ;
327: }
328:
329: void* & ios::pword(int x)
330: {
331: if ( x < 0 ) x = 0 ;
332: if ( x >= nuser ) uresize(x) ;
333: return x_user[x].p ;
334: }
335:
336:
337: long ios::bitalloc()
338: {
339: long w = nextbit ;
340: nextbit = nextbit << 1 ;
341: return w ;
342: }
343:
344: long ios::xalloc()
345: {
346: return nextword++ ;
347: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.