Chemical Data Processing Library C++ API - Version 1.2.0
CompressionStreams.hpp
Go to the documentation of this file.
1 /*
2  * CompressionStreams.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_COMPRESSIONSTREAMS_HPP
30 #define CDPL_UTIL_COMPRESSIONSTREAMS_HPP
31 
32 #include <fstream>
33 
34 #include <boost/iostreams/copy.hpp>
35 #include <boost/iostreams/filtering_stream.hpp>
36 #include <boost/iostreams/filter/gzip.hpp>
37 #include <boost/iostreams/filter/bzip2.hpp>
38 
41 
42 
43 namespace CDPL
44 {
45 
46  namespace Util
47  {
48 
50  {
51 
53  BZIP2
54  };
55 
56  template <CompressionAlgo CompAlgo>
58 
59  template <>
61  {
62 
63  typedef boost::iostreams::gzip_decompressor DecompFilter;
64  typedef boost::iostreams::gzip_compressor CompFilter;
65  };
66 
67  template <>
69  {
70 
71  typedef boost::iostreams::bzip2_decompressor DecompFilter;
72  typedef boost::iostreams::bzip2_compressor CompFilter;
73  };
74 
75  template <CompressionAlgo CompAlgo, typename StreamType>
76  class CompressionStreamBase : public StreamType
77  {
78 
79  public:
80  typedef typename StreamType::char_type char_type;
81  typedef typename StreamType::traits_type traits_type;
82  typedef typename StreamType::int_type int_type;
83  typedef typename StreamType::pos_type pos_type;
84  typedef typename StreamType::off_type off_type;
85  typedef std::basic_istream<char_type, traits_type> IStreamType;
86  typedef std::basic_ostream<char_type, traits_type> OStreamType;
87 
89 
90  protected:
92 
93  void closeTmpFile();
94  void openTmpFile();
95 
98 
99  private:
100  typedef std::basic_filebuf<char_type, traits_type> FileBufType;
101 
102  FileBufType tmpFileBuf;
103  };
104 
105  template <CompressionAlgo CompAlgo, typename CharT = char, typename TraitsT = std::char_traits<CharT> >
106  class DecompressionIStream : public CompressionStreamBase<CompAlgo, std::basic_istream<CharT, TraitsT> >
107  {
108 
109  public:
110  typedef typename std::basic_istream<CharT, TraitsT> StreamType;
111  typedef typename StreamType::char_type char_type;
112  typedef typename StreamType::traits_type traits_type;
113  typedef typename StreamType::int_type int_type;
114  typedef typename StreamType::pos_type pos_type;
115  typedef typename StreamType::off_type off_type;
116 
118  DecompressionIStream(StreamType& stream);
119 
120  void open(StreamType& stream);
121  void close();
122  };
123 
124  template <CompressionAlgo CompAlgo, typename CharT = char, typename TraitsT = std::char_traits<CharT> >
125  class CompressionOStream : public CompressionStreamBase<CompAlgo, std::basic_ostream<CharT, TraitsT> >
126  {
127 
128  public:
129  typedef typename std::basic_ostream<CharT, TraitsT> StreamType;
130  typedef typename StreamType::char_type char_type;
131  typedef typename StreamType::traits_type traits_type;
132  typedef typename StreamType::int_type int_type;
133  typedef typename StreamType::pos_type pos_type;
134  typedef typename StreamType::off_type off_type;
135 
137  CompressionOStream(StreamType& stream);
138 
140 
141  void open(StreamType& stream);
142  void close();
143 
144  private:
145  StreamType* stream;
146  off_type outPos;
147  };
148 
149  template <CompressionAlgo CompAlgo, typename CharT = char, typename TraitsT = std::char_traits<CharT> >
150  class CompressedIOStream : public CompressionStreamBase<CompAlgo, std::basic_iostream<CharT, TraitsT> >
151  {
152 
153  public:
154  typedef typename std::basic_iostream<CharT, TraitsT> StreamType;
155  typedef typename StreamType::char_type char_type;
156  typedef typename StreamType::traits_type traits_type;
157  typedef typename StreamType::int_type int_type;
158  typedef typename StreamType::pos_type pos_type;
159  typedef typename StreamType::off_type off_type;
160 
162  CompressedIOStream(StreamType& stream);
163 
165 
166  void open(StreamType& stream);
167  void close();
168 
169  private:
170  StreamType* stream;
171  off_type outPos;
172  };
173 
180  } // namespace Util
181 } // namespace CDPL
182 
183 
184 // CompressionStreamBase Implementation
185 
186 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
188  StreamType(&tmpFileBuf)
189 {}
190 
191 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
193 {
194  if (!tmpFileBuf.close())
195  this->setstate(std::ios_base::failbit);
196  else
197  this->clear();
198 }
199 
200 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
202 {
203  FileRemover tmp_file_rem(genCheckedTempFilePath());
204 
205  if (!tmpFileBuf.open(tmp_file_rem.getPath().c_str(),
206  std::ios_base::in | std::ios_base::out |
207  std::ios_base::trunc | std::ios_base::binary))
208  this->setstate(std::ios_base::failbit);
209  else
210  this->clear(std::ios_base::goodbit);
211 }
212 
213 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
215 {
216  off_type rpos = is.tellg();
217 
218  is.seekg(0, std::ios_base::end);
219 
220  off_type rend = is.tellg();
221 
222  if (!is.good()) {
223  this->setstate(std::ios_base::failbit);
224  return;
225  }
226 
227  if (rpos == rend)
228  return;
229 
230  is.seekg(rpos);
231 
232  boost::iostreams::filtering_stream<boost::iostreams::input, char_type, traits_type> fs;
233 
235  fs.push(is);
236 
237  boost::iostreams::copy(fs, *this->rdbuf());
238 
239  if (this->tmpFileBuf.pubseekpos(0, std::ios_base::in | std::ios_base::out) != 0)
240  this->setstate(std::ios_base::failbit);
241 
242  this->setstate(is.rdstate() | fs.rdstate());
243 }
244 
245 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
247 {
248  if (tmpFileBuf.pubseekpos(0, std::ios_base::in) != 0) {
249  this->setstate(std::ios_base::failbit);
250  return;
251  }
252 
253  boost::iostreams::filtering_stream<boost::iostreams::output, char_type, traits_type> fs;
254 
256  fs.push(os);
257 
258  boost::iostreams::copy(*this->rdbuf(), fs);
259 
260  this->setstate(os.rdstate() | fs.rdstate());
261 }
262 
263 // DecompressionIStream Implementation
264 
265 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
267 {}
268 
269 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
271 {
272  open(stream);
273 }
274 
275 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
277 {
278  this->openTmpFile();
279 
280  if (this->good())
281  this->decompInput(stream);
282 }
283 
284 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
286 {
287  this->closeTmpFile();
288 }
289 
290 // CompressionOStream Implementation
291 
292 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
294  stream(0)
295 {}
296 
297 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
299  stream(0)
300 {
301  open(stream);
302 }
303 
304 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
306 {
307  if (stream) {
308  try {
309  stream->seekp(outPos);
310  this->compOutput(*stream);
311  } catch (...) {}
312  }
313 }
314 
315 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
317 {
318  this->openTmpFile();
319 
320  if (!this->good())
321  return;
322 
323  outPos = stream.tellp();
324  this->setstate(stream.rdstate());
325 
326  if (this->good())
327  this->stream = &stream;
328 }
329 
330 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
332 {
333  if (stream) {
334  stream->seekp(outPos);
335  this->compOutput(*stream);
336 
337  if (!this->good())
338  return;
339 
340  stream = 0;
341  }
342 
343  this->closeTmpFile();
344 }
345 
346 // CompressedIOStream Implementation
347 
348 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
350  stream(0)
351 {}
352 
353 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
355  stream(0)
356 {
357  open(stream);
358 }
359 
360 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
362 {
363  if (stream) {
364  try {
365  stream->seekp(outPos);
366  this->compOutput(*stream);
367  } catch (...) {}
368  }
369 }
370 
371 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
373 {
374  this->openTmpFile();
375 
376  if (!this->good())
377  return;
378 
379  outPos = stream.tellp();
380  this->setstate(stream.rdstate());
381 
382  if (!this->good())
383  return;
384 
385  this->decompInput(stream);
386 
387  if (this->good())
388  this->stream = &stream;
389 }
390 
391 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
393 {
394  if (stream) {
395  stream->seekp(outPos);
396  this->compOutput(*stream);
397 
398  if (!this->good())
399  return;
400 
401  stream = 0;
402  }
403 
404  this->closeTmpFile();
405 }
406 
407 #endif // CDPL_UTIL_COMPRESSIONSTREAMS_HPP
Declaration of filesystem-related functions.
Definition of the class CDPL::Util::FileRemover.
Definition: CompressionStreams.hpp:151
void close()
Definition: CompressionStreams.hpp:392
CompressedIOStream()
Definition: CompressionStreams.hpp:349
StreamType::char_type char_type
Definition: CompressionStreams.hpp:155
~CompressedIOStream()
Definition: CompressionStreams.hpp:361
StreamType::off_type off_type
Definition: CompressionStreams.hpp:159
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:156
void open(StreamType &stream)
Definition: CompressionStreams.hpp:372
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:158
StreamType::int_type int_type
Definition: CompressionStreams.hpp:157
Definition: CompressionStreams.hpp:126
StreamType::int_type int_type
Definition: CompressionStreams.hpp:132
~CompressionOStream()
Definition: CompressionStreams.hpp:305
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:131
CompressionOStream()
Definition: CompressionStreams.hpp:293
StreamType::off_type off_type
Definition: CompressionStreams.hpp:134
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:133
StreamType::char_type char_type
Definition: CompressionStreams.hpp:130
void close()
Definition: CompressionStreams.hpp:331
void open(StreamType &stream)
Definition: CompressionStreams.hpp:316
Definition: CompressionStreams.hpp:77
std::basic_istream< char_type, traits_type > IStreamType
Definition: CompressionStreams.hpp:85
void decompInput(IStreamType &is)
Definition: CompressionStreams.hpp:214
StreamType::off_type off_type
Definition: CompressionStreams.hpp:84
StreamType::char_type char_type
Definition: CompressionStreams.hpp:80
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:81
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:83
void openTmpFile()
Definition: CompressionStreams.hpp:201
std::basic_ostream< char_type, traits_type > OStreamType
Definition: CompressionStreams.hpp:86
void closeTmpFile()
Definition: CompressionStreams.hpp:192
StreamType::int_type int_type
Definition: CompressionStreams.hpp:82
CompressionStreamBase()
Definition: CompressionStreams.hpp:187
void compOutput(OStreamType &os)
Definition: CompressionStreams.hpp:246
virtual ~CompressionStreamBase()
Definition: CompressionStreams.hpp:91
Definition: CompressionStreams.hpp:107
void open(StreamType &stream)
Definition: CompressionStreams.hpp:276
StreamType::char_type char_type
Definition: CompressionStreams.hpp:111
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:112
StreamType::int_type int_type
Definition: CompressionStreams.hpp:113
StreamType::off_type off_type
Definition: CompressionStreams.hpp:115
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:114
void close()
Definition: CompressionStreams.hpp:285
DecompressionIStream()
Definition: CompressionStreams.hpp:266
Definition: FileRemover.hpp:44
const std::string & getPath() const
CDPL_UTIL_API std::string genCheckedTempFilePath(const std::string &dir="", const std::string &ptn="%%%%-%%%%-%%%%-%%%%")
CompressedIOStream< GZIP > GZipIOStream
Definition: CompressionStreams.hpp:178
CompressionAlgo
Definition: CompressionStreams.hpp:50
@ BZIP2
Definition: CompressionStreams.hpp:53
@ GZIP
Definition: CompressionStreams.hpp:52
DecompressionIStream< GZIP > GZipIStream
Definition: CompressionStreams.hpp:174
CompressionOStream< BZIP2 > BZip2OStream
Definition: CompressionStreams.hpp:177
DecompressionIStream< BZIP2 > BZip2IStream
Definition: CompressionStreams.hpp:175
CompressionOStream< GZIP > GZipOStream
Definition: CompressionStreams.hpp:176
CompressedIOStream< BZIP2 > BZip2IOStream
Definition: CompressionStreams.hpp:179
The namespace of the Chemical Data Processing Library.
boost::iostreams::bzip2_compressor CompFilter
Definition: CompressionStreams.hpp:72
boost::iostreams::bzip2_decompressor DecompFilter
Definition: CompressionStreams.hpp:71
boost::iostreams::gzip_compressor CompFilter
Definition: CompressionStreams.hpp:64
boost::iostreams::gzip_decompressor DecompFilter
Definition: CompressionStreams.hpp:63
Definition: CompressionStreams.hpp:57