summaryrefslogtreecommitdiffstats
path: root/private/crt32/iostream/istream.cxx
blob: d5916001d637c7ad96b30af3bec430da257a33df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***
* istream.cxx - definitions for istream and istream_withassign classes
*
*	Copyright (c) 1991-1992, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	Definitions of member functions for istream and istream_withassign
*	classes.
*	[AT&T C++]
*
*Revision History:
*	07-15-91   KRS	Created.
*	08-15-91   KRS	Fix handling of 1-length strings in get(char*,int,int)
*	08-20-91   KRS  Make read() not do text translation (for filebufs)
*	08-21-91   KRS  Fix >>(streambuf *) to advance pointer properly.
*	08-22-91   KRS  Fix octal error in getint().
*	08-26-91   KRS	Fix for Windows DLL's and set max. ibuffer[] lengths.
*	09-05-91   KRS	Don't special-case 0x in getint(). Spec. conformance...
*	09-10-91   KRS	Reinstate text translation (by default) for read().
*	09-12-91   KRS	Treat count as unsigned in get() and read().
*	09-16-91   KRS	Fix get(char *, int lim, char) for lim=0 case.
*	09-23-91   KRS	Split up flie for granularity purposes.
*	10-21-91   KRS	Make eatwhite() return void again.
*	10-24-91   KRS	Move istream_withassign::operator=(streambuf*) here.
*	11-04-91   KRS	Make constructors work with virtual base.
*			Fix whitespace error handling in ipfx().
*	11-20-91   KRS	Add/fix copy constructor and assignment operators.
*	01-23-92   KRS	C700 #5883: Fix behaviour of peek() to call ipfx(1).
*
*******************************************************************************/

#include <cruntime.h>
#include <internal.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream.h>
#pragma hdrstop

	istream::istream()
// : ios()
{
	x_flags |= ios::skipws;
	x_gcount = 0;
	_fGline = 0;
	// CONSIDER: do anything else?
}

	istream::istream(streambuf* _inistbf)
// : ios()
{
	init(_inistbf);

	x_flags |= ios::skipws;
	x_gcount = 0;
	_fGline = 0;
	// CONSIDER: do anything else?
}

	istream::istream(const istream& _istrm)
// : ios()
{
	init(_istrm.rdbuf());

	x_flags |= ios::skipws;
	x_gcount = 0;
	_fGline = 0;
	// CONSIDER: do anything else?
}

	istream::~istream()
// : ~ios()
{
	// CONSIDER: do anything else?
}

// used by ios::sync_with_stdio()
istream& istream::operator=(streambuf * _sbuf)
{
// consider: may be some redundency here, depending on spec.
	if (delbuf() && rdbuf())
	    delete rdbuf();
	bp = 0;

	this->ios::operator=(ios());	// initialize ios members
	delbuf(0);			// important!
	init(_sbuf);	// set up bp

	x_flags |= ios::skipws;		// init istream members too
	x_gcount = 0;
//	_fGline = 0;			// not necessary

	return *this;
}
int istream::ipfx(int need)
{
    lock();
    if (need)		// reset gcount if unformatted input
	x_gcount = 0;
    if (state)		// return 0 iff error condition
	{
	state |= ios::failbit;	// solves cin>>buf problem
	unlock();
	return 0;
	}
    if (x_tie && ((need==0) || (need > bp->in_avail())))
	{
	x_tie->flush();
	}
    lockbuf();
    if ((need==0) && (x_flags & ios::skipws))
	{
	eatwhite();
	if (state)	// eof or error
	    {
	    state |= ios::failbit;
	    unlockbuf();
	    unlock();
	    return 0;
	    }
	}
    // leave locked ; isfx() will unlock
    return 1;		// return nz if okay
}

// formatted input functions

istream& istream::operator>>(char * s)
{
    int c;
    unsigned int i, lim;
    if (ipfx(0))
	{
	lim = (unsigned)(x_width-1);
	x_width = 0;
	if (!s)
	    {
	    state |= ios::failbit;
	    }
	else
	    {
	    for (i=0; i< lim; i++)
		{
		c=bp->sgetc();
		if (c==EOF)
		    {
		    state |= ios::eofbit;
		    if (!i)
			state |= ios::failbit|ios::badbit;
		    break;
		    }
		else if (isspace(c))
		    {
		    break;
		    }
		else
		    {
		    s[i] = (char)c;
		    bp->stossc(); // advance pointer
		    }
	        }
	    if (!i)
		state |= ios::failbit;
	    else
		s[i] = '\0';
	    }
	isfx();
	}
    return *this;
}

#if 0
istream& istream::operator>>(char& c)
{
    int tchar;
    if (ipfx(0))
	{
	tchar=bp->sbumpc();
	if (tchar==EOF)
	    {
	    state |= ios::eofbit|ios::badbit;
	    }
	else
	    {
	    c = (char)tchar;
	    }
	isfx();
	}
    return *this;
}
#endif

int istream::peek()
{
int retval;
    if (ipfx(1))
	{
	retval = (bp->sgetc());
	isfx();
	}
    else
	retval = EOF;
    return retval;
}

istream& istream::putback(char c)
{
//    if (!bad())		// CONSIDER: just return if bad error?
      if (good())		// this is how it's spec.ed out...
	{
	lockbuf();

	if (bp->sputbackc(c)==EOF)
	    {
	    clear(state | ios::failbit);
	    }
//	else
//	    clear(state & ~(ios::eof)); // no longer at end of file, if we were...

	unlockbuf();
	}
    return *this;
}

int istream::sync()
{
    int retval;
    lockbuf();

    if ((retval=bp->sync())==EOF)
	{
	clear(state | (ios::failbit|ios::badbit));
	}

    unlockbuf();
    return retval;
}

void istream::eatwhite()
{
    int c;
    lockbuf();
    c = bp->sgetc();
    for ( ; ; )
	{
	if (c==EOF)
	    {
	    clear(state | ios::eofbit);
	    break;
	    }
	if (isspace(c))
	    {
	    c = bp->snextc();
	    }
	else
	    {
	    break;
	    }
	}
    unlockbuf();
}

	istream_withassign::istream_withassign()
: istream()
{
	// CONSIDER: do anything else?
}

	istream_withassign::istream_withassign(streambuf* _is)
: istream(_is)
{
	// CONSIDER: do anything else?
}

	istream_withassign::~istream_withassign()
// : ~istream()
{
	// CONSIDER: do anything else?
}