3628
|
1 /* |
|
2 |
|
3 Copyright (C) 2000 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3628
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3629
|
28 #include "c-file-ptr-stream.h" |
3628
|
29 |
|
30 #ifndef SEEK_SET |
|
31 #define SEEK_SET 0 |
|
32 #endif |
|
33 |
|
34 #ifndef SEEK_CUR |
|
35 #define SEEK_CUR 1 |
|
36 #endif |
|
37 |
|
38 #ifndef SEEK_END |
|
39 #define SEEK_END 2 |
|
40 #endif |
|
41 |
3642
|
42 c_file_ptr_buf::~c_file_ptr_buf (void) |
|
43 { |
3693
|
44 close (); |
3642
|
45 } |
3628
|
46 |
|
47 // XXX FIXME XXX -- I'm sure there is room for improvement here... |
|
48 |
3775
|
49 c_file_ptr_buf::int_type |
|
50 c_file_ptr_buf::overflow (int_type c) |
3628
|
51 { |
3775
|
52 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
53 if (f) |
|
54 return (c != traits_type::eof ()) ? fputc (c, f) : flush (); |
|
55 else |
|
56 return traits_type::not_eof (c); |
|
57 #else |
3644
|
58 if (f) |
3652
|
59 return (c != EOF) ? fputc (c, f) : flush (); |
3644
|
60 else |
|
61 return EOF; |
3775
|
62 #endif |
3628
|
63 } |
|
64 |
3775
|
65 c_file_ptr_buf::int_type |
4310
|
66 c_file_ptr_buf::underflow_common (bool bump) |
3628
|
67 { |
3644
|
68 if (f) |
4310
|
69 { |
|
70 int_type c = fgetc (f); |
|
71 |
|
72 if (! bump |
|
73 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
74 && c != traits_type::eof ()) |
|
75 #else |
|
76 && c != EOF) |
|
77 #endif |
|
78 ungetc (c, f); |
|
79 |
|
80 return c; |
|
81 } |
3644
|
82 else |
3775
|
83 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
84 return traits_type::eof (); |
|
85 #else |
3644
|
86 return EOF; |
3775
|
87 #endif |
3628
|
88 } |
|
89 |
3775
|
90 c_file_ptr_buf::int_type |
|
91 c_file_ptr_buf::pbackfail (int_type c) |
3628
|
92 { |
3775
|
93 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
94 return (c != traits_type::eof () && f) ? ungetc (c, f) : |
|
95 traits_type::not_eof (c); |
|
96 #else |
3644
|
97 return (c != EOF && f) ? ungetc (c, f) : EOF; |
3775
|
98 #endif |
3628
|
99 } |
|
100 |
|
101 std::streamsize |
|
102 c_file_ptr_buf::xsputn (const char* s, std::streamsize n) |
|
103 { |
3644
|
104 if (f) |
|
105 return fwrite (s, 1, n, f); |
|
106 else |
|
107 return 0; |
3628
|
108 } |
|
109 |
|
110 std::streamsize |
|
111 c_file_ptr_buf::xsgetn (char *s, std::streamsize n) |
|
112 { |
3644
|
113 if (f) |
|
114 return fread (s, 1, n, f); |
|
115 else |
|
116 return 0; |
3628
|
117 } |
|
118 |
|
119 static inline int |
|
120 seekdir_to_whence (std::ios::seekdir dir) |
|
121 { |
|
122 return ((dir == std::ios::beg) ? SEEK_SET : |
|
123 (dir == std::ios::cur) ? SEEK_CUR : |
|
124 (dir == std::ios::end) ? SEEK_END : |
|
125 dir); |
|
126 } |
|
127 |
|
128 std::streampos |
|
129 c_file_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, |
|
130 std::ios::openmode) |
|
131 { |
4643
|
132 // XXX FIXME XXX |
|
133 #if 0 |
3644
|
134 if (f) |
|
135 { |
|
136 fseek (f, offset, seekdir_to_whence (dir)); |
3628
|
137 |
3644
|
138 return ftell (f); |
|
139 } |
|
140 else |
|
141 return 0; |
4643
|
142 #endif |
|
143 return -1; |
3628
|
144 } |
|
145 |
|
146 std::streampos |
|
147 c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode) |
|
148 { |
4643
|
149 // XXX FIXME XXX |
|
150 #if 0 |
3644
|
151 if (f) |
|
152 { |
|
153 fseek (f, offset, SEEK_SET); |
3628
|
154 |
3644
|
155 return ftell (f); |
|
156 } |
|
157 else |
|
158 return 0; |
4643
|
159 #endif |
|
160 return -1; |
3628
|
161 } |
|
162 |
|
163 int |
|
164 c_file_ptr_buf::sync (void) |
|
165 { |
3652
|
166 flush (); |
|
167 |
3628
|
168 return 0; |
|
169 } |
|
170 |
3652
|
171 int |
|
172 c_file_ptr_buf::flush (void) |
|
173 { |
|
174 return f ? fflush (f) : EOF; |
|
175 } |
|
176 |
|
177 int |
|
178 c_file_ptr_buf::close (void) |
|
179 { |
3693
|
180 int retval = -1; |
|
181 |
3716
|
182 flush (); |
|
183 |
3652
|
184 if (f) |
3693
|
185 { |
3716
|
186 retval = cf (f); |
3693
|
187 f = 0; |
|
188 } |
|
189 |
|
190 return retval; |
3652
|
191 } |
|
192 |
5325
|
193 #ifdef HAVE_ZLIB |
|
194 |
|
195 c_zfile_ptr_buf::~c_zfile_ptr_buf (void) |
|
196 { |
|
197 close (); |
|
198 } |
|
199 |
|
200 // XXX FIXME XXX -- I'm sure there is room for improvement here... |
|
201 |
|
202 c_zfile_ptr_buf::int_type |
|
203 c_zfile_ptr_buf::overflow (int_type c) |
|
204 { |
|
205 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
206 if (f) |
|
207 return (c != traits_type::eof ()) ? gzputc (f, c) : flush (); |
|
208 else |
|
209 return traits_type::not_eof (c); |
|
210 #else |
|
211 if (f) |
|
212 return (c != EOF) ? gzputc (f, c) : flush (); |
|
213 else |
|
214 return EOF; |
|
215 #endif |
|
216 } |
|
217 |
|
218 c_zfile_ptr_buf::int_type |
|
219 c_zfile_ptr_buf::underflow_common (bool bump) |
|
220 { |
|
221 if (f) |
|
222 { |
|
223 int_type c = gzgetc (f); |
|
224 |
|
225 if (! bump |
|
226 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
227 && c != traits_type::eof ()) |
|
228 #else |
|
229 && c != EOF) |
|
230 #endif |
|
231 gzungetc (c, f); |
|
232 |
|
233 return c; |
|
234 } |
|
235 else |
|
236 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
237 return traits_type::eof (); |
|
238 #else |
|
239 return EOF; |
|
240 #endif |
|
241 } |
|
242 |
|
243 c_zfile_ptr_buf::int_type |
|
244 c_zfile_ptr_buf::pbackfail (int_type c) |
|
245 { |
|
246 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
247 return (c != traits_type::eof () && f) ? gzungetc (c, f) : |
|
248 traits_type::not_eof (c); |
|
249 #else |
|
250 return (c != EOF && f) ? gzungetc (c, f) : EOF; |
|
251 #endif |
|
252 } |
|
253 |
|
254 std::streamsize |
|
255 c_zfile_ptr_buf::xsputn (const char* s, std::streamsize n) |
|
256 { |
|
257 if (f) |
|
258 return gzwrite (f, s, n); |
|
259 else |
|
260 return 0; |
|
261 } |
|
262 |
|
263 std::streamsize |
|
264 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n) |
|
265 { |
|
266 if (f) |
|
267 return gzread (f, s, n); |
|
268 else |
|
269 return 0; |
|
270 } |
|
271 |
|
272 std::streampos |
|
273 c_zfile_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, |
|
274 std::ios::openmode) |
|
275 { |
|
276 // XXX FIXME XXX |
|
277 #if 0 |
|
278 if (f) |
|
279 { |
|
280 gzseek (f, offset, seekdir_to_whence (dir)); |
|
281 |
|
282 return gztell (f); |
|
283 } |
|
284 else |
|
285 return 0; |
|
286 #endif |
|
287 return -1; |
|
288 } |
|
289 |
|
290 std::streampos |
|
291 c_zfile_ptr_buf::seekpos (std::streampos offset, std::ios::openmode) |
|
292 { |
|
293 // XXX FIXME XXX |
|
294 #if 0 |
|
295 if (f) |
|
296 { |
|
297 gzseek (f, offset, SEEK_SET); |
|
298 |
|
299 return gztell (f); |
|
300 } |
|
301 else |
|
302 return 0; |
|
303 #endif |
|
304 return -1; |
|
305 } |
|
306 |
|
307 int |
|
308 c_zfile_ptr_buf::sync (void) |
|
309 { |
|
310 flush (); |
|
311 |
|
312 return 0; |
|
313 } |
|
314 |
|
315 int |
|
316 c_zfile_ptr_buf::flush (void) |
|
317 { |
|
318 // XXX FIXME XXX -- do we need something more complex here, passing |
|
319 // something other than 0 for the second argument to gzflush and |
|
320 // checking the return value, etc.? |
|
321 |
|
322 return f ? gzflush (f, 0) : EOF; |
|
323 } |
|
324 |
|
325 int |
|
326 c_zfile_ptr_buf::close (void) |
|
327 { |
|
328 int retval = -1; |
|
329 |
|
330 flush (); |
|
331 |
|
332 if (f) |
|
333 { |
|
334 retval = cf (f); |
|
335 f = 0; |
|
336 } |
|
337 |
|
338 return retval; |
|
339 } |
|
340 |
|
341 #endif |
|
342 |
3628
|
343 /* |
|
344 ;;; Local Variables: *** |
|
345 ;;; mode: C++ *** |
|
346 ;;; End: *** |
|
347 */ |