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