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; |
|
190 if (testb) |
|
191 strcat(c_mode, "b"); |
|
192 return true; |
|
193 } |
|
194 |
|
195 // Determine number of characters in internal get buffer |
|
196 std::streamsize |
|
197 gzfilebuf::showmanyc() |
|
198 { |
|
199 // Calls to underflow will fail if file not opened for reading |
|
200 if (!this->is_open() || !(io_mode & std::ios_base::in)) |
|
201 return -1; |
|
202 // Make sure get area is in use |
|
203 if (this->gptr() && (this->gptr() < this->egptr())) |
|
204 return std::streamsize(this->egptr() - this->gptr()); |
|
205 else |
|
206 return 0; |
|
207 } |
|
208 |
|
209 // Fill get area from gzipped file |
|
210 gzfilebuf::int_type |
|
211 gzfilebuf::underflow() |
|
212 { |
|
213 // If something is left in the get area by chance, return it |
|
214 // (this shouldn't normally happen, as underflow is only supposed |
|
215 // to be called when gptr >= egptr, but it serves as error check) |
|
216 if (this->gptr() && (this->gptr() < this->egptr())) |
|
217 return traits_type::to_int_type(*(this->gptr())); |
|
218 |
|
219 // If the file hasn't been opened for reading, produce error |
|
220 if (!this->is_open() || !(io_mode & std::ios_base::in)) |
|
221 return traits_type::eof(); |
|
222 |
|
223 // Attempt to fill internal buffer from gzipped file |
|
224 // (buffer must be guaranteed to exist...) |
|
225 int bytes_read = gzread(file, buffer, buffer_size); |
|
226 // Indicates error or EOF |
|
227 if (bytes_read <= 0) |
|
228 { |
|
229 // Reset get area |
|
230 this->setg(buffer, buffer, buffer); |
|
231 return traits_type::eof(); |
|
232 } |
|
233 // Make all bytes read from file available as get area |
|
234 this->setg(buffer, buffer, buffer + bytes_read); |
|
235 |
|
236 // Return next character in get area |
|
237 return traits_type::to_int_type(*(this->gptr())); |
|
238 } |
|
239 |
|
240 // Write put area to gzipped file |
|
241 gzfilebuf::int_type |
|
242 gzfilebuf::overflow(int_type c) |
|
243 { |
|
244 // Determine whether put area is in use |
|
245 if (this->pbase()) |
|
246 { |
|
247 // Double-check pointer range |
|
248 if (this->pptr() > this->epptr() || this->pptr() < this->pbase()) |
|
249 return traits_type::eof(); |
|
250 // Add extra character to buffer if not EOF |
|
251 if (!traits_type::eq_int_type(c, traits_type::eof())) |
|
252 { |
|
253 *(this->pptr()) = traits_type::to_char_type(c); |
|
254 this->pbump(1); |
|
255 } |
|
256 // Number of characters to write to file |
|
257 int bytes_to_write = this->pptr() - this->pbase(); |
|
258 // Overflow doesn't fail if nothing is to be written |
|
259 if (bytes_to_write > 0) |
|
260 { |
|
261 // If the file hasn't been opened for writing, produce error |
|
262 if (!this->is_open() || !(io_mode & std::ios_base::out)) |
|
263 return traits_type::eof(); |
|
264 // If gzipped file won't accept all bytes written to it, fail |
|
265 if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write) |
|
266 return traits_type::eof(); |
|
267 // Reset next pointer to point to pbase on success |
|
268 this->pbump(-bytes_to_write); |
|
269 } |
|
270 } |
|
271 // Write extra character to file if not EOF |
|
272 else if (!traits_type::eq_int_type(c, traits_type::eof())) |
|
273 { |
|
274 // If the file hasn't been opened for writing, produce error |
|
275 if (!this->is_open() || !(io_mode & std::ios_base::out)) |
|
276 return traits_type::eof(); |
|
277 // Impromptu char buffer (allows "unbuffered" output) |
|
278 char_type last_char = traits_type::to_char_type(c); |
|
279 // If gzipped file won't accept this character, fail |
|
280 if (gzwrite(file, &last_char, 1) != 1) |
|
281 return traits_type::eof(); |
|
282 } |
|
283 |
|
284 // If you got here, you have succeeded (even if c was EOF) |
|
285 // The return value should therefore be non-EOF |
|
286 if (traits_type::eq_int_type(c, traits_type::eof())) |
|
287 return traits_type::not_eof(c); |
|
288 else |
|
289 return c; |
|
290 } |
|
291 |
|
292 // Assign new buffer |
|
293 std::streambuf* |
|
294 gzfilebuf::setbuf(char_type* p, |
|
295 std::streamsize n) |
|
296 { |
|
297 // First make sure stuff is sync'ed, for safety |
|
298 if (this->sync() == -1) |
|
299 return NULL; |
|
300 // If buffering is turned off on purpose via setbuf(0,0), still allocate one... |
|
301 // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at |
|
302 // least a buffer of size 1 (very inefficient though, therefore make it bigger?) |
|
303 // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems) |
|
304 if (!p || !n) |
|
305 { |
|
306 // Replace existing buffer (if any) with small internal buffer |
|
307 this->disable_buffer(); |
|
308 buffer = NULL; |
|
309 buffer_size = 0; |
|
310 own_buffer = true; |
|
311 this->enable_buffer(); |
|
312 } |
|
313 else |
|
314 { |
|
315 // Replace existing buffer (if any) with external buffer |
|
316 this->disable_buffer(); |
|
317 buffer = p; |
|
318 buffer_size = n; |
|
319 own_buffer = false; |
|
320 this->enable_buffer(); |
|
321 } |
|
322 return this; |
|
323 } |
|
324 |
|
325 // Write put area to gzipped file (i.e. ensures that put area is empty) |
|
326 int |
|
327 gzfilebuf::sync() |
|
328 { |
|
329 return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0; |
|
330 } |
|
331 |
|
332 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
|
333 |
|
334 // Allocate internal buffer |
|
335 void |
|
336 gzfilebuf::enable_buffer() |
|
337 { |
|
338 // If internal buffer required, allocate one |
|
339 if (own_buffer && !buffer) |
|
340 { |
|
341 // Check for buffered vs. "unbuffered" |
|
342 if (buffer_size > 0) |
|
343 { |
|
344 // Allocate internal buffer |
|
345 buffer = new char_type[buffer_size]; |
|
346 // Get area starts empty and will be expanded by underflow as need arises |
|
347 this->setg(buffer, buffer, buffer); |
|
348 // Setup entire internal buffer as put area. |
|
349 // The one-past-end pointer actually points to the last element of the buffer, |
|
350 // so that overflow(c) can safely add the extra character c to the sequence. |
|
351 // These pointers remain in place for the duration of the buffer |
|
352 this->setp(buffer, buffer + buffer_size - 1); |
|
353 } |
|
354 else |
|
355 { |
|
356 // Even in "unbuffered" case, (small?) get buffer is still required |
|
357 buffer_size = SMALLBUFSIZE; |
|
358 buffer = new char_type[buffer_size]; |
|
359 this->setg(buffer, buffer, buffer); |
|
360 // "Unbuffered" means no put buffer |
|
361 this->setp(0, 0); |
|
362 } |
|
363 } |
|
364 else |
|
365 { |
|
366 // If buffer already allocated, reset buffer pointers just to make sure no |
|
367 // stale chars are lying around |
|
368 this->setg(buffer, buffer, buffer); |
|
369 this->setp(buffer, buffer + buffer_size - 1); |
|
370 } |
|
371 } |
|
372 |
|
373 // Destroy internal buffer |
|
374 void |
|
375 gzfilebuf::disable_buffer() |
|
376 { |
|
377 // If internal buffer exists, deallocate it |
|
378 if (own_buffer && buffer) |
|
379 { |
|
380 // Preserve unbuffered status by zeroing size |
|
381 if (!this->pbase()) |
|
382 buffer_size = 0; |
|
383 delete[] buffer; |
|
384 buffer = NULL; |
|
385 this->setg(0, 0, 0); |
|
386 this->setp(0, 0); |
|
387 } |
|
388 else |
|
389 { |
|
390 // Reset buffer pointers to initial state if external buffer exists |
|
391 this->setg(buffer, buffer, buffer); |
|
392 if (buffer) |
|
393 this->setp(buffer, buffer + buffer_size - 1); |
|
394 else |
|
395 this->setp(0, 0); |
|
396 } |
|
397 } |
|
398 |
|
399 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
|
400 |
|
401 // Seek functions |
|
402 gzfilebuf::pos_type |
|
403 gzfilebuf::seekoff(off_type off, std::ios_base::seekdir way, |
|
404 std::ios_base::openmode) |
|
405 { |
|
406 pos_type ret = pos_type (off_type (-1)); |
|
407 |
|
408 if (this->is_open()) |
|
409 { |
|
410 off_type computed_off = off; |
|
411 |
|
412 if ((io_mode & std::ios_base::in) && way == std::ios_base::cur) |
|
413 computed_off += this->gptr() - this->egptr(); |
|
414 |
|
415 if (way == std::ios_base::beg) |
|
416 ret = pos_type (gzseek (file, computed_off, SEEK_SET)); |
|
417 else if (way == std::ios_base::cur) |
|
418 ret = pos_type (gzseek (file, computed_off, SEEK_CUR)); |
|
419 else |
|
420 // Can't seek from end of a gzipped file, so this will give -1 |
|
421 ret = pos_type (gzseek (file, computed_off, SEEK_END)); |
|
422 |
|
423 if (io_mode & std::ios_base::in) |
|
424 // Invalidates contents of the buffer |
|
425 enable_buffer (); |
|
426 else |
|
427 // flush contents of buffer to file |
|
428 overflow (); |
|
429 } |
|
430 |
|
431 return ret; |
|
432 } |
|
433 |
|
434 gzfilebuf::pos_type |
|
435 gzfilebuf::seekpos(pos_type sp, std::ios_base::openmode) |
|
436 { |
|
437 pos_type ret = pos_type (off_type (-1)); |
|
438 |
|
439 if (this->is_open ()) |
|
440 { |
|
441 ret = pos_type (gzseek (file, sp, SEEK_SET)); |
|
442 |
|
443 if (io_mode & std::ios_base::in) |
|
444 // Invalidates contents of the buffer |
|
445 enable_buffer (); |
|
446 else |
|
447 // flush contents of buffer to file |
|
448 overflow (); |
|
449 } |
|
450 |
|
451 return ret; |
|
452 } |
|
453 |
|
454 /*****************************************************************************/ |
|
455 |
|
456 // Default constructor initializes stream buffer |
|
457 gzifstream::gzifstream() |
|
458 : std::istream(NULL), sb() |
|
459 { this->init(&sb); } |
|
460 |
|
461 // Initialize stream buffer and open file |
|
462 gzifstream::gzifstream(const char* name, |
|
463 std::ios_base::openmode mode) |
|
464 : std::istream(NULL), sb() |
|
465 { |
|
466 this->init(&sb); |
|
467 this->open(name, mode); |
|
468 } |
|
469 |
|
470 // Initialize stream buffer and attach to file |
|
471 gzifstream::gzifstream(int fd, |
|
472 std::ios_base::openmode mode) |
|
473 : std::istream(NULL), sb() |
|
474 { |
|
475 this->init(&sb); |
|
476 this->attach(fd, mode); |
|
477 } |
|
478 |
|
479 // Open file and go into fail() state if unsuccessful |
|
480 void |
|
481 gzifstream::open(const char* name, |
|
482 std::ios_base::openmode mode) |
|
483 { |
|
484 if (!sb.open(name, mode | std::ios_base::in)) |
|
485 this->setstate(std::ios_base::failbit); |
|
486 else |
|
487 this->clear(); |
|
488 } |
|
489 |
|
490 // Attach to file and go into fail() state if unsuccessful |
|
491 void |
|
492 gzifstream::attach(int fd, |
|
493 std::ios_base::openmode mode) |
|
494 { |
|
495 if (!sb.attach(fd, mode | std::ios_base::in)) |
|
496 this->setstate(std::ios_base::failbit); |
|
497 else |
|
498 this->clear(); |
|
499 } |
|
500 |
|
501 // Close file |
|
502 void |
|
503 gzifstream::close() |
|
504 { |
|
505 if (!sb.close()) |
|
506 this->setstate(std::ios_base::failbit); |
|
507 } |
|
508 |
|
509 /*****************************************************************************/ |
|
510 |
|
511 // Default constructor initializes stream buffer |
|
512 gzofstream::gzofstream() |
|
513 : std::ostream(NULL), sb() |
|
514 { this->init(&sb); } |
|
515 |
|
516 // Initialize stream buffer and open file |
|
517 gzofstream::gzofstream(const char* name, |
|
518 std::ios_base::openmode mode) |
|
519 : std::ostream(NULL), sb() |
|
520 { |
|
521 this->init(&sb); |
|
522 this->open(name, mode); |
|
523 } |
|
524 |
|
525 // Initialize stream buffer and attach to file |
|
526 gzofstream::gzofstream(int fd, |
|
527 std::ios_base::openmode mode) |
|
528 : std::ostream(NULL), sb() |
|
529 { |
|
530 this->init(&sb); |
|
531 this->attach(fd, mode); |
|
532 } |
|
533 |
|
534 // Open file and go into fail() state if unsuccessful |
|
535 void |
|
536 gzofstream::open(const char* name, |
|
537 std::ios_base::openmode mode) |
|
538 { |
|
539 if (!sb.open(name, mode | std::ios_base::out)) |
|
540 this->setstate(std::ios_base::failbit); |
|
541 else |
|
542 this->clear(); |
|
543 } |
|
544 |
|
545 // Attach to file and go into fail() state if unsuccessful |
|
546 void |
|
547 gzofstream::attach(int fd, |
|
548 std::ios_base::openmode mode) |
|
549 { |
|
550 if (!sb.attach(fd, mode | std::ios_base::out)) |
|
551 this->setstate(std::ios_base::failbit); |
|
552 else |
|
553 this->clear(); |
|
554 } |
|
555 |
|
556 // Close file |
|
557 void |
|
558 gzofstream::close() |
|
559 { |
|
560 if (!sb.close()) |
|
561 this->setstate(std::ios_base::failbit); |
|
562 } |
|
563 |
|
564 #endif // HAVE_ZLIB |
|
565 |
|
566 /* |
|
567 ;;; Local Variables: *** |
|
568 ;;; mode: C++ *** |
|
569 ;;; End: *** |
|
570 */ |