29 #ifndef CDPL_UTIL_COMPRESSIONSTREAMS_HPP
30 #define CDPL_UTIL_COMPRESSIONSTREAMS_HPP
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>
66 template <CompressionAlgo CompAlgo>
104 template <CompressionAlgo CompAlgo,
typename StreamType>
139 typedef std::basic_filebuf<char_type, traits_type> FileBufType;
141 FileBufType tmpFileBuf;
151 template <CompressionAlgo CompAlgo,
typename CharT =
char,
typename TraitsT = std::
char_traits<CharT> >
157 typedef typename std::basic_istream<CharT, TraitsT> StreamType;
184 void open(StreamType& stream);
200 template <CompressionAlgo CompAlgo,
typename CharT =
char,
typename TraitsT = std::
char_traits<CharT> >
206 typedef typename std::basic_ostream<CharT, TraitsT> StreamType;
238 void open(StreamType& stream);
258 template <CompressionAlgo CompAlgo,
typename CharT =
char,
typename TraitsT = std::
char_traits<CharT> >
264 typedef typename std::basic_iostream<CharT, TraitsT> StreamType;
296 void open(StreamType& stream);
326 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
328 StreamType(&tmpFileBuf)
331 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
334 if (!tmpFileBuf.close())
335 this->setstate(std::ios_base::failbit);
340 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
345 if (!tmpFileBuf.open(tmp_file_rem.
getPath().c_str(),
346 std::ios_base::in | std::ios_base::out |
347 std::ios_base::trunc | std::ios_base::binary))
348 this->setstate(std::ios_base::failbit);
350 this->clear(std::ios_base::goodbit);
353 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
358 is.seekg(0, std::ios_base::end);
363 this->setstate(std::ios_base::failbit);
372 boost::iostreams::filtering_stream<boost::iostreams::input, char_type, traits_type> fs;
377 boost::iostreams::copy(fs, *this->rdbuf());
379 if (this->tmpFileBuf.pubseekpos(0, std::ios_base::in | std::ios_base::out) != 0)
380 this->setstate(std::ios_base::failbit);
382 this->setstate(is.rdstate() | fs.rdstate());
385 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
388 if (tmpFileBuf.pubseekpos(0, std::ios_base::in) != 0) {
389 this->setstate(std::ios_base::failbit);
393 boost::iostreams::filtering_stream<boost::iostreams::output, char_type, traits_type> fs;
398 boost::iostreams::copy(*this->rdbuf(), fs);
400 this->setstate(os.rdstate() | fs.rdstate());
405 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
409 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
415 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
421 this->decompInput(stream);
424 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
427 this->closeTmpFile();
432 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
437 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
444 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
449 stream->seekp(outPos);
450 this->compOutput(*stream);
455 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
463 outPos = stream.tellp();
464 this->setstate(stream.rdstate());
467 this->stream = &stream;
470 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
474 stream->seekp(outPos);
475 this->compOutput(*stream);
483 this->closeTmpFile();
488 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
493 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
500 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
505 stream->seekp(outPos);
506 this->compOutput(*stream);
511 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
519 outPos = stream.tellp();
520 this->setstate(stream.rdstate());
525 this->decompInput(stream);
528 this->stream = &stream;
531 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
535 stream->seekp(outPos);
536 this->compOutput(*stream);
544 this->closeTmpFile();
Declaration of filesystem-related functions.
Definition of class CDPL::Util::FileRemover.
Bidirectional stream wrapper that decompresses data read from stream into a temporary buffer and re-c...
Definition: CompressionStreams.hpp:260
void close()
Closes the stream and writes any pending modifications back as compressed output.
Definition: CompressionStreams.hpp:532
CompressedIOStream()
Constructs the CompressedIOStream instance without an associated source/sink stream.
Definition: CompressionStreams.hpp:489
StreamType::char_type char_type
The character type of the stream.
Definition: CompressionStreams.hpp:266
~CompressedIOStream()
Destructor. Flushes any pending modifications back to the sink stream as compressed output.
Definition: CompressionStreams.hpp:501
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:274
StreamType::traits_type traits_type
The character traits type of the stream.
Definition: CompressionStreams.hpp:268
void open(StreamType &stream)
Opens the bidirectional compression stream on stream.
Definition: CompressionStreams.hpp:512
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:272
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:270
Output stream wrapper that transparently compresses data and writes it to an underlying sink stream w...
Definition: CompressionStreams.hpp:202
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:212
~CompressionOStream()
Destructor. Flushes any buffered data to the sink stream as compressed output.
Definition: CompressionStreams.hpp:445
StreamType::traits_type traits_type
The character traits type of the stream.
Definition: CompressionStreams.hpp:210
CompressionOStream()
Constructs the CompressionOStream instance without an associated sink stream.
Definition: CompressionStreams.hpp:433
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:216
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:214
StreamType::char_type char_type
The character type of the stream.
Definition: CompressionStreams.hpp:208
void close()
Closes the compression stream and writes the compressed output to the sink stream.
Definition: CompressionStreams.hpp:471
void open(StreamType &stream)
Opens the compression stream on stream.
Definition: CompressionStreams.hpp:456
Base class for stream wrappers that buffer (de)compressed data through a temporary file.
Definition: CompressionStreams.hpp:106
std::basic_istream< char_type, traits_type > IStreamType
Input-stream type with matching character and traits types.
Definition: CompressionStreams.hpp:120
void decompInput(IStreamType &is)
Definition: CompressionStreams.hpp:354
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:118
StreamType::char_type char_type
The character type of the wrapped stream.
Definition: CompressionStreams.hpp:110
StreamType::traits_type traits_type
The character traits type of the wrapped stream.
Definition: CompressionStreams.hpp:112
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:116
void openTmpFile()
Definition: CompressionStreams.hpp:341
std::basic_ostream< char_type, traits_type > OStreamType
Output-stream type with matching character and traits types.
Definition: CompressionStreams.hpp:122
void closeTmpFile()
Definition: CompressionStreams.hpp:332
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:114
CompressionStreamBase()
Constructs the CompressionStreamBase instance and installs the temporary-file buffer.
Definition: CompressionStreams.hpp:327
void compOutput(OStreamType &os)
Definition: CompressionStreams.hpp:386
virtual ~CompressionStreamBase()
Definition: CompressionStreams.hpp:130
Input stream wrapper that transparently decompresses data read from an underlying compressed source s...
Definition: CompressionStreams.hpp:153
void open(StreamType &stream)
Opens the decompression stream on stream.
Definition: CompressionStreams.hpp:416
StreamType::char_type char_type
The character type of the stream.
Definition: CompressionStreams.hpp:159
StreamType::traits_type traits_type
The character traits type of the stream.
Definition: CompressionStreams.hpp:161
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:163
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:167
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:165
void close()
Closes the decompression stream and releases the temporary buffer.
Definition: CompressionStreams.hpp:425
DecompressionIStream()
Constructs the DecompressionIStream instance without an associated source stream.
Definition: CompressionStreams.hpp:406
RAII helper that deletes a file when the FileRemover instance goes out of scope (unless released befo...
Definition: FileRemover.hpp:48
const std::string & getPath() const
Returns the file-system path currently guarded by the FileRemover.
CDPL_UTIL_API std::string genCheckedTempFilePath(const std::string &dir="", const std::string &ptn="%%%%-%%%%-%%%%-%%%%")
Generates a temporary file path inside dir whose basename matches the supplied randomization ptn and ...
CompressedIOStream< GZIP > GZipIOStream
Bidirectional stream that transparently (de)compresses gzip data.
Definition: CompressionStreams.hpp:317
CompressionAlgo
Identifiers for the compression algorithms supported by the Util compression-stream wrappers.
Definition: CompressionStreams.hpp:53
@ BZIP2
Identifier for the bzip2 compression algorithm.
Definition: CompressionStreams.hpp:58
@ GZIP
Identifier for the gzip compression algorithm.
Definition: CompressionStreams.hpp:56
DecompressionIStream< GZIP > GZipIStream
Input stream that transparently decompresses gzip-compressed data.
Definition: CompressionStreams.hpp:309
CompressionOStream< BZIP2 > BZip2OStream
Output stream that transparently writes bzip2-compressed data.
Definition: CompressionStreams.hpp:315
DecompressionIStream< BZIP2 > BZip2IStream
Input stream that transparently decompresses bzip2-compressed data.
Definition: CompressionStreams.hpp:311
CompressionOStream< GZIP > GZipOStream
Output stream that transparently writes gzip-compressed data.
Definition: CompressionStreams.hpp:313
CompressedIOStream< BZIP2 > BZip2IOStream
Bidirectional stream that transparently (de)compresses bzip2 data.
Definition: CompressionStreams.hpp:319
The namespace of the Chemical Data Processing Library.
boost::iostreams::bzip2_compressor CompFilter
Compression filter type (bzip2).
Definition: CompressionStreams.hpp:92
boost::iostreams::bzip2_decompressor DecompFilter
Decompression filter type (bzip2).
Definition: CompressionStreams.hpp:90
boost::iostreams::gzip_compressor CompFilter
Compression filter type (gzip).
Definition: CompressionStreams.hpp:79
boost::iostreams::gzip_decompressor DecompFilter
Decompression filter type (gzip).
Definition: CompressionStreams.hpp:77
Traits-style template selecting the boost::iostreams compression/decompression filters that implement...
Definition: CompressionStreams.hpp:67