GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
zfstream.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2005-2017 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
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 #if ! defined (octave_zfsstream_h)
34 #define octave_zfsstream_h 1
35 
36 #include "octave-config.h"
37 
38 #if defined (HAVE_ZLIB)
39 
40 #include <iosfwd>
41 
42 #include "zlib.h"
43 
44 /**
45  * @brief Gzipped file stream buffer class.
46  *
47  * This class implements basic_filebuf for gzipped files. It doesn't yet
48  * support seeking (allowed by zlib but slow/limited), putback and read/write
49  * access * (tricky). Otherwise, it attempts to be a drop-in replacement for
50  * the standard file streambuf.
51 */
52 class gzfilebuf : public std::streambuf
53 {
54 public:
55  // Default constructor.
56  gzfilebuf ();
57 
58  // Destructor.
59  virtual
60  ~gzfilebuf ();
61 
62  /**
63  * @brief Set compression level and strategy on the fly.
64  * @param comp_level Compression level (see zlib.h for allowed values)
65  * @param comp_strategy Compression strategy (see zlib.h for allowed values)
66  * @return Z_OK on success, Z_STREAM_ERROR otherwise.
67  *
68  * Unfortunately, these parameters cannot be modified separately, as the
69  * previous zfstream version assumed. Since the strategy is seldom changed,
70  * it can default and setcompression(level) then becomes like the old
71  * setcompressionlevel(level).
72  */
73  int
74  setcompression (int comp_level,
75  int comp_strategy = Z_DEFAULT_STRATEGY);
76 
77  /**
78  * @brief Check if file is open.
79  * @return True if file is open.
80  */
81  bool
82  is_open () const { return (file != 0); }
83 
84  /**
85  * @brief Open gzipped file.
86  * @param name Filename.
87  * @param mode Open mode flags.
88  * @return @c this on success, NULL on failure.
89  */
90  gzfilebuf*
91  open (const char* name,
92  std::ios_base::openmode mode);
93 
94  /**
95  * @brief Attach to already open gzipped file.
96  * @param fd File descriptor.
97  * @param mode Open mode flags.
98  * @return @c this on success, NULL on failure.
99  */
100  gzfilebuf*
101  attach (int fd,
102  std::ios_base::openmode mode);
103 
104  /**
105  * @brief Close gzipped file.
106  * @return @c this on success, NULL on failure.
107  */
108  gzfilebuf*
109  close ();
110 
111 protected:
112  /**
113  * @brief Convert ios open mode int to mode string used by zlib.
114  * @return True if valid mode flag combination.
115  */
116  bool
117  open_mode (std::ios_base::openmode mode,
118  char* c_mode) const;
119 
120  /**
121  * @brief Number of characters available in stream buffer.
122  * @return Number of characters.
123  *
124  * This indicates number of characters in get area of stream buffer.
125  * These characters can be read without accessing the gzipped file.
126  */
127  virtual std::streamsize
128  showmanyc ();
129 
130  /**
131  * @brief Fill get area from gzipped file.
132  * @return First character in get area on success, EOF on error.
133  *
134  * This actually reads characters from gzipped file to stream
135  * buffer. Always buffered.
136  */
137  virtual int_type
138  underflow ();
139 
140  /**
141  * @brief Write put area to gzipped file.
142  * @param c Extra character to add to buffer contents.
143  * @return Non-EOF on success, EOF on error.
144  *
145  * This actually writes characters in stream buffer to
146  * gzipped file. With unbuffered output this is done one
147  * character at a time.
148  */
149  virtual int_type
150  overflow (int_type c = traits_type::eof ());
151 
152  /**
153  * @brief Installs external stream buffer.
154  * @param p Pointer to char buffer.
155  * @param n Size of external buffer.
156  * @return @c this on success, NULL on failure.
157  *
158  * Call setbuf(0,0) to enable unbuffered output.
159  */
160  virtual std::streambuf*
161  setbuf (char_type* p,
162  std::streamsize n);
163 
164  /**
165  * @brief Flush stream buffer to file.
166  * @return 0 on success, -1 on error.
167  *
168  * This calls underflow(EOF) to do the job.
169  */
170  virtual int
171  sync ();
172 
173  /**
174  * @brief Alters the stream positions.
175  *
176  * Each derived class provides its own appropriate behavior.
177  */
178  virtual pos_type
179  seekoff (off_type off, std::ios_base::seekdir way,
180  std::ios_base::openmode mode =
181  std::ios_base::in | std::ios_base::out);
182 
183  /**
184  * @brief Alters the stream positions.
185  *
186  * Each derived class provides its own appropriate behavior.
187  */
188  virtual pos_type
189  seekpos (pos_type sp, std::ios_base::openmode mode =
190  std::ios_base::in | std::ios_base::out);
191 
192  virtual int_type
193  pbackfail (int_type c = traits_type::eof ());
194 
195 //
196 // Some future enhancements
197 //
198 // virtual int_type uflow();
199 // virtual int_type pbackfail(int_type c = traits_type::eof());
200 
201 private:
202 
203  // No copying!
204 
205  gzfilebuf (const gzfilebuf&);
206 
208 
209  /**
210  * @brief Allocate internal buffer.
211  *
212  * This function is safe to call multiple times. It will ensure
213  * that a proper internal buffer exists if it is required. If the
214  * buffer already exists or is external, the buffer pointers will be
215  * reset to their original state.
216  */
217  void
218  enable_buffer ();
219 
220  /**
221  * @brief Destroy internal buffer.
222  *
223  * This function is safe to call multiple times. It will ensure
224  * that the internal buffer is deallocated if it exists. In any
225  * case, it will also reset the buffer pointers.
226  */
227  void
228  disable_buffer ();
229 
230  /**
231  * Underlying file pointer.
232  */
233  gzFile file;
234 
235  /**
236  * Mode in which file was opened.
237  */
238  std::ios_base::openmode io_mode;
239 
240  /**
241  * @brief True if this object owns file descriptor.
242  *
243  * This makes the class responsible for closing the file
244  * upon destruction.
245  */
246  bool own_fd;
247 
248  /**
249  * @brief Stream buffer.
250  *
251  * For simplicity this remains allocated on the free store for the
252  * entire life span of the gzfilebuf object, unless replaced by setbuf.
253  */
254  char_type* buffer;
255 
256  /**
257  * @brief Stream buffer size.
258  *
259  * Defaults to system default buffer size (typically 8192 bytes).
260  * Modified by setbuf.
261  */
262  std::streamsize buffer_size;
263 
264  /**
265  * @brief True if this object owns stream buffer.
266  *
267  * This makes the class responsible for deleting the buffer
268  * upon destruction.
269  */
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 Filename.
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 Filename.
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  */
358 };
359 
360 /**
361  * @brief Gzipped file output stream class.
362  *
363  * This class implements ofstream for gzipped files. Seeking and putback
364  * is not supported yet.
365 */
366 class gzofstream : public std::ostream
367 {
368 public:
369  // Default constructor
370  gzofstream ();
371 
372  /**
373  * @brief Construct stream on gzipped file to be opened.
374  * @param name Filename.
375  * @param mode Open mode flags (forced to contain ios::out).
376  */
377  explicit
378  gzofstream (const char* name,
379  std::ios_base::openmode mode = std::ios_base::out);
380 
381  /**
382  * @brief Construct stream on already open gzipped file.
383  * @param fd File descriptor.
384  * @param mode Open mode flags (forced to contain ios::out).
385  */
386  explicit
387  gzofstream (int fd,
388  std::ios_base::openmode mode = std::ios_base::out);
389 
390  /**
391  * Obtain underlying stream buffer.
392  */
393  gzfilebuf*
394  rdbuf () const
395  { return const_cast<gzfilebuf*>(&sb); }
396 
397  /**
398  * @brief Check if file is open.
399  * @return True if file is open.
400  */
401  bool
402  is_open () { return sb.is_open (); }
403 
404  /**
405  * @brief Open gzipped file.
406  * @param name Filename.
407  * @param mode Open mode flags (forced to contain ios::out).
408  *
409  * Stream will be in state good() if file opens successfully;
410  * otherwise in state fail(). This differs from the behavior of
411  * ofstream, which never sets the state to good() and therefore
412  * won't allow you to reuse the stream for a second file unless
413  * you manually clear() the state. The choice is a matter of
414  * convenience.
415  */
416  void
417  open (const char* name,
418  std::ios_base::openmode mode = std::ios_base::out);
419 
420  /**
421  * @brief Attach to already open gzipped file.
422  * @param fd File descriptor.
423  * @param mode Open mode flags (forced to contain ios::out).
424  *
425  * Stream will be in state good() if attach succeeded; otherwise
426  * in state fail().
427  */
428  void
429  attach (int fd,
430  std::ios_base::openmode mode = std::ios_base::out);
431 
432  /**
433  * @brief Close gzipped file.
434  *
435  * Stream will be in state fail() if close failed.
436  */
437  void
438  close ();
439 
440 private:
441  /**
442  * Underlying stream buffer.
443  */
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>
455 {
456 public:
457  // Allows insertor to peek at internals
458  template <typename Ta, typename Tb>
459  friend 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 // Manipulator function thunks through to stream buffer
478 inline gzofstream&
479 setcompression (gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
480 {
481  (gzs.rdbuf ())->setcompression (l, s);
482  return gzs;
483 }
484 
485 // Manipulator constructor stores arguments
486 template <typename T1, typename T2>
487 inline
489  T1 v1,
490  T2 v2)
491  : func(f), val1(v1), val2(v2)
492 { }
493 
494 // Insertor applies underlying manipulator function to stream
495 template <typename T1, typename T2>
496 inline gzofstream&
497 operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
498 { return (*m.func)(s, m.val1, m.val2); }
499 
500 // Insert this onto stream to simplify setting of compression level
501 inline gzomanip2<int,int>
502 setcompression (int l, int s = Z_DEFAULT_STRATEGY)
503 { return gzomanip2<int,int>(&setcompression, l, s); }
504 
505 #endif
506 
507 #endif
gzofstream & setcompression(gzofstream &gzs, int l, int s=Z_DEFAULT_STRATEGY)
Definition: zfstream.h:479
gzfilebuf * open(const char *name, std::ios_base::openmode mode)
Open gzipped file.
Definition: zfstream.cc:83
void attach(int fd, std::ios_base::openmode mode=std::ios_base::out)
Attach to already open gzipped file.
Definition: zfstream.cc:599
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Alters the stream positions.
Definition: zfstream.cc:461
gzfilebuf sb
Underlying stream buffer.
Definition: zfstream.h:357
void open(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Open gzipped file.
Definition: zfstream.cc:540
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
Gzipped file output stream manipulator class.
Definition: zfstream.h:454
Gzipped file output stream class.
Definition: zfstream.h:366
virtual std::streambuf * setbuf(char_type *p, std::streamsize n)
Installs external stream buffer.
Definition: zfstream.cc:350
gzfilebuf & operator=(const gzfilebuf &)
void enable_buffer()
Allocate internal buffer.
Definition: zfstream.cc:393
gzfilebuf * rdbuf() const
Obtain underlying stream buffer.
Definition: zfstream.h:394
friend gzofstream & operator<<(gzofstream &, const gzomanip2< Ta, Tb > &)
s
Definition: file-io.cc:2682
gzfilebuf()
Definition: zfstream.cc:54
virtual int_type overflow(int_type c=traits_type::eof())
Write put area to gzipped file.
Definition: zfstream.cc:298
int setcompression(int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
Set compression level and strategy on the fly.
Definition: zfstream.cc:76
gzfilebuf * close()
Close gzipped file.
Definition: zfstream.cc:137
std::ios_base::openmode io_mode
Mode in which file was opened.
Definition: zfstream.h:238
gzFile file
Underlying file pointer.
Definition: zfstream.h:233
gzfilebuf * rdbuf() const
Obtain underlying stream buffer.
Definition: zfstream.h:307
bool is_open()
Check if file is open.
Definition: zfstream.h:315
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
bool is_open()
Check if file is open.
Definition: zfstream.h:402
virtual int_type pbackfail(int_type c=traits_type::eof())
Definition: zfstream.cc:217
Gzipped file stream buffer class.
Definition: zfstream.h:52
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
void open(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Open gzipped file.
Definition: zfstream.cc:589
bool own_buffer
True if this object owns stream buffer.
Definition: zfstream.h:270
gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), T1 v1, T2 v2)
Definition: zfstream.h:488
void attach(int fd, std::ios_base::openmode mode=std::ios_base::in)
Attach to already open gzipped file.
Definition: zfstream.cc:550
void close()
Close gzipped file.
Definition: zfstream.cc:609
char_type * buffer
Stream buffer.
Definition: zfstream.h:254
bool open_mode(std::ios_base::openmode mode, char *c_mode) const
Convert ios open mode int to mode string used by zlib.
Definition: zfstream.cc:161
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
virtual pos_type seekpos(pos_type sp, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Alters the stream positions.
Definition: zfstream.cc:498
gzfilebuf sb
Underlying stream buffer.
Definition: zfstream.h:444
bool is_open() const
Check if file is open.
Definition: zfstream.h:82
Gzipped file input stream class.
Definition: zfstream.h:279
void disable_buffer()
Destroy internal buffer.
Definition: zfstream.cc:433
std::streamsize buffer_size
Stream buffer size.
Definition: zfstream.h:262
const octave_char_matrix & v2
gzfilebuf * attach(int fd, std::ios_base::openmode mode)
Attach to already open gzipped file.
Definition: zfstream.cc:110
virtual std::streamsize showmanyc()
Number of characters available in stream buffer.
Definition: zfstream.cc:200
virtual ~gzfilebuf()
Definition: zfstream.cc:63
virtual int sync()
Flush stream buffer to file.
Definition: zfstream.cc:383
p
Definition: lu.cc:138
bool own_fd
True if this object owns file descriptor.
Definition: zfstream.h:246
void close()
Close gzipped file.
Definition: zfstream.cc:560
virtual int_type underflow()
Fill get area from gzipped file.
Definition: zfstream.cc:255