ThingWorx C SDK
zfstream.h
1 /*
2  * A C++ I/O streams interface to the zlib gz* functions
3  *
4  * by Ludwig Schwardt <schwardt@sun.ac.za>
5  * original version by Kevin Ruland <kevin@rodin.wustl.edu>
6  *
7  * This version is standard-compliant and compatible with gcc 3.x.
8  */
9 
10 #ifndef ZFSTREAM_H
11 #define ZFSTREAM_H
12 
13 #include <istream> // not iostream, since we don't need cin/cout
14 #include <ostream>
15 #include "zlib.h"
16 
17 /*****************************************************************************/
18 
27 class gzfilebuf : public std::streambuf
28 {
29 public:
30  // Default constructor.
31  gzfilebuf();
32 
33  // Destructor.
34  virtual
35  ~gzfilebuf();
36 
48  int
49  setcompression(int comp_level,
50  int comp_strategy = Z_DEFAULT_STRATEGY);
51 
56  bool
57  is_open() const { return (file != NULL); }
58 
65  gzfilebuf*
66  open(const char* name,
67  std::ios_base::openmode mode);
68 
75  gzfilebuf*
76  attach(int fd,
77  std::ios_base::openmode mode);
78 
83  gzfilebuf*
84  close();
85 
86 protected:
91  bool
92  open_mode(std::ios_base::openmode mode,
93  char* c_mode) const;
94 
102  virtual std::streamsize
103  showmanyc();
104 
112  virtual int_type
113  underflow();
114 
124  virtual int_type
125  overflow(int_type c = traits_type::eof());
126 
135  virtual std::streambuf*
136  setbuf(char_type* p,
137  std::streamsize n);
138 
145  virtual int
146  sync();
147 
148 //
149 // Some future enhancements
150 //
151 // virtual int_type uflow();
152 // virtual int_type pbackfail(int_type c = traits_type::eof());
153 // virtual pos_type
154 // seekoff(off_type off,
155 // std::ios_base::seekdir way,
156 // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
157 // virtual pos_type
158 // seekpos(pos_type sp,
159 // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
160 
161 private:
170  void
171  enable_buffer();
172 
180  void
181  disable_buffer();
182 
186  gzFile file;
187 
191  std::ios_base::openmode io_mode;
192 
199  bool own_fd;
200 
207  char_type* buffer;
208 
215  std::streamsize buffer_size;
216 
223  bool own_buffer;
224 };
225 
226 /*****************************************************************************/
227 
234 class gzifstream : public std::istream
235 {
236 public:
237  // Default constructor
238  gzifstream();
239 
245  explicit
246  gzifstream(const char* name,
247  std::ios_base::openmode mode = std::ios_base::in);
248 
254  explicit
255  gzifstream(int fd,
256  std::ios_base::openmode mode = std::ios_base::in);
257 
261  gzfilebuf*
262  rdbuf() const
263  { return const_cast<gzfilebuf*>(&sb); }
264 
269  bool
270  is_open() { return sb.is_open(); }
271 
284  void
285  open(const char* name,
286  std::ios_base::openmode mode = std::ios_base::in);
287 
296  void
297  attach(int fd,
298  std::ios_base::openmode mode = std::ios_base::in);
299 
305  void
306  close();
307 
308 private:
312  gzfilebuf sb;
313 };
314 
315 /*****************************************************************************/
316 
323 class gzofstream : public std::ostream
324 {
325 public:
326  // Default constructor
327  gzofstream();
328 
334  explicit
335  gzofstream(const char* name,
336  std::ios_base::openmode mode = std::ios_base::out);
337 
343  explicit
344  gzofstream(int fd,
345  std::ios_base::openmode mode = std::ios_base::out);
346 
350  gzfilebuf*
351  rdbuf() const
352  { return const_cast<gzfilebuf*>(&sb); }
353 
358  bool
359  is_open() { return sb.is_open(); }
360 
373  void
374  open(const char* name,
375  std::ios_base::openmode mode = std::ios_base::out);
376 
385  void
386  attach(int fd,
387  std::ios_base::openmode mode = std::ios_base::out);
388 
394  void
395  close();
396 
397 private:
401  gzfilebuf sb;
402 };
403 
404 /*****************************************************************************/
405 
412 template<typename T1, typename T2>
413  class gzomanip2
414  {
415  public:
416  // Allows insertor to peek at internals
417  template <typename Ta, typename Tb>
418  friend gzofstream&
419  operator<<(gzofstream&,
420  const gzomanip2<Ta,Tb>&);
421 
422  // Constructor
423  gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
424  T1 v1,
425  T2 v2);
426  private:
427  // Underlying manipulator function
428  gzofstream&
429  (*func)(gzofstream&, T1, T2);
430 
431  // Arguments for manipulator function
432  T1 val1;
433  T2 val2;
434  };
435 
436 /*****************************************************************************/
437 
438 // Manipulator function thunks through to stream buffer
439 inline gzofstream&
440 setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
441 {
442  (gzs.rdbuf())->setcompression(l, s);
443  return gzs;
444 }
445 
446 // Manipulator constructor stores arguments
447 template<typename T1, typename T2>
448  inline
450  T1 v1,
451  T2 v2)
452  : func(f), val1(v1), val2(v2)
453  { }
454 
455 // Insertor applies underlying manipulator function to stream
456 template<typename T1, typename T2>
457  inline gzofstream&
458  operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
459  { return (*m.func)(s, m.val1, m.val2); }
460 
461 // Insert this onto stream to simplify setting of compression level
462 inline gzomanip2<int,int>
463 setcompression(int l, int s = Z_DEFAULT_STRATEGY)
464 { return gzomanip2<int,int>(&setcompression, l, s); }
465 
466 #endif // ZFSTREAM_H
void attach(int fd, std::ios_base::openmode mode=std::ios_base::out)
Attach to already open gzipped file.
Definition: zfstream.cc:464
void open(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Open gzipped file.
Definition: zfstream.cc:398
Gzipped file output stream manipulator class.
Definition: zfstream.h:413
Gzipped file output stream class.
Definition: zfstream.h:80
virtual std::streambuf * setbuf(char_type *p, std::streamsize n)
Installs external stream buffer.
Definition: zfstream.cc:266
gzfilebuf * rdbuf() const
Definition: zfstream.h:351
int setcompression(int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
Set compression level and strategy on the fly.
Definition: zfstream.cc:43
gzfilebuf * rdbuf() const
Definition: zfstream.h:262
bool is_open()
Check if file is open.
Definition: zfstream.h:270
bool is_open()
Check if file is open.
Definition: zfstream.h:359
Gzipped file stream buffer class.
Definition: zfstream.h:8
void open(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Open gzipped file.
Definition: zfstream.cc:453
void attach(int fd, std::ios_base::openmode mode=std::ios_base::in)
Attach to already open gzipped file.
Definition: zfstream.cc:409
void close()
Close gzipped file.
Definition: zfstream.cc:475
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:131
bool is_open() const
Check if file is open.
Definition: zfstream.h:57
Gzipped file input stream class.
Definition: zfstream.h:68
virtual std::streamsize showmanyc()
Number of characters available in stream buffer.
Definition: zfstream.cc:169
Definition: zlib.h:1817
void close()
Close gzipped file.
Definition: zfstream.cc:420
Definition: gzappend.c:170