src/util/compress/stream.cpp

Go to the documentation of this file.
00001 /*  $Id: stream.cpp 149145 2009-01-07 19:24:11Z ivanov $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors:  Vladimir Ivanov
00027  *
00028  * File Description:  CCompression based C++ I/O streams
00029  *
00030  */
00031 
00032 #include <ncbi_pch.hpp>
00033 #include "streambuf.hpp"
00034 #include <memory>
00035 
00036 
00037 BEGIN_NCBI_SCOPE
00038 
00039 
00040 
00041 //////////////////////////////////////////////////////////////////////////////
00042 //
00043 // CCompressionStreamProcessor
00044 //
00045 
00046 CCompressionStreamProcessor::CCompressionStreamProcessor(
00047     CCompressionProcessor*  processor,
00048     EDeleteProcessor        need_delete,
00049     streamsize              in_bufsize,
00050     streamsize              out_bufsize)
00051 
00052     : m_Processor(processor), 
00053       m_InBufSize(in_bufsize <= 1 ? kCompressionDefaultBufSize : in_bufsize),
00054       m_OutBufSize(out_bufsize <= 1 ? kCompressionDefaultBufSize :out_bufsize),
00055       m_NeedDelete(need_delete), m_State(eDone)
00056 {
00057     Init();
00058     return;
00059 }
00060 
00061 
00062 CCompressionStreamProcessor::~CCompressionStreamProcessor(void)
00063 {
00064     if ( m_Processor  &&  m_NeedDelete == eDelete ) {
00065         delete m_Processor;
00066     }
00067     m_Processor = 0;
00068 }
00069 
00070 
00071 void CCompressionStreamProcessor::Init(void)
00072 {
00073     if ( m_Processor ) {
00074         if ( m_State == eDone ) {
00075             m_Processor->Init();
00076         } else if (m_InBuf != 0) { // reinitializing
00077             m_Processor->End();    // avoid leaking memory
00078             m_Processor->Init();
00079         }
00080     }
00081     m_InBuf         = 0;
00082     m_OutBuf        = 0;
00083     m_Begin         = 0;
00084     m_End           = 0;
00085     m_LastStatus    = CCompressionProcessor::eStatus_Success;
00086     m_State         = eActive;
00087 }
00088 
00089 
00090 //////////////////////////////////////////////////////////////////////////////
00091 //
00092 // CCompressionStream
00093 //
00094 
00095 CCompressionStream::CCompressionStream(CNcbiIos&                    stream,
00096                                        CCompressionStreamProcessor* read_sp,
00097                                        CCompressionStreamProcessor* write_sp,
00098                                        TOwnership                   ownership)
00099     : CNcbiIos(0), m_Stream(&stream), m_StreamBuf(0),
00100       m_Reader(read_sp), m_Writer(write_sp), m_Ownership(ownership)
00101 {
00102     // Create a new stream buffer
00103     auto_ptr<CCompressionStreambuf> sb(
00104         new CCompressionStreambuf(&stream, read_sp, write_sp));
00105     init(sb.get());
00106     m_StreamBuf = sb.release();
00107     if ( !m_StreamBuf->IsOkay() ) {
00108         setstate(badbit | eofbit);
00109     }
00110 }
00111 
00112 
00113 CCompressionStream::~CCompressionStream(void)
00114 {
00115     // Delete stream buffer
00116     streambuf* sb = rdbuf();
00117     delete sb;
00118     if ( sb != m_StreamBuf ) {
00119         delete m_StreamBuf;
00120     }
00121 #ifdef AUTOMATIC_STREAMBUF_DESTRUCTION
00122     rdbuf(0);
00123 #endif
00124     // Delete owned objects
00125     if ( m_Stream   &&   m_Ownership & fOwnStream ) {
00126 #if defined(NCBI_COMPILER_GCC)  &&  NCBI_COMPILER_VERSION < 300
00127         // On GCC 2.9x ios::~ios() is protected
00128 #else
00129         delete m_Stream;
00130         m_Stream = 0;
00131 #endif
00132     }
00133     if ( m_Reader  &&  m_Ownership & fOwnReader ) {
00134         if ( m_Reader == m_Writer  &&  m_Ownership & fOwnWriter ) {
00135             m_Writer = 0;
00136         }
00137         delete m_Reader;
00138         m_Reader = 0;
00139     }
00140     if ( m_Writer  &&  m_Ownership & fOwnWriter ) {
00141         delete m_Writer;
00142         m_Writer = 0;
00143     }
00144 }
00145 
00146 
00147 void CCompressionStream::Finalize(CCompressionStream::EDirection dir) 
00148 {
00149     if ( m_StreamBuf ) {
00150         m_StreamBuf->Finalize(dir);
00151     }
00152 }
00153 
00154 
00155 CCompressionProcessor::EStatus 
00156 CCompressionStream::x_GetStatus(CCompressionStream::EDirection dir)
00157 {
00158     CCompressionStreamProcessor* sp = (dir == eRead) ? m_Reader : m_Writer;
00159     if ( !sp ) {
00160         return CCompressionProcessor::eStatus_Unknown;
00161     }
00162     return sp->m_LastStatus;
00163 }
00164 
00165 
00166 bool CCompressionStream::x_GetError(CCompressionStream::EDirection dir,
00167                                     int& status, string& description)
00168 {
00169     CCompressionStreamProcessor* sp = (dir == eRead) ? m_Reader : m_Writer;
00170     status = 0;
00171     description.clear();
00172     if (!sp  ||  !sp->m_Processor) {
00173         return false;
00174     }
00175     // We pass CCompression-CCompressionStreamProcessor derived object
00176     // to compression stream as 'stream processor', at least internally,
00177     // so try to get error code from CCompression.
00178     CCompression* cmp = dynamic_cast<CCompression*>(sp->m_Processor);
00179     if (!cmp) {
00180         return false;
00181     }
00182     status = cmp->GetErrorCode();
00183     description = cmp->GetErrorDescription();
00184     return true;
00185 }
00186 
00187 
00188 unsigned long CCompressionStream::x_GetProcessedSize(
00189                                   CCompressionStream::EDirection dir)
00190 {
00191     CCompressionStreamProcessor* sp = (dir == eRead) ? m_Reader : m_Writer;
00192     if (!sp  ||  !sp->m_Processor) {
00193         return 0;
00194     }
00195     return sp->m_Processor->GetProcessedSize();
00196 }
00197 
00198 
00199 unsigned long CCompressionStream::x_GetOutputSize(
00200                                   CCompressionStream::EDirection dir)
00201 {
00202     CCompressionStreamProcessor* sp = (dir == eRead) ? m_Reader : m_Writer;
00203     if (!sp  ||  !sp->m_Processor) {
00204         return 0;
00205     }
00206     return sp->m_Processor->GetOutputSize();
00207 }
00208 
00209 
00210 END_NCBI_SCOPE
00211 
00212 

Generated on Sun Dec 6 22:44:00 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:21:14 2009 by modify_doxy.py rev. 173732