[188] | 1 | /*
|
---|
| 2 | * The Apache Software License, Version 1.1
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
|
---|
| 5 | * reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | *
|
---|
| 14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in
|
---|
| 16 | * the documentation and/or other materials provided with the
|
---|
| 17 | * distribution.
|
---|
| 18 | *
|
---|
| 19 | * 3. The end-user documentation included with the redistribution,
|
---|
| 20 | * if any, must include the following acknowledgment:
|
---|
| 21 | * "This product includes software developed by the
|
---|
| 22 | * Apache Software Foundation (http://www.apache.org/)."
|
---|
| 23 | * Alternately, this acknowledgment may appear in the software itself,
|
---|
| 24 | * if and wherever such third-party acknowledgments normally appear.
|
---|
| 25 | *
|
---|
| 26 | * 4. The names "Xerces" and "Apache Software Foundation" must
|
---|
| 27 | * not be used to endorse or promote products derived from this
|
---|
| 28 | * software without prior written permission. For written
|
---|
| 29 | * permission, please contact apache\@apache.org.
|
---|
| 30 | *
|
---|
| 31 | * 5. Products derived from this software may not be called "Apache",
|
---|
| 32 | * nor may "Apache" appear in their name, without prior written
|
---|
| 33 | * permission of the Apache Software Foundation.
|
---|
| 34 | *
|
---|
| 35 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
---|
| 36 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 37 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 38 | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
---|
| 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 41 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
---|
| 42 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 43 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
| 44 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
| 45 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 46 | * SUCH DAMAGE.
|
---|
| 47 | * ====================================================================
|
---|
| 48 | *
|
---|
| 49 | * This software consists of voluntary contributions made by many
|
---|
| 50 | * individuals on behalf of the Apache Software Foundation, and was
|
---|
| 51 | * originally based on software copyright (c) 1999, International
|
---|
| 52 | * Business Machines, Inc., http://www.ibm.com . For more information
|
---|
| 53 | * on the Apache Software Foundation, please see
|
---|
| 54 | * <http://www.apache.org/>.
|
---|
| 55 | */
|
---|
| 56 |
|
---|
| 57 | /*
|
---|
| 58 | * $Log: XMLBuffer.hpp,v $
|
---|
| 59 | * Revision 1.6 2004/01/29 11:46:29 cargilld
|
---|
| 60 | * Code cleanup changes to get rid of various compiler diagnostic messages.
|
---|
| 61 | *
|
---|
| 62 | * Revision 1.5 2003/05/16 21:36:55 knoaman
|
---|
| 63 | * Memory manager implementation: Modify constructors to pass in the memory manager.
|
---|
| 64 | *
|
---|
| 65 | * Revision 1.4 2003/05/15 18:26:07 knoaman
|
---|
| 66 | * Partial implementation of the configurable memory manager.
|
---|
| 67 | *
|
---|
| 68 | * Revision 1.3 2002/11/04 15:00:21 tng
|
---|
| 69 | * C++ Namespace Support.
|
---|
| 70 | *
|
---|
| 71 | * Revision 1.2 2002/08/21 18:54:52 tng
|
---|
| 72 | * [Bug 11869] Add the const modifier (XMLBuffer.hpp).
|
---|
| 73 | *
|
---|
| 74 | * Revision 1.1.1.1 2002/02/01 22:21:51 peiyongz
|
---|
| 75 | * sane_include
|
---|
| 76 | *
|
---|
| 77 | * Revision 1.6 2001/06/27 20:29:09 tng
|
---|
| 78 | * [Bug 2365] Huge performance problem with the parser in XMLScanner::sendCharData() . By David Bertoni.
|
---|
| 79 | *
|
---|
| 80 | * Revision 1.5 2000/03/02 19:54:24 roddey
|
---|
| 81 | * This checkin includes many changes done while waiting for the
|
---|
| 82 | * 1.1.0 code to be finished. I can't list them all here, but a list is
|
---|
| 83 | * available elsewhere.
|
---|
| 84 | *
|
---|
| 85 | * Revision 1.4 2000/02/24 20:00:22 abagchi
|
---|
| 86 | * Swat for removing Log from API docs
|
---|
| 87 | *
|
---|
| 88 | * Revision 1.3 2000/02/15 01:21:30 roddey
|
---|
| 89 | * Some initial documentation improvements. More to come...
|
---|
| 90 | *
|
---|
| 91 | * Revision 1.2 2000/02/06 07:47:47 rahulj
|
---|
| 92 | * Year 2K copyright swat.
|
---|
| 93 | *
|
---|
| 94 | * Revision 1.1.1.1 1999/11/09 01:08:29 twl
|
---|
| 95 | * Initial checkin
|
---|
| 96 | *
|
---|
| 97 | * Revision 1.2 1999/11/08 20:44:36 rahul
|
---|
| 98 | * Swat for adding in Product name and CVS comment log variable.
|
---|
| 99 | *
|
---|
| 100 | */
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | #if !defined(XMLBUFFER_HPP)
|
---|
| 104 | #define XMLBUFFER_HPP
|
---|
| 105 |
|
---|
| 106 | #include <xercesc/util/XMemory.hpp>
|
---|
| 107 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
| 108 | #include <xercesc/framework/MemoryManager.hpp>
|
---|
| 109 |
|
---|
| 110 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * XMLBuffer is a lightweight, expandable Unicode text buffer. Since XML is
|
---|
| 114 | * inherently theoretically unbounded in terms of the sizes of things, we
|
---|
| 115 | * very often need to have expandable buffers. The primary concern here is
|
---|
| 116 | * that appends of characters and other buffers or strings be very fast, so
|
---|
| 117 | * it always maintains the current buffer size.
|
---|
| 118 | *
|
---|
| 119 | * The buffer is not nul terminated until some asks to see the raw buffer
|
---|
| 120 | * contents. This also avoids overhead during append operations.
|
---|
| 121 | */
|
---|
| 122 | class XMLPARSER_EXPORT XMLBuffer : public XMemory
|
---|
| 123 | {
|
---|
| 124 | public :
|
---|
| 125 | // -----------------------------------------------------------------------
|
---|
| 126 | // Constructors and Destructor
|
---|
| 127 | // -----------------------------------------------------------------------
|
---|
| 128 |
|
---|
| 129 | /** @name Constructor */
|
---|
| 130 | //@{
|
---|
| 131 | XMLBuffer(int capacity = 1023
|
---|
| 132 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) :
|
---|
| 133 |
|
---|
| 134 | fUsed(false)
|
---|
| 135 | , fIndex(0)
|
---|
| 136 | , fCapacity(capacity)
|
---|
| 137 | , fMemoryManager(manager)
|
---|
| 138 | , fBuffer(0)
|
---|
| 139 | {
|
---|
| 140 | // Buffer is one larger than capacity, to allow for zero term
|
---|
| 141 | fBuffer = (XMLCh*) manager->allocate((capacity+1) * sizeof(XMLCh)); //new XMLCh[fCapacity+1];
|
---|
| 142 |
|
---|
| 143 | // Keep it null terminated
|
---|
| 144 | fBuffer[0] = XMLCh(0);
|
---|
| 145 | }
|
---|
| 146 | //@}
|
---|
| 147 |
|
---|
| 148 | /** @name Destructor */
|
---|
| 149 | //@{
|
---|
| 150 | ~XMLBuffer()
|
---|
| 151 | {
|
---|
| 152 | fMemoryManager->deallocate(fBuffer); //delete [] fBuffer;
|
---|
| 153 | }
|
---|
| 154 | //@}
|
---|
| 155 |
|
---|
| 156 | // -----------------------------------------------------------------------
|
---|
| 157 | // Buffer Management
|
---|
| 158 | // -----------------------------------------------------------------------
|
---|
| 159 | void append(const XMLCh toAppend)
|
---|
| 160 | {
|
---|
| 161 | if (fIndex == fCapacity)
|
---|
| 162 | expand();
|
---|
| 163 |
|
---|
| 164 | // Put in char and bump the index
|
---|
| 165 | fBuffer[fIndex++] = toAppend;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | void append
|
---|
| 169 | (
|
---|
| 170 | const XMLCh* const chars
|
---|
| 171 | , const unsigned int count = 0
|
---|
| 172 | );
|
---|
| 173 |
|
---|
| 174 | const XMLCh* getRawBuffer() const
|
---|
| 175 | {
|
---|
| 176 | fBuffer[fIndex] = 0;
|
---|
| 177 | return fBuffer;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | XMLCh* getRawBuffer()
|
---|
| 181 | {
|
---|
| 182 | fBuffer[fIndex] = 0;
|
---|
| 183 | return fBuffer;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | void reset()
|
---|
| 187 | {
|
---|
| 188 | fIndex = 0;
|
---|
| 189 | fBuffer[0] = 0;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | void set
|
---|
| 193 | (
|
---|
| 194 | const XMLCh* const chars
|
---|
| 195 | , const unsigned int count = 0
|
---|
| 196 | );
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | // -----------------------------------------------------------------------
|
---|
| 200 | // Getters
|
---|
| 201 | // -----------------------------------------------------------------------
|
---|
| 202 | bool getInUse() const
|
---|
| 203 | {
|
---|
| 204 | return fUsed;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | unsigned int getLen() const
|
---|
| 208 | {
|
---|
| 209 | return fIndex;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | bool isEmpty() const
|
---|
| 213 | {
|
---|
| 214 | return (fIndex == 0);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | // -----------------------------------------------------------------------
|
---|
| 219 | // Setters
|
---|
| 220 | // -----------------------------------------------------------------------
|
---|
| 221 | void setInUse(const bool newValue)
|
---|
| 222 | {
|
---|
| 223 | fUsed = newValue;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | private :
|
---|
| 228 | // -----------------------------------------------------------------------
|
---|
| 229 | // Unimplemented constructors and operators
|
---|
| 230 | // -----------------------------------------------------------------------
|
---|
| 231 | XMLBuffer(const XMLBuffer&);
|
---|
| 232 | XMLBuffer& operator=(const XMLBuffer&);
|
---|
| 233 |
|
---|
| 234 | // -----------------------------------------------------------------------
|
---|
| 235 | // Declare our friends
|
---|
| 236 | // -----------------------------------------------------------------------
|
---|
| 237 | friend class XMLBufBid;
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | // -----------------------------------------------------------------------
|
---|
| 241 | // Private helpers
|
---|
| 242 | // -----------------------------------------------------------------------
|
---|
| 243 | void expand();
|
---|
| 244 | void insureCapacity(const unsigned int extraNeeded);
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 | // -----------------------------------------------------------------------
|
---|
| 248 | // Private data members
|
---|
| 249 | //
|
---|
| 250 | // fBuffer
|
---|
| 251 | // The pointer to the buffer data. Its grown as needed. Its always
|
---|
| 252 | // one larger than fCapacity, to leave room for the null terminator.
|
---|
| 253 | //
|
---|
| 254 | // fIndex
|
---|
| 255 | // The current index into the buffer, as characters are appended
|
---|
| 256 | // to it. If its zero, then the buffer is empty.
|
---|
| 257 | //
|
---|
| 258 | // fCapacity
|
---|
| 259 | // The current capacity of the buffer. Its actually always one
|
---|
| 260 | // larger, to leave room for the null terminator.
|
---|
| 261 | //
|
---|
| 262 | // fUsed
|
---|
| 263 | // Indicates whether this buffer is in use or not.
|
---|
| 264 | // -----------------------------------------------------------------------
|
---|
| 265 | bool fUsed;
|
---|
| 266 | unsigned int fIndex;
|
---|
| 267 | unsigned int fCapacity;
|
---|
| 268 | MemoryManager* fMemoryManager;
|
---|
| 269 | XMLCh* fBuffer;
|
---|
| 270 | };
|
---|
| 271 |
|
---|
| 272 | XERCES_CPP_NAMESPACE_END
|
---|
| 273 |
|
---|
| 274 | #endif
|
---|