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