source: icXML/icXML-devel/src/xercesc/internal/BinMemOutputStream.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 3.2 KB
Line 
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: BinMemOutputStream.cpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22#include <xercesc/internal/BinMemOutputStream.hpp>
23#include <icxercesc/util/XMLString.hpp>
24#include <string.h>
25
26XERCES_CPP_NAMESPACE_BEGIN
27
28BinMemOutputStream::BinMemOutputStream( 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    );
40
41    // Keep it null terminated
42    fDataBuf[0] = XMLByte(0);
43}
44
45BinMemOutputStream::~BinMemOutputStream()
46{
47    fMemoryManager->deallocate(fDataBuf);
48}
49
50void BinMemOutputStream::writeBytes( const XMLByte*     const      toGo
51                                   , const XMLSize_t               maxToWrite)
52{
53
54    if (maxToWrite) 
55    {
56        ensureCapacity(maxToWrite);
57        memcpy(&fDataBuf[fIndex], toGo, maxToWrite * sizeof(XMLByte));
58        fIndex += maxToWrite;
59    }
60
61}
62
63const XMLByte* BinMemOutputStream::getRawBuffer() const
64{
65    fDataBuf[fIndex] = 0;
66    fDataBuf[fIndex + 1] = 0;
67    fDataBuf[fIndex + 2] = 0;
68    fDataBuf[fIndex + 3] = 0;
69    return fDataBuf;
70}
71
72void BinMemOutputStream::reset()
73{
74    fIndex = 0;
75    for (int i = 0; i < 4; i++)
76    {
77        fDataBuf[fIndex + i] = 0;
78    }
79}
80
81XMLFilePos BinMemOutputStream::curPos() const
82{
83    return fIndex;
84}
85
86XMLFilePos BinMemOutputStream::getSize() const
87{
88    return fCapacity;
89}
90
91// ---------------------------------------------------------------------------
92//  BinMemOutputStream: Private helper methods
93// ---------------------------------------------------------------------------
94void BinMemOutputStream::ensureCapacity(const XMLSize_t extraNeeded)
95{
96    // If we can handle it, do nothing yet
97    if (fIndex + extraNeeded < fCapacity)
98        return;
99
100    // Oops, not enough room. Calc new capacity and allocate new buffer
101    const XMLSize_t newCap = ((fIndex + extraNeeded) * 2);
102    XMLByte* newBuf = (XMLByte*) fMemoryManager->allocate
103    (
104        (newCap+4) * sizeof(XMLByte)
105    );
106
107    memset(newBuf, 0, (newCap+4) * sizeof(XMLByte));
108
109    // Copy over the old stuff
110    memcpy(newBuf, fDataBuf, fCapacity * sizeof(XMLByte) + 4);
111
112    // Clean up old buffer and store new stuff
113    fMemoryManager->deallocate(fDataBuf); 
114    fDataBuf = newBuf;
115    fCapacity = newCap;
116}
117
118
119XERCES_CPP_NAMESPACE_END
120
Note: See TracBrowser for help on using the repository browser.