source: icXML/icXML-devel/src/icxercesc/framework/XMLBuffer.cpp @ 2721

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

Fix imports in icXML modified Xerces files

File size: 3.1 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: XMLBuffer.cpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <icxercesc/framework/XMLBuffer.hpp>
27#include <icxercesc/util/XMLString.hpp>
28#include <xercesc/util/RuntimeException.hpp>
29#include <icxmlc/XMLConfig.hpp>
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33// ---------------------------------------------------------------------------
34//  XMLBuffer: Buffer management
35// ---------------------------------------------------------------------------
36
37void XMLBuffer::ensureCapacity(const XMLSize_t extraNeeded)
38{
39
40        // If we can't handle it, try doubling the buffer size.
41        XMLSize_t newCap = (fIndex + extraNeeded) * 2 + ADDITIONAL_PADDING;
42
43        // If a maximum size is set, and double the current buffer size exceeds that
44        // maximum, first check if the maximum size will accomodate the extra needed.
45        if (fFullHandler && (newCap > fFullSize))
46        {
47                // If the maximum buffer size accomodates the extra needed, resize to
48                // the maximum
49                if (fIndex + extraNeeded <= fFullSize)
50                {
51                        newCap = fFullSize;
52                }
53
54                // Otherwise, allow the registered full-handler to try to empty the buffer.
55                // If it claims success, and we can accommodate the extra needed in the buffer
56                // to be expanded, resize to the maximum
57                // Note the order of evaluation: bufferFull() has the intentional side-effect
58                // of modifying fIndex.
59                else if (fFullHandler->bufferFull(*this) && (fIndex + extraNeeded <= fFullSize))
60                {
61                        newCap = fFullSize;
62                }
63
64                // Finally, if the full-handler failed, or the buffer (of maximum size)
65                // still can't accomodate the extra needed, we must fail.
66                else
67                {
68                        ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Array_BadNewSize, fMemoryManager);
69                }
70        }
71
72        // Note the previous if block can modify newCap, so we may not need to allocate
73        // at all.
74        if (newCap > fCapacity)
75        {
76                // Allocate new buffer
77                XMLCh* newBuf = (XMLCh*) fMemoryManager->allocate((newCap + 1) * sizeof(XMLCh)); //new XMLCh[newCap+1];
78                // Copy over the old stuff
79                memcpy(newBuf, fBuffer, fIndex * sizeof(XMLCh));
80                // Clean up old buffer and store new stuff
81                fMemoryManager->deallocate(fBuffer); //delete [] fBuffer;
82                fBuffer = newBuf;
83                fCapacity = newCap;
84        }
85}
86
87XERCES_CPP_NAMESPACE_END
88
Note: See TracBrowser for help on using the repository browser.