Mercurial > hg > octave-nkf
annotate src/zfstream.cc @ 14348:95c43fc8dbe1 stable rc-3-6-1-0
3.6.1 release candidate 0
* configure.ac (AC_INIT): Version is now 3.6.1-rc0.
(OCTAVE_RELEASE_DATE): Now 2012-02-07.
* liboctave/Makefile.am: Bump liboctave revision version.
* src/Makefile.am: Bump liboctave revision version.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 09 Feb 2012 11:25:04 -0500 |
parents | 72c96de7a403 |
children | 460a3c6d8bf1 c1c5b3cc2996 |
rev | line source |
---|---|
5269 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
3 Copyright (C) 2005-2012 Ludwig Schwardt, Kevin Ruland |
5269 | 4 |
5 | |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5269 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5269 | 21 |
22 */ | |
23 | |
24 /* | |
25 | |
26 This file is adapted from the zlib 1.2.2 contrib/iostream3 code, | |
27 written by | |
28 | |
29 Ludwig Schwardt <schwardt@sun.ac.za> | |
30 original version by Kevin Ruland <kevin@rodin.wustl.edu> | |
31 | |
32 */ | |
33 | |
8025
60e938e1459b
zfstream.cc: include <config.h>
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
34 #ifdef HAVE_CONFIG_H |
60e938e1459b
zfstream.cc: include <config.h>
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
35 #include <config.h> |
60e938e1459b
zfstream.cc: include <config.h>
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
36 #endif |
60e938e1459b
zfstream.cc: include <config.h>
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
37 |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
38 #include <iostream> |
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
39 |
5269 | 40 #include "zfstream.h" |
41 | |
42 #ifdef HAVE_ZLIB | |
43 | |
44 #include <cstring> // for strcpy, strcat, strlen (mode strings) | |
45 #include <cstdio> // for BUFSIZ | |
46 | |
47 // Internal buffer sizes (default and "unbuffered" versions) | |
6783 | 48 #define STASHED_CHARACTERS 16 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
49 #define BIGBUFSIZE (256 * 1024 + STASHED_CHARACTERS) |
5269 | 50 #define SMALLBUFSIZE 1 |
51 | |
52 /*****************************************************************************/ | |
53 | |
54 // Default constructor | |
55 gzfilebuf::gzfilebuf() | |
7520 | 56 : file(0), io_mode(std::ios_base::openmode(0)), own_fd(false), |
57 buffer(0), buffer_size(BIGBUFSIZE), own_buffer(true) | |
5269 | 58 { |
59 // No buffers to start with | |
60 this->disable_buffer(); | |
61 } | |
62 | |
63 // Destructor | |
64 gzfilebuf::~gzfilebuf() | |
65 { | |
66 // Sync output buffer and close only if responsible for file | |
67 // (i.e. attached streams should be left open at this stage) | |
68 this->sync(); | |
69 if (own_fd) | |
70 this->close(); | |
71 // Make sure internal buffer is deallocated | |
72 this->disable_buffer(); | |
73 } | |
74 | |
75 // Set compression level and strategy | |
76 int | |
77 gzfilebuf::setcompression(int comp_level, | |
78 int comp_strategy) | |
79 { | |
80 return gzsetparams(file, comp_level, comp_strategy); | |
81 } | |
82 | |
83 // Open gzipped file | |
84 gzfilebuf* | |
85 gzfilebuf::open(const char *name, | |
86 std::ios_base::openmode mode) | |
87 { | |
88 // Fail if file already open | |
89 if (this->is_open()) | |
7520 | 90 return 0; |
5269 | 91 // Don't support simultaneous read/write access (yet) |
92 if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) | |
7520 | 93 return 0; |
5269 | 94 |
95 // Build mode string for gzopen and check it [27.8.1.3.2] | |
96 char char_mode[6] = "\0\0\0\0\0"; | |
97 if (!this->open_mode(mode, char_mode)) | |
7520 | 98 return 0; |
5269 | 99 |
100 // Attempt to open file | |
7520 | 101 if ((file = gzopen(name, char_mode)) == 0) |
102 return 0; | |
5269 | 103 |
104 // On success, allocate internal buffer and set flags | |
105 this->enable_buffer(); | |
106 io_mode = mode; | |
107 own_fd = true; | |
108 return this; | |
109 } | |
110 | |
111 // Attach to gzipped file | |
112 gzfilebuf* | |
113 gzfilebuf::attach(int fd, | |
114 std::ios_base::openmode mode) | |
115 { | |
116 // Fail if file already open | |
117 if (this->is_open()) | |
7520 | 118 return 0; |
5269 | 119 // Don't support simultaneous read/write access (yet) |
120 if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) | |
7520 | 121 return 0; |
5269 | 122 |
123 // Build mode string for gzdopen and check it [27.8.1.3.2] | |
124 char char_mode[6] = "\0\0\0\0\0"; | |
125 if (!this->open_mode(mode, char_mode)) | |
7520 | 126 return 0; |
5269 | 127 |
128 // Attempt to attach to file | |
7520 | 129 if ((file = gzdopen(fd, char_mode)) == 0) |
130 return 0; | |
5269 | 131 |
132 // On success, allocate internal buffer and set flags | |
133 this->enable_buffer(); | |
134 io_mode = mode; | |
135 own_fd = false; | |
136 return this; | |
137 } | |
138 | |
139 // Close gzipped file | |
140 gzfilebuf* | |
141 gzfilebuf::close() | |
142 { | |
143 // Fail immediately if no file is open | |
144 if (!this->is_open()) | |
7520 | 145 return 0; |
5269 | 146 // Assume success |
147 gzfilebuf* retval = this; | |
148 // Attempt to sync and close gzipped file | |
149 if (this->sync() == -1) | |
7520 | 150 retval = 0; |
5269 | 151 if (gzclose(file) < 0) |
7520 | 152 retval = 0; |
5269 | 153 // File is now gone anyway (postcondition [27.8.1.3.8]) |
7520 | 154 file = 0; |
5269 | 155 own_fd = false; |
156 // Destroy internal buffer if it exists | |
157 this->disable_buffer(); | |
158 return retval; | |
159 } | |
160 | |
161 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
162 | |
163 // Convert int open mode to mode string | |
164 bool | |
165 gzfilebuf::open_mode(std::ios_base::openmode mode, | |
166 char* c_mode) const | |
167 { | |
6959 | 168 // FIXME -- do we need testb? |
169 // bool testb = mode & std::ios_base::binary; | |
5269 | 170 bool testi = mode & std::ios_base::in; |
171 bool testo = mode & std::ios_base::out; | |
172 bool testt = mode & std::ios_base::trunc; | |
173 bool testa = mode & std::ios_base::app; | |
174 | |
175 // Check for valid flag combinations - see [27.8.1.3.2] (Table 92) | |
176 // Original zfstream hardcoded the compression level to maximum here... | |
177 // Double the time for less than 1% size improvement seems | |
178 // excessive though - keeping it at the default level | |
179 // To change back, just append "9" to the next three mode strings | |
180 if (!testi && testo && !testt && !testa) | |
181 strcpy(c_mode, "w"); | |
182 if (!testi && testo && !testt && testa) | |
183 strcpy(c_mode, "a"); | |
184 if (!testi && testo && testt && !testa) | |
185 strcpy(c_mode, "w"); | |
186 if (testi && !testo && !testt && !testa) | |
187 strcpy(c_mode, "r"); | |
188 // No read/write mode yet | |
189 // if (testi && testo && !testt && !testa) | |
190 // strcpy(c_mode, "r+"); | |
191 // if (testi && testo && testt && !testa) | |
192 // strcpy(c_mode, "w+"); | |
193 | |
194 // Mode string should be empty for invalid combination of flags | |
195 if (strlen(c_mode) == 0) | |
196 return false; | |
6275 | 197 |
198 strcat(c_mode, "b"); | |
199 | |
5269 | 200 return true; |
201 } | |
202 | |
203 // Determine number of characters in internal get buffer | |
204 std::streamsize | |
205 gzfilebuf::showmanyc() | |
206 { | |
207 // Calls to underflow will fail if file not opened for reading | |
208 if (!this->is_open() || !(io_mode & std::ios_base::in)) | |
209 return -1; | |
210 // Make sure get area is in use | |
211 if (this->gptr() && (this->gptr() < this->egptr())) | |
212 return std::streamsize(this->egptr() - this->gptr()); | |
213 else | |
214 return 0; | |
215 } | |
216 | |
6777 | 217 // Puts back a character to the stream in two cases. Firstly, when there |
218 // is no putback position available, and secondly when the character putback | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
219 // differs from the one in the file. We can only support the first case |
6777 | 220 // with gzipped files. |
221 gzfilebuf::int_type | |
222 gzfilebuf::pbackfail (gzfilebuf::int_type c) | |
223 { | |
224 if (this->is_open()) | |
225 { | |
226 if (gzseek (file, this->gptr() - this->egptr() - 1, SEEK_CUR) < 0) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
227 return traits_type::eof(); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
228 |
6777 | 229 // Invalidates contents of the buffer |
230 enable_buffer (); | |
231 | |
232 // Attempt to fill internal buffer from gzipped file | |
233 // (buffer must be guaranteed to exist...) | |
234 int bytes_read = gzread(file, buffer, buffer_size); | |
235 // Indicates error or EOF | |
236 if (bytes_read <= 0) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
237 { |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
238 // Reset get area |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
239 this->setg(buffer, buffer, buffer); |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
240 return traits_type::eof(); |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
241 } |
6777 | 242 |
243 // Make all bytes read from file available as get area | |
244 this->setg(buffer, buffer, buffer + bytes_read); | |
245 | |
246 // If next character in get area differs from putback character | |
247 // flag a failure | |
248 gzfilebuf::int_type ret = traits_type::to_int_type(*(this->gptr())); | |
249 if (ret != c) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
250 return traits_type::eof(); |
6777 | 251 else |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
252 return ret; |
6777 | 253 } |
254 else | |
255 return traits_type::eof(); | |
256 } | |
257 | |
5269 | 258 // Fill get area from gzipped file |
259 gzfilebuf::int_type | |
260 gzfilebuf::underflow() | |
261 { | |
262 // If something is left in the get area by chance, return it | |
263 // (this shouldn't normally happen, as underflow is only supposed | |
264 // to be called when gptr >= egptr, but it serves as error check) | |
265 if (this->gptr() && (this->gptr() < this->egptr())) | |
266 return traits_type::to_int_type(*(this->gptr())); | |
267 | |
268 // If the file hasn't been opened for reading, produce error | |
269 if (!this->is_open() || !(io_mode & std::ios_base::in)) | |
270 return traits_type::eof(); | |
271 | |
6783 | 272 // Copy the final characters to the front of the buffer |
273 int stash = 0; | |
274 if (this->eback() && buffer && buffer_size > STASHED_CHARACTERS) | |
275 { | |
276 char_type *ptr1 = buffer; | |
277 char_type *ptr2 = this->egptr() - STASHED_CHARACTERS + 1; | |
278 if (ptr2 > this->eback()) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
279 while (stash++ <= STASHED_CHARACTERS) |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
280 *ptr1++ = *ptr2++; |
6783 | 281 } |
282 | |
5269 | 283 // Attempt to fill internal buffer from gzipped file |
284 // (buffer must be guaranteed to exist...) | |
6783 | 285 int bytes_read = gzread(file, buffer + stash, buffer_size - stash); |
286 | |
5269 | 287 // Indicates error or EOF |
288 if (bytes_read <= 0) | |
289 { | |
290 // Reset get area | |
291 this->setg(buffer, buffer, buffer); | |
292 return traits_type::eof(); | |
293 } | |
6783 | 294 // Make all bytes read from file plus the stash available as get area |
295 this->setg(buffer, buffer + stash, buffer + bytes_read + stash); | |
5269 | 296 |
297 // Return next character in get area | |
298 return traits_type::to_int_type(*(this->gptr())); | |
299 } | |
300 | |
301 // Write put area to gzipped file | |
302 gzfilebuf::int_type | |
303 gzfilebuf::overflow(int_type c) | |
304 { | |
305 // Determine whether put area is in use | |
306 if (this->pbase()) | |
307 { | |
308 // Double-check pointer range | |
309 if (this->pptr() > this->epptr() || this->pptr() < this->pbase()) | |
310 return traits_type::eof(); | |
311 // Add extra character to buffer if not EOF | |
312 if (!traits_type::eq_int_type(c, traits_type::eof())) | |
313 { | |
314 *(this->pptr()) = traits_type::to_char_type(c); | |
315 this->pbump(1); | |
316 } | |
317 // Number of characters to write to file | |
318 int bytes_to_write = this->pptr() - this->pbase(); | |
319 // Overflow doesn't fail if nothing is to be written | |
320 if (bytes_to_write > 0) | |
321 { | |
322 // If the file hasn't been opened for writing, produce error | |
323 if (!this->is_open() || !(io_mode & std::ios_base::out)) | |
324 return traits_type::eof(); | |
325 // If gzipped file won't accept all bytes written to it, fail | |
326 if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write) | |
327 return traits_type::eof(); | |
328 // Reset next pointer to point to pbase on success | |
329 this->pbump(-bytes_to_write); | |
330 } | |
331 } | |
332 // Write extra character to file if not EOF | |
333 else if (!traits_type::eq_int_type(c, traits_type::eof())) | |
334 { | |
335 // If the file hasn't been opened for writing, produce error | |
336 if (!this->is_open() || !(io_mode & std::ios_base::out)) | |
337 return traits_type::eof(); | |
338 // Impromptu char buffer (allows "unbuffered" output) | |
339 char_type last_char = traits_type::to_char_type(c); | |
340 // If gzipped file won't accept this character, fail | |
341 if (gzwrite(file, &last_char, 1) != 1) | |
342 return traits_type::eof(); | |
343 } | |
344 | |
345 // If you got here, you have succeeded (even if c was EOF) | |
346 // The return value should therefore be non-EOF | |
347 if (traits_type::eq_int_type(c, traits_type::eof())) | |
348 return traits_type::not_eof(c); | |
349 else | |
350 return c; | |
351 } | |
352 | |
353 // Assign new buffer | |
354 std::streambuf* | |
355 gzfilebuf::setbuf(char_type* p, | |
356 std::streamsize n) | |
357 { | |
358 // First make sure stuff is sync'ed, for safety | |
359 if (this->sync() == -1) | |
7520 | 360 return 0; |
5269 | 361 // If buffering is turned off on purpose via setbuf(0,0), still allocate one... |
362 // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at | |
363 // least a buffer of size 1 (very inefficient though, therefore make it bigger?) | |
364 // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems) | |
365 if (!p || !n) | |
366 { | |
367 // Replace existing buffer (if any) with small internal buffer | |
368 this->disable_buffer(); | |
7520 | 369 buffer = 0; |
5269 | 370 buffer_size = 0; |
371 own_buffer = true; | |
372 this->enable_buffer(); | |
373 } | |
374 else | |
375 { | |
376 // Replace existing buffer (if any) with external buffer | |
377 this->disable_buffer(); | |
378 buffer = p; | |
379 buffer_size = n; | |
380 own_buffer = false; | |
381 this->enable_buffer(); | |
382 } | |
383 return this; | |
384 } | |
385 | |
386 // Write put area to gzipped file (i.e. ensures that put area is empty) | |
387 int | |
388 gzfilebuf::sync() | |
389 { | |
390 return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0; | |
391 } | |
392 | |
393 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
394 | |
395 // Allocate internal buffer | |
396 void | |
397 gzfilebuf::enable_buffer() | |
398 { | |
399 // If internal buffer required, allocate one | |
400 if (own_buffer && !buffer) | |
401 { | |
402 // Check for buffered vs. "unbuffered" | |
403 if (buffer_size > 0) | |
404 { | |
405 // Allocate internal buffer | |
406 buffer = new char_type[buffer_size]; | |
407 // Get area starts empty and will be expanded by underflow as need arises | |
408 this->setg(buffer, buffer, buffer); | |
409 // Setup entire internal buffer as put area. | |
410 // The one-past-end pointer actually points to the last element of the buffer, | |
411 // so that overflow(c) can safely add the extra character c to the sequence. | |
412 // These pointers remain in place for the duration of the buffer | |
413 this->setp(buffer, buffer + buffer_size - 1); | |
414 } | |
415 else | |
416 { | |
417 // Even in "unbuffered" case, (small?) get buffer is still required | |
418 buffer_size = SMALLBUFSIZE; | |
419 buffer = new char_type[buffer_size]; | |
420 this->setg(buffer, buffer, buffer); | |
421 // "Unbuffered" means no put buffer | |
422 this->setp(0, 0); | |
423 } | |
424 } | |
425 else | |
426 { | |
427 // If buffer already allocated, reset buffer pointers just to make sure no | |
428 // stale chars are lying around | |
429 this->setg(buffer, buffer, buffer); | |
430 this->setp(buffer, buffer + buffer_size - 1); | |
431 } | |
432 } | |
433 | |
434 // Destroy internal buffer | |
435 void | |
436 gzfilebuf::disable_buffer() | |
437 { | |
438 // If internal buffer exists, deallocate it | |
439 if (own_buffer && buffer) | |
440 { | |
441 // Preserve unbuffered status by zeroing size | |
442 if (!this->pbase()) | |
443 buffer_size = 0; | |
444 delete[] buffer; | |
7520 | 445 buffer = 0; |
5269 | 446 this->setg(0, 0, 0); |
447 this->setp(0, 0); | |
448 } | |
449 else | |
450 { | |
451 // Reset buffer pointers to initial state if external buffer exists | |
452 this->setg(buffer, buffer, buffer); | |
453 if (buffer) | |
454 this->setp(buffer, buffer + buffer_size - 1); | |
455 else | |
456 this->setp(0, 0); | |
457 } | |
458 } | |
459 | |
460 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
461 | |
462 // Seek functions | |
463 gzfilebuf::pos_type | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
464 gzfilebuf::seekoff(off_type off, std::ios_base::seekdir way, |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
465 std::ios_base::openmode) |
5269 | 466 { |
467 pos_type ret = pos_type (off_type (-1)); | |
468 | |
469 if (this->is_open()) | |
470 { | |
471 off_type computed_off = off; | |
472 | |
473 if ((io_mode & std::ios_base::in) && way == std::ios_base::cur) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
474 computed_off += this->gptr() - this->egptr(); |
5269 | 475 |
476 if (way == std::ios_base::beg) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
477 ret = pos_type (gzseek (file, computed_off, SEEK_SET)); |
5269 | 478 else if (way == std::ios_base::cur) |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
479 ret = pos_type (gzseek (file, computed_off, SEEK_CUR)); |
5269 | 480 else |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
481 // Can't seek from end of a gzipped file, so this will give -1 |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
482 ret = pos_type (gzseek (file, computed_off, SEEK_END)); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
483 |
5269 | 484 if (io_mode & std::ios_base::in) |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
485 // Invalidates contents of the buffer |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
486 enable_buffer (); |
5269 | 487 else |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
488 // flush contents of buffer to file |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
489 overflow (); |
5269 | 490 } |
491 | |
492 return ret; | |
493 } | |
494 | |
495 gzfilebuf::pos_type | |
496 gzfilebuf::seekpos(pos_type sp, std::ios_base::openmode) | |
497 { | |
498 pos_type ret = pos_type (off_type (-1)); | |
499 | |
500 if (this->is_open ()) | |
501 { | |
502 ret = pos_type (gzseek (file, sp, SEEK_SET)); | |
503 | |
504 if (io_mode & std::ios_base::in) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
505 // Invalidates contents of the buffer |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
506 enable_buffer (); |
5269 | 507 else |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
508 // flush contents of buffer to file |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
509 overflow (); |
5269 | 510 } |
511 | |
512 return ret; | |
513 } | |
514 | |
515 /*****************************************************************************/ | |
516 | |
517 // Default constructor initializes stream buffer | |
518 gzifstream::gzifstream() | |
7520 | 519 : std::istream(0), sb() |
5269 | 520 { this->init(&sb); } |
521 | |
522 // Initialize stream buffer and open file | |
523 gzifstream::gzifstream(const char* name, | |
524 std::ios_base::openmode mode) | |
7520 | 525 : std::istream(0), sb() |
5269 | 526 { |
527 this->init(&sb); | |
528 this->open(name, mode); | |
529 } | |
530 | |
531 // Initialize stream buffer and attach to file | |
532 gzifstream::gzifstream(int fd, | |
533 std::ios_base::openmode mode) | |
7520 | 534 : std::istream(0), sb() |
5269 | 535 { |
536 this->init(&sb); | |
537 this->attach(fd, mode); | |
538 } | |
539 | |
540 // Open file and go into fail() state if unsuccessful | |
541 void | |
542 gzifstream::open(const char* name, | |
543 std::ios_base::openmode mode) | |
544 { | |
545 if (!sb.open(name, mode | std::ios_base::in)) | |
546 this->setstate(std::ios_base::failbit); | |
547 else | |
548 this->clear(); | |
549 } | |
550 | |
551 // Attach to file and go into fail() state if unsuccessful | |
552 void | |
553 gzifstream::attach(int fd, | |
554 std::ios_base::openmode mode) | |
555 { | |
556 if (!sb.attach(fd, mode | std::ios_base::in)) | |
557 this->setstate(std::ios_base::failbit); | |
558 else | |
559 this->clear(); | |
560 } | |
561 | |
562 // Close file | |
563 void | |
564 gzifstream::close() | |
565 { | |
566 if (!sb.close()) | |
567 this->setstate(std::ios_base::failbit); | |
568 } | |
569 | |
570 /*****************************************************************************/ | |
571 | |
572 // Default constructor initializes stream buffer | |
573 gzofstream::gzofstream() | |
7520 | 574 : std::ostream(0), sb() |
5269 | 575 { this->init(&sb); } |
576 | |
577 // Initialize stream buffer and open file | |
578 gzofstream::gzofstream(const char* name, | |
579 std::ios_base::openmode mode) | |
7520 | 580 : std::ostream(0), sb() |
5269 | 581 { |
582 this->init(&sb); | |
583 this->open(name, mode); | |
584 } | |
585 | |
586 // Initialize stream buffer and attach to file | |
587 gzofstream::gzofstream(int fd, | |
588 std::ios_base::openmode mode) | |
7520 | 589 : std::ostream(0), sb() |
5269 | 590 { |
591 this->init(&sb); | |
592 this->attach(fd, mode); | |
593 } | |
594 | |
595 // Open file and go into fail() state if unsuccessful | |
596 void | |
597 gzofstream::open(const char* name, | |
598 std::ios_base::openmode mode) | |
599 { | |
600 if (!sb.open(name, mode | std::ios_base::out)) | |
601 this->setstate(std::ios_base::failbit); | |
602 else | |
603 this->clear(); | |
604 } | |
605 | |
606 // Attach to file and go into fail() state if unsuccessful | |
607 void | |
608 gzofstream::attach(int fd, | |
609 std::ios_base::openmode mode) | |
610 { | |
611 if (!sb.attach(fd, mode | std::ios_base::out)) | |
612 this->setstate(std::ios_base::failbit); | |
613 else | |
614 this->clear(); | |
615 } | |
616 | |
617 // Close file | |
618 void | |
619 gzofstream::close() | |
620 { | |
621 if (!sb.close()) | |
622 this->setstate(std::ios_base::failbit); | |
623 } | |
624 | |
625 #endif // HAVE_ZLIB |