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