📄 streambuf.cpp
字号:
return CT_EOF;}CT_INT_TYPE CCompressionStreambuf::underflow(void){ // Here we don't make a check for the streambuf finalization because // underflow() can be called after Finalize() to read a rest of // produced data. if ( !IsOkay() || !m_Reader || !m_Reader->m_Processor ) { return CT_EOF; } // Reset pointer to the processed data setg(m_Reader->m_OutBuf, m_Reader->m_OutBuf, m_Reader->m_OutBuf); // Try to process next data if ( !ProcessStreamRead() || gptr() == egptr() ) { return CT_EOF; } return CT_TO_INT_TYPE(*gptr());}bool CCompressionStreambuf::ProcessStreamRead(){ unsigned long in_len, in_avail, out_size, out_avail; streamsize n_read; // End of stream has been detected if ( m_Reader->m_LastStatus == CP::eStatus_EndOfData ) { return false; } // Put data into the compressor until there is something // in the output buffer do { in_avail = 0; out_avail = 0; out_size = m_Reader->m_OutBuf + m_Reader->m_OutBufSize - egptr(); // Refill the output buffer if necessary if ( m_Reader->m_LastStatus != CP::eStatus_Overflow ) { // Refill the input buffer if necessary if ( m_Reader->m_Begin == m_Reader->m_End || !m_Reader->m_LastOutAvail) { n_read = m_Stream->rdbuf()->sgetn(m_Reader->m_InBuf, m_Reader->m_InBufSize); if ( !n_read ) { // We can't read more of data return false; } // Update the input buffer pointers m_Reader->m_Begin = m_Reader->m_InBuf; m_Reader->m_End = m_Reader->m_InBuf + n_read; } // Process next data portion in_len = m_Reader->m_End - m_Reader->m_Begin; m_Reader->m_LastStatus = m_Reader->m_Processor->Process( m_Reader->m_Begin, in_len, egptr(), out_size, &in_avail, &out_avail); if ( m_Reader->m_LastStatus != CP::eStatus_Success && m_Reader->m_LastStatus != CP::eStatus_EndOfData ) { return false; } m_Reader->m_LastOutAvail = out_avail; } else { // Get unprocessed data size in_len = m_Reader->m_End - m_Reader->m_Begin; } // Try to flush the compressor if it has not produced a data // via Process() if ( !out_avail ) { m_Reader->m_LastStatus = m_Reader->m_Processor->Flush( egptr(), out_size, &out_avail); if ( m_Reader->m_LastStatus == CP::eStatus_Error ) { return false; } m_Reader->m_LastOutAvail = out_avail; } // Update pointer to an unprocessed data m_Reader->m_Begin += (in_len - in_avail); // Update the get's pointers setg(m_Reader->m_OutBuf, gptr(), egptr() + out_avail); } while ( !out_avail ); return true;}bool CCompressionStreambuf::ProcessStreamWrite(){ const char* in_buf = pbase(); const streamsize count = pptr() - pbase(); unsigned long in_avail = count; // End of stream has been detected if ( m_Writer->m_LastStatus == CP::eStatus_EndOfData ) { return false; } // Loop until no data is left while ( in_avail ) { // Process next data piece unsigned long out_avail = 0; m_Writer->m_LastStatus = m_Writer->m_Processor->Process( in_buf + count - in_avail, in_avail, m_Writer->m_OutBuf, m_Writer->m_OutBufSize, &in_avail, &out_avail); if ( m_Writer->m_LastStatus != CP::eStatus_Success && m_Writer->m_LastStatus != CP::eStatus_EndOfData ) { return false; } // Write the data to the underlying stream if ( out_avail && m_Stream->rdbuf()->sputn(m_Writer->m_OutBuf, out_avail) != (streamsize)out_avail) { return false; } } // Decrease the put pointer pbump(-count); return true;}streamsize CCompressionStreambuf::xsputn(const CT_CHAR_TYPE* buf, streamsize count){ // Check processor status if ( !IsStreamProcessorOkay(CCompressionStream::eWrite) ) { return CT_EOF; } // Check parameters if ( !buf || count <= 0 ) { return 0; } // The number of chars copied streamsize done = 0; // Loop until no data is left while ( done < count ) { // Get the number of chars to write in this iteration // (we've got one more char than epptr thinks) size_t block_size = min(size_t(count-done), size_t(epptr()-pptr()+1)); // Write them memcpy(pptr(), buf + done, block_size); // Update the write pointer pbump(block_size); // Process block if necessary if ( pptr() >= epptr() && !ProcessStreamWrite() ) { break; } done += block_size; } return done;};streamsize CCompressionStreambuf::xsgetn(CT_CHAR_TYPE* buf, streamsize count){ // We don't doing here a check for the streambuf finalization because // underflow() can be called after Finalize() to read a rest of // produced data. if ( !IsOkay() || !m_Reader->m_Processor ) { return 0; } // Check parameters if ( !buf || count <= 0 ) { return 0; } // The number of chars copied streamsize done = 0; // Loop until all data are not read yet for (;;) { // Get the number of chars to write in this iteration size_t block_size = min(size_t(count-done), size_t(egptr()-gptr())); // Copy them if ( block_size ) { memcpy(buf + done, gptr(), block_size); done += block_size; // Update get pointers. // Satisfy "usual backup condition", see standard: 27.5.2.4.3.13 if ( block_size == egptr() - gptr() ) { *m_Reader->m_OutBuf = buf[done - 1]; setg(m_Reader->m_OutBuf, m_Reader->m_OutBuf + 1, m_Reader->m_OutBuf + 1); } else { // Update the read pointer gbump(block_size); } } // Process block if necessary if ( done == count || !ProcessStreamRead() ) { break; } } return done;}END_NCBI_SCOPE/* * =========================================================================== * $Log: streambuf.cpp,v $ * Revision 1000.3 2004/06/03 17:11:36 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.12 * * Revision 1.12 2004/06/02 16:15:46 ivanov * Fixed updating get pointers in the xsgetn() * * Revision 1.11 2004/05/17 21:07:25 gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 1.10 2004/05/10 11:56:08 ivanov * Added gzip file format support * * Revision 1.9 2004/01/20 20:37:35 lavr * Fix "Okay" spelling * * Revision 1.8 2003/09/25 17:51:08 dicuccio * Reordered headers to avoid compilation warning on MSVC * * Revision 1.7 2003/07/15 15:52:50 ivanov * Added correct handling end of stream state. * * Revision 1.6 2003/06/17 15:47:31 ivanov * The second Compression API redesign. Rewritten CCompressionStreambuf to use * I/O stream processors of class CCompressionStreamProcessor. * * Revision 1.5 2003/06/04 21:11:11 ivanov * Get rid of non-initialized variables in the ProcessStream[Read|Write] * * Revision 1.4 2003/06/03 20:09:16 ivanov * The Compression API redesign. Added some new classes, rewritten old. * * Revision 1.3 2003/04/15 16:51:12 ivanov * Fixed error with flushing the streambuf after it finalizaton * * Revision 1.2 2003/04/11 19:55:28 ivanov * Move streambuf.hpp from 'include/...' to 'src/...' * * Revision 1.1 2003/04/07 20:21:35 ivanov * Initial revision * * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -