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>
71 template <CompressionAlgo CompAlgo>
119 template <CompressionAlgo CompAlgo,
typename StreamType>
174 typedef std::basic_filebuf<char_type, traits_type> FileBufType;
176 FileBufType tmpFileBuf;
186 template <CompressionAlgo CompAlgo,
typename CharT =
char,
typename TraitsT = std::
char_traits<CharT> >
194 typedef typename std::basic_istream<CharT, TraitsT> StreamType;
236 void open(StreamType& stream);
252 template <CompressionAlgo CompAlgo,
typename CharT =
char,
typename TraitsT = std::
char_traits<CharT> >
260 typedef typename std::basic_ostream<CharT, TraitsT> StreamType;
307 void open(StreamType& stream);
327 template <CompressionAlgo CompAlgo,
typename CharT =
char,
typename TraitsT = std::
char_traits<CharT> >
335 typedef typename std::basic_iostream<CharT, TraitsT> StreamType;
382 void open(StreamType& stream);
429 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
431 StreamType(&tmpFileBuf)
434 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
437 if (!tmpFileBuf.close())
438 this->setstate(std::ios_base::failbit);
443 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
448 if (!tmpFileBuf.open(tmp_file_rem.
getPath().c_str(),
449 std::ios_base::in | std::ios_base::out |
450 std::ios_base::trunc | std::ios_base::binary))
451 this->setstate(std::ios_base::failbit);
453 this->clear(std::ios_base::goodbit);
456 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
461 is.seekg(0, std::ios_base::end);
466 this->setstate(std::ios_base::failbit);
475 boost::iostreams::filtering_stream<boost::iostreams::input, char_type, traits_type> fs;
480 boost::iostreams::copy(fs, *this->rdbuf());
482 if (this->tmpFileBuf.pubseekpos(0, std::ios_base::in | std::ios_base::out) != 0)
483 this->setstate(std::ios_base::failbit);
485 this->setstate(is.rdstate() | fs.rdstate());
488 template <CDPL::Util::CompressionAlgo CompAlgo,
typename StreamType>
491 if (tmpFileBuf.pubseekpos(0, std::ios_base::in) != 0) {
492 this->setstate(std::ios_base::failbit);
496 boost::iostreams::filtering_stream<boost::iostreams::output, char_type, traits_type> fs;
501 boost::iostreams::copy(*this->rdbuf(), fs);
503 this->setstate(os.rdstate() | fs.rdstate());
508 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
512 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
518 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
524 this->decompInput(stream);
527 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
530 this->closeTmpFile();
535 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
540 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
547 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
552 stream->seekp(outPos);
553 this->compOutput(*stream);
558 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
566 outPos = stream.tellp();
567 this->setstate(stream.rdstate());
570 this->stream = &stream;
573 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
577 stream->seekp(outPos);
578 this->compOutput(*stream);
586 this->closeTmpFile();
591 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
596 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
603 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
608 stream->seekp(outPos);
609 this->compOutput(*stream);
614 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
622 outPos = stream.tellp();
623 this->setstate(stream.rdstate());
628 this->decompInput(stream);
631 this->stream = &stream;
634 template <CDPL::Util::CompressionAlgo CompAlgo,
typename CharT,
typename TraitsT>
638 stream->seekp(outPos);
639 this->compOutput(*stream);
647 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:329
void close()
Closes the stream and writes any pending modifications back as compressed output.
Definition: CompressionStreams.hpp:635
CompressedIOStream()
Constructs the CompressedIOStream instance without an associated source/sink stream.
Definition: CompressionStreams.hpp:592
StreamType::char_type char_type
The character type of the stream.
Definition: CompressionStreams.hpp:340
~CompressedIOStream()
Destructor. Flushes any pending modifications back to the sink stream as compressed output.
Definition: CompressionStreams.hpp:604
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:360
StreamType::traits_type traits_type
The character traits type of the stream.
Definition: CompressionStreams.hpp:345
void open(StreamType &stream)
Opens the bidirectional compression stream on stream.
Definition: CompressionStreams.hpp:615
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:355
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:350
Output stream wrapper that transparently compresses data and writes it to an underlying sink stream w...
Definition: CompressionStreams.hpp:254
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:275
~CompressionOStream()
Destructor. Flushes any buffered data to the sink stream as compressed output.
Definition: CompressionStreams.hpp:548
StreamType::traits_type traits_type
The character traits type of the stream.
Definition: CompressionStreams.hpp:270
CompressionOStream()
Constructs the CompressionOStream instance without an associated sink stream.
Definition: CompressionStreams.hpp:536
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:285
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:280
StreamType::char_type char_type
The character type of the stream.
Definition: CompressionStreams.hpp:265
void close()
Closes the compression stream and writes the compressed output to the sink stream.
Definition: CompressionStreams.hpp:574
void open(StreamType &stream)
Opens the compression stream on stream.
Definition: CompressionStreams.hpp:559
Base class for stream wrappers that buffer (de)compressed data through a temporary file.
Definition: CompressionStreams.hpp:121
std::basic_istream< char_type, traits_type > IStreamType
Input-stream type with matching character and traits types.
Definition: CompressionStreams.hpp:152
void decompInput(IStreamType &is)
Definition: CompressionStreams.hpp:457
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:147
StreamType::char_type char_type
The character type of the wrapped stream.
Definition: CompressionStreams.hpp:127
StreamType::traits_type traits_type
The character traits type of the wrapped stream.
Definition: CompressionStreams.hpp:132
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:142
void openTmpFile()
Definition: CompressionStreams.hpp:444
std::basic_ostream< char_type, traits_type > OStreamType
Output-stream type with matching character and traits types.
Definition: CompressionStreams.hpp:157
void closeTmpFile()
Definition: CompressionStreams.hpp:435
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:137
CompressionStreamBase()
Constructs the CompressionStreamBase instance and installs the temporary-file buffer.
Definition: CompressionStreams.hpp:430
void compOutput(OStreamType &os)
Definition: CompressionStreams.hpp:489
virtual ~CompressionStreamBase()
Definition: CompressionStreams.hpp:165
Input stream wrapper that transparently decompresses data read from an underlying compressed source s...
Definition: CompressionStreams.hpp:188
void open(StreamType &stream)
Opens the decompression stream on stream.
Definition: CompressionStreams.hpp:519
StreamType::char_type char_type
The character type of the stream.
Definition: CompressionStreams.hpp:199
StreamType::traits_type traits_type
The character traits type of the stream.
Definition: CompressionStreams.hpp:204
StreamType::int_type int_type
The integer type used to represent characters and EOF.
Definition: CompressionStreams.hpp:209
StreamType::off_type off_type
The type used to represent stream offsets.
Definition: CompressionStreams.hpp:219
StreamType::pos_type pos_type
The type used to represent stream positions.
Definition: CompressionStreams.hpp:214
void close()
Closes the decompression stream and releases the temporary buffer.
Definition: CompressionStreams.hpp:528
DecompressionIStream()
Constructs the DecompressionIStream instance without an associated source stream.
Definition: CompressionStreams.hpp:509
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:417
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:63
@ GZIP
Identifier for the gzip compression algorithm.
Definition: CompressionStreams.hpp:58
DecompressionIStream< GZIP > GZipIStream
Input stream that transparently decompresses gzip-compressed data.
Definition: CompressionStreams.hpp:397
CompressionOStream< BZIP2 > BZip2OStream
Output stream that transparently writes bzip2-compressed data.
Definition: CompressionStreams.hpp:412
DecompressionIStream< BZIP2 > BZip2IStream
Input stream that transparently decompresses bzip2-compressed data.
Definition: CompressionStreams.hpp:402
CompressionOStream< GZIP > GZipOStream
Output stream that transparently writes gzip-compressed data.
Definition: CompressionStreams.hpp:407
CompressedIOStream< BZIP2 > BZip2IOStream
Bidirectional stream that transparently (de)compresses bzip2 data.
Definition: CompressionStreams.hpp:422
The namespace of the Chemical Data Processing Library.
boost::iostreams::bzip2_compressor CompFilter
Compression filter type (bzip2).
Definition: CompressionStreams.hpp:107
boost::iostreams::bzip2_decompressor DecompFilter
Decompression filter type (bzip2).
Definition: CompressionStreams.hpp:102
boost::iostreams::gzip_compressor CompFilter
Compression filter type (gzip).
Definition: CompressionStreams.hpp:89
boost::iostreams::gzip_decompressor DecompFilter
Decompression filter type (gzip).
Definition: CompressionStreams.hpp:84
Traits-style template selecting the boost::iostreams compression/decompression filters that implement...
Definition: CompressionStreams.hpp:72