1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id: MemBufFormatTarget.cpp 932887 2010-04-11 13:04:59Z borisk $
20  */
21 
22 #include <xercesc/framework/MemBufFormatTarget.hpp>
23 #include <xercesc/util/XMLString.hpp>
24 #include <string.h>
25 
26 XERCES_CPP_NAMESPACE_BEGIN
27 
MemBufFormatTarget(XMLSize_t initCapacity,MemoryManager * const manager)28 MemBufFormatTarget::MemBufFormatTarget( XMLSize_t            initCapacity
29                                       , MemoryManager* const manager)
30     : fMemoryManager(manager)
31     , fDataBuf(0)
32     , fIndex(0)
33     , fCapacity(initCapacity)
34 {
35     // Buffer is one larger than capacity, to allow for zero term
36     fDataBuf = (XMLByte*) fMemoryManager->allocate
37     (
38         (fCapacity + 4) * sizeof(XMLByte)
39     );//new XMLByte[fCapacity+4];
40 
41     // Keep it null terminated
42     fDataBuf[0] = XMLByte(0);
43 }
44 
~MemBufFormatTarget()45 MemBufFormatTarget::~MemBufFormatTarget()
46 {
47     fMemoryManager->deallocate(fDataBuf);//delete [] fDataBuf;
48 }
49 
writeChars(const XMLByte * const toWrite,const XMLSize_t count,XMLFormatter * const)50 void MemBufFormatTarget::writeChars(const XMLByte* const toWrite
51                                   , const XMLSize_t      count
52                                   , XMLFormatter * const)
53 {
54 
55     if (count)
56     {
57       if (fIndex + count >= fCapacity)
58         ensureCapacity(count);
59 
60       memcpy(&fDataBuf[fIndex], toWrite, count * sizeof(XMLByte));
61       fIndex += count;
62     }
63 
64 }
65 
getRawBuffer() const66 const XMLByte* MemBufFormatTarget::getRawBuffer() const
67 {
68     fDataBuf[fIndex] = 0;
69     fDataBuf[fIndex + 1] = 0;
70     fDataBuf[fIndex + 2] = 0;
71     fDataBuf[fIndex + 3] = 0;
72     return fDataBuf;
73 }
74 
reset()75 void MemBufFormatTarget::reset()
76 {
77     fIndex = 0;
78     fDataBuf[0] = 0;
79     fDataBuf[fIndex + 1] = 0;
80     fDataBuf[fIndex + 2] = 0;
81     fDataBuf[fIndex + 3] = 0;
82 }
83 
84 // ---------------------------------------------------------------------------
85 //  MemBufFormatTarget: Private helper methods
86 // ---------------------------------------------------------------------------
ensureCapacity(const XMLSize_t extraNeeded)87 void MemBufFormatTarget::ensureCapacity(const XMLSize_t extraNeeded)
88 {
89     // Oops, not enough room. Calc new capacity and allocate new buffer
90     const XMLSize_t newCap = ((fIndex + extraNeeded) * 2);
91     XMLByte* newBuf = (XMLByte*) fMemoryManager->allocate
92     (
93         (newCap+4) * sizeof(XMLByte)
94     );//new XMLByte[newCap+4];
95 
96     // Copy over the old stuff
97     memcpy(newBuf, fDataBuf, fIndex * sizeof(XMLByte));
98 
99     // Clean up old buffer and store new stuff
100     fMemoryManager->deallocate(fDataBuf); //delete [] fDataBuf;
101     fDataBuf = newBuf;
102     fCapacity = newCap;
103 }
104 
105 XERCES_CPP_NAMESPACE_END
106