File: gzip_stream.hpp

package info (click to toggle)
quorum 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,092 kB
  • sloc: cpp: 21,390; perl: 201; makefile: 90; sh: 81
file content (41 lines) | stat: -rw-r--r-- 970 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef __GZIP_STREAM__
#define __GZIP_STREAM__

#include <iostream>
#include <stdio.h>
#include <ext/stdio_filebuf.h>
#include <stdexcept>

template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
class basic_gzipstream : public std::ostream {
  typedef __gnu_cxx::stdio_filebuf<_CharT> stdbuf;
public:
  basic_gzipstream(const char *filename) : std::ostream(open_gzip(filename)), closed(false) { }
  virtual ~basic_gzipstream() {
    close();
    delete rdbuf();
  }
  void close() {
    if(!closed) {
      flush();
      pclose(((stdbuf*)rdbuf())->file());
      closed = true;
    }
  }

private:
  static stdbuf *open_gzip(const char *filename) {
    std::string command("gzip -1 > '");
    command += filename;
    command += "'";
    FILE *f = popen(command.c_str(), "w");
    if(!f)
      throw std::runtime_error("popen failed");
    return new stdbuf(f, std::ios::out);
  }
  bool closed;
};

typedef basic_gzipstream<char> gzipstream;

#endif