summaryrefslogtreecommitdiffstats
path: root/private/crt32/iostream/ifstream.cxx
blob: e49e26d40caade88a19125a283fbc6d8eb76c088 (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
/***
*ifstream.cxx - ifstream member function definitions
*
*	Copyright (c) 1991-1992, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	Contains the member functions for the ifstream class.
*
*Revision History:
*	09-21-91   KRS  Created.  Split up fstream.cxx for granularity.
*	10-22-91   KRS	C700 #4883: fix error status for ifstream::open().
*
*******************************************************************************/

#include <cruntime.h>
#include <internal.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys\types.h>
#include <io.h>
#include <fstream.h>
#pragma hdrstop

#include <sys\stat.h>

/***
*ifstream::ifstream() - ifstream default constructor
*
*Purpose:
*	Default constructor for ifstream objects.
*
*Entry:
*	None.
*
*Exit:
*	None.
*
*Exceptions:
*
*******************************************************************************/
	ifstream::ifstream()
: istream(new filebuf)
{
    delbuf(1);	// schedule automatic deletion too...
    // CONSIDER: do anything else?
}

/***
*ifstream::ifstream(const char * name, int mode, int prot) - ifstream ctor
*
*Purpose:
*	Constructor for ifstream objects.  Creates an associated filebuf object,
*	opens a named file and attaches it to the new filebuf.
*
*Entry:
*	name = filename to open.
*	mode = see filebuf::open mode argument.  ios::in is implied.
*	prot = see filebuf::open share argument
*
*Exit:
*	None.
*
*Exceptions:
*	Sets failbit if open fails.
*
*******************************************************************************/
	ifstream::ifstream(const char * name, int mode, int prot)
: istream(new filebuf)
{
    delbuf(1);	// schedule automatic deletion too...
    if (!(rdbuf()->open(name, (mode|ios::in), prot)))
	state |= ios::failbit;
}

/***
*ifstream::ifstream(filedesc fd) - ifstream constructor
*
*Purpose:
*	Constructor for ifstream objects.  Creates an associated filebuf object
*	and attaches it to the given file descriptor.
*
*Entry:
*	fd = file descriptor of file previously opened using _open or _sopen.
*
*Exit:
*	None.
*
*Exceptions:
*
*******************************************************************************/
	ifstream::ifstream(filedesc _fd)
: istream(new filebuf(_fd))
{
    delbuf(1);
}

/***
*ifstream::ifstream(filedesc fd, char * sbuf, int len) - ifstream constructor
*
*Purpose:
*	Constructor for ifstream objects.  Creates an associated filebuf object
*	and attaches it to the given file descriptor.  Filebuf object uses
*	user-supplied buffer or is unbuffered if sbuf or len = 0.
*
*Entry:
*	fd   = file descriptor of file previously opened using _open or _sopen.
*	sbuf = pointer to character buffer or NULL if request for unbuffered.
*	len  = lenght of character buffer sbuf or 0 if request for unbuffered.
*
*Exit:
*	None.
*
*Exceptions:
*
*******************************************************************************/
	ifstream::ifstream(filedesc _fd, char * sbuf, int len)
: istream(new filebuf(_fd, sbuf, len))
//: istream(new filebuf);
{
    delbuf(1);
}

/***
*ifstream::~ifstream() - ifstream destructor
*
*Purpose:
*	ifstream destructor.
*
*Entry:
*	None.
*
*Exit:
*	None.
*
*Exceptions:
*
*******************************************************************************/
	ifstream::~ifstream()
{
    // CONSIDER: do anything else?
}

/***
*streambuf* ifstream::setbuf(char * ptr, int len) - setbuf function
*
*Purpose:
*	ifstream setbuf function
*
*Entry:
*	ptr = pointer to buffer or NULL for unbuffered.
*	len = length of buffer or zero for unbuffered.
*
*Exit:
*	Returns rdbuf() or NULL if error.
*
*Exceptions:
*	If ifstream is already open or if rdbuf()->setbuf fails, sets failbit
*	and returns NULL.
*
*******************************************************************************/
streambuf * ifstream::setbuf(char * ptr, int len)
{
    if ((is_open()) || (!(rdbuf()->setbuf(ptr, len))))
	{
	clear(state | ios::failbit);
	return NULL;
	}
    // UNDONE: turn off output buffer??
    return rdbuf();
}

/***
*void ifstream::attach(filedesc _fd) - attach member function
*
*Purpose:
*	ifstream attach member function.  Just calls rdbuf()->attach().
*
*Entry:
*	_fd = file descriptor of previously opened file.
*
*Exit:
*	None.
*
*Exceptions:
*	Sets failbit if rdbuf()->attach fails.
*
*******************************************************************************/
void ifstream::attach(filedesc _fd)	// CONSIDER: inline?
{
    if (!(rdbuf()->attach(_fd)))
	clear(state | ios::failbit);
}

/***
*void ifstream::open(const char * name, int mode, int prot) - ifstream open()
*
*Purpose:
*	Opens a named file and attaches it to the associated filebuf.
*	Just calls rdbuf()->open().
*
*Entry:
*	name = filename to open.
*	mode = see filebuf::open mode argument.  ios::in is implied.
*	prot = see filebuf::open share argument
*
*Exit:
*	None.
*
*Exceptions:
*	Sets failbit if already open or rdbuf()->open() fails.
*
*******************************************************************************/
void ifstream::open(const char * name, int mode, int prot)
{
    if (is_open() || !(rdbuf()->open(name, (mode|ios::in), prot)))
	clear(state | ios::failbit);
}

/***
*void ifstream::close() - close member function
*
*Purpose:
*	ifstream close member function.  Just calls rdbuf()->close().
*	Clears rdstate() error bits if successful.
*
*Entry:
*	None.
*
*Exit:
*	None.
*
*Exceptions:
*	Sets failbit if rdbuf()->close fails.
*
*******************************************************************************/
void ifstream::close()
{
    clear((rdbuf()->close()) ? 0 : (state | ios::failbit));
}