5269
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 Ludwig Schwardt, Kevin Ruland |
|
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. |
5269
|
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/>. |
5269
|
20 |
|
21 */ |
|
22 |
|
23 /* |
|
24 |
|
25 This file is adapted from the zlib 1.2.2 contrib/iostream3 code, |
|
26 written by |
|
27 |
|
28 Ludwig Schwardt <schwardt@sun.ac.za> |
|
29 original version by Kevin Ruland <kevin@rodin.wustl.edu> |
|
30 |
|
31 */ |
|
32 |
|
33 #ifndef ZFSTREAM_H |
|
34 #define ZFSTREAM_H |
|
35 |
|
36 #ifdef HAVE_CONFIG_H |
|
37 #include <config.h> |
|
38 #endif |
|
39 |
|
40 #ifdef HAVE_ZLIB |
|
41 |
|
42 #include <istream> // not iostream, since we don't need cin/cout |
|
43 #include <ostream> |
|
44 #include "zlib.h" |
|
45 |
|
46 /*****************************************************************************/ |
|
47 |
|
48 /** |
|
49 * @brief Gzipped file stream buffer class. |
|
50 * |
|
51 * This class implements basic_filebuf for gzipped files. It doesn't yet support |
|
52 * seeking (allowed by zlib but slow/limited), putback and read/write access |
|
53 * (tricky). Otherwise, it attempts to be a drop-in replacement for the standard |
|
54 * file streambuf. |
|
55 */ |
|
56 class gzfilebuf : public std::streambuf |
|
57 { |
|
58 public: |
|
59 // Default constructor. |
|
60 gzfilebuf(); |
|
61 |
|
62 // Destructor. |
|
63 virtual |
|
64 ~gzfilebuf(); |
|
65 |
|
66 /** |
|
67 * @brief Set compression level and strategy on the fly. |
|
68 * @param comp_level Compression level (see zlib.h for allowed values) |
|
69 * @param comp_strategy Compression strategy (see zlib.h for allowed values) |
|
70 * @return Z_OK on success, Z_STREAM_ERROR otherwise. |
|
71 * |
|
72 * Unfortunately, these parameters cannot be modified separately, as the |
|
73 * previous zfstream version assumed. Since the strategy is seldom changed, |
|
74 * it can default and setcompression(level) then becomes like the old |
|
75 * setcompressionlevel(level). |
|
76 */ |
|
77 int |
|
78 setcompression(int comp_level, |
|
79 int comp_strategy = Z_DEFAULT_STRATEGY); |
|
80 |
|
81 /** |
|
82 * @brief Check if file is open. |
|
83 * @return True if file is open. |
|
84 */ |
|
85 bool |
|
86 is_open() const { return (file != NULL); } |
|
87 |
|
88 /** |
|
89 * @brief Open gzipped file. |
|
90 * @param name File name. |
|
91 * @param mode Open mode flags. |
|
92 * @return @c this on success, NULL on failure. |
|
93 */ |
|
94 gzfilebuf* |
|
95 open(const char* name, |
|
96 std::ios_base::openmode mode); |
|
97 |
|
98 /** |
|
99 * @brief Attach to already open gzipped file. |
|
100 * @param fd File descriptor. |
|
101 * @param mode Open mode flags. |
|
102 * @return @c this on success, NULL on failure. |
|
103 */ |
|
104 gzfilebuf* |
|
105 attach(int fd, |
|
106 std::ios_base::openmode mode); |
|
107 |
|
108 /** |
|
109 * @brief Close gzipped file. |
|
110 * @return @c this on success, NULL on failure. |
|
111 */ |
|
112 gzfilebuf* |
|
113 close(); |
|
114 |
|
115 protected: |
|
116 /** |
|
117 * @brief Convert ios open mode int to mode string used by zlib. |
|
118 * @return True if valid mode flag combination. |
|
119 */ |
|
120 bool |
|
121 open_mode(std::ios_base::openmode mode, |
|
122 char* c_mode) const; |
|
123 |
|
124 /** |
|
125 * @brief Number of characters available in stream buffer. |
|
126 * @return Number of characters. |
|
127 * |
|
128 * This indicates number of characters in get area of stream buffer. |
|
129 * These characters can be read without accessing the gzipped file. |
|
130 */ |
|
131 virtual std::streamsize |
|
132 showmanyc(); |
|
133 |
|
134 /** |
|
135 * @brief Fill get area from gzipped file. |
|
136 * @return First character in get area on success, EOF on error. |
|
137 * |
|
138 * This actually reads characters from gzipped file to stream |
|
139 * buffer. Always buffered. |
|
140 */ |
|
141 virtual int_type |
|
142 underflow(); |
|
143 |
|
144 /** |
|
145 * @brief Write put area to gzipped file. |
|
146 * @param c Extra character to add to buffer contents. |
|
147 * @return Non-EOF on success, EOF on error. |
|
148 * |
|
149 * This actually writes characters in stream buffer to |
|
150 * gzipped file. With unbuffered output this is done one |
|
151 * character at a time. |
|
152 */ |
|
153 virtual int_type |
|
154 overflow(int_type c = traits_type::eof()); |
|
155 |
|
156 /** |
|
157 * @brief Installs external stream buffer. |
|
158 * @param p Pointer to char buffer. |
|
159 * @param n Size of external buffer. |
|
160 * @return @c this on success, NULL on failure. |
|
161 * |
|
162 * Call setbuf(0,0) to enable unbuffered output. |
|
163 */ |
|
164 virtual std::streambuf* |
|
165 setbuf(char_type* p, |
|
166 std::streamsize n); |
|
167 |
|
168 /** |
|
169 * @brief Flush stream buffer to file. |
|
170 * @return 0 on success, -1 on error. |
|
171 * |
|
172 * This calls underflow(EOF) to do the job. |
|
173 */ |
|
174 virtual int |
|
175 sync(); |
|
176 |
|
177 /** |
|
178 * @brief Alters the stream positions. |
|
179 * |
|
180 * Each derived class provides its own appropriate behavior. |
|
181 */ |
|
182 virtual pos_type |
|
183 seekoff(off_type off, std::ios_base::seekdir way, |
|
184 std::ios_base::openmode mode = |
|
185 std::ios_base::in|std::ios_base::out); |
|
186 |
|
187 /** |
|
188 * @brief Alters the stream positions. |
|
189 * |
|
190 * Each derived class provides its own appropriate behavior. |
|
191 */ |
|
192 virtual pos_type |
|
193 seekpos(pos_type sp, std::ios_base::openmode mode = |
|
194 std::ios_base::in|std::ios_base::out); |
|
195 |
6777
|
196 virtual int_type |
|
197 pbackfail (int_type c = traits_type::eof()); |
|
198 |
5269
|
199 // |
|
200 // Some future enhancements |
|
201 // |
|
202 // virtual int_type uflow(); |
|
203 // virtual int_type pbackfail(int_type c = traits_type::eof()); |
|
204 |
|
205 private: |
|
206 /** |
|
207 * @brief Allocate internal buffer. |
|
208 * |
|
209 * This function is safe to call multiple times. It will ensure |
|
210 * that a proper internal buffer exists if it is required. If the |
|
211 * buffer already exists or is external, the buffer pointers will be |
|
212 * reset to their original state. |
|
213 */ |
|
214 void |
|
215 enable_buffer(); |
|
216 |
|
217 /** |
|
218 * @brief Destroy internal buffer. |
|
219 * |
|
220 * This function is safe to call multiple times. It will ensure |
|
221 * that the internal buffer is deallocated if it exists. In any |
|
222 * case, it will also reset the buffer pointers. |
|
223 */ |
|
224 void |
|
225 disable_buffer(); |
|
226 |
|
227 /** |
|
228 * Underlying file pointer. |
|
229 */ |
|
230 gzFile file; |
|
231 |
|
232 /** |
|
233 * Mode in which file was opened. |
|
234 */ |
|
235 std::ios_base::openmode io_mode; |
|
236 |
|
237 /** |
|
238 * @brief True if this object owns file descriptor. |
|
239 * |
|
240 * This makes the class responsible for closing the file |
|
241 * upon destruction. |
|
242 */ |
|
243 bool own_fd; |
|
244 |
|
245 /** |
|
246 * @brief Stream buffer. |
|
247 * |
|
248 * For simplicity this remains allocated on the free store for the |
|
249 * entire life span of the gzfilebuf object, unless replaced by setbuf. |
|
250 */ |
|
251 char_type* buffer; |
|
252 |
|
253 /** |
|
254 * @brief Stream buffer size. |
|
255 * |
|
256 * Defaults to system default buffer size (typically 8192 bytes). |
|
257 * Modified by setbuf. |
|
258 */ |
|
259 std::streamsize buffer_size; |
|
260 |
|
261 /** |
|
262 * @brief True if this object owns stream buffer. |
|
263 * |
|
264 * This makes the class responsible for deleting the buffer |
|
265 * upon destruction. |
|
266 */ |
|
267 bool own_buffer; |
|
268 }; |
|
269 |
|
270 /*****************************************************************************/ |
|
271 |
|
272 /** |
|
273 * @brief Gzipped file input stream class. |
|
274 * |
|
275 * This class implements ifstream for gzipped files. Seeking and putback |
|
276 * is not supported yet. |
|
277 */ |
|
278 class gzifstream : public std::istream |
|
279 { |
|
280 public: |
|
281 // Default constructor |
|
282 gzifstream(); |
|
283 |
|
284 /** |
|
285 * @brief Construct stream on gzipped file to be opened. |
|
286 * @param name File name. |
|
287 * @param mode Open mode flags (forced to contain ios::in). |
|
288 */ |
|
289 explicit |
|
290 gzifstream(const char* name, |
|
291 std::ios_base::openmode mode = std::ios_base::in); |
|
292 |
|
293 /** |
|
294 * @brief Construct stream on already open gzipped file. |
|
295 * @param fd File descriptor. |
|
296 * @param mode Open mode flags (forced to contain ios::in). |
|
297 */ |
|
298 explicit |
|
299 gzifstream(int fd, |
|
300 std::ios_base::openmode mode = std::ios_base::in); |
|
301 |
|
302 /** |
|
303 * Obtain underlying stream buffer. |
|
304 */ |
|
305 gzfilebuf* |
|
306 rdbuf() const |
|
307 { return const_cast<gzfilebuf*>(&sb); } |
|
308 |
|
309 /** |
|
310 * @brief Check if file is open. |
|
311 * @return True if file is open. |
|
312 */ |
|
313 bool |
|
314 is_open() { return sb.is_open(); } |
|
315 |
|
316 /** |
|
317 * @brief Open gzipped file. |
|
318 * @param name File name. |
|
319 * @param mode Open mode flags (forced to contain ios::in). |
|
320 * |
|
321 * Stream will be in state good() if file opens successfully; |
|
322 * otherwise in state fail(). This differs from the behavior of |
|
323 * ifstream, which never sets the state to good() and therefore |
|
324 * won't allow you to reuse the stream for a second file unless |
|
325 * you manually clear() the state. The choice is a matter of |
|
326 * convenience. |
|
327 */ |
|
328 void |
|
329 open(const char* name, |
|
330 std::ios_base::openmode mode = std::ios_base::in); |
|
331 |
|
332 /** |
|
333 * @brief Attach to already open gzipped file. |
|
334 * @param fd File descriptor. |
|
335 * @param mode Open mode flags (forced to contain ios::in). |
|
336 * |
|
337 * Stream will be in state good() if attach succeeded; otherwise |
|
338 * in state fail(). |
|
339 */ |
|
340 void |
|
341 attach(int fd, |
|
342 std::ios_base::openmode mode = std::ios_base::in); |
|
343 |
|
344 /** |
|
345 * @brief Close gzipped file. |
|
346 * |
|
347 * Stream will be in state fail() if close failed. |
|
348 */ |
|
349 void |
|
350 close(); |
|
351 |
|
352 private: |
|
353 /** |
|
354 * Underlying stream buffer. |
|
355 */ |
|
356 gzfilebuf sb; |
|
357 }; |
|
358 |
|
359 /*****************************************************************************/ |
|
360 |
|
361 /** |
|
362 * @brief Gzipped file output stream class. |
|
363 * |
|
364 * This class implements ofstream for gzipped files. Seeking and putback |
|
365 * is not supported yet. |
|
366 */ |
|
367 class gzofstream : public std::ostream |
|
368 { |
|
369 public: |
|
370 // Default constructor |
|
371 gzofstream(); |
|
372 |
|
373 /** |
|
374 * @brief Construct stream on gzipped file to be opened. |
|
375 * @param name File name. |
|
376 * @param mode Open mode flags (forced to contain ios::out). |
|
377 */ |
|
378 explicit |
|
379 gzofstream(const char* name, |
|
380 std::ios_base::openmode mode = std::ios_base::out); |
|
381 |
|
382 /** |
|
383 * @brief Construct stream on already open gzipped file. |
|
384 * @param fd File descriptor. |
|
385 * @param mode Open mode flags (forced to contain ios::out). |
|
386 */ |
|
387 explicit |
|
388 gzofstream(int fd, |
|
389 std::ios_base::openmode mode = std::ios_base::out); |
|
390 |
|
391 /** |
|
392 * Obtain underlying stream buffer. |
|
393 */ |
|
394 gzfilebuf* |
|
395 rdbuf() const |
|
396 { return const_cast<gzfilebuf*>(&sb); } |
|
397 |
|
398 /** |
|
399 * @brief Check if file is open. |
|
400 * @return True if file is open. |
|
401 */ |
|
402 bool |
|
403 is_open() { return sb.is_open(); } |
|
404 |
|
405 /** |
|
406 * @brief Open gzipped file. |
|
407 * @param name File name. |
|
408 * @param mode Open mode flags (forced to contain ios::out). |
|
409 * |
|
410 * Stream will be in state good() if file opens successfully; |
|
411 * otherwise in state fail(). This differs from the behavior of |
|
412 * ofstream, which never sets the state to good() and therefore |
|
413 * won't allow you to reuse the stream for a second file unless |
|
414 * you manually clear() the state. The choice is a matter of |
|
415 * convenience. |
|
416 */ |
|
417 void |
|
418 open(const char* name, |
|
419 std::ios_base::openmode mode = std::ios_base::out); |
|
420 |
|
421 /** |
|
422 * @brief Attach to already open gzipped file. |
|
423 * @param fd File descriptor. |
|
424 * @param mode Open mode flags (forced to contain ios::out). |
|
425 * |
|
426 * Stream will be in state good() if attach succeeded; otherwise |
|
427 * in state fail(). |
|
428 */ |
|
429 void |
|
430 attach(int fd, |
|
431 std::ios_base::openmode mode = std::ios_base::out); |
|
432 |
|
433 /** |
|
434 * @brief Close gzipped file. |
|
435 * |
|
436 * Stream will be in state fail() if close failed. |
|
437 */ |
|
438 void |
|
439 close(); |
|
440 |
|
441 private: |
|
442 /** |
|
443 * Underlying stream buffer. |
|
444 */ |
|
445 gzfilebuf sb; |
|
446 }; |
|
447 |
|
448 /*****************************************************************************/ |
|
449 |
|
450 /** |
|
451 * @brief Gzipped file output stream manipulator class. |
|
452 * |
|
453 * This class defines a two-argument manipulator for gzofstream. It is used |
|
454 * as base for the setcompression(int,int) manipulator. |
|
455 */ |
|
456 template<typename T1, typename T2> |
|
457 class gzomanip2 |
|
458 { |
|
459 public: |
|
460 // Allows insertor to peek at internals |
|
461 template <typename Ta, typename Tb> |
|
462 friend gzofstream& |
|
463 operator<<(gzofstream&, |
|
464 const gzomanip2<Ta,Tb>&); |
|
465 |
|
466 // Constructor |
|
467 gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2), |
|
468 T1 v1, |
|
469 T2 v2); |
|
470 private: |
|
471 // Underlying manipulator function |
|
472 gzofstream& |
|
473 (*func)(gzofstream&, T1, T2); |
|
474 |
|
475 // Arguments for manipulator function |
|
476 T1 val1; |
|
477 T2 val2; |
|
478 }; |
|
479 |
|
480 /*****************************************************************************/ |
|
481 |
|
482 // Manipulator function thunks through to stream buffer |
|
483 inline gzofstream& |
|
484 setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY) |
|
485 { |
|
486 (gzs.rdbuf())->setcompression(l, s); |
|
487 return gzs; |
|
488 } |
|
489 |
|
490 // Manipulator constructor stores arguments |
|
491 template<typename T1, typename T2> |
|
492 inline |
|
493 gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), |
|
494 T1 v1, |
|
495 T2 v2) |
|
496 : func(f), val1(v1), val2(v2) |
|
497 { } |
|
498 |
|
499 // Insertor applies underlying manipulator function to stream |
|
500 template<typename T1, typename T2> |
|
501 inline gzofstream& |
|
502 operator<<(gzofstream& s, const gzomanip2<T1,T2>& m) |
|
503 { return (*m.func)(s, m.val1, m.val2); } |
|
504 |
|
505 // Insert this onto stream to simplify setting of compression level |
|
506 inline gzomanip2<int,int> |
|
507 setcompression(int l, int s = Z_DEFAULT_STRATEGY) |
|
508 { return gzomanip2<int,int>(&setcompression, l, s); } |
|
509 |
|
510 #endif // HAVE_ZLIB |
|
511 |
|
512 #endif // ZFSTREAM_H |
|
513 |
|
514 /* |
|
515 ;;; Local Variables: *** |
|
516 ;;; mode: C++ *** |
|
517 ;;; End: *** |
|
518 */ |
|
519 |