00001
00002
00003
00004
00005
00006
00007
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 # include "zipbuffer.hpp"
00041
00042 BEGIN_XDFLENGINE_NS
00043
00044
00045
00046
00047
00048
00049
00050
00051 void ZipBuffer::setTmpDir( const char* p_pszTmpDir)
00052 {
00053 gs_pszZipBufferTmpDir = p_pszTmpDir;
00054 }
00055
00056
00057
00058
00059
00060 ZipBuffer::ZipBuffer(const char* p_cFileName, bool p_boolReadOnly, unsigned int p_uiBufferSize)
00061 {
00062 m_boolReadOnly=p_boolReadOnly;
00063 m_bOwnFile=false;
00064 m_bOpened=false;
00065 m_uiBufferSize=p_uiBufferSize;
00066 if( m_uiBufferSize == 0 ) m_uiBufferSize = ZIPBUFFER_COPYBUF;
00067
00068 setMode("ab");
00069 m_pOut=0;
00070
00071 if( p_cFileName) strcpy( (char*) &m_cFN, p_cFileName);
00072 else
00073 {
00074
00075 strcpy((char*) &m_cFN,gs_pszZipBufferTmpDir);
00076 tmpnam((char*) &m_cFN+strlen(gs_pszZipBufferTmpDir));
00077 m_bOwnFile=true;
00078 flush();
00079 }
00080
00081 DEBUG_CREATE(ZipBuffer)
00082 }
00083
00084
00085
00086
00087 ZipBuffer::~ZipBuffer()
00088 {
00089 DEBUG_DEL(ZipBuffer)
00090
00091 DEBUG_ECHO << "delete buffer " << m_cFN << "\n";
00092
00093 closefile();
00094 if(m_bOwnFile && !m_boolReadOnly) delFile();
00095
00096 m_boolReadOnly=false;
00097 m_bOwnFile=false;
00098 m_bOpened=false;
00099 m_pOut=0;
00100 }
00101
00102
00103
00104
00105
00106 XMLStreamProvider & ZipBuffer::operator>> ( XMLStreamConsumer& p_pStreamConsumer)
00107 {
00108
00109 void* l_cBuf=0;
00110 int l_intRead;
00111 int l_intTotRead;
00112 unsigned int l_written;
00113 int l_intError;
00114 const char* l_cError=0;
00115
00116
00117
00118 DEBUG_IN(ZipBuffer::>>(XMLStreamBuffer))
00119 PREP_CATCH_XML_FLOW_ERROR;
00120
00121 WATCH_XML_FLOW_ERROR
00122 {
00123 setMode("rb");
00124 openfile();
00125
00126
00127 if( gzrewind(m_pOut) == -1) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE , "Unable rewind gz file.", m_cFN, "ZipBuffer::operator>>", "", false );
00128
00129
00130 l_cBuf = malloc(m_uiBufferSize+1);
00131
00132 l_intRead=-1;
00133 l_intTotRead=0;
00134 l_written=0;
00135
00136 while( !( gzeof(m_pOut) || (l_intRead==0) ) )
00137 {
00138
00139 logout << "\t" << (char*) &m_cFN << " : " << "read " << l_intRead << " bytes from compressed file.\n";
00140 l_intRead = gzread(m_pOut, l_cBuf, m_uiBufferSize);
00141
00142
00143 if( l_intRead == -1) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE ,"Unable read data from gz file." ,m_cFN, "ZipBuffer::operator>>", "", false );
00144
00145 if(l_intRead > 0)
00146 {
00147 l_intTotRead+=l_intRead;
00148 l_written+=l_intRead;
00149
00150
00151 p_pStreamConsumer.writeData((const char*)l_cBuf,l_intRead);
00152 p_pStreamConsumer.commitStream(false);
00153 }
00154 l_intError=0;
00155 l_cError = gzerror(m_pOut, &l_intError);
00156 }
00157 closefile();
00158 }
00159 CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN;
00160
00161 RELEASE_AND_RETURN:
00162
00163
00164 free( l_cBuf);
00165
00166 DEBUG_OUT(ZipBuffer::>>(XMLStreamBuffer))
00167 ON_XML_FLOW_ERROR_THROW;
00168
00169 return *this;
00170 }
00171
00172
00173
00174
00175
00176 void ZipBuffer::flush ()
00177 {
00178 DEBUG_FUNC(ZipBuffer::flush)
00179
00180 if (!m_boolReadOnly)
00181 {
00182 logout << "\t" << (char*) &m_cFN << " : " << "flush compressed file.\n";
00183
00184 closefile();
00185 delFile();
00186 setMode("ab");
00187 }
00188 }
00189
00190
00191
00192
00193
00194 void ZipBuffer::writeData( const char* p_pszData, unsigned int p_uiDataLen)
00195 {
00196
00197 int l_intwritten;
00198 int l_intError;
00199
00200
00201 if (!m_boolReadOnly && (p_uiDataLen>0) )
00202 {
00203 setMode("ab");
00204 openfile();
00205
00206
00207 l_intwritten=gzwrite( m_pOut, (void*) p_pszData, p_uiDataLen );
00208 if( l_intwritten == 0) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE , "Unable to write data to gz file." , m_cFN, "ZipBuffer::write", "", false );
00209
00210 m_commitwritten+=l_intwritten;
00211
00212 l_intError=0;
00213 gzerror(m_pOut, &l_intError);
00214 if (l_intError) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE , "Unable to write data to gz file.",m_cFN, "ZipBuffer::write", "", false );
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223 bool ZipBuffer::commitStream(bool fFinal)
00224 {
00225 if (m_bOpened && !m_boolReadOnly && m_commitwritten>0)
00226 {
00227 logout << "\t" << (char*) &m_cFN << " : " << "write " << m_commitwritten << " bytes to compressed file.\n";
00228
00229 if( gzflush(m_pOut,Z_SYNC_FLUSH ) !=0 ) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE , "Unable to flush data to gz file.", m_cFN, "ZipBuffer::write", "", false );
00230 if( fFinal) closefile();
00231 }
00232 m_commitwritten=0;
00233
00234 return true;
00235 }
00236
00237
00238
00239
00240
00241
00242 void ZipBuffer::setMode( const char* p_cMode)
00243 {
00244 if(strcmp(p_cMode,m_cMode)!=0)
00245 {
00246 closefile();
00247 strcpy( m_cMode, p_cMode);
00248 }
00249 }
00250
00251
00252
00253
00254 void ZipBuffer::openfile()
00255 {
00256 if( !m_bOpened)
00257 {
00258
00259 try{
00260
00261 logout << "\t" << (char*) &m_cFN << " : " << "open compressed file.\n";
00262 if( ! m_boolReadOnly) m_pOut=gzopen( m_cFN, m_cMode);
00263 else m_pOut=gzopen( m_cFN, "rb");
00264 m_bOpened=true;
00265 }
00266 catch(...){}
00267
00268 if( !m_bOpened) m_pOut=0;
00269 if( !m_pOut) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE , "Unable to open file." , m_cFN, "ZipBuffer::openfile", "", false );
00270
00271 m_commitwritten=0;
00272 }
00273 }
00274
00275
00276
00277
00278 void ZipBuffer::closefile()
00279 {
00280
00281 if( m_bOpened && (m_pOut !=0 ) )
00282 {
00283 logout << "\t" << (char*) &m_cFN << " : " << "close compressed file.\n";
00284 if( gzclose(m_pOut) !=0 ) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_ZIPBUFFER + ERRCODE_CAUSE_FILE , "Unable to close file.", m_cFN, "ZipBuffer::closefile", "", false );
00285 m_bOpened=false;
00286 }
00287 }
00288
00289
00290
00291
00292 void ZipBuffer::delFile()
00293 {
00294 if( strlen((const char*) &m_cFN)>0)
00295 {
00296 closefile();
00297 logout << "\t" << (char*) &m_cFN << " : " << "delete compressed file.\n";
00298 remove( (const char*) &m_cFN);
00299 }
00300 }
00301
00302
00303
00304
00305 END_XDFLENGINE_NS
00306
00307