00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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) {
00077 m_Processor->End();
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
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
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
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
00125 if ( m_Stream && m_Ownership & fOwnStream ) {
00126 #if defined(NCBI_COMPILER_GCC) && NCBI_COMPILER_VERSION < 300
00127
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
00176
00177
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