Mercurial > hg > octave-nkf
annotate src/c-file-ptr-stream.cc @ 12927:f9c1f7c1ead0 stable
maint: undo part of previous gnulib changes
* c-file-ptr-stream.cc (c_file_ptr_buf::seek):
Move here from c-file-ptr-stream.h. Don't call gnulib::fseek.
* c-file-ptr-stream.cc (c_file_ptr_buf::tell):
Move here from c-file-ptr-stream.h.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 05 Aug 2011 18:50:43 -0400 |
parents | e116dd862879 |
children | 9a498efac5f1 |
rev | line source |
---|---|
3628 | 1 /* |
2 | |
11523 | 3 Copyright (C) 2000-2011 John W. Eaton |
3628 | 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 | |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
27 #include <iostream> |
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
28 |
3629 | 29 #include "c-file-ptr-stream.h" |
3628 | 30 |
31 #ifndef SEEK_SET | |
32 #define SEEK_SET 0 | |
33 #endif | |
34 | |
35 #ifndef SEEK_CUR | |
36 #define SEEK_CUR 1 | |
37 #endif | |
38 | |
39 #ifndef SEEK_END | |
40 #define SEEK_END 2 | |
41 #endif | |
42 | |
3642 | 43 c_file_ptr_buf::~c_file_ptr_buf (void) |
44 { | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
45 buf_close (); |
3642 | 46 } |
3628 | 47 |
5775 | 48 // FIXME -- I'm sure there is room for improvement here... |
3628 | 49 |
3775 | 50 c_file_ptr_buf::int_type |
51 c_file_ptr_buf::overflow (int_type c) | |
3628 | 52 { |
3775 | 53 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
54 if (f) | |
10411 | 55 return (c != traits_type::eof ()) ? gnulib::fputc (c, f) : flush (); |
3775 | 56 else |
57 return traits_type::not_eof (c); | |
58 #else | |
3644 | 59 if (f) |
10411 | 60 return (c != EOF) ? gnulib::fputc (c, f) : flush (); |
3644 | 61 else |
62 return EOF; | |
3775 | 63 #endif |
3628 | 64 } |
65 | |
3775 | 66 c_file_ptr_buf::int_type |
4310 | 67 c_file_ptr_buf::underflow_common (bool bump) |
3628 | 68 { |
3644 | 69 if (f) |
4310 | 70 { |
12912
e116dd862879
use gnulib:: qualifiers for more stdio functions
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
71 int_type c = gnulib::fgetc (f); |
4310 | 72 |
73 if (! bump | |
74 #if defined (CXX_ISO_COMPLIANT_LIBRARY) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
75 && c != traits_type::eof ()) |
4310 | 76 #else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
77 && c != EOF) |
4310 | 78 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
79 ungetc (c, f); |
4310 | 80 |
81 return c; | |
82 } | |
3644 | 83 else |
3775 | 84 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
85 return traits_type::eof (); | |
86 #else | |
3644 | 87 return EOF; |
3775 | 88 #endif |
3628 | 89 } |
90 | |
3775 | 91 c_file_ptr_buf::int_type |
92 c_file_ptr_buf::pbackfail (int_type c) | |
3628 | 93 { |
3775 | 94 #if defined (CXX_ISO_COMPLIANT_LIBRARY) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
95 return (c != traits_type::eof () && f) ? ungetc (c, f) : |
3775 | 96 traits_type::not_eof (c); |
97 #else | |
3644 | 98 return (c != EOF && f) ? ungetc (c, f) : EOF; |
3775 | 99 #endif |
3628 | 100 } |
101 | |
102 std::streamsize | |
103 c_file_ptr_buf::xsputn (const char* s, std::streamsize n) | |
104 { | |
3644 | 105 if (f) |
10411 | 106 return gnulib::fwrite (s, 1, n, f); |
3644 | 107 else |
108 return 0; | |
3628 | 109 } |
110 | |
111 std::streamsize | |
112 c_file_ptr_buf::xsgetn (char *s, std::streamsize n) | |
113 { | |
3644 | 114 if (f) |
12912
e116dd862879
use gnulib:: qualifiers for more stdio functions
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
115 return gnulib::fread (s, 1, n, f); |
3644 | 116 else |
117 return 0; | |
3628 | 118 } |
119 | |
120 static inline int | |
121 seekdir_to_whence (std::ios::seekdir dir) | |
122 { | |
123 return ((dir == std::ios::beg) ? SEEK_SET : | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
124 (dir == std::ios::cur) ? SEEK_CUR : |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
125 (dir == std::ios::end) ? SEEK_END : |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
126 dir); |
3628 | 127 } |
128 | |
129 std::streampos | |
8802
061780d8da1e
c-file-ptr-stream.cc: avoid unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
130 c_file_ptr_buf::seekoff (std::streamoff /* offset */, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
131 std::ios::seekdir /* dir */, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
132 std::ios::openmode) |
3628 | 133 { |
5775 | 134 // FIXME |
4643 | 135 #if 0 |
3644 | 136 if (f) |
137 { | |
138 fseek (f, offset, seekdir_to_whence (dir)); | |
3628 | 139 |
3644 | 140 return ftell (f); |
141 } | |
142 else | |
143 return 0; | |
4643 | 144 #endif |
145 return -1; | |
3628 | 146 } |
147 | |
148 std::streampos | |
8802
061780d8da1e
c-file-ptr-stream.cc: avoid unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
149 c_file_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode) |
3628 | 150 { |
5775 | 151 // FIXME |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
152 #if 0 |
3644 | 153 if (f) |
154 { | |
155 fseek (f, offset, SEEK_SET); | |
3628 | 156 |
3644 | 157 return ftell (f); |
158 } | |
159 else | |
160 return 0; | |
4643 | 161 #endif |
162 return -1; | |
3628 | 163 } |
164 | |
165 int | |
166 c_file_ptr_buf::sync (void) | |
167 { | |
3652 | 168 flush (); |
169 | |
3628 | 170 return 0; |
171 } | |
172 | |
3652 | 173 int |
174 c_file_ptr_buf::flush (void) | |
175 { | |
12912
e116dd862879
use gnulib:: qualifiers for more stdio functions
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
176 return f ? gnulib::fflush (f) : EOF; |
3652 | 177 } |
178 | |
179 int | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
180 c_file_ptr_buf::buf_close (void) |
3652 | 181 { |
3693 | 182 int retval = -1; |
183 | |
3716 | 184 flush (); |
185 | |
3652 | 186 if (f) |
3693 | 187 { |
3716 | 188 retval = cf (f); |
3693 | 189 f = 0; |
190 } | |
191 | |
192 return retval; | |
3652 | 193 } |
194 | |
10411 | 195 int |
12927
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
196 c_file_ptr_buf::seek (long offset, int origin) |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
197 { |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
198 // gnulib::fseek doesn't seem to work, so don't use it until problem |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
199 // can be properly diagnosed and fixed. |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
200 return f ? fseek (f, offset, origin) : -1; |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
201 } |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
202 |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
203 long |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
204 c_file_ptr_buf::tell (void) |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
205 { |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
206 return f ? gnulib::ftell (f) : -1; |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
207 } |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
208 |
f9c1f7c1ead0
maint: undo part of previous gnulib changes
John W. Eaton <jwe@octave.org>
parents:
12912
diff
changeset
|
209 int |
10411 | 210 c_file_ptr_buf::file_close (FILE *f) |
211 { | |
212 return gnulib::fclose (f); | |
213 } | |
214 | |
5325 | 215 #ifdef HAVE_ZLIB |
216 | |
217 c_zfile_ptr_buf::~c_zfile_ptr_buf (void) | |
218 { | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
219 buf_close (); |
5325 | 220 } |
221 | |
5775 | 222 // FIXME -- I'm sure there is room for improvement here... |
5325 | 223 |
224 c_zfile_ptr_buf::int_type | |
225 c_zfile_ptr_buf::overflow (int_type c) | |
226 { | |
227 #if defined (CXX_ISO_COMPLIANT_LIBRARY) | |
228 if (f) | |
229 return (c != traits_type::eof ()) ? gzputc (f, c) : flush (); | |
230 else | |
231 return traits_type::not_eof (c); | |
232 #else | |
233 if (f) | |
234 return (c != EOF) ? gzputc (f, c) : flush (); | |
235 else | |
236 return EOF; | |
237 #endif | |
238 } | |
239 | |
240 c_zfile_ptr_buf::int_type | |
241 c_zfile_ptr_buf::underflow_common (bool bump) | |
242 { | |
243 if (f) | |
244 { | |
245 int_type c = gzgetc (f); | |
246 | |
247 if (! bump | |
248 #if defined (CXX_ISO_COMPLIANT_LIBRARY) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
249 && c != traits_type::eof ()) |
5325 | 250 #else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
251 && c != EOF) |
5325 | 252 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
253 gzungetc (c, f); |
5325 | 254 |
255 return c; | |
256 } | |
257 else | |
258 #if defined (CXX_ISO_COMPLIANT_LIBRARY) | |
259 return traits_type::eof (); | |
260 #else | |
261 return EOF; | |
262 #endif | |
263 } | |
264 | |
265 c_zfile_ptr_buf::int_type | |
266 c_zfile_ptr_buf::pbackfail (int_type c) | |
267 { | |
268 #if defined (CXX_ISO_COMPLIANT_LIBRARY) | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
269 return (c != traits_type::eof () && f) ? gzungetc (c, f) : |
5325 | 270 traits_type::not_eof (c); |
271 #else | |
272 return (c != EOF && f) ? gzungetc (c, f) : EOF; | |
273 #endif | |
274 } | |
275 | |
276 std::streamsize | |
277 c_zfile_ptr_buf::xsputn (const char* s, std::streamsize n) | |
278 { | |
279 if (f) | |
280 return gzwrite (f, s, n); | |
281 else | |
282 return 0; | |
283 } | |
284 | |
285 std::streamsize | |
286 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n) | |
287 { | |
288 if (f) | |
289 return gzread (f, s, n); | |
290 else | |
291 return 0; | |
292 } | |
293 | |
294 std::streampos | |
8802
061780d8da1e
c-file-ptr-stream.cc: avoid unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
295 c_zfile_ptr_buf::seekoff (std::streamoff /* offset */, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
296 std::ios::seekdir /* dir */, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10288
diff
changeset
|
297 std::ios::openmode) |
5325 | 298 { |
5775 | 299 // FIXME |
5325 | 300 #if 0 |
301 if (f) | |
302 { | |
303 gzseek (f, offset, seekdir_to_whence (dir)); | |
304 | |
305 return gztell (f); | |
306 } | |
307 else | |
308 return 0; | |
309 #endif | |
310 return -1; | |
311 } | |
312 | |
313 std::streampos | |
8802
061780d8da1e
c-file-ptr-stream.cc: avoid unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
314 c_zfile_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode) |
5325 | 315 { |
5775 | 316 // FIXME |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
317 #if 0 |
5325 | 318 if (f) |
319 { | |
320 gzseek (f, offset, SEEK_SET); | |
321 | |
322 return gztell (f); | |
323 } | |
324 else | |
325 return 0; | |
326 #endif | |
327 return -1; | |
328 } | |
329 | |
330 int | |
331 c_zfile_ptr_buf::sync (void) | |
332 { | |
333 flush (); | |
334 | |
335 return 0; | |
336 } | |
337 | |
338 int | |
339 c_zfile_ptr_buf::flush (void) | |
340 { | |
5775 | 341 // FIXME -- do we need something more complex here, passing |
5325 | 342 // something other than 0 for the second argument to gzflush and |
343 // checking the return value, etc.? | |
344 | |
345 return f ? gzflush (f, 0) : EOF; | |
346 } | |
347 | |
348 int | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
349 c_zfile_ptr_buf::buf_close (void) |
5325 | 350 { |
351 int retval = -1; | |
352 | |
353 flush (); | |
354 | |
355 if (f) | |
356 { | |
357 retval = cf (f); | |
358 f = 0; | |
359 } | |
360 | |
361 return retval; | |
362 } | |
363 | |
364 #endif |