Chemical Data Processing Library C++ API - Version 1.0.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/filesystem.hpp>
35 #include <boost/iostreams/copy.hpp>
36 #include <boost/iostreams/filtering_stream.hpp>
37 #include <boost/iostreams/filter/gzip.hpp>
38 #include <boost/iostreams/filter/bzip2.hpp>
39 
42 
43 
44 namespace CDPL
45 {
46 
47  namespace Util
48  {
49 
51  {
52 
54  BZIP2
55  };
56 
57  template <CompressionAlgo CompAlgo>
59 
60  template <>
62  {
63 
64  typedef boost::iostreams::gzip_decompressor DecompFilter;
65  typedef boost::iostreams::gzip_compressor CompFilter;
66  };
67 
68  template <>
70  {
71 
72  typedef boost::iostreams::bzip2_decompressor DecompFilter;
73  typedef boost::iostreams::bzip2_compressor CompFilter;
74  };
75 
76  template <CompressionAlgo CompAlgo, typename StreamType>
77  class CompressionStreamBase : public StreamType
78  {
79 
80  public:
81  typedef typename StreamType::char_type char_type;
82  typedef typename StreamType::traits_type traits_type;
83  typedef typename StreamType::int_type int_type;
84  typedef typename StreamType::pos_type pos_type;
85  typedef typename StreamType::off_type off_type;
86  typedef std::basic_istream<char_type, traits_type> IStreamType;
87  typedef std::basic_ostream<char_type, traits_type> OStreamType;
88 
90 
91  protected:
93 
94  void closeTmpFile();
95  void openTmpFile();
96 
99 
100  private:
101  typedef std::basic_filebuf<char_type, traits_type> FileBufType;
102 
103  FileBufType tmpFileBuf;
104  };
105 
106  template <CompressionAlgo CompAlgo, typename CharT = char, typename TraitsT = std::char_traits<CharT> >
107  class DecompressionIStream : public CompressionStreamBase<CompAlgo, std::basic_istream<CharT, TraitsT> >
108  {
109 
110  public:
111  typedef typename std::basic_istream<CharT, TraitsT> StreamType;
112  typedef typename StreamType::char_type char_type;
113  typedef typename StreamType::traits_type traits_type;
114  typedef typename StreamType::int_type int_type;
115  typedef typename StreamType::pos_type pos_type;
116  typedef typename StreamType::off_type off_type;
117 
119  DecompressionIStream(StreamType& stream);
120 
121  void open(StreamType& stream);
122  void close();
123  };
124 
125  template <CompressionAlgo CompAlgo, typename CharT = char, typename TraitsT = std::char_traits<CharT> >
126  class CompressionOStream : public CompressionStreamBase<CompAlgo, std::basic_ostream<CharT, TraitsT> >
127  {
128 
129  public:
130  typedef typename std::basic_ostream<CharT, TraitsT> StreamType;
131  typedef typename StreamType::char_type char_type;
132  typedef typename StreamType::traits_type traits_type;
133  typedef typename StreamType::int_type int_type;
134  typedef typename StreamType::pos_type pos_type;
135  typedef typename StreamType::off_type off_type;
136 
138  CompressionOStream(StreamType& stream);
139 
141 
142  void open(StreamType& stream);
143  void close();
144 
145  private:
146  StreamType* stream;
147  off_type outPos;
148  };
149 
150  template <CompressionAlgo CompAlgo, typename CharT = char, typename TraitsT = std::char_traits<CharT> >
151  class CompressedIOStream : public CompressionStreamBase<CompAlgo, std::basic_iostream<CharT, TraitsT> >
152  {
153 
154  public:
155  typedef typename std::basic_iostream<CharT, TraitsT> StreamType;
156  typedef typename StreamType::char_type char_type;
157  typedef typename StreamType::traits_type traits_type;
158  typedef typename StreamType::int_type int_type;
159  typedef typename StreamType::pos_type pos_type;
160  typedef typename StreamType::off_type off_type;
161 
163  CompressedIOStream(StreamType& stream);
164 
166 
167  void open(StreamType& stream);
168  void close();
169 
170  private:
171  StreamType* stream;
172  off_type outPos;
173  };
174 
181  } // namespace Util
182 } // namespace CDPL
183 
184 
185 // CompressionStreamBase Implementation
186 
187 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
189  StreamType(&tmpFileBuf)
190 {}
191 
192 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
194 {
195  if (!tmpFileBuf.close())
196  this->setstate(std::ios_base::failbit);
197  else
198  this->clear();
199 }
200 
201 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
203 {
204  FileRemover tmp_file_rem(genCheckedTempFilePath());
205 
206  if (!tmpFileBuf.open(tmp_file_rem.getPath().c_str(),
207  std::ios_base::in | std::ios_base::out |
208  std::ios_base::trunc | std::ios_base::binary))
209  this->setstate(std::ios_base::failbit);
210  else
211  this->clear(std::ios_base::goodbit);
212 }
213 
214 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
216 {
217  off_type rpos = is.tellg();
218 
219  is.seekg(0, std::ios_base::end);
220 
221  off_type rend = is.tellg();
222 
223  if (!is.good()) {
224  this->setstate(std::ios_base::failbit);
225  return;
226  }
227 
228  if (rpos == rend)
229  return;
230 
231  is.seekg(rpos);
232 
233  boost::iostreams::filtering_stream<boost::iostreams::input, char_type, traits_type> fs;
234 
236  fs.push(is);
237 
238  boost::iostreams::copy(fs, *this->rdbuf());
239 
240  if (this->tmpFileBuf.pubseekpos(0, std::ios_base::in | std::ios_base::out) != 0)
241  this->setstate(std::ios_base::failbit);
242 
243  this->setstate(is.rdstate() | fs.rdstate());
244 }
245 
246 template <CDPL::Util::CompressionAlgo CompAlgo, typename StreamType>
248 {
249  if (tmpFileBuf.pubseekpos(0, std::ios_base::in) != 0) {
250  this->setstate(std::ios_base::failbit);
251  return;
252  }
253 
254  boost::iostreams::filtering_stream<boost::iostreams::output, char_type, traits_type> fs;
255 
257  fs.push(os);
258 
259  boost::iostreams::copy(*this->rdbuf(), fs);
260 
261  this->setstate(os.rdstate() | fs.rdstate());
262 }
263 
264 // DecompressionIStream Implementation
265 
266 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
268 {}
269 
270 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
272 {
273  open(stream);
274 }
275 
276 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
278 {
279  this->openTmpFile();
280 
281  if (this->good())
282  this->decompInput(stream);
283 }
284 
285 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
287 {
288  this->closeTmpFile();
289 }
290 
291 // CompressionOStream Implementation
292 
293 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
295  stream(0)
296 {}
297 
298 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
300  stream(0)
301 {
302  open(stream);
303 }
304 
305 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
307 {
308  if (stream) {
309  try {
310  stream->seekp(outPos);
311  this->compOutput(*stream);
312  } catch (...) {}
313  }
314 }
315 
316 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
318 {
319  this->openTmpFile();
320 
321  if (!this->good())
322  return;
323 
324  outPos = stream.tellp();
325  this->setstate(stream.rdstate());
326 
327  if (this->good())
328  this->stream = &stream;
329 }
330 
331 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
333 {
334  if (stream) {
335  stream->seekp(outPos);
336  this->compOutput(*stream);
337 
338  if (!this->good())
339  return;
340 
341  stream = 0;
342  }
343 
344  this->closeTmpFile();
345 }
346 
347 // CompressedIOStream Implementation
348 
349 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
351  stream(0)
352 {}
353 
354 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
356  stream(0)
357 {
358  open(stream);
359 }
360 
361 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
363 {
364  if (stream) {
365  try {
366  stream->seekp(outPos);
367  this->compOutput(*stream);
368  } catch (...) {}
369  }
370 }
371 
372 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
374 {
375  this->openTmpFile();
376 
377  if (!this->good())
378  return;
379 
380  outPos = stream.tellp();
381  this->setstate(stream.rdstate());
382 
383  if (!this->good())
384  return;
385 
386  this->decompInput(stream);
387 
388  if (this->good())
389  this->stream = &stream;
390 }
391 
392 template <CDPL::Util::CompressionAlgo CompAlgo, typename CharT, typename TraitsT>
394 {
395  if (stream) {
396  stream->seekp(outPos);
397  this->compOutput(*stream);
398 
399  if (!this->good())
400  return;
401 
402  stream = 0;
403  }
404 
405  this->closeTmpFile();
406 }
407 
408 #endif // CDPL_UTIL_COMPRESSIONSTREAMS_HPP
CDPL::Util::DecompressionIStream
Definition: CompressionStreams.hpp:108
CDPL::Util::CompressionOStream::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:134
CDPL::Util::CompressedIOStream::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:156
CDPL::Util::CompressionAlgoTraits< BZIP2 >::DecompFilter
boost::iostreams::bzip2_decompressor DecompFilter
Definition: CompressionStreams.hpp:72
CDPL::Util::CompressionAlgoTraits< GZIP >::DecompFilter
boost::iostreams::gzip_decompressor DecompFilter
Definition: CompressionStreams.hpp:64
CDPL::Util::CompressedIOStream::close
void close()
Definition: CompressionStreams.hpp:393
CDPL::Util::CompressionStreamBase::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:81
CDPL::Util::DecompressionIStream::DecompressionIStream
DecompressionIStream()
Definition: CompressionStreams.hpp:267
CDPL::Util::DecompressionIStream::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:113
CDPL::Util::BZip2IOStream
CompressedIOStream< BZIP2 > BZip2IOStream
Definition: CompressionStreams.hpp:180
CDPL::Util::CompressionStreamBase::IStreamType
std::basic_istream< char_type, traits_type > IStreamType
Definition: CompressionStreams.hpp:86
CDPL::Util::FileRemover
Definition: FileRemover.hpp:44
CDPL::Util::CompressionStreamBase::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:82
CDPL::Util::CompressedIOStream::CompressedIOStream
CompressedIOStream()
Definition: CompressionStreams.hpp:350
CDPL::Util::BZIP2
@ BZIP2
Definition: CompressionStreams.hpp:54
CDPL::Util::CompressionOStream::open
void open(StreamType &stream)
Definition: CompressionStreams.hpp:317
CDPL::Util::CompressionStreamBase::OStreamType
std::basic_ostream< char_type, traits_type > OStreamType
Definition: CompressionStreams.hpp:87
CDPL::Util::CompressionStreamBase::closeTmpFile
void closeTmpFile()
Definition: CompressionStreams.hpp:193
CDPL::Util::CompressionStreamBase::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:83
CDPL::Util::BZip2OStream
CompressionOStream< BZIP2 > BZip2OStream
Definition: CompressionStreams.hpp:178
CDPL::Util::CompressionOStream::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:131
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:115
CDPL::Util::CompressionAlgo
CompressionAlgo
Definition: CompressionStreams.hpp:51
CDPL::Util::CompressionStreamBase
Definition: CompressionStreams.hpp:78
CDPL::Util::DecompressionIStream::open
void open(StreamType &stream)
Definition: CompressionStreams.hpp:277
CDPL::Util::CompressedIOStream
Definition: CompressionStreams.hpp:152
CDPL::Util::CompressionStreamBase::decompInput
void decompInput(IStreamType &is)
Definition: CompressionStreams.hpp:215
CDPL::Util::CompressionOStream::CompressionOStream
CompressionOStream()
Definition: CompressionStreams.hpp:294
CDPL::Util::CompressionStreamBase::~CompressionStreamBase
virtual ~CompressionStreamBase()
Definition: CompressionStreams.hpp:92
CDPL::Util::CompressionStreamBase::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:84
CDPL::Util::DecompressionIStream::close
void close()
Definition: CompressionStreams.hpp:286
CDPL::Util::CompressedIOStream::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:157
CDPL::Util::GZipOStream
CompressionOStream< GZIP > GZipOStream
Definition: CompressionStreams.hpp:177
CDPL::Util::CompressedIOStream::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:160
CDPL::Util::CompressionAlgoTraits
Definition: CompressionStreams.hpp:58
CDPL::Util::CompressionOStream::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:133
CDPL::Util::GZIP
@ GZIP
Definition: CompressionStreams.hpp:53
CDPL::Util::DecompressionIStream::char_type
StreamType::char_type char_type
Definition: CompressionStreams.hpp:112
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::CompressedIOStream::open
void open(StreamType &stream)
Definition: CompressionStreams.hpp:373
CDPL::Util::CompressedIOStream::pos_type
StreamType::pos_type pos_type
Definition: CompressionStreams.hpp:159
CDPL::Util::GZipIOStream
CompressedIOStream< GZIP > GZipIOStream
Definition: CompressionStreams.hpp:179
CDPL::Util::CompressionAlgoTraits< GZIP >::CompFilter
boost::iostreams::gzip_compressor CompFilter
Definition: CompressionStreams.hpp:65
CDPL::Util::genCheckedTempFilePath
CDPL_UTIL_API std::string genCheckedTempFilePath(const std::string &dir="", const std::string &ptn="%%%%-%%%%-%%%%-%%%%")
CDPL::Util::CompressionOStream
Definition: CompressionStreams.hpp:127
CDPL::Util::DecompressionIStream::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:116
CDPL::Util::CompressionOStream::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:135
CDPL::Util::CompressionOStream::~CompressionOStream
~CompressionOStream()
Definition: CompressionStreams.hpp:306
CDPL::Util::DecompressionIStream::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:114
CDPL::Util::CompressionOStream::close
void close()
Definition: CompressionStreams.hpp:332
CDPL::Util::CompressedIOStream::~CompressedIOStream
~CompressedIOStream()
Definition: CompressionStreams.hpp:362
CDPL::Util::CompressionStreamBase::CompressionStreamBase
CompressionStreamBase()
Definition: CompressionStreams.hpp:188
CDPL::Util::BZip2IStream
DecompressionIStream< BZIP2 > BZip2IStream
Definition: CompressionStreams.hpp:176
CDPL::Util::CompressionStreamBase::off_type
StreamType::off_type off_type
Definition: CompressionStreams.hpp:85
CDPL::Util::CompressedIOStream::int_type
StreamType::int_type int_type
Definition: CompressionStreams.hpp:158
CDPL::Util::CompressionStreamBase::compOutput
void compOutput(OStreamType &os)
Definition: CompressionStreams.hpp:247
CDPL::Util::CompressionOStream::traits_type
StreamType::traits_type traits_type
Definition: CompressionStreams.hpp:132
CDPL::Util::CompressionAlgoTraits< BZIP2 >::CompFilter
boost::iostreams::bzip2_compressor CompFilter
Definition: CompressionStreams.hpp:73
CDPL::Util::GZipIStream
DecompressionIStream< GZIP > GZipIStream
Definition: CompressionStreams.hpp:175
FileFunctions.hpp
Declaration of filesystem-related functions.
CDPL::Util::CompressionStreamBase::openTmpFile
void openTmpFile()
Definition: CompressionStreams.hpp:202