00001 //============================================================================= 00002 // 00003 // XDFLengine library 00004 // 00005 //----------------------------------------------------------------------------- 00006 // MEMBUFFER.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 "membuffer.hpp" 00041 00042 BEGIN_XDFLENGINE_NS 00043 00044 //============================================================================= 00045 // CLASS MEMBUFFER 00046 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00047 00048 // CONSTRUCTOR & DESTRUCTOR 00049 00050 //_________________________________________________________________________ 00051 // MEMBUFFER 00052 //------------------------------------------------------------------------- 00053 MemBuffer::MemBuffer() 00054 { 00055 m_intSize=0; 00056 m_bBuf=0; 00057 m_uiBufSize=0; 00058 00059 DEBUG_CREATE(MemBuffer) 00060 } 00061 00062 //_________________________________________________________________________ 00063 // ~MEMBUFFER 00064 //------------------------------------------------------------------------- 00065 MemBuffer::~MemBuffer() 00066 { 00067 DEBUG_DEL(MemBuffer) 00068 00069 if(m_bBuf) free(m_bBuf); 00070 } 00071 00072 // XMLSTREAMPROVIDER 00073 00074 //_________________________________________________________________________ 00075 // OPERATOR >> 00076 //------------------------------------------------------------------------- 00077 XMLStreamProvider & MemBuffer::operator>> ( XMLStreamConsumer& p_pStreamConsumer) 00078 { 00079 DEBUG_FUNC(MemBuffer::>>(XMLStreamBuffer)) 00080 00081 if(length()>0) p_pStreamConsumer.writeData( (const char*) m_bBuf, length()); 00082 p_pStreamConsumer.commitStream(false); 00083 return *this; 00084 } 00085 00086 00087 // XMLSTREAMCONSUMER 00088 00089 00090 //_________________________________________________________________________ 00091 // WRITE 00092 //------------------------------------------------------------------------- 00093 void MemBuffer::writeData( const char* p_pszData, unsigned int p_uiDataLen) 00094 { 00095 unsigned int l_uiNewBufSize; 00096 00097 DEBUG_FUNC(MemBuffer::writeData) 00098 00099 if(p_uiDataLen>0) 00100 { 00101 if( (unsigned int)(m_intSize+p_uiDataLen) >= m_uiBufSize) 00102 { 00103 l_uiNewBufSize = m_uiBufSize; 00104 if(l_uiNewBufSize==0) l_uiNewBufSize=128; 00105 while( l_uiNewBufSize-m_intSize-1 < p_uiDataLen) l_uiNewBufSize = l_uiNewBufSize*2; 00106 m_uiBufSize = l_uiNewBufSize; 00107 m_bBuf = realloc( m_bBuf, m_uiBufSize+1); 00108 if( m_bBuf == NULL) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_MEMBUFFER + ERRCODE_CAUSE_MEMORY , "Unable to allocate mem buffer." , "", "MemBuffer::write", "", false ); 00109 } 00110 00111 memcpy((char*)m_bBuf + m_intSize, p_pszData, p_uiDataLen); 00112 m_intSize+=p_uiDataLen; 00113 ((char*)m_bBuf)[m_intSize]='\0'; 00114 } 00115 } 00116 00117 00118 00119 // MEMBUFFER 00120 00121 //_________________________________________________________________________ 00122 // FLUSH 00123 //------------------------------------------------------------------------- 00124 void MemBuffer::flush () 00125 { 00126 DEBUG_FUNC(MemBuffer::flush) 00127 00128 if(m_bBuf) free(m_bBuf); 00129 m_bBuf=0; 00130 m_intSize=0; 00131 m_uiBufSize=0; 00132 } 00133 00134 //_________________________________________________________________________ 00135 // LENGTH 00136 //------------------------------------------------------------------------- 00137 int MemBuffer::length() const 00138 { 00139 DEBUG_FUNC(MemBuffer::length) 00140 00141 return m_intSize; 00142 } 00143 00144 //_________________________________________________________________________ 00145 // DATA 00146 //------------------------------------------------------------------------- 00147 const char* MemBuffer::data() const 00148 { 00149 DEBUG_FUNC(MemBuffer::data); 00150 if(m_bBuf) return (const char*) m_bBuf; 00151 return 0; 00152 } 00153 00154 00155 00156 00157 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00158 //============================================================================= 00159 00160 END_XDFLENGINE_NS 00161