GME  13
MemBufFormatTarget.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: MemBufFormatTarget.cpp 932887 2010-04-11 13:04:59Z borisk $
00020  */
00021 
00022 #include <xercesc/framework/MemBufFormatTarget.hpp>
00023 #include <xercesc/util/XMLString.hpp>
00024 #include <string.h>
00025 
00026 XERCES_CPP_NAMESPACE_BEGIN
00027 
00028 MemBufFormatTarget::MemBufFormatTarget( XMLSize_t            initCapacity
00029                                       , MemoryManager* const manager)
00030     : fMemoryManager(manager)
00031     , fDataBuf(0)
00032     , fIndex(0)
00033     , fCapacity(initCapacity)
00034 {
00035     // Buffer is one larger than capacity, to allow for zero term
00036     fDataBuf = (XMLByte*) fMemoryManager->allocate
00037     (
00038         (fCapacity + 4) * sizeof(XMLByte)
00039     );//new XMLByte[fCapacity+4];
00040 
00041     // Keep it null terminated
00042     fDataBuf[0] = XMLByte(0);
00043 }
00044 
00045 MemBufFormatTarget::~MemBufFormatTarget()
00046 {
00047     fMemoryManager->deallocate(fDataBuf);//delete [] fDataBuf;
00048 }
00049 
00050 void MemBufFormatTarget::writeChars(const XMLByte* const toWrite
00051                                   , const XMLSize_t      count
00052                                   , XMLFormatter * const)
00053 {
00054 
00055     if (count)
00056     {
00057       if (fIndex + count >= fCapacity)
00058         ensureCapacity(count);
00059 
00060       memcpy(&fDataBuf[fIndex], toWrite, count * sizeof(XMLByte));
00061       fIndex += count;
00062     }
00063 
00064 }
00065 
00066 const XMLByte* MemBufFormatTarget::getRawBuffer() const
00067 {
00068     fDataBuf[fIndex] = 0;
00069     fDataBuf[fIndex + 1] = 0;
00070     fDataBuf[fIndex + 2] = 0;
00071     fDataBuf[fIndex + 3] = 0;
00072     return fDataBuf;
00073 }
00074 
00075 void MemBufFormatTarget::reset()
00076 {
00077     fIndex = 0;
00078     fDataBuf[0] = 0;
00079     fDataBuf[fIndex + 1] = 0;
00080     fDataBuf[fIndex + 2] = 0;
00081     fDataBuf[fIndex + 3] = 0;
00082 }
00083 
00084 // ---------------------------------------------------------------------------
00085 //  MemBufFormatTarget: Private helper methods
00086 // ---------------------------------------------------------------------------
00087 void MemBufFormatTarget::ensureCapacity(const XMLSize_t extraNeeded)
00088 {
00089     // Oops, not enough room. Calc new capacity and allocate new buffer
00090     const XMLSize_t newCap = ((fIndex + extraNeeded) * 2);
00091     XMLByte* newBuf = (XMLByte*) fMemoryManager->allocate
00092     (
00093         (newCap+4) * sizeof(XMLByte)
00094     );//new XMLByte[newCap+4];
00095 
00096     // Copy over the old stuff
00097     memcpy(newBuf, fDataBuf, fIndex * sizeof(XMLByte));
00098 
00099     // Clean up old buffer and store new stuff
00100     fMemoryManager->deallocate(fDataBuf); //delete [] fDataBuf;
00101     fDataBuf = newBuf;
00102     fCapacity = newCap;
00103 }
00104 
00105 XERCES_CPP_NAMESPACE_END