Panda3D
pandaFileStream.I
1 // Filename: pandaFileStream.I
2 // Created by: drose (08Sep08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: IFileStream::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE IFileStream::
22 IFileStream() : istream(&_buf) {
23 }
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: IFileStream::Constructor
27 // Access: Published
28 // Description:
29 ////////////////////////////////////////////////////////////////////
30 INLINE IFileStream::
31 IFileStream(const char *filename, ios::openmode mode) : istream(&_buf) {
32  open(filename, mode);
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: IFileStream::Destructor
37 // Access: Published
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 INLINE IFileStream::
41 ~IFileStream() {
42  close();
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: IFileStream::open
47 // Access: Published
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 INLINE void IFileStream::
51 open(const char *filename, ios::openmode mode) {
52  clear((ios_iostate)0);
53  _buf.open(filename, mode);
54  if (!_buf.is_open()) {
55  clear(ios::failbit);
56  }
57 }
58 
59 #ifdef _WIN32
60 ////////////////////////////////////////////////////////////////////
61 // Function: IFileStream::attach
62 // Access: Public
63 // Description: Connects the file stream to the existing OS-defined
64 // stream, presumably opened via a low-level OS call.
65 // The filename is for reporting only. When the file
66 // stream is closed, it will also close the underlying
67 // OS handle.
68 //
69 // This function is the Windows-specific variant.
70 ////////////////////////////////////////////////////////////////////
71 void IFileStream::
72 attach(const char *filename, HANDLE handle, ios::openmode mode) {
73  clear((ios_iostate)0);
74  _buf.attach(filename, handle, mode);
75  if (!_buf.is_open()) {
76  clear(ios::failbit);
77  }
78 }
79 #endif // _WIN32
80 
81 #ifndef _WIN32
82 ////////////////////////////////////////////////////////////////////
83 // Function: IFileStream::attach
84 // Access: Public
85 // Description: Connects the file stream to the existing OS-defined
86 // stream, presumably opened via a low-level OS call.
87 // The filename is for reporting only. When the file
88 // stream is closed, it will also close the underlying
89 // OS handle.
90 //
91 // This function is the Posix-specific variant.
92 ////////////////////////////////////////////////////////////////////
93 void IFileStream::
94 attach(const char *filename, int fd, ios::openmode mode) {
95  clear((ios_iostate)0);
96  _buf.attach(filename, fd, mode);
97  if (!_buf.is_open()) {
98  clear(ios::failbit);
99  }
100 }
101 #endif // _WIN32
102 
103 ////////////////////////////////////////////////////////////////////
104 // Function: IFileStream::close
105 // Access: Published
106 // Description:
107 ////////////////////////////////////////////////////////////////////
108 INLINE void IFileStream::
109 close() {
110  _buf.close();
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: OFileStream::Constructor
115 // Access: Published
116 // Description:
117 ////////////////////////////////////////////////////////////////////
118 INLINE OFileStream::
119 OFileStream() : ostream(&_buf) {
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: OFileStream::Constructor
124 // Access: Published
125 // Description:
126 ////////////////////////////////////////////////////////////////////
127 INLINE OFileStream::
128 OFileStream(const char *filename, ios::openmode mode) : ostream(&_buf) {
129  open(filename, mode);
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: OFileStream::Destructor
134 // Access: Published
135 // Description:
136 ////////////////////////////////////////////////////////////////////
137 INLINE OFileStream::
138 ~OFileStream() {
139  close();
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: OFileStream::open
144 // Access: Published
145 // Description:
146 ////////////////////////////////////////////////////////////////////
147 INLINE void OFileStream::
148 open(const char *filename, ios::openmode mode) {
149  clear((ios_iostate)0);
150  _buf.open(filename, mode);
151  if (!_buf.is_open()) {
152  clear(ios::failbit);
153  }
154 }
155 
156 #ifdef _WIN32
157 ////////////////////////////////////////////////////////////////////
158 // Function: OFileStream::attach
159 // Access: Public
160 // Description: Connects the file stream to the existing OS-defined
161 // stream, presumably opened via a low-level OS call.
162 // The filename is for reporting only. When the file
163 // stream is closed, it will also close the underlying
164 // OS handle.
165 //
166 // This function is the Windows-specific variant.
167 ////////////////////////////////////////////////////////////////////
168 void OFileStream::
169 attach(const char *filename, HANDLE handle, ios::openmode mode) {
170  clear((ios_iostate)0);
171  _buf.attach(filename, handle, mode);
172  if (!_buf.is_open()) {
173  clear(ios::failbit);
174  }
175 }
176 #endif // _WIN32
177 
178 #ifndef _WIN32
179 ////////////////////////////////////////////////////////////////////
180 // Function: OFileStream::attach
181 // Access: Public
182 // Description: Connects the file stream to the existing OS-defined
183 // stream, presumably opened via a low-level OS call.
184 // The filename is for reporting only. When the file
185 // stream is closed, it will also close the underlying
186 // OS handle.
187 //
188 // This function is the Posix-specific variant.
189 ////////////////////////////////////////////////////////////////////
190 void OFileStream::
191 attach(const char *filename, int fd, ios::openmode mode) {
192  clear((ios_iostate)0);
193  _buf.attach(filename, fd, mode);
194  if (!_buf.is_open()) {
195  clear(ios::failbit);
196  }
197 }
198 #endif // _WIN32
199 
200 ////////////////////////////////////////////////////////////////////
201 // Function: OFileStream::close
202 // Access: Published
203 // Description:
204 ////////////////////////////////////////////////////////////////////
205 INLINE void OFileStream::
206 close() {
207  _buf.close();
208 }
209 
210 ////////////////////////////////////////////////////////////////////
211 // Function: FileStream::Constructor
212 // Access: Published
213 // Description:
214 ////////////////////////////////////////////////////////////////////
215 INLINE FileStream::
216 FileStream() : iostream(&_buf) {
217 }
218 
219 ////////////////////////////////////////////////////////////////////
220 // Function: FileStream::Constructor
221 // Access: Published
222 // Description:
223 ////////////////////////////////////////////////////////////////////
224 INLINE FileStream::
225 FileStream(const char *filename, ios::openmode mode) : iostream(&_buf) {
226  open(filename, mode);
227 }
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: FileStream::Destructor
231 // Access: Published
232 // Description:
233 ////////////////////////////////////////////////////////////////////
234 INLINE FileStream::
235 ~FileStream() {
236  close();
237 }
238 
239 ////////////////////////////////////////////////////////////////////
240 // Function: FileStream::open
241 // Access: Published
242 // Description:
243 ////////////////////////////////////////////////////////////////////
244 INLINE void FileStream::
245 open(const char *filename, ios::openmode mode) {
246  clear((ios_iostate)0);
247  _buf.open(filename, mode);
248  if (!_buf.is_open()) {
249  clear(ios::failbit);
250  }
251 }
252 
253 #ifdef _WIN32
254 ////////////////////////////////////////////////////////////////////
255 // Function: FileStream::attach
256 // Access: Public
257 // Description: Connects the file stream to the existing OS-defined
258 // stream, presumably opened via a low-level OS call.
259 // The filename is for reporting only. When the file
260 // stream is closed, it will also close the underlying
261 // OS handle.
262 //
263 // This function is the Windows-specific variant.
264 ////////////////////////////////////////////////////////////////////
265 void FileStream::
266 attach(const char *filename, HANDLE handle, ios::openmode mode) {
267  clear((ios_iostate)0);
268  _buf.attach(filename, handle, mode);
269  if (!_buf.is_open()) {
270  clear(ios::failbit);
271  }
272 }
273 #endif // _WIN32
274 
275 #ifndef _WIN32
276 ////////////////////////////////////////////////////////////////////
277 // Function: FileStream::attach
278 // Access: Public
279 // Description: Connects the file stream to the existing OS-defined
280 // stream, presumably opened via a low-level OS call.
281 // The filename is for reporting only. When the file
282 // stream is closed, it will also close the underlying
283 // OS handle.
284 //
285 // This function is the Posix-specific variant.
286 ////////////////////////////////////////////////////////////////////
287 void FileStream::
288 attach(const char *filename, int fd, ios::openmode mode) {
289  clear((ios_iostate)0);
290  _buf.attach(filename, fd, mode);
291  if (!_buf.is_open()) {
292  clear(ios::failbit);
293  }
294 }
295 #endif // _WIN32
296 
297 ////////////////////////////////////////////////////////////////////
298 // Function: FileStream::close
299 // Access: Published
300 // Description:
301 ////////////////////////////////////////////////////////////////////
302 INLINE void FileStream::
303 close() {
304  _buf.close();
305 }