Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

zipbuffer.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //-----------------------------------------------------------------------------
00006 //  ZIPBUFFER.CPP
00007 //----------------------------------------------------------------------------- 
00013 //_____________________________________________________________________________
00014 //
00015 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00016 //
00017 //  This file is part of the XDFLengine project.
00018 //
00019 //  The XDFLengine is free software; you can redistribute it and/or modify
00020 //  it under the terms of the GNU General Public License as published by
00021 //  the Free Software Foundation; either version 2 of the License, or
00022 //  (at your option) any later version.
00023 //
00024 //  This program is distributed in the hope that it will be useful,
00025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 //  GNU General Public License for more details.
00028 //
00029 //  You should have received a copy of the GNU General Public License
00030 //  along with this program; if not, write to the Free Software
00031 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00032 //  USA.
00033 //
00034 //  For more information, 
00035 //      contact  : guillaume@baurand.net 
00036 //      or visit : http://xdflengine.sourceforge.net
00037 //
00038 //============================================================================= 
00039 
00040 #   include "zipbuffer.hpp"
00041 
00042 BEGIN_XDFLENGINE_NS
00043 
00044 //============================================================================= 
00045 //  CLASS ZIPBUFFER
00046 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00047 
00048     //_____________________________________________________________________
00049     //  Settmpdir
00050     //--------------------------------------------------------------------- 
00051     void ZipBuffer::setTmpDir( const char* p_pszTmpDir)
00052     {
00053         gs_pszZipBufferTmpDir = p_pszTmpDir;
00054     }
00055 
00056 
00057     //_________________________________________________________________________
00058     //  ZIPBUFFER
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         // Set default file open mode
00068         setMode("ab");
00069         m_pOut=0;
00070 
00071         if( p_cFileName) strcpy( (char*) &m_cFN, p_cFileName);
00072         else
00073         {
00074             // Build file name
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     //  ~ZIPBUFFER
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     //  OPERATOR >>
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             // rewind file
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             // allocate copy buffer
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                 // read data from compressed file
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                 // ERRCHECK : read data
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                     // Write file content to output buffer  
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         // Release
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     //  FLUSH
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     //  writeData
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             // write data to file
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     //  COMMITSTREAM
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             // flush data
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     //  SETMODE
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     //  OPENFILE
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     //  CLOSEFILE
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     //  DELFILE
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 

Generated on Sat Oct 4 13:20:05 2003 for XDFLengine by doxygen1.3-rc2