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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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 |
|
196 // |
|
197 // Some future enhancements |
|
198 // |
|
199 // virtual int_type uflow(); |
|
200 // virtual int_type pbackfail(int_type c = traits_type::eof()); |
|
201 |
|
202 private: |
|
203 /** |
|
204 * @brief Allocate internal buffer. |
|
205 * |
|
206 * This function is safe to call multiple times. It will ensure |
|
207 * that a proper internal buffer exists if it is required. If the |
|
208 * buffer already exists or is external, the buffer pointers will be |
|
209 * reset to their original state. |
|
210 */ |
|
211 void |
|
212 enable_buffer(); |
|
213 |
|
214 /** |
|
215 * @brief Destroy internal buffer. |
|
216 * |
|
217 * This function is safe to call multiple times. It will ensure |
|
218 * that the internal buffer is deallocated if it exists. In any |
|
219 * case, it will also reset the buffer pointers. |
|
220 */ |
|
221 void |
|
222 disable_buffer(); |
|
223 |
|
224 /** |
|
225 * Underlying file pointer. |
|
226 */ |
|
227 gzFile file; |
|
228 |
|
229 /** |
|
230 * Mode in which file was opened. |
|
231 */ |
|
232 std::ios_base::openmode io_mode; |
|
233 |
|
234 /** |
|
235 * @brief True if this object owns file descriptor. |
|
236 * |
|
237 * This makes the class responsible for closing the file |
|
238 * upon destruction. |
|
239 */ |
|
240 bool own_fd; |
|
241 |
|
242 /** |
|
243 * @brief Stream buffer. |
|
244 * |
|
245 * For simplicity this remains allocated on the free store for the |
|
246 * entire life span of the gzfilebuf object, unless replaced by setbuf. |
|
247 */ |
|
248 char_type* buffer; |
|
249 |
|
250 /** |
|
251 * @brief Stream buffer size. |
|
252 * |
|
253 * Defaults to system default buffer size (typically 8192 bytes). |
|
254 * Modified by setbuf. |
|
255 */ |
|
256 std::streamsize buffer_size; |
|
257 |
|
258 /** |
|
259 * @brief True if this object owns stream buffer. |
|
260 * |
|
261 * This makes the class responsible for deleting the buffer |
|
262 * upon destruction. |
|
263 */ |
|
264 bool own_buffer; |
|
265 }; |
|
266 |
|
267 /*****************************************************************************/ |
|
268 |
|
269 /** |
|
270 * @brief Gzipped file input stream class. |
|
271 * |
|
272 * This class implements ifstream for gzipped files. Seeking and putback |
|
273 * is not supported yet. |
|
274 */ |
|
275 class gzifstream : public std::istream |
|
276 { |
|
277 public: |
|
278 // Default constructor |
|
279 gzifstream(); |
|
280 |
|
281 /** |
|
282 * @brief Construct stream on gzipped file to be opened. |
|
283 * @param name File name. |
|
284 * @param mode Open mode flags (forced to contain ios::in). |
|
285 */ |
|
286 explicit |
|
287 gzifstream(const char* name, |
|
288 std::ios_base::openmode mode = std::ios_base::in); |
|
289 |
|
290 /** |
|
291 * @brief Construct stream on already open gzipped file. |
|
292 * @param fd File descriptor. |
|
293 * @param mode Open mode flags (forced to contain ios::in). |
|
294 */ |
|
295 explicit |
|
296 gzifstream(int fd, |
|
297 std::ios_base::openmode mode = std::ios_base::in); |
|
298 |
|
299 /** |
|
300 * Obtain underlying stream buffer. |
|
301 */ |
|
302 gzfilebuf* |
|
303 rdbuf() const |
|
304 { return const_cast<gzfilebuf*>(&sb); } |
|
305 |
|
306 /** |
|
307 * @brief Check if file is open. |
|
308 * @return True if file is open. |
|
309 */ |
|
310 bool |
|
311 is_open() { return sb.is_open(); } |
|
312 |
|
313 /** |
|
314 * @brief Open gzipped file. |
|
315 * @param name File name. |
|
316 * @param mode Open mode flags (forced to contain ios::in). |
|
317 * |
|
318 * Stream will be in state good() if file opens successfully; |
|
319 * otherwise in state fail(). This differs from the behavior of |
|
320 * ifstream, which never sets the state to good() and therefore |
|
321 * won't allow you to reuse the stream for a second file unless |
|
322 * you manually clear() the state. The choice is a matter of |
|
323 * convenience. |
|
324 */ |
|
325 void |
|
326 open(const char* name, |
|
327 std::ios_base::openmode mode = std::ios_base::in); |
|
328 |
|
329 /** |
|
330 * @brief Attach to already open gzipped file. |
|
331 * @param fd File descriptor. |
|
332 * @param mode Open mode flags (forced to contain ios::in). |
|
333 * |
|
334 * Stream will be in state good() if attach succeeded; otherwise |
|
335 * in state fail(). |
|
336 */ |
|
337 void |
|
338 attach(int fd, |
|
339 std::ios_base::openmode mode = std::ios_base::in); |
|
340 |
|
341 /** |
|
342 * @brief Close gzipped file. |
|
343 * |
|
344 * Stream will be in state fail() if close failed. |
|
345 */ |
|
346 void |
|
347 close(); |
|
348 |
|
349 private: |
|
350 /** |
|
351 * Underlying stream buffer. |
|
352 */ |
|
353 gzfilebuf sb; |
|
354 }; |
|
355 |
|
356 /*****************************************************************************/ |
|
357 |
|
358 /** |
|
359 * @brief Gzipped file output stream class. |
|
360 * |
|
361 * This class implements ofstream for gzipped files. Seeking and putback |
|
362 * is not supported yet. |
|
363 */ |
|
364 class gzofstream : public std::ostream |
|
365 { |
|
366 public: |
|
367 // Default constructor |
|
368 gzofstream(); |
|
369 |
|
370 /** |
|
371 * @brief Construct stream on gzipped file to be opened. |
|
372 * @param name File name. |
|
373 * @param mode Open mode flags (forced to contain ios::out). |
|
374 */ |
|
375 explicit |
|
376 gzofstream(const char* name, |
|
377 std::ios_base::openmode mode = std::ios_base::out); |
|
378 |
|
379 /** |
|
380 * @brief Construct stream on already open gzipped file. |
|
381 * @param fd File descriptor. |
|
382 * @param mode Open mode flags (forced to contain ios::out). |
|
383 */ |
|
384 explicit |
|
385 gzofstream(int fd, |
|
386 std::ios_base::openmode mode = std::ios_base::out); |
|
387 |
|
388 /** |
|
389 * Obtain underlying stream buffer. |
|
390 */ |
|
391 gzfilebuf* |
|
392 rdbuf() const |
|
393 { return const_cast<gzfilebuf*>(&sb); } |
|
394 |
|
395 /** |
|
396 * @brief Check if file is open. |
|
397 * @return True if file is open. |
|
398 */ |
|
399 bool |
|
400 is_open() { return sb.is_open(); } |
|
401 |
|
402 /** |
|
403 * @brief Open gzipped file. |
|
404 * @param name File name. |
|
405 * @param mode Open mode flags (forced to contain ios::out). |
|
406 * |
|
407 * Stream will be in state good() if file opens successfully; |
|
408 * otherwise in state fail(). This differs from the behavior of |
|
409 * ofstream, which never sets the state to good() and therefore |
|
410 * won't allow you to reuse the stream for a second file unless |
|
411 * you manually clear() the state. The choice is a matter of |
|
412 * convenience. |
|
413 */ |
|
414 void |
|
415 open(const char* name, |
|
416 std::ios_base::openmode mode = std::ios_base::out); |
|
417 |
|
418 /** |
|
419 * @brief Attach to already open gzipped file. |
|
420 * @param fd File descriptor. |
|
421 * @param mode Open mode flags (forced to contain ios::out). |
|
422 * |
|
423 * Stream will be in state good() if attach succeeded; otherwise |
|
424 * in state fail(). |
|
425 */ |
|
426 void |
|
427 attach(int fd, |
|
428 std::ios_base::openmode mode = std::ios_base::out); |
|
429 |
|
430 /** |
|
431 * @brief Close gzipped file. |
|
432 * |
|
433 * Stream will be in state fail() if close failed. |
|
434 */ |
|
435 void |
|
436 close(); |
|
437 |
|
438 private: |
|
439 /** |
|
440 * Underlying stream buffer. |
|
441 */ |
|
442 gzfilebuf sb; |
|
443 }; |
|
444 |
|
445 /*****************************************************************************/ |
|
446 |
|
447 /** |
|
448 * @brief Gzipped file output stream manipulator class. |
|
449 * |
|
450 * This class defines a two-argument manipulator for gzofstream. It is used |
|
451 * as base for the setcompression(int,int) manipulator. |
|
452 */ |
|
453 template<typename T1, typename T2> |
|
454 class gzomanip2 |
|
455 { |
|
456 public: |
|
457 // Allows insertor to peek at internals |
|
458 template <typename Ta, typename Tb> |
|
459 friend gzofstream& |
|
460 operator<<(gzofstream&, |
|
461 const gzomanip2<Ta,Tb>&); |
|
462 |
|
463 // Constructor |
|
464 gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2), |
|
465 T1 v1, |
|
466 T2 v2); |
|
467 private: |
|
468 // Underlying manipulator function |
|
469 gzofstream& |
|
470 (*func)(gzofstream&, T1, T2); |
|
471 |
|
472 // Arguments for manipulator function |
|
473 T1 val1; |
|
474 T2 val2; |
|
475 }; |
|
476 |
|
477 /*****************************************************************************/ |
|
478 |
|
479 // Manipulator function thunks through to stream buffer |
|
480 inline gzofstream& |
|
481 setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY) |
|
482 { |
|
483 (gzs.rdbuf())->setcompression(l, s); |
|
484 return gzs; |
|
485 } |
|
486 |
|
487 // Manipulator constructor stores arguments |
|
488 template<typename T1, typename T2> |
|
489 inline |
|
490 gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), |
|
491 T1 v1, |
|
492 T2 v2) |
|
493 : func(f), val1(v1), val2(v2) |
|
494 { } |
|
495 |
|
496 // Insertor applies underlying manipulator function to stream |
|
497 template<typename T1, typename T2> |
|
498 inline gzofstream& |
|
499 operator<<(gzofstream& s, const gzomanip2<T1,T2>& m) |
|
500 { return (*m.func)(s, m.val1, m.val2); } |
|
501 |
|
502 // Insert this onto stream to simplify setting of compression level |
|
503 inline gzomanip2<int,int> |
|
504 setcompression(int l, int s = Z_DEFAULT_STRATEGY) |
|
505 { return gzomanip2<int,int>(&setcompression, l, s); } |
|
506 |
|
507 #endif // HAVE_ZLIB |
|
508 |
|
509 #endif // ZFSTREAM_H |
|
510 |
|
511 /* |
|
512 ;;; Local Variables: *** |
|
513 ;;; mode: C++ *** |
|
514 ;;; End: *** |
|
515 */ |
|
516 |