FST  openfst-1.7.5
OpenFst Library
gzfile.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Resource handles for gzip files written to or read from stringstreams. These
5 // are necessary to provide the compression routines with streams reading from
6 // or writing to compressed files (or the UNIX standard streams), and are not
7 // intended for general use.
8 
9 #ifndef FST_EXTENSIONS_COMPRESS_GZFILE_H_
10 #define FST_EXTENSIONS_COMPRESS_GZFILE_H_
11 
12 #include <cstddef>
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 
17 #include <fst/compat.h>
18 #include <fst/log.h>
19 #include <fst/fst.h>
20 #include <zlib.h>
21 
22 namespace fst {
23 
24 // Gives the zlib gzFile type an OO-like interface. String inputs are all
25 // C-style strings. The caller is responsible to get the file modes appropriate
26 // for the IO methods being called. The ! operator can be used to check for
27 // errors after construction or read/writing.
28 class GzFile {
29  public:
30  GzFile(const char *source, const char *mode)
31  : gzfile_(gzopen(source, mode)), error_(gzfile_ == nullptr) {}
32 
33  ~GzFile() { gzclose(gzfile_); }
34 
35  bool operator!() const { return error_; }
36 
37  // Returns false on EOF and sets error if short read does not reach an EOF.
38  int Read(void *buf, unsigned int size) {
39  const auto bytes_read = gzread(gzfile_, buf, size);
40  if ((bytes_read < size) && !gzeof(gzfile_)) error_ = true;
41  return bytes_read;
42  }
43 
44  // Sets error on short writes.
45  void Write(const char *buf, unsigned int size) {
46  if (gzwrite(gzfile_, buf, size) != size) error_ = true;
47  }
48 
49  private:
50  gzFile gzfile_;
51  bool error_;
52 };
53 
54 // Resource handle for writing stringstream to GzFile.
55 class OGzFile {
56  public:
57  explicit OGzFile(const std::string &source) : OGzFile(source.c_str()) {}
58 
59  explicit OGzFile(const char *source) : gz_(GzFile(source, "wb")) {}
60 
61  inline bool operator!() const { return !gz_; }
62 
63  void Write(const std::stringstream &ssbuf) {
64  const auto sbuf = ssbuf.str();
65  gz_.Write(sbuf.data(), sbuf.size());
66  }
67 
68  private:
69  GzFile gz_;
70 };
71 
72 // Resource handle for reading stringstream from GzFile.
73 class IGzFile {
74  public:
75  explicit IGzFile(const std::string &source) : IGzFile(source.c_str()) {}
76 
77  explicit IGzFile(const char *source) : gz_(GzFile(source, "rb")) {}
78 
79  inline bool operator!() const { return !gz_; }
80 
81  // This is a great case for "move", but GCC 4 is missing the C+11 standard
82  // move constructor for stringstream, so a unique_ptr is the next best thing.
83  std::unique_ptr<std::stringstream> Read() {
84  char buf[bufsize_];
85  std::unique_ptr<std::stringstream> sstrm(new std::stringstream);
86  // We always read at least once, and the result of the last read is always
87  // pushed onto the stringstream. We use the "write" member because << onto
88  // a stringstream stops at the null byte, which might be data!
89  int bytes_read;
90  while ((bytes_read = gz_.Read(buf, bufsize_)) == bufsize_) {
91  sstrm->write(buf, bufsize_);
92  }
93  sstrm->write(buf, bytes_read);
94  return sstrm;
95  }
96 
97  private:
98  GzFile gz_;
99  // This is the same size as the default internal buffer for zlib.
100  static const size_t bufsize_ = 8192;
101 };
102 
103 } // namespace fst
104 
105 #endif // FST_EXTENSIONS_COMPRESS_GZFILE_H_
IGzFile(const char *source)
Definition: gzfile.h:77
void Write(const std::stringstream &ssbuf)
Definition: gzfile.h:63
bool operator!() const
Definition: gzfile.h:61
OGzFile(const char *source)
Definition: gzfile.h:59
bool operator!() const
Definition: gzfile.h:35
std::unique_ptr< std::stringstream > Read()
Definition: gzfile.h:83
bool operator!() const
Definition: gzfile.h:79
IGzFile(const std::string &source)
Definition: gzfile.h:75
int Read(void *buf, unsigned int size)
Definition: gzfile.h:38
GzFile(const char *source, const char *mode)
Definition: gzfile.h:30
void Write(const char *buf, unsigned int size)
Definition: gzfile.h:45
OGzFile(const std::string &source)
Definition: gzfile.h:57
~GzFile()
Definition: gzfile.h:33