diff --git a/include/seqan3/contrib/stream/bgzf_istream.hpp b/include/seqan3/contrib/stream/bgzf_istream.hpp index 05e34e7ba0..0f01139363 100644 --- a/include/seqan3/contrib/stream/bgzf_istream.hpp +++ b/include/seqan3/contrib/stream/bgzf_istream.hpp @@ -22,8 +22,8 @@ #pragma once -#include #include +#include #include #include @@ -42,49 +42,42 @@ namespace seqan3::contrib // Class basic_bgzf_istreambuf // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> class basic_bgzf_istreambuf : public std::basic_streambuf { public: - - typedef Tr traits_type; + typedef Tr traits_type; typedef typename traits_type::char_type char_type; - typedef typename traits_type::int_type int_type; - typedef typename traits_type::off_type off_type; - typedef typename traits_type::pos_type pos_type; + typedef typename traits_type::int_type int_type; + typedef typename traits_type::off_type off_type; + typedef typename traits_type::pos_type pos_type; private: + typedef std::basic_istream & istream_reference; - typedef std::basic_istream& istream_reference; - - typedef ElemA char_allocator_type; - typedef ByteT byte_type; - typedef ByteAT byte_allocator_type; - typedef byte_type* byte_buffer_type; + typedef ElemA char_allocator_type; + typedef ByteT byte_type; + typedef ByteAT byte_allocator_type; + typedef byte_type * byte_buffer_type; typedef std::vector TBuffer; - typedef fixed_buffer_queue TJobQueue; + typedef fixed_buffer_queue TJobQueue; - static const size_t MAX_PUTBACK = 4; + static size_t const MAX_PUTBACK = 4; // Allows serialized access to the underlying buffer. struct Serializer { - istream_reference istream; - std::mutex lock; - io_error *error; - off_type fileOfs; - - Serializer(istream_reference istream) : - istream(istream), - error(NULL), - fileOfs(0u) + istream_reference istream; + std::mutex lock; + io_error * error; + off_type fileOfs; + + Serializer(istream_reference istream) : istream(istream), error(NULL), fileOfs(0u) {} ~Serializer() @@ -99,16 +92,16 @@ class basic_bgzf_istreambuf : public std::basic_streambuf { typedef std::vector TInputBuffer; - TInputBuffer inputBuffer; - TBuffer buffer; - off_type fileOfs; - int32_t size; - uint32_t compressedSize; + TInputBuffer inputBuffer; + TBuffer buffer; + off_type fileOfs; + int32_t size; + uint32_t compressedSize; - std::mutex cs; + std::mutex cs; std::condition_variable readyEvent; - bool ready; - bool bgzfEofMarker; + bool ready; + bool bgzfEofMarker; DecompressionJob() : inputBuffer(DefaultPageSize::MAX_BLOCK_SIZE, 0), @@ -121,7 +114,7 @@ class basic_bgzf_istreambuf : public std::basic_streambuf bgzfEofMarker(false) {} - DecompressionJob(DecompressionJob const &other) : + DecompressionJob(DecompressionJob const & other) : inputBuffer(other.inputBuffer), buffer(other.buffer), fileOfs(other.fileOfs), @@ -134,18 +127,18 @@ class basic_bgzf_istreambuf : public std::basic_streambuf }; // string of recyclable jobs - size_t numThreads; - size_t numJobs; + size_t numThreads; + size_t numJobs; std::vector jobs; - TJobQueue runningQueue; - TJobQueue todoQueue; - detail::reader_writer_manager runningQueueManager; // synchronises reader, writer with running queue. + TJobQueue runningQueue; + TJobQueue todoQueue; + detail::reader_writer_manager runningQueueManager; // synchronises reader, writer with running queue. detail::reader_writer_manager todoQueueManager; // synchronises reader, writer with todo queue. - int currentJobId; + int currentJobId; struct DecompressionThread { - basic_bgzf_istreambuf *streamBuf; + basic_bgzf_istreambuf * streamBuf; CompressionContext compressionCtx; void operator()() @@ -163,7 +156,7 @@ class basic_bgzf_istreambuf : public std::basic_streambuf if (streamBuf->todoQueue.wait_pop(jobId) == queue_op_status::closed) return; - DecompressionJob &job = streamBuf->jobs[jobId]; + DecompressionJob & job = streamBuf->jobs[jobId]; size_t tailLen = 0; // typically the idle queue contains only ready jobs @@ -172,7 +165,11 @@ class basic_bgzf_istreambuf : public std::basic_streambuf if (!job.ready) { std::unique_lock lock(job.cs); - job.readyEvent.wait(lock, [&job]{return job.ready;}); + job.readyEvent.wait(lock, + [&job] + { + return job.ready; + }); assert(job.ready == true); } @@ -193,7 +190,7 @@ class basic_bgzf_istreambuf : public std::basic_streambuf { // read header streamBuf->serializer.istream.read( - (char_type*)&job.inputBuffer[0], + (char_type *)&job.inputBuffer[0], DefaultPageSize::BLOCK_HEADER_LENGTH); if (!streamBuf->serializer.istream.good()) @@ -214,18 +211,20 @@ class basic_bgzf_istreambuf : public std::basic_streambuf } // extract length of compressed data - tailLen = _bgzfUnpack16(&job.inputBuffer[0] + 16) + - 1u - DefaultPageSize::BLOCK_HEADER_LENGTH; + tailLen = _bgzfUnpack16(&job.inputBuffer[0] + 16) + 1u + - DefaultPageSize::BLOCK_HEADER_LENGTH; // read compressed data and tail streamBuf->serializer.istream.read( - (char_type*)&job.inputBuffer[0] + DefaultPageSize::BLOCK_HEADER_LENGTH, + (char_type *)&job.inputBuffer[0] + + DefaultPageSize::BLOCK_HEADER_LENGTH, tailLen); // Check if end-of-file marker is set if (memcmp(reinterpret_cast(&job.inputBuffer[0]), reinterpret_cast(&BGZF_END_OF_FILE_MARKER[0]), - 28) == 0) + 28) + == 0) { job.bgzfEofMarker = true; } @@ -244,8 +243,8 @@ class basic_bgzf_istreambuf : public std::basic_streambuf job.ready = false; eofSkip: - streamBuf->serializer.istream.clear( - streamBuf->serializer.istream.rdstate() & ~std::ios_base::failbit); + streamBuf->serializer.istream.clear(streamBuf->serializer.istream.rdstate() + & ~std::ios_base::failbit); } if (streamBuf->runningQueue.try_push(jobId) != queue_op_status::success) @@ -256,16 +255,18 @@ class basic_bgzf_istreambuf : public std::basic_streambuf job.ready = true; } job.readyEvent.notify_all(); - return; // Terminate this thread. + return; // Terminate this thread. } } if (!job.ready) { // decompress block - job.size = _decompressBlock( - &job.buffer[0] + MAX_PUTBACK, job.buffer.capacity(), - &job.inputBuffer[0], job.compressedSize, compressionCtx); + job.size = _decompressBlock(&job.buffer[0] + MAX_PUTBACK, + job.buffer.capacity(), + &job.inputBuffer[0], + job.compressedSize, + compressionCtx); // signal that job is ready { @@ -278,14 +279,11 @@ class basic_bgzf_istreambuf : public std::basic_streambuf } }; - std::vector pool; // pool of worker threads - TBuffer putbackBuffer; + std::vector pool; // pool of worker threads + TBuffer putbackBuffer; public: - - basic_bgzf_istreambuf(istream_reference istream_, - size_t numThreads = bgzf_thread_count, - size_t jobsPerThread = 8) : + basic_bgzf_istreambuf(istream_reference istream_, size_t numThreads = bgzf_thread_count, size_t jobsPerThread = 8) : serializer(istream_), numThreads(numThreads), numJobs(numThreads * jobsPerThread), @@ -338,14 +336,11 @@ class basic_bgzf_istreambuf : public std::basic_streambuf // save at most MAX_PUTBACK characters from previous page to putback buffer if (putback != 0) - std::copy( - this->gptr() - putback, - this->gptr(), - &putbackBuffer[0]); + std::copy(this->gptr() - putback, this->gptr(), &putbackBuffer[0]); if (currentJobId >= 0) todoQueue.wait_push(currentJobId); - // appendValue(todoQueue, currentJobId); + // appendValue(todoQueue, currentJobId); while (true) { @@ -358,36 +353,36 @@ class basic_bgzf_istreambuf : public std::basic_streambuf return EOF; } - DecompressionJob &job = jobs[currentJobId]; + DecompressionJob & job = jobs[currentJobId]; // restore putback buffer this->setp(&job.buffer[0], &job.buffer[0] + (job.buffer.size() - 1)); if (putback != 0) - std::copy( - &putbackBuffer[0], - &putbackBuffer[0] + putback, - &job.buffer[0] + (MAX_PUTBACK - putback)); + std::copy(&putbackBuffer[0], &putbackBuffer[0] + putback, &job.buffer[0] + (MAX_PUTBACK - putback)); // wait for the end of decompression { std::unique_lock lock(job.cs); - job.readyEvent.wait(lock, [&job]{return job.ready;}); + job.readyEvent.wait(lock, + [&job] + { + return job.ready; + }); } - size_t size = (job.size != -1)? job.size : 0; + size_t size = (job.size != -1) ? job.size : 0; // reset buffer pointers - this->setg( - &job.buffer[0] + (MAX_PUTBACK - putback), // beginning of putback area - &job.buffer[0] + MAX_PUTBACK, // read position - &job.buffer[0] + (MAX_PUTBACK + size)); // end of buffer + this->setg(&job.buffer[0] + (MAX_PUTBACK - putback), // beginning of putback area + &job.buffer[0] + MAX_PUTBACK, // read position + &job.buffer[0] + (MAX_PUTBACK + size)); // end of buffer // The end of the bgzf file is reached, either if there was an error, or if the // end-of-file marker was reached, while the uncompressed block had zero size. if (job.size == -1 || (job.size == 0 && job.bgzfEofMarker)) return EOF; else if (job.size > 0) - return traits_type::to_int_type(*this->gptr()); // return next character + return traits_type::to_int_type(*this->gptr()); // return next character throw io_error("BGZF: Invalid end condition in decompression. " "Most likely due to an empty bgzf block without end-of-file marker."); @@ -410,20 +405,18 @@ class basic_bgzf_istreambuf : public std::basic_streambuf if (currentJobId >= 0 && ofs <= this->egptr() - this->gptr()) { - DecompressionJob &job = jobs[currentJobId]; + DecompressionJob & job = jobs[currentJobId]; // reset buffer pointers - this->setg( - this->eback(), // beginning of putback area - this->gptr() + ofs, // read position - this->egptr()); // end of buffer + this->setg(this->eback(), // beginning of putback area + this->gptr() + ofs, // read position + this->egptr()); // end of buffer if (this->gptr() != this->egptr()) return pos_type((job.fileOfs << 16) + ((this->gptr() - &job.buffer[MAX_PUTBACK]))); else return pos_type((job.fileOfs + job.compressedSize) << 16); } - } else if (dir == std::ios_base::beg) { @@ -433,13 +426,12 @@ class basic_bgzf_istreambuf : public std::basic_streambuf // are we in the same block? if (currentJobId >= 0 && jobs[currentJobId].fileOfs == (off_type)destFileOfs) { - DecompressionJob &job = jobs[currentJobId]; + DecompressionJob & job = jobs[currentJobId]; // reset buffer pointers - this->setg( - this->eback(), // beginning of putback area - &job.buffer[0] + (MAX_PUTBACK + (ofs & 0xffff)), // read position - this->egptr()); // end of buffer + this->setg(this->eback(), // beginning of putback area + &job.buffer[0] + (MAX_PUTBACK + (ofs & 0xff'ff)), // read position + this->egptr()); // end of buffer return ofs; } @@ -479,7 +471,7 @@ class basic_bgzf_istreambuf : public std::basic_streambuf if (serializer.istream.rdbuf()->pubseekpos(destFileOfs, std::ios_base::in) == destFileOfs) serializer.fileOfs = destFileOfs; else - currentJobId = -2; // temporarily signals a seek error + currentJobId = -2; // temporarily signals a seek error } } @@ -493,20 +485,23 @@ class basic_bgzf_istreambuf : public std::basic_streambuf if (currentJobId >= 0) { // wait for the end of decompression - DecompressionJob &job = jobs[currentJobId]; + DecompressionJob & job = jobs[currentJobId]; { std::unique_lock lock(job.cs); - job.readyEvent.wait(lock, [&job]{return job.ready;}); + job.readyEvent.wait(lock, + [&job] + { + return job.ready; + }); } assert(job.fileOfs == (off_type)destFileOfs); // reset buffer pointers - this->setg( - &job.buffer[0] + MAX_PUTBACK, // no putback area - &job.buffer[0] + (MAX_PUTBACK + (ofs & 0xffff)), // read position - &job.buffer[0] + (MAX_PUTBACK + job.size)); // end of buffer + this->setg(&job.buffer[0] + MAX_PUTBACK, // no putback area + &job.buffer[0] + (MAX_PUTBACK + (ofs & 0xff'ff)), // read position + &job.buffer[0] + (MAX_PUTBACK + job.size)); // end of buffer return ofs; } } @@ -520,43 +515,58 @@ class basic_bgzf_istreambuf : public std::basic_streambuf } // returns the compressed input istream - istream_reference get_istream() { return serializer.istream; }; + istream_reference get_istream() + { + return serializer.istream; + }; }; // -------------------------------------------------------------------------- // Class basic_bgzf_istreambase // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> -class basic_bgzf_istreambase : virtual public std::basic_ios +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> +class basic_bgzf_istreambase : virtual public std::basic_ios { public: - typedef std::basic_istream& istream_reference; - typedef basic_bgzf_istreambuf decompression_bgzf_streambuf_type; + typedef std::basic_istream & istream_reference; + typedef basic_bgzf_istreambuf decompression_bgzf_streambuf_type; - basic_bgzf_istreambase(istream_reference istream_) - : m_buf(istream_) + basic_bgzf_istreambase(istream_reference istream_) : m_buf(istream_) { this->init(&m_buf); }; // returns the underlying decompression bgzf istream object - decompression_bgzf_streambuf_type* rdbuf() { return &m_buf; }; + decompression_bgzf_streambuf_type * rdbuf() + { + return &m_buf; + }; // returns the bgzf error state - int get_zerr() const { return m_buf.get_zerr(); }; + int get_zerr() const + { + return m_buf.get_zerr(); + }; // returns the uncompressed data crc - long get_crc() const { return m_buf.get_crc(); }; + long get_crc() const + { + return m_buf.get_crc(); + }; // returns the uncompressed data size - long get_out_size() const { return m_buf.get_out_size(); }; + long get_out_size() const + { + return m_buf.get_out_size(); + }; // returns the compressed data size - long get_in_size() const { return m_buf.get_in_size(); }; + long get_in_size() const + { + return m_buf.get_in_size(); + }; private: decompression_bgzf_streambuf_type m_buf; @@ -566,50 +576,59 @@ class basic_bgzf_istreambase : virtual public std::basic_ios // Class basic_bgzf_istream // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> class basic_bgzf_istream : - public basic_bgzf_istreambase, - public std::basic_istream + public basic_bgzf_istreambase, + public std::basic_istream { public: - typedef basic_bgzf_istreambase bgzf_istreambase_type; - typedef std::basic_istream istream_type; - typedef istream_type & istream_reference; - typedef char byte_type; + typedef basic_bgzf_istreambase bgzf_istreambase_type; + typedef std::basic_istream istream_type; + typedef istream_type & istream_reference; + typedef char byte_type; basic_bgzf_istream(istream_reference istream_) : bgzf_istreambase_type(istream_), istream_type(bgzf_istreambase_type::rdbuf()), m_is_gzip(false), - m_gbgzf_data_size(0) - {}; + m_gbgzf_data_size(0) {}; // returns true if it is a gzip file - bool is_gzip() const { return m_is_gzip; }; + bool is_gzip() const + { + return m_is_gzip; + }; // return data size check - bool check_data_size() const { return this->get_out_size() == m_gbgzf_data_size; }; + bool check_data_size() const + { + return this->get_out_size() == m_gbgzf_data_size; + }; // return the data size in the file - long get_gbgzf_data_size() const { return m_gbgzf_data_size; }; + long get_gbgzf_data_size() const + { + return m_gbgzf_data_size; + }; protected: - static void read_long(istream_reference in_, unsigned long& x_); + static void read_long(istream_reference in_, unsigned long & x_); int check_header(); bool m_is_gzip; unsigned long m_gbgzf_data_size; -#ifdef _WIN32 +# ifdef _WIN32 + private: - void _Add_vtordisp1() { } // Required to avoid VC++ warning C4250 - void _Add_vtordisp2() { } // Required to avoid VC++ warning C4250 -#endif + void _Add_vtordisp1() + {} // Required to avoid VC++ warning C4250 + void _Add_vtordisp2() + {} // Required to avoid VC++ warning C4250 +# endif }; // =========================================================================== diff --git a/include/seqan3/contrib/stream/bgzf_ostream.hpp b/include/seqan3/contrib/stream/bgzf_ostream.hpp index 3a442337ba..3affbc1908 100644 --- a/include/seqan3/contrib/stream/bgzf_ostream.hpp +++ b/include/seqan3/contrib/stream/bgzf_ostream.hpp @@ -38,31 +38,27 @@ namespace seqan3::contrib // Class basic_bgzf_ostreambuf // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> class basic_bgzf_ostreambuf : public std::basic_streambuf { private: - - typedef std::basic_ostream& ostream_reference; - typedef ElemA char_allocator_type; - typedef ByteT byte_type; - typedef ByteAT byte_allocator_type; - typedef byte_type* byte_buffer_type; - typedef ConcurrentQueue > job_queue_type; + typedef std::basic_ostream & ostream_reference; + typedef ElemA char_allocator_type; + typedef ByteT byte_type; + typedef ByteAT byte_allocator_type; + typedef byte_type * byte_buffer_type; + typedef ConcurrentQueue> job_queue_type; public: - - typedef Tr traits_type; - typedef typename traits_type::char_type char_type; - typedef typename traits_type::int_type int_type; - typedef typename traits_type::pos_type pos_type; - typedef typename traits_type::off_type off_type; + typedef Tr traits_type; + typedef typename traits_type::char_type char_type; + typedef typename traits_type::int_type int_type; + typedef typename traits_type::pos_type pos_type; + typedef typename traits_type::off_type off_type; struct ScopedLock { @@ -80,8 +76,8 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf // One compressed block. struct OutputBuffer { - char buffer[DefaultPageSize::MAX_BLOCK_SIZE]; - size_t size; + char buffer[DefaultPageSize::MAX_BLOCK_SIZE]; + size_t size; }; // Writes the output to the underlying stream when invoked. @@ -89,11 +85,10 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf { ostream_reference ostream; - BufferWriter(ostream_reference ostream) : - ostream(ostream) + BufferWriter(ostream_reference ostream) : ostream(ostream) {} - bool operator() (OutputBuffer const & outputBuffer) + bool operator()(OutputBuffer const & outputBuffer) { ostream.write(outputBuffer.buffer, outputBuffer.size); return ostream.good(); @@ -104,9 +99,9 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf { typedef std::vector TBuffer; - TBuffer buffer; - size_t size; - OutputBuffer *outputBuffer; + TBuffer buffer; + size_t size; + OutputBuffer * outputBuffer; CompressionJob() : buffer(DefaultPageSize::VALUE / sizeof(char_type), 0), @@ -116,25 +111,31 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf }; // string of recycable jobs - size_t numThreads; - size_t numJobs; - std::vector jobs; - job_queue_type jobQueue; - job_queue_type idleQueue; + size_t numThreads; + size_t numJobs; + std::vector jobs; + job_queue_type jobQueue; + job_queue_type idleQueue; Serializer serializer; - size_t currentJobId; - bool currentJobAvail; + size_t currentJobId; + bool currentJobAvail; struct CompressionThread { - basic_bgzf_ostreambuf *streamBuf; + basic_bgzf_ostreambuf * streamBuf; CompressionContext compressionCtx; void operator()() { - ScopedLock readLock{[this] () mutable { unlockReading(this->streamBuf->jobQueue); }}; + ScopedLock readLock{[this]() mutable + { + unlockReading(this->streamBuf->jobQueue); + }}; // ScopedReadLock readLock(streamBuf->jobQueue); - ScopedLock writeLock{[this] () mutable { unlockWriting(this->streamBuf->idleQueue); }}; + ScopedLock writeLock{[this]() mutable + { + unlockWriting(this->streamBuf->idleQueue); + }}; // ScopedWriteLock{obQueue> writeLock{str}amBuf->idleQueue); // wait for a new job to become available @@ -145,12 +146,14 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf if (!popFront(jobId, streamBuf->jobQueue)) return; - CompressionJob &job = streamBuf->jobs[jobId]; + CompressionJob & job = streamBuf->jobs[jobId]; // compress block with zlib - job.outputBuffer->size = _compressBlock( - job.outputBuffer->buffer, sizeof(job.outputBuffer->buffer), - &job.buffer[0], job.size, compressionCtx); + job.outputBuffer->size = _compressBlock(job.outputBuffer->buffer, + sizeof(job.outputBuffer->buffer), + &job.buffer[0], + job.size, + compressionCtx); success = releaseValue(streamBuf->serializer, job.outputBuffer); appendValue(streamBuf->idleQueue, jobId); @@ -160,11 +163,9 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf // array of worker threads // using TFuture = decltype(std::async(CompressionThread{nullptr, CompressionContext{}, static_cast(0)})); - std::vector pool; + std::vector pool; - basic_bgzf_ostreambuf(ostream_reference ostream_, - size_t numThreads = bgzf_thread_count, - size_t jobsPerThread = 8) : + basic_bgzf_ostreambuf(ostream_reference ostream_, size_t numThreads = bgzf_thread_count, size_t jobsPerThread = 8) : numThreads(numThreads), numJobs(numThreads * jobsPerThread), jobQueue(numJobs), @@ -193,7 +194,7 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf currentJobAvail = popFront(currentJobId, idleQueue); assert(currentJobAvail); - CompressionJob &job = jobs[currentJobId]; + CompressionJob & job = jobs[currentJobId]; job.outputBuffer = aquireValue(serializer); this->setp(&job.buffer[0], &job.buffer[0] + (job.buffer.size() - 1)); } @@ -236,20 +237,20 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf int_type overflow(int_type c) { int w = static_cast(this->pptr() - this->pbase()); - if (c != static_cast(EOF)) + if (!Tr::eq_int_type(c, Tr::eof())) { *this->pptr() = c; ++w; } if (compressBuffer(w)) { - CompressionJob &job = jobs[currentJobId]; + CompressionJob & job = jobs[currentJobId]; this->setp(&job.buffer[0], &job.buffer[0] + (job.buffer.size() - 1)); - return c; + return Tr::not_eof(c); } else { - return EOF; + return Tr::eof(); } } @@ -258,7 +259,7 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf int w = static_cast(this->pptr() - this->pbase()); if ((w != 0 || flushEmptyBuffer) && compressBuffer(w)) { - CompressionJob &job = jobs[currentJobId]; + CompressionJob & job = jobs[currentJobId]; this->setp(&job.buffer[0], &job.buffer[0] + (job.buffer.size() - 1)); } else @@ -292,42 +293,57 @@ class basic_bgzf_ostreambuf : public std::basic_streambuf } // returns a reference to the output stream - ostream_reference get_ostream() const { return serializer.worker.ostream; }; + ostream_reference get_ostream() const + { + return serializer.worker.ostream; + }; }; // -------------------------------------------------------------------------- // Class basic_bgzf_ostreambase // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> -class basic_bgzf_ostreambase : virtual public std::basic_ios +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> +class basic_bgzf_ostreambase : virtual public std::basic_ios { public: - typedef std::basic_ostream& ostream_reference; + typedef std::basic_ostream & ostream_reference; typedef basic_bgzf_ostreambuf bgzf_streambuf_type; - basic_bgzf_ostreambase(ostream_reference ostream_) - : m_buf(ostream_) + basic_bgzf_ostreambase(ostream_reference ostream_) : m_buf(ostream_) { - this->init(&m_buf ); + this->init(&m_buf); }; // returns the underlying zip ostream object - bgzf_streambuf_type* rdbuf() { return &m_buf; }; + bgzf_streambuf_type * rdbuf() + { + return &m_buf; + }; // returns the bgzf error state - int get_zerr() const { return m_buf.get_err(); }; + int get_zerr() const + { + return m_buf.get_err(); + }; // returns the uncompressed data crc - long get_crc() const { return m_buf.get_crc(); }; + long get_crc() const + { + return m_buf.get_crc(); + }; // returns the compressed data size - long get_out_size() const { return m_buf.get_out_size(); }; + long get_out_size() const + { + return m_buf.get_out_size(); + }; // returns the uncompressed data size - long get_in_size() const { return m_buf.get_in_size(); }; + long get_in_size() const + { + return m_buf.get_in_size(); + }; private: bgzf_streambuf_type m_buf; @@ -337,21 +353,19 @@ class basic_bgzf_ostreambase : virtual public std::basic_ios // Class basic_bgzf_ostream // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> class basic_bgzf_ostream : - public basic_bgzf_ostreambase, - public std::basic_ostream + public basic_bgzf_ostreambase, + public std::basic_ostream { public: - typedef basic_bgzf_ostreambase bgzf_ostreambase_type; - typedef std::basic_ostream ostream_type; - typedef ostream_type& ostream_reference; + typedef basic_bgzf_ostreambase bgzf_ostreambase_type; + typedef std::basic_ostream ostream_type; + typedef ostream_type & ostream_reference; basic_bgzf_ostream(ostream_reference ostream_) : bgzf_ostreambase_type(ostream_), @@ -359,9 +373,11 @@ class basic_bgzf_ostream : {} // flush inner buffer and zipper buffer - basic_bgzf_ostream& flush() + basic_bgzf_ostream & flush() { - ostream_type::flush(); this->rdbuf()->flush(); return *this; + ostream_type::flush(); + this->rdbuf()->flush(); + return *this; }; ~basic_bgzf_ostream() @@ -371,10 +387,12 @@ class basic_bgzf_ostream : private: static void put_long(ostream_reference out_, unsigned long x_); -#ifdef _WIN32 - void _Add_vtordisp1() { } // Required to avoid VC++ warning C4250 - void _Add_vtordisp2() { } // Required to avoid VC++ warning C4250 -#endif +# ifdef _WIN32 + void _Add_vtordisp1() + {} // Required to avoid VC++ warning C4250 + void _Add_vtordisp2() + {} // Required to avoid VC++ warning C4250 +# endif }; // =========================================================================== diff --git a/include/seqan3/contrib/stream/bgzf_stream_util.hpp b/include/seqan3/contrib/stream/bgzf_stream_util.hpp index d608a71f14..b0afa3fd62 100644 --- a/include/seqan3/contrib/stream/bgzf_stream_util.hpp +++ b/include/seqan3/contrib/stream/bgzf_stream_util.hpp @@ -171,8 +171,8 @@ inline TDestCapacity _compressBlock(TDestValue * dstBegin, TSourceLength srcLength, CompressionContext & ctx) { - const size_t BLOCK_HEADER_LENGTH = DefaultPageSize::BLOCK_HEADER_LENGTH; - const size_t BLOCK_FOOTER_LENGTH = DefaultPageSize::BLOCK_FOOTER_LENGTH; + size_t const BLOCK_HEADER_LENGTH = DefaultPageSize::BLOCK_HEADER_LENGTH; + size_t const BLOCK_FOOTER_LENGTH = DefaultPageSize::BLOCK_FOOTER_LENGTH; assert(dstCapacity > BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH); assert(sizeof(TDestValue) == 1u); @@ -249,8 +249,8 @@ inline TDestCapacity _decompressBlock(TDestValue * dstBegin, TSourceLength srcLength, CompressionContext & ctx) { - const size_t BLOCK_HEADER_LENGTH = DefaultPageSize::BLOCK_HEADER_LENGTH; - const size_t BLOCK_FOOTER_LENGTH = DefaultPageSize::BLOCK_FOOTER_LENGTH; + size_t const BLOCK_HEADER_LENGTH = DefaultPageSize::BLOCK_HEADER_LENGTH; + size_t const BLOCK_FOOTER_LENGTH = DefaultPageSize::BLOCK_FOOTER_LENGTH; assert(sizeof(TSourceValue) == 1u); assert(sizeof(unsigned) == 4u); diff --git a/include/seqan3/contrib/stream/bz2_istream.hpp b/include/seqan3/contrib/stream/bz2_istream.hpp index 40dcebbe08..9b6a2e17ec 100644 --- a/include/seqan3/contrib/stream/bz2_istream.hpp +++ b/include/seqan3/contrib/stream/bz2_istream.hpp @@ -30,8 +30,8 @@ #if SEQAN3_HAS_BZIP2 -#define BZ_NO_STDIO -#include +# define BZ_NO_STDIO +# include namespace seqan3::contrib { @@ -40,46 +40,51 @@ namespace seqan3::contrib // Class basic_bz2_istreambuf // -------------------------------------------------------------------------- -const size_t BZ2_INPUT_DEFAULT_BUFFER_SIZE = 4096; - -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> -class basic_bz2_istreambuf : - public std::basic_streambuf +size_t const BZ2_INPUT_DEFAULT_BUFFER_SIZE = 4096; + +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> +class basic_bz2_istreambuf : public std::basic_streambuf { public: - typedef std::basic_istream& istream_reference; + typedef std::basic_istream & istream_reference; typedef ElemA char_allocator_type; typedef ByteT byte_type; typedef ByteAT byte_allocator_type; - typedef byte_type* byte_buffer_type; + typedef byte_type * byte_buffer_type; typedef typename Tr::char_type char_type; typedef typename Tr::int_type int_type; - typedef std::vector byte_vector_type; - typedef std::vector char_vector_type; + typedef std::vector byte_vector_type; + typedef std::vector char_vector_type; - basic_bz2_istreambuf( - istream_reference istream_, - size_t verbosity_, - bool small_, - size_t read_buffer_size_, - size_t input_buffer_size_ - ); + basic_bz2_istreambuf(istream_reference istream_, + size_t verbosity_, + bool small_, + size_t read_buffer_size_, + size_t input_buffer_size_); ~basic_bz2_istreambuf(); int_type underflow(); - istream_reference get_istream() { return m_istream;}; - bz_stream& get_bzip2_stream() { return m_bzip2_stream;}; - int get_zerr() const { return m_err;}; + istream_reference get_istream() + { + return m_istream; + }; + bz_stream & get_bzip2_stream() + { + return m_bzip2_stream; + }; + int get_zerr() const + { + return m_err; + }; + private: - std::streamsize unbzip2_from_stream( char_type*, std::streamsize); + std::streamsize unbzip2_from_stream(char_type *, std::streamsize); void put_back_from_bzip2_stream(); size_t fill_input_buffer(); @@ -94,222 +99,140 @@ class basic_bz2_istreambuf : // Class basic_bz2_istreambuf implementation // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::basic_bz2_istreambuf( - istream_reference istream_, - size_t verbosity_, - bool small_, - size_t read_buffer_size_, - size_t input_buffer_size_ -) -: +template +basic_bz2_istreambuf::basic_bz2_istreambuf(istream_reference istream_, + size_t verbosity_, + bool small_, + size_t read_buffer_size_, + size_t input_buffer_size_) : m_istream(istream_), m_input_buffer(input_buffer_size_), m_buffer(read_buffer_size_) { // setting zalloc, zfree and opaque - m_bzip2_stream.bzalloc=NULL; - m_bzip2_stream.bzfree=NULL; - - m_bzip2_stream.next_in=NULL; - m_bzip2_stream.avail_in=0; - m_bzip2_stream.avail_out=0; - m_bzip2_stream.next_out=NULL; + m_bzip2_stream.bzalloc = NULL; + m_bzip2_stream.bzfree = NULL; + m_bzip2_stream.next_in = NULL; + m_bzip2_stream.avail_in = 0; + m_bzip2_stream.avail_out = 0; + m_bzip2_stream.next_out = NULL; - m_err=BZ2_bzDecompressInit ( - &m_bzip2_stream, - std::min(4, static_cast(verbosity_)), - static_cast(small_) - ); + m_err = BZ2_bzDecompressInit(&m_bzip2_stream, std::min(4, static_cast(verbosity_)), static_cast(small_)); - this->setg( - &(m_buffer[0])+4, // beginning of putback area - &(m_buffer[0])+4, // read position - &(m_buffer[0])+4); // end position + this->setg(&(m_buffer[0]) + 4, // beginning of putback area + &(m_buffer[0]) + 4, // read position + &(m_buffer[0]) + 4); // end position } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -size_t basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::fill_input_buffer() +template +size_t basic_bz2_istreambuf::fill_input_buffer() { - m_bzip2_stream.next_in=&(m_input_buffer[0]); - m_istream.read( - (char_type*)(&(m_input_buffer[0])), - static_cast(m_input_buffer.size()/sizeof(char_type)) - ); - return m_bzip2_stream.avail_in=m_istream.gcount()*sizeof(char_type); + m_bzip2_stream.next_in = &(m_input_buffer[0]); + m_istream.read((char_type *)(&(m_input_buffer[0])), + static_cast(m_input_buffer.size() / sizeof(char_type))); + return m_bzip2_stream.avail_in = m_istream.gcount() * sizeof(char_type); } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -void basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::put_back_from_bzip2_stream() +template +void basic_bz2_istreambuf::put_back_from_bzip2_stream() { - if (m_bzip2_stream.avail_in==0) + if (m_bzip2_stream.avail_in == 0) return; - m_istream.clear( std::ios::goodbit ); - m_istream.seekg( - -static_cast(m_bzip2_stream.avail_in), - std::ios_base::cur - ); + m_istream.clear(std::ios::goodbit); + m_istream.seekg(-static_cast(m_bzip2_stream.avail_in), std::ios_base::cur); - m_bzip2_stream.avail_in=0; + m_bzip2_stream.avail_in = 0; } - -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::~basic_bz2_istreambuf() +template +basic_bz2_istreambuf::~basic_bz2_istreambuf() { BZ2_bzDecompressEnd(&m_bzip2_stream); } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -typename basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::int_type - basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::underflow() +template +typename basic_bz2_istreambuf::int_type +basic_bz2_istreambuf::underflow() { - if ( this->gptr() && ( this->gptr() < this->egptr())) - return * reinterpret_cast( this->gptr()); + if (this->gptr() && (this->gptr() < this->egptr())) + return *reinterpret_cast(this->gptr()); int n_putback = static_cast(this->gptr() - this->eback()); - if ( n_putback > 4) + if (n_putback > 4) n_putback = 4; - std::memmove( - &(m_buffer[0]) + (4 - n_putback), - this->gptr() - n_putback, - n_putback*sizeof(char_type) - ); - - int num = unbzip2_from_stream( - &(m_buffer[0])+4, - static_cast((m_buffer.size()-4)*sizeof(char_type)) - ); + std::memmove(&(m_buffer[0]) + (4 - n_putback), this->gptr() - n_putback, n_putback * sizeof(char_type)); + + int num = unbzip2_from_stream(&(m_buffer[0]) + 4, + static_cast((m_buffer.size() - 4) * sizeof(char_type))); if (num <= 0) // ERROR or EOF return EOF; // reset buffer pointers - this->setg( - &(m_buffer[0]) + (4 - n_putback), // beginning of putback area - &(m_buffer[0]) + 4, // read position - &(m_buffer[0]) + 4 + num); // end of buffer - - // return next character - return* reinterpret_cast( this->gptr()); - } + this->setg(&(m_buffer[0]) + (4 - n_putback), // beginning of putback area + &(m_buffer[0]) + 4, // read position + &(m_buffer[0]) + 4 + num); // end of buffer + // return next character + return *reinterpret_cast(this->gptr()); +} -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -std::streamsize basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::unbzip2_from_stream( - char_type* buffer_, - std::streamsize buffer_size_ - ) +template +std::streamsize basic_bz2_istreambuf::unbzip2_from_stream(char_type * buffer_, + std::streamsize buffer_size_) { - m_bzip2_stream.next_out=(byte_buffer_type)buffer_; - m_bzip2_stream.avail_out=buffer_size_*sizeof(char_type); - size_t count =m_bzip2_stream.avail_in; + m_bzip2_stream.next_out = (byte_buffer_type)buffer_; + m_bzip2_stream.avail_out = buffer_size_ * sizeof(char_type); + size_t count = m_bzip2_stream.avail_in; do { - if (m_bzip2_stream.avail_in==0) - count=fill_input_buffer(); + if (m_bzip2_stream.avail_in == 0) + count = fill_input_buffer(); if (m_bzip2_stream.avail_in) { - m_err = BZ2_bzDecompress( &m_bzip2_stream ); + m_err = BZ2_bzDecompress(&m_bzip2_stream); } - } while (m_err==BZ_OK && m_bzip2_stream.avail_out != 0 && count != 0); + } + while (m_err == BZ_OK && m_bzip2_stream.avail_out != 0 && count != 0); if (m_err == BZ_STREAM_END) put_back_from_bzip2_stream(); - return buffer_size_ - m_bzip2_stream.avail_out/sizeof(char_type); + return buffer_size_ - m_bzip2_stream.avail_out / sizeof(char_type); } // -------------------------------------------------------------------------- // Class basic_bz2_istreambase // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> -class basic_bz2_istreambase : virtual public std::basic_ios +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> +class basic_bz2_istreambase : virtual public std::basic_ios { public: - typedef std::basic_istream& istream_reference; - typedef basic_bz2_istreambuf< - Elem,Tr,ElemA,ByteT,ByteAT> unbzip2_streambuf_type; - - basic_bz2_istreambase( - istream_reference ostream_, - size_t verbosity_, - bool small_, - size_t read_buffer_size_, - size_t input_buffer_size_ - ) - : m_buf( - ostream_, - verbosity_, - small_, - read_buffer_size_, - input_buffer_size_ - ) + typedef std::basic_istream & istream_reference; + typedef basic_bz2_istreambuf unbzip2_streambuf_type; + + basic_bz2_istreambase(istream_reference ostream_, + size_t verbosity_, + bool small_, + size_t read_buffer_size_, + size_t input_buffer_size_) : + m_buf(ostream_, verbosity_, small_, read_buffer_size_, input_buffer_size_) { - this->init(&m_buf ); + this->init(&m_buf); }; - unbzip2_streambuf_type* rdbuf() { return &m_buf; }; + unbzip2_streambuf_type * rdbuf() + { + return &m_buf; + }; private: unbzip2_streambuf_type m_buf; @@ -319,40 +242,36 @@ class basic_bz2_istreambase : virtual public std::basic_ios // Class basic_bz2_istream // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> class basic_bz2_istream : - public basic_bz2_istreambase, - public std::basic_istream + public basic_bz2_istreambase, + public std::basic_istream { public: - typedef basic_bz2_istreambase< - Elem,Tr,ElemA,ByteT,ByteAT> bzip2_istreambase_type; - typedef std::basic_istream istream_type; - typedef istream_type& istream_reference; + typedef basic_bz2_istreambase bzip2_istreambase_type; + typedef std::basic_istream istream_type; + typedef istream_type & istream_reference; typedef unsigned char byte_type; - basic_bz2_istream( - istream_reference istream_, - size_t verbosity_ = 0, - bool small_ = false, - size_t read_buffer_size_ = BZ2_INPUT_DEFAULT_BUFFER_SIZE, - size_t input_buffer_size_ = BZ2_INPUT_DEFAULT_BUFFER_SIZE - ) - : - bzip2_istreambase_type(istream_,verbosity_, small_, read_buffer_size_, input_buffer_size_), - istream_type(bzip2_istreambase_type::rdbuf()) - {}; -#ifdef _WIN32 + basic_bz2_istream(istream_reference istream_, + size_t verbosity_ = 0, + bool small_ = false, + size_t read_buffer_size_ = BZ2_INPUT_DEFAULT_BUFFER_SIZE, + size_t input_buffer_size_ = BZ2_INPUT_DEFAULT_BUFFER_SIZE) : + bzip2_istreambase_type(istream_, verbosity_, small_, read_buffer_size_, input_buffer_size_), + istream_type(bzip2_istreambase_type::rdbuf()) {}; +# ifdef _WIN32 + private: - void _Add_vtordisp1() { } // Required to avoid VC++ warning C4250 - void _Add_vtordisp2() { } // Required to avoid VC++ warning C4250 -#endif + void _Add_vtordisp1() + {} // Required to avoid VC++ warning C4250 + void _Add_vtordisp2() + {} // Required to avoid VC++ warning C4250 +# endif }; // -------------------------------------------------------------------------- diff --git a/include/seqan3/contrib/stream/bz2_ostream.hpp b/include/seqan3/contrib/stream/bz2_ostream.hpp index 113ec6a5df..2b51383d49 100644 --- a/include/seqan3/contrib/stream/bz2_ostream.hpp +++ b/include/seqan3/contrib/stream/bz2_ostream.hpp @@ -31,8 +31,8 @@ #if SEQAN3_HAS_BZIP2 -#define BZ_NO_STDIO -#include +# define BZ_NO_STDIO +# include namespace seqan3::contrib { @@ -41,62 +41,58 @@ namespace seqan3::contrib // Class basic_bz2_ostreambuf // -------------------------------------------------------------------------- -const size_t BZ2_OUTPUT_DEFAULT_BUFFER_SIZE = 4096; - -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> -class basic_bz2_ostreambuf : - public std::basic_streambuf +size_t const BZ2_OUTPUT_DEFAULT_BUFFER_SIZE = 4096; + +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> +class basic_bz2_ostreambuf : public std::basic_streambuf { public: - typedef std::basic_streambuf< Elem, Tr > basic_streambuf_type; - typedef std::basic_ostream& ostream_reference; + typedef std::basic_streambuf basic_streambuf_type; + typedef std::basic_ostream & ostream_reference; typedef ElemA char_allocator_type; typedef ByteT byte_type; typedef ByteAT byte_allocator_type; - typedef byte_type* byte_buffer_type; + typedef byte_type * byte_buffer_type; typedef typename Tr::char_type char_type; typedef typename Tr::int_type int_type; - typedef std::vector byte_vector_type; - typedef std::vector char_vector_type; + typedef std::vector byte_vector_type; + typedef std::vector char_vector_type; using basic_streambuf_type::epptr; using basic_streambuf_type::pbase; using basic_streambuf_type::pptr; - basic_bz2_ostreambuf( - ostream_reference ostream_, - size_t block_size_100k_ , - size_t verbosity_ , - size_t work_factor_, - size_t buffer_size_ - ); + basic_bz2_ostreambuf(ostream_reference ostream_, + size_t block_size_100k_, + size_t verbosity_, + size_t work_factor_, + size_t buffer_size_); ~basic_bz2_ostreambuf(); - int sync (); - int_type overflow (int_type c); + int sync(); + int_type overflow(int_type c); std::streamsize flush(int flush_mode); int get_zerr() const - { return m_err;}; + { + return m_err; + }; uint64_t get_in_size() const { - return ((uint64_t)m_bzip2_stream.total_in_hi32 << 32) - + m_bzip2_stream.total_in_lo32; + return ((uint64_t)m_bzip2_stream.total_in_hi32 << 32) + m_bzip2_stream.total_in_lo32; } uint64_t get_out_size() const { - return ((uint64_t)m_bzip2_stream.total_out_hi32 << 32) - + m_bzip2_stream.total_out_lo32; + return ((uint64_t)m_bzip2_stream.total_out_hi32 << 32) + m_bzip2_stream.total_out_lo32; } + private: - bool bzip2_to_stream( char_type*, std::streamsize); + bool bzip2_to_stream(char_type *, std::streamsize); size_t fill_input_buffer(); ostream_reference m_ostream; @@ -110,163 +106,107 @@ class basic_bz2_ostreambuf : // Class basic_bz2_ostreambuf implementation // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >:: basic_bz2_ostreambuf( - ostream_reference ostream_, - size_t block_size_100k_, - size_t verbosity_, - size_t work_factor_, - size_t buffer_size_ - ) -: +template +basic_bz2_ostreambuf::basic_bz2_ostreambuf(ostream_reference ostream_, + size_t block_size_100k_, + size_t verbosity_, + size_t work_factor_, + size_t buffer_size_) : m_ostream(ostream_), - m_output_buffer(buffer_size_,0), - m_buffer(buffer_size_,0) + m_output_buffer(buffer_size_, 0), + m_buffer(buffer_size_, 0) { - m_bzip2_stream.bzalloc=NULL; - m_bzip2_stream.bzfree=NULL; - - m_bzip2_stream.next_in=NULL; - m_bzip2_stream.avail_in=0; - m_bzip2_stream.avail_out=0; - m_bzip2_stream.next_out=NULL; - - m_err=BZ2_bzCompressInit( - &m_bzip2_stream, - std::min( 9, static_cast(block_size_100k_) ), - std::min( 4, static_cast(verbosity_) ), - std::min( 250, static_cast(work_factor_) ) - ); - - this->setp( &(m_buffer[0]), &(m_buffer[m_buffer.size()-1])); + m_bzip2_stream.bzalloc = NULL; + m_bzip2_stream.bzfree = NULL; + + m_bzip2_stream.next_in = NULL; + m_bzip2_stream.avail_in = 0; + m_bzip2_stream.avail_out = 0; + m_bzip2_stream.next_out = NULL; + + m_err = BZ2_bzCompressInit(&m_bzip2_stream, + std::min(9, static_cast(block_size_100k_)), + std::min(4, static_cast(verbosity_)), + std::min(250, static_cast(work_factor_))); + + this->setp(&(m_buffer[0]), &(m_buffer[m_buffer.size() - 1])); } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::~basic_bz2_ostreambuf() +template +basic_bz2_ostreambuf::~basic_bz2_ostreambuf() { flush(BZ_FINISH); m_ostream.flush(); - m_err=BZ2_bzCompressEnd(&m_bzip2_stream); + m_err = BZ2_bzCompressEnd(&m_bzip2_stream); } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -int basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::sync () +template +int basic_bz2_ostreambuf::sync() { - if ( this->pptr() && this->pptr() > this->pbase()) + if (this->pptr() && this->pptr() > this->pbase()) { - int c = overflow( EOF); + int c = overflow(EOF); - if ( c == EOF) + if (c == EOF) return -1; } return 0; } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -typename basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::int_type - basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::overflow ( - typename basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::int_type c - ) +template +typename basic_bz2_ostreambuf::int_type +basic_bz2_ostreambuf::overflow( + typename basic_bz2_ostreambuf::int_type c) { int w = static_cast(this->pptr() - this->pbase()); - if (c != EOF) { - *this->pptr() = c; - ++w; - } - if ( bzip2_to_stream( this->pbase(), w)) { - this->setp( this->pbase(), this->epptr()); - return c; - } else - return EOF; + if (!Tr::eq_int_type(c, Tr::eof())) + { + *this->pptr() = c; + ++w; + } + if (bzip2_to_stream(this->pbase(), w)) + { + this->setp(this->pbase(), this->epptr()); + return Tr::not_eof(c); + } + else + return Tr::eof(); } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -bool basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::bzip2_to_stream( - typename basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::char_type* buffer_, - std::streamsize buffer_size_ - ) +template +bool basic_bz2_ostreambuf::bzip2_to_stream( + typename basic_bz2_ostreambuf::char_type * buffer_, + std::streamsize buffer_size_) { - std::streamsize written_byte_size=0; + std::streamsize written_byte_size = 0; - m_bzip2_stream.next_in=(byte_buffer_type)buffer_; - m_bzip2_stream.avail_in=buffer_size_*sizeof(char_type); - m_bzip2_stream.avail_out=static_cast(m_output_buffer.size()); - m_bzip2_stream.next_out=&(m_output_buffer[0]); - size_t remainder=0; + m_bzip2_stream.next_in = (byte_buffer_type)buffer_; + m_bzip2_stream.avail_in = buffer_size_ * sizeof(char_type); + m_bzip2_stream.avail_out = static_cast(m_output_buffer.size()); + m_bzip2_stream.next_out = &(m_output_buffer[0]); + size_t remainder = 0; do { - m_err = BZ2_bzCompress (&m_bzip2_stream, BZ_RUN ); + m_err = BZ2_bzCompress(&m_bzip2_stream, BZ_RUN); - if (m_err == BZ_RUN_OK || m_err == BZ_STREAM_END) + if (m_err == BZ_RUN_OK || m_err == BZ_STREAM_END) { - written_byte_size= static_cast(m_output_buffer.size()) - m_bzip2_stream.avail_out; + written_byte_size = static_cast(m_output_buffer.size()) - m_bzip2_stream.avail_out; // output buffer is full, dumping to ostream - m_ostream.write( - (const char_type*) &(m_output_buffer[0]), - static_cast( written_byte_size/sizeof(char_type) ) - ); + m_ostream.write((char_type const *)&(m_output_buffer[0]), + static_cast(written_byte_size / sizeof(char_type))); // checking if some bytes were not written. - if ( (remainder = written_byte_size%sizeof(char_type))!=0) + if ((remainder = written_byte_size % sizeof(char_type)) != 0) { // copy to the beginning of the stream - std::memmove( - &(m_output_buffer[0]), - &(m_output_buffer[written_byte_size-remainder]), - remainder); - + std::memmove(&(m_output_buffer[0]), &(m_output_buffer[written_byte_size - remainder]), remainder); } - m_bzip2_stream.avail_out=static_cast(m_output_buffer.size()-remainder); - m_bzip2_stream.next_out=&m_output_buffer[remainder]; + m_bzip2_stream.avail_out = static_cast(m_output_buffer.size() - remainder); + m_bzip2_stream.next_out = &m_output_buffer[remainder]; } } while (m_bzip2_stream.avail_in != 0 && m_err == BZ_RUN_OK); @@ -274,57 +214,42 @@ bool basic_bz2_ostreambuf< return m_err == BZ_RUN_OK || m_err == BZ_FLUSH_OK; } -template< - typename Elem, - typename Tr, - typename ElemA, - typename ByteT, - typename ByteAT -> -std::streamsize basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT - >::flush(int flush_mode) +template +std::streamsize basic_bz2_ostreambuf::flush(int flush_mode) { - std::streamsize written_byte_size=0, total_written_byte_size=0; + std::streamsize written_byte_size = 0, total_written_byte_size = 0; - int const buffer_size = static_cast< int >( pptr() - pbase() ); // amount of data currently in buffer + int const buffer_size = static_cast(pptr() - pbase()); // amount of data currently in buffer - m_bzip2_stream.next_in=(byte_buffer_type)pbase(); - m_bzip2_stream.avail_in=static_cast< unsigned int >(buffer_size*sizeof(char_type)); - m_bzip2_stream.avail_out=static_cast< unsigned int >(m_output_buffer.size()); - m_bzip2_stream.next_out=&(m_output_buffer[0]); - size_t remainder=0; + m_bzip2_stream.next_in = (byte_buffer_type)pbase(); + m_bzip2_stream.avail_in = static_cast(buffer_size * sizeof(char_type)); + m_bzip2_stream.avail_out = static_cast(m_output_buffer.size()); + m_bzip2_stream.next_out = &(m_output_buffer[0]); + size_t remainder = 0; do { - m_err = BZ2_bzCompress (&m_bzip2_stream, flush_mode); + m_err = BZ2_bzCompress(&m_bzip2_stream, flush_mode); if (m_err == BZ_FINISH_OK || m_err == BZ_STREAM_END) { - written_byte_size= - static_cast(m_output_buffer.size()) - - m_bzip2_stream.avail_out; - total_written_byte_size+=written_byte_size; + written_byte_size = static_cast(m_output_buffer.size()) - m_bzip2_stream.avail_out; + total_written_byte_size += written_byte_size; // output buffer is full, dumping to ostream - m_ostream.write( - (const char_type*) &(m_output_buffer[0]), - static_cast( written_byte_size/sizeof(char_type)*sizeof(char) ) - ); + m_ostream.write((char_type const *)&(m_output_buffer[0]), + static_cast(written_byte_size / sizeof(char_type) * sizeof(char))); // checking if some bytes were not written. - if ( (remainder = written_byte_size%sizeof(char_type))!=0) + if ((remainder = written_byte_size % sizeof(char_type)) != 0) { // copy to the beginning of the stream - std::memmove( - &(m_output_buffer[0]), - &(m_output_buffer[written_byte_size-remainder]), - remainder); - + std::memmove(&(m_output_buffer[0]), &(m_output_buffer[written_byte_size - remainder]), remainder); } - m_bzip2_stream.avail_out=static_cast(m_output_buffer.size()-remainder); - m_bzip2_stream.next_out=&(m_output_buffer[remainder]); + m_bzip2_stream.avail_out = static_cast(m_output_buffer.size() - remainder); + m_bzip2_stream.next_out = &(m_output_buffer[remainder]); } - } while (m_err == BZ_FINISH_OK); + } + while (m_err == BZ_FINISH_OK); m_ostream.flush(); @@ -335,33 +260,31 @@ std::streamsize basic_bz2_ostreambuf< // Class basic_bz2_ostreambase // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> -class basic_bz2_ostreambase : virtual public std::basic_ios +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> +class basic_bz2_ostreambase : virtual public std::basic_ios { public: - typedef std::basic_ostream& ostream_reference; - typedef basic_bz2_ostreambuf< - Elem,Tr,ElemA,ByteT,ByteAT> bzip2_streambuf_type; - - basic_bz2_ostreambase( - ostream_reference ostream_, - size_t block_size_100k_ , - size_t verbosity_ , - size_t work_factor_, - size_t buffer_size_ - ) - : m_buf(ostream_,block_size_100k_, verbosity_, work_factor_, buffer_size_) + typedef std::basic_ostream & ostream_reference; + typedef basic_bz2_ostreambuf bzip2_streambuf_type; + + basic_bz2_ostreambase(ostream_reference ostream_, + size_t block_size_100k_, + size_t verbosity_, + size_t work_factor_, + size_t buffer_size_) : + m_buf(ostream_, block_size_100k_, verbosity_, work_factor_, buffer_size_) { - this->init(&m_buf ); + this->init(&m_buf); }; - bzip2_streambuf_type* rdbuf() { return &m_buf; }; + bzip2_streambuf_type * rdbuf() + { + return &m_buf; + }; private: bzip2_streambuf_type m_buf; @@ -371,55 +294,53 @@ class basic_bz2_ostreambase : virtual public std::basic_ios // Class basic_bz2_ostream // -------------------------------------------------------------------------- -template< - typename Elem, - typename Tr = std::char_traits, - typename ElemA = std::allocator, - typename ByteT = char, - typename ByteAT = std::allocator -> +template , + typename ElemA = std::allocator, + typename ByteT = char, + typename ByteAT = std::allocator> class basic_bz2_ostream : - public basic_bz2_ostreambase, - public std::basic_ostream + public basic_bz2_ostreambase, + public std::basic_ostream { public: - typedef basic_bz2_ostreambase< - Elem,Tr,ElemA,ByteT,ByteAT> bzip2_ostreambase_type; - typedef std::basic_ostream ostream_type; - typedef ostream_type& ostream_reference; - - basic_bz2_ostream( - ostream_reference ostream_, - size_t block_size_100k_ = 9, - size_t verbosity_ = 0, - size_t work_factor_ = 30, - size_t buffer_size_ = BZ2_OUTPUT_DEFAULT_BUFFER_SIZE - ) - : - bzip2_ostreambase_type(ostream_,block_size_100k_, verbosity_, work_factor_,buffer_size_), - ostream_type(bzip2_ostreambase_type::rdbuf()) + typedef basic_bz2_ostreambase bzip2_ostreambase_type; + typedef std::basic_ostream ostream_type; + typedef ostream_type & ostream_reference; + + basic_bz2_ostream(ostream_reference ostream_, + size_t block_size_100k_ = 9, + size_t verbosity_ = 0, + size_t work_factor_ = 30, + size_t buffer_size_ = BZ2_OUTPUT_DEFAULT_BUFFER_SIZE) : + bzip2_ostreambase_type(ostream_, block_size_100k_, verbosity_, work_factor_, buffer_size_), + ostream_type(bzip2_ostreambase_type::rdbuf()) { + + }; + + basic_bz2_ostream & add_header(); + basic_bz2_ostream & zflush() { - + this->flush(); + this->rdbuf()->flush(); + return *this; }; - basic_bz2_ostream& add_header(); - basic_bz2_ostream& zflush() - { - this->flush(); this->rdbuf()->flush(); return *this; - }; +# ifdef _WIN32 -#ifdef _WIN32 private: - void _Add_vtordisp1() { } // Required to avoid VC++ warning C4250 - void _Add_vtordisp2() { } // Required to avoid VC++ warning C4250 -#endif + void _Add_vtordisp1() + {} // Required to avoid VC++ warning C4250 + void _Add_vtordisp2() + {} // Required to avoid VC++ warning C4250 +# endif }; // -------------------------------------------------------------------------- // Typedefs // -------------------------------------------------------------------------- -typedef basic_bz2_ostream bz2_ostream; +typedef basic_bz2_ostream bz2_ostream; typedef basic_bz2_ostream bz2_wostream; } // namespace seqan3::contrib diff --git a/include/seqan3/contrib/stream/gz_istream.hpp b/include/seqan3/contrib/stream/gz_istream.hpp index 165b1a068f..4306b2b5ed 100644 --- a/include/seqan3/contrib/stream/gz_istream.hpp +++ b/include/seqan3/contrib/stream/gz_istream.hpp @@ -24,21 +24,21 @@ #pragma once -#include #include +#include #include #include #if SEQAN3_HAS_ZLIB -#include +# include namespace seqan3::contrib { // Default gzip buffer size, change this to suite your needs. -const size_t GZ_INPUT_DEFAULT_BUFFER_SIZE = 921600; +size_t const GZ_INPUT_DEFAULT_BUFFER_SIZE = 921600; // -------------------------------------------------------------------------- // Class basic_gz_istreambuf @@ -50,38 +50,42 @@ template , typename ElemA = std::allocator, typename ByteT = unsigned char, - typename ByteAT = std::allocator - > -class basic_gz_istreambuf : - public std::basic_streambuf + typename ByteAT = std::allocator> +class basic_gz_istreambuf : public std::basic_streambuf { public: - typedef std::basic_istream & istream_reference; - typedef ElemA char_allocator_type; - typedef ByteT byte_type; - typedef ByteAT byte_allocator_type; - typedef byte_type * byte_buffer_type; - typedef Tr traits_type; - typedef typename Tr::char_type char_type; - typedef typename Tr::int_type int_type; + typedef std::basic_istream & istream_reference; + typedef ElemA char_allocator_type; + typedef ByteT byte_type; + typedef ByteAT byte_allocator_type; + typedef byte_type * byte_buffer_type; + typedef Tr traits_type; + typedef typename Tr::char_type char_type; + typedef typename Tr::int_type int_type; typedef std::vector byte_vector_type; typedef std::vector char_vector_type; // Construct a unzip stream // More info on the following parameters can be found in the zlib documentation. basic_gz_istreambuf(istream_reference istream_, - size_t window_size_, - size_t read_buffer_size_, - size_t input_buffer_size_); + size_t window_size_, + size_t read_buffer_size_, + size_t input_buffer_size_); ~basic_gz_istreambuf(); int_type underflow(); // returns the compressed input istream - istream_reference get_istream() { return m_istream; } + istream_reference get_istream() + { + return m_istream; + } // returns the zlib stream structure - z_stream & get_zip_stream() { return m_zip_stream; } + z_stream & get_zip_stream() + { + return m_zip_stream; + } private: void put_back_from_zip_stream(); @@ -99,17 +103,11 @@ class basic_gz_istreambuf : // Class basic_gz_istreambuf implementation // -------------------------------------------------------------------------- -template -basic_gz_istreambuf::basic_gz_istreambuf( - istream_reference istream_, - size_t window_size_, - size_t read_buffer_size_, - size_t input_buffer_size_ - ) : +template +basic_gz_istreambuf::basic_gz_istreambuf(istream_reference istream_, + size_t window_size_, + size_t read_buffer_size_, + size_t input_buffer_size_) : m_istream(istream_), m_input_buffer(input_buffer_size_), m_buffer(read_buffer_size_) @@ -130,21 +128,13 @@ basic_gz_istreambuf::basic_gz_istreambuf( &(m_buffer[0]) + 4); // end position } -template +template basic_gz_istreambuf::~basic_gz_istreambuf() { inflateEnd(&m_zip_stream); } -template +template typename basic_gz_istreambuf::int_type basic_gz_istreambuf::underflow() { @@ -157,29 +147,24 @@ basic_gz_istreambuf::underflow() std::memmove(&(m_buffer[0]) + (4 - n_putback), this->gptr() - n_putback, n_putback * sizeof(char_type)); - int num = unzip_from_stream(&(m_buffer[0]) + 4, - static_cast((m_buffer.size() - 4) * sizeof(char_type))); + int num = + unzip_from_stream(&(m_buffer[0]) + 4, static_cast((m_buffer.size() - 4) * sizeof(char_type))); - if (num <= 0) // ERROR or EOF + if (num <= 0) // ERROR or EOF return traits_type::eof(); // reset buffer pointers - this->setg(&(m_buffer[0]) + (4 - n_putback), // beginning of putback area - &(m_buffer[0]) + 4, // read position - &(m_buffer[0]) + 4 + num); // end of buffer + this->setg(&(m_buffer[0]) + (4 - n_putback), // beginning of putback area + &(m_buffer[0]) + 4, // read position + &(m_buffer[0]) + 4 + num); // end of buffer // return next character return *reinterpret_cast(this->gptr()); } -template -std::streamsize basic_gz_istreambuf::unzip_from_stream( - char_type * buffer_, - std::streamsize buffer_size_) +template +std::streamsize basic_gz_istreambuf::unzip_from_stream(char_type * buffer_, + std::streamsize buffer_size_) { m_zip_stream.next_out = (byte_buffer_type)buffer_; m_zip_stream.avail_out = static_cast(buffer_size_ * sizeof(char_type)); @@ -209,11 +194,7 @@ std::streamsize basic_gz_istreambuf::unzip_from_ return n_read; } -template +template size_t basic_gz_istreambuf::fill_input_buffer() { m_zip_stream.next_in = &(m_input_buffer[0]); @@ -222,11 +203,7 @@ size_t basic_gz_istreambuf::fill_input_buffer() return m_zip_stream.avail_in = m_istream.gcount() * sizeof(char_type); } -template +template void basic_gz_istreambuf::put_back_from_zip_stream() { if (m_zip_stream.avail_in == 0) @@ -248,26 +225,27 @@ template , typename ElemA = std::allocator, typename ByteT = unsigned char, - typename ByteAT = std::allocator - > -class basic_gz_istreambase : - virtual public std::basic_ios + typename ByteAT = std::allocator> +class basic_gz_istreambase : virtual public std::basic_ios { public: - typedef std::basic_istream & istream_reference; + typedef std::basic_istream & istream_reference; typedef basic_gz_istreambuf unzip_streambuf_type; basic_gz_istreambase(istream_reference ostream_, - size_t window_size_, - size_t read_buffer_size_, - size_t input_buffer_size_) : + size_t window_size_, + size_t read_buffer_size_, + size_t input_buffer_size_) : m_buf(ostream_, window_size_, read_buffer_size_, input_buffer_size_) { this->init(&m_buf); } // returns the underlying unzip istream object - unzip_streambuf_type * rdbuf() { return &m_buf; } + unzip_streambuf_type * rdbuf() + { + return &m_buf; + } private: unzip_streambuf_type m_buf; @@ -294,18 +272,17 @@ template , typename ElemA = std::allocator, typename ByteT = unsigned char, - typename ByteAT = std::allocator - > + typename ByteAT = std::allocator> class basic_gz_istream : public basic_gz_istreambase, public std::basic_istream { public: typedef basic_gz_istreambase zip_istreambase_type; - typedef std::basic_istream istream_type; - typedef istream_type & istream_reference; - typedef ByteT byte_type; - typedef Tr traits_type; + typedef std::basic_istream istream_type; + typedef istream_type & istream_reference; + typedef ByteT byte_type; + typedef Tr traits_type; // Construct a unzipper stream // @@ -315,18 +292,21 @@ class basic_gz_istream : // input_buffer_size_ basic_gz_istream(istream_reference istream_, - size_t window_size_ = 31, // 15 (size) + 16 (gzip header) - size_t read_buffer_size_ = GZ_INPUT_DEFAULT_BUFFER_SIZE, - size_t input_buffer_size_ = GZ_INPUT_DEFAULT_BUFFER_SIZE) : + size_t window_size_ = 31, // 15 (size) + 16 (gzip header) + size_t read_buffer_size_ = GZ_INPUT_DEFAULT_BUFFER_SIZE, + size_t input_buffer_size_ = GZ_INPUT_DEFAULT_BUFFER_SIZE) : zip_istreambase_type(istream_, window_size_, read_buffer_size_, input_buffer_size_), istream_type(this->rdbuf()) {} -#ifdef _WIN32 +# ifdef _WIN32 + private: - void _Add_vtordisp1() {} // Required to avoid VC++ warning C4250 - void _Add_vtordisp2() {} // Required to avoid VC++ warning C4250 -#endif + void _Add_vtordisp1() + {} // Required to avoid VC++ warning C4250 + void _Add_vtordisp2() + {} // Required to avoid VC++ warning C4250 +# endif }; // =========================================================================== @@ -334,9 +314,9 @@ class basic_gz_istream : // =========================================================================== // A typedef for basic_gz_istream -typedef basic_gz_istream gz_istream; +typedef basic_gz_istream gz_istream; // A typedef for basic_gz_istream -typedef basic_gz_istream gz_wistream; +typedef basic_gz_istream gz_wistream; } // namespace seqan3::contrib diff --git a/include/seqan3/contrib/stream/gz_ostream.hpp b/include/seqan3/contrib/stream/gz_ostream.hpp index 1f4d036a22..8a4d51806e 100644 --- a/include/seqan3/contrib/stream/gz_ostream.hpp +++ b/include/seqan3/contrib/stream/gz_ostream.hpp @@ -23,21 +23,21 @@ #pragma once -#include #include +#include #include #include #if SEQAN3_HAS_ZLIB -#include +# include namespace seqan3::contrib { // Default gzip buffer size, change this to suite your needs. -const size_t GZ_OUTPUT_DEFAULT_BUFFER_SIZE = 921600; +size_t const GZ_OUTPUT_DEFAULT_BUFFER_SIZE = 921600; // -------------------------------------------------------------------------- // Enum EStrategy @@ -61,20 +61,18 @@ template , typename ElemA = std::allocator, typename ByteT = unsigned char, - typename ByteAT = std::allocator - > -class basic_gz_ostreambuf : - public std::basic_streambuf + typename ByteAT = std::allocator> +class basic_gz_ostreambuf : public std::basic_streambuf { public: - typedef std::basic_ostream & ostream_reference; - typedef ElemA char_allocator_type; - typedef ByteT byte_type; - typedef ByteAT byte_allocator_type; - typedef byte_type * byte_buffer_type; - typedef Tr traits_type; - typedef typename Tr::char_type char_type; - typedef typename Tr::int_type int_type; + typedef std::basic_ostream & ostream_reference; + typedef ElemA char_allocator_type; + typedef ByteT byte_type; + typedef ByteAT byte_allocator_type; + typedef byte_type * byte_buffer_type; + typedef Tr traits_type; + typedef typename Tr::char_type char_type; + typedef typename Tr::int_type int_type; typedef std::vector byte_vector_type; typedef std::vector char_vector_type; @@ -101,7 +99,6 @@ class basic_gz_ostreambuf : // This method should be called at the end of the compression. std::streamsize flush_finalize(); - private: bool zip_to_stream(char_type *, std::streamsize); size_t fill_input_buffer(); @@ -119,19 +116,13 @@ class basic_gz_ostreambuf : // Class basic_gz_ostreambuf implementation // -------------------------------------------------------------------------- -template -basic_gz_ostreambuf::basic_gz_ostreambuf( - ostream_reference ostream_, - size_t level_, - EStrategy strategy_, - size_t window_size_, - size_t memory_level_, - size_t buffer_size_ - ) : +template +basic_gz_ostreambuf::basic_gz_ostreambuf(ostream_reference ostream_, + size_t level_, + EStrategy strategy_, + size_t window_size_, + size_t memory_level_, + size_t buffer_size_) : m_ostream(ostream_), m_output_buffer(buffer_size_, 0), m_buffer(buffer_size_, 0) @@ -144,23 +135,17 @@ basic_gz_ostreambuf::basic_gz_ostreambuf( m_zip_stream.avail_out = 0; m_zip_stream.next_out = NULL; - m_err = deflateInit2( - &m_zip_stream, - std::min(9, static_cast(level_)), - Z_DEFLATED, - static_cast(window_size_), - std::min(9, static_cast(memory_level_)), - static_cast(strategy_) - ); + m_err = deflateInit2(&m_zip_stream, + std::min(9, static_cast(level_)), + Z_DEFLATED, + static_cast(window_size_), + std::min(9, static_cast(memory_level_)), + static_cast(strategy_)); this->setp(&(m_buffer[0]), &(m_buffer[m_buffer.size() - 1])); } -template +template basic_gz_ostreambuf::~basic_gz_ostreambuf() { flush_finalize(); @@ -168,11 +153,7 @@ basic_gz_ostreambuf::~basic_gz_ostreambuf() m_err = deflateEnd(&m_zip_stream); } -template +template int basic_gz_ostreambuf::sync() { if (this->pptr() && this->pptr() > this->pbase()) @@ -184,11 +165,7 @@ int basic_gz_ostreambuf::sync() return 0; } -template +template typename basic_gz_ostreambuf::int_type basic_gz_ostreambuf::overflow( typename basic_gz_ostreambuf::int_type c) @@ -204,7 +181,7 @@ basic_gz_ostreambuf::overflow( if (zip_to_stream(this->pbase(), w)) { this->setp(this->pbase(), this->epptr() - 1); - return c; + return traits_type::not_eof(c); } else { @@ -212,11 +189,7 @@ basic_gz_ostreambuf::overflow( } } -template +template bool basic_gz_ostreambuf::zip_to_stream( typename basic_gz_ostreambuf::char_type * buffer_, std::streamsize buffer_size_) @@ -233,21 +206,19 @@ bool basic_gz_ostreambuf::zip_to_stream( { m_err = deflate(&m_zip_stream, 0); - if (m_err == Z_OK || m_err == Z_STREAM_END) + if (m_err == Z_OK || m_err == Z_STREAM_END) { written_byte_size = static_cast(m_output_buffer.size()) - m_zip_stream.avail_out; // output buffer is full, dumping to ostream - m_ostream.write((const char_type *) &(m_output_buffer[0]), + m_ostream.write((char_type const *)&(m_output_buffer[0]), static_cast(written_byte_size / sizeof(char_type))); // checking if some bytes were not written. if ((remainder = written_byte_size % sizeof(char_type)) != 0) { // copy to the beginning of the stream - std::memmove(&(m_output_buffer[0]), - &(m_output_buffer[written_byte_size - remainder]), - remainder); + std::memmove(&(m_output_buffer[0]), &(m_output_buffer[written_byte_size - remainder]), remainder); } m_zip_stream.avail_out = static_cast(m_output_buffer.size() - remainder); @@ -259,18 +230,14 @@ bool basic_gz_ostreambuf::zip_to_stream( return m_err == Z_OK; } -template +template std::streamsize basic_gz_ostreambuf::flush(int flush_mode) { int const buffer_size = static_cast(this->pptr() - this->pbase()); // amount of data currently in buffer std::streamsize written_byte_size = 0, total_written_byte_size = 0; - m_zip_stream.next_in = (byte_buffer_type) this->pbase(); + m_zip_stream.next_in = (byte_buffer_type)this->pbase(); m_zip_stream.avail_in = static_cast(buffer_size * sizeof(char_type)); m_zip_stream.avail_out = static_cast(m_output_buffer.size()); m_zip_stream.next_out = &(m_output_buffer[0]); @@ -285,16 +252,14 @@ std::streamsize basic_gz_ostreambuf::flush(int f total_written_byte_size += written_byte_size; // output buffer is full, dumping to ostream - m_ostream.write((const char_type *) &(m_output_buffer[0]), + m_ostream.write((char_type const *)&(m_output_buffer[0]), static_cast(written_byte_size / sizeof(char_type) * sizeof(byte_type))); // checking if some bytes were not written. if ((remainder = written_byte_size % sizeof(char_type)) != 0) { // copy to the beginning of the stream - std::memmove(&(m_output_buffer[0]), - &(m_output_buffer[written_byte_size - remainder]), - remainder); + std::memmove(&(m_output_buffer[0]), &(m_output_buffer[written_byte_size - remainder]), remainder); } m_zip_stream.avail_out = static_cast(m_output_buffer.size() - remainder); @@ -308,21 +273,13 @@ std::streamsize basic_gz_ostreambuf::flush(int f return total_written_byte_size; } -template +template std::streamsize basic_gz_ostreambuf::flush() { return flush(Z_SYNC_FLUSH); } -template +template std::streamsize basic_gz_ostreambuf::flush_finalize() { return flush(Z_FINISH); @@ -338,30 +295,31 @@ template , typename ElemA = std::allocator, typename ByteT = unsigned char, - typename ByteAT = std::allocator - > -class basic_gz_ostreambase : - virtual public std::basic_ios + typename ByteAT = std::allocator> +class basic_gz_ostreambase : virtual public std::basic_ios { public: - typedef std::basic_ostream & ostream_reference; + typedef std::basic_ostream & ostream_reference; typedef basic_gz_ostreambuf zip_streambuf_type; // Construct a zip stream // More info on the following parameters can be found in the zlib documentation. basic_gz_ostreambase(ostream_reference ostream_, - size_t level_, - EStrategy strategy_, - size_t window_size_, - size_t memory_level_, - size_t buffer_size_) : + size_t level_, + EStrategy strategy_, + size_t window_size_, + size_t memory_level_, + size_t buffer_size_) : m_buf(ostream_, level_, strategy_, window_size_, memory_level_, buffer_size_) { this->init(&m_buf); } // returns the underlying zip ostream object - zip_streambuf_type * rdbuf() { return &m_buf; } + zip_streambuf_type * rdbuf() + { + return &m_buf; + } private: zip_streambuf_type m_buf; @@ -392,16 +350,15 @@ template , typename ElemA = std::allocator, typename ByteT = unsigned char, - typename ByteAT = std::allocator - > + typename ByteAT = std::allocator> class basic_gz_ostream : public basic_gz_ostreambase, public std::basic_ostream { public: typedef basic_gz_ostreambase zip_ostreambase_type; - typedef std::basic_ostream ostream_type; - typedef ostream_type & ostream_reference; + typedef std::basic_ostream ostream_type; + typedef ostream_type & ostream_reference; // Constructs a zipper ostream decorator // @@ -414,31 +371,37 @@ class basic_gz_ostream : // buffer_size_ the buffer size used to zip data basic_gz_ostream(ostream_reference ostream_, - size_t level_ = Z_DEFAULT_COMPRESSION, - EStrategy strategy_ = DefaultStrategy, - size_t window_size_ = 31, // 15 (size) + 16 (gzip header) - size_t memory_level_ = 8, - size_t buffer_size_ = GZ_OUTPUT_DEFAULT_BUFFER_SIZE) : + size_t level_ = Z_DEFAULT_COMPRESSION, + EStrategy strategy_ = DefaultStrategy, + size_t window_size_ = 31, // 15 (size) + 16 (gzip header) + size_t memory_level_ = 8, + size_t buffer_size_ = GZ_OUTPUT_DEFAULT_BUFFER_SIZE) : zip_ostreambase_type(ostream_, level_, strategy_, window_size_, memory_level_, buffer_size_), ostream_type(this->rdbuf()) {} ~basic_gz_ostream() { - ostream_type::flush(); this->rdbuf()->flush_finalize(); + ostream_type::flush(); + this->rdbuf()->flush_finalize(); } // flush inner buffer and zipper buffer basic_gz_ostream & flush() { - ostream_type::flush(); this->rdbuf()->flush(); return *this; + ostream_type::flush(); + this->rdbuf()->flush(); + return *this; } -#ifdef _WIN32 +# ifdef _WIN32 + private: - void _Add_vtordisp1() {} // Required to avoid VC++ warning C4250 - void _Add_vtordisp2() {} // Required to avoid VC++ warning C4250 -#endif + void _Add_vtordisp1() + {} // Required to avoid VC++ warning C4250 + void _Add_vtordisp2() + {} // Required to avoid VC++ warning C4250 +# endif }; // =========================================================================== @@ -446,9 +409,9 @@ class basic_gz_ostream : // =========================================================================== // A typedef for basic_gz_ostream -typedef basic_gz_ostream gz_ostream; +typedef basic_gz_ostream gz_ostream; // A typedef for basic_gz_ostream -typedef basic_gz_ostream gz_wostream; +typedef basic_gz_ostream gz_wostream; } // namespace seqan3::contrib diff --git a/test/unit/io/stream/ostream_test_template.hpp b/test/unit/io/stream/ostream_test_template.hpp index c59c2344de..6bdee16b3a 100644 --- a/test/unit/io/stream/ostream_test_template.hpp +++ b/test/unit/io/stream/ostream_test_template.hpp @@ -75,4 +75,23 @@ TYPED_TEST_P(ostream, output_type_erased) EXPECT_EQ(buffer, TestFixture::compressed); } -REGISTER_TYPED_TEST_SUITE_P(ostream, concept_check, output, output_type_erased); +TYPED_TEST_P(ostream, overflow) +{ + seqan3::test::tmp_directory tmp{}; + auto filename = tmp.path() / "ostream_test"; + + { + std::ofstream of{filename}; + + TypeParam stream{of}; + + // Works for gz and bgzf, `deflate` with empty buffer is no error + // But not for bzip2, `BZ2_bzCompress` in `BZ_RUN` mode returns error (`BZ_FINISH` would be fine) + if constexpr (TestFixture::zero_out_os_byte) + EXPECT_NE(EOF, stream.rdbuf()->overflow(EOF)); + else + EXPECT_EQ(EOF, stream.rdbuf()->overflow(EOF)); // Todo: Fix + } +} + +REGISTER_TYPED_TEST_SUITE_P(ostream, concept_check, output, output_type_erased, overflow);