Chemical Data Processing Library C++ API - Version 1.1.1
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
CDPL::Util::DecompressionIStream
Definition: CompressionStreams.hpp:107
CDPL::Util::CompressionOStream::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:133
CDPL::Util::CompressedIOStream::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:155
CDPL::Util::CompressionAlgoTraits< BZIP2 >::DecompFilter
boost::iostreams::bzip2_decompressor DecompFilter
Definition: CompressionStreams.hpp:71
CDPL::Util::CompressionAlgoTraits< GZIP >::DecompFilter
boost::iostreams::gzip_decompressor DecompFilter
Definition: CompressionStreams.hpp:63
CDPL::Util::CompressedIOStream::close
void close()
Definition: CompressionStreams.hpp:392
CDPL::Util::CompressionStreamBase::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:80
CDPL::Util::DecompressionIStream::DecompressionIStream
DecompressionIStream()
Definition: CompressionStreams.hpp:266
CDPL::Util::DecompressionIStream::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:112
CDPL::Util::BZip2IOStream
CompressedIOStream< BZIP2 > BZip2IOStream
Definition: CompressionStreams.hpp:179
CDPL::Util::CompressionStreamBase::IStreamType
std::basic_istream< char_type, traits_type > IStreamType
Definition: CompressionStreams.hpp:85
CDPL::Util::FileRemover
Definition: FileRemover.hpp:44
CDPL::Util::CompressionStreamBase::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:81
CDPL::Util::CompressedIOStream::CompressedIOStream
CompressedIOStream()
Definition: CompressionStreams.hpp:349
CDPL::Util::BZIP2
@ BZIP2
Definition: CompressionStreams.hpp:53
CDPL::Util::CompressionOStream::open
void open(StreamType &stream)
Definition: CompressionStreams.hpp:316
CDPL::Util::CompressionStreamBase::OStreamType
std::basic_ostream< char_type, traits_type > OStreamType
Definition: CompressionStreams.hpp:86
CDPL::Util::CompressionStreamBase::closeTmpFile
void closeTmpFile()
Definition: CompressionStreams.hpp:192
CDPL::Util::CompressionStreamBase::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:82
CDPL::Util::BZip2OStream
CompressionOStream< BZIP2 > BZip2OStream
Definition: CompressionStreams.hpp:177
CDPL::Util::CompressionOStream::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:130
CDPL::Util::FileRemover::getPath
const std::string & getPath() const
FileRemover.hpp
Definition of the class CDPL::Util::FileRemover.
CDPL::Util::DecompressionIStream::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:114
CDPL::Util::CompressionAlgo
CompressionAlgo
Definition: CompressionStreams.hpp:50
CDPL::Util::CompressionStreamBase
Definition: CompressionStreams.hpp:77
CDPL::Util::DecompressionIStream::open
void open(StreamType &stream)
Definition: CompressionStreams.hpp:276
CDPL::Util::CompressedIOStream
Definition: CompressionStreams.hpp:151
CDPL::Util::CompressionStreamBase::decompInput
void decompInput(IStreamType &is)
Definition: CompressionStreams.hpp:214
CDPL::Util::CompressionOStream::CompressionOStream
CompressionOStream()
Definition: CompressionStreams.hpp:293
CDPL::Util::CompressionStreamBase::~CompressionStreamBase
virtual ~CompressionStreamBase()
Definition: CompressionStreams.hpp:91
CDPL::Util::CompressionStreamBase::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:83
CDPL::Util::DecompressionIStream::close
void close()
Definition: CompressionStreams.hpp:285
CDPL::Util::CompressedIOStream::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:156
CDPL::Util::GZipOStream
CompressionOStream< GZIP > GZipOStream
Definition: CompressionStreams.hpp:176
CDPL::Util::CompressedIOStream::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:159
CDPL::Util::CompressionAlgoTraits
Definition: CompressionStreams.hpp:57
CDPL::Util::CompressionOStream::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:132
CDPL::Util::GZIP
@ GZIP
Definition: CompressionStreams.hpp:52
CDPL::Util::DecompressionIStream::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:111
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::CompressedIOStream::open
void open(StreamType &stream)
Definition: CompressionStreams.hpp:372
CDPL::Util::CompressedIOStream::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:158
CDPL::Util::GZipIOStream
CompressedIOStream< GZIP > GZipIOStream
Definition: CompressionStreams.hpp:178
CDPL::Util::CompressionAlgoTraits< GZIP >::CompFilter
boost::iostreams::gzip_compressor CompFilter
Definition: CompressionStreams.hpp:64
CDPL::Util::genCheckedTempFilePath
CDPL_UTIL_API std::string genCheckedTempFilePath(const std::string &dir="", const std::string &ptn="%%%%-%%%%-%%%%-%%%%")
CDPL::Util::CompressionOStream
Definition: CompressionStreams.hpp:126
CDPL::Util::DecompressionIStream::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:115
CDPL::Util::CompressionOStream::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:134
CDPL::Util::CompressionOStream::~CompressionOStream
~CompressionOStream()
Definition: CompressionStreams.hpp:305
CDPL::Util::DecompressionIStream::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:113
CDPL::Util::CompressionOStream::close
void close()
Definition: CompressionStreams.hpp:331
CDPL::Util::CompressedIOStream::~CompressedIOStream
~CompressedIOStream()
Definition: CompressionStreams.hpp:361
CDPL::Util::CompressionStreamBase::CompressionStreamBase
CompressionStreamBase()
Definition: CompressionStreams.hpp:187
CDPL::Util::BZip2IStream
DecompressionIStream< BZIP2 > BZip2IStream
Definition: CompressionStreams.hpp:175
CDPL::Util::CompressionStreamBase::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:84
CDPL::Util::CompressedIOStream::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:157
CDPL::Util::CompressionStreamBase::compOutput
void compOutput(OStreamType &os)
Definition: CompressionStreams.hpp:246
CDPL::Util::CompressionOStream::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:131
CDPL::Util::CompressionAlgoTraits< BZIP2 >::CompFilter
boost::iostreams::bzip2_compressor CompFilter
Definition: CompressionStreams.hpp:72
CDPL::Util::GZipIStream
DecompressionIStream< GZIP > GZipIStream
Definition: CompressionStreams.hpp:174
FileFunctions.hpp
Declaration of filesystem-related functions.
CDPL::Util::CompressionStreamBase::openTmpFile
void openTmpFile()
Definition: CompressionStreams.hpp:201