Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:02

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_DOMSTRINGPOOL_HPP)
0023 #define XERCESC_INCLUDE_GUARD_DOMSTRINGPOOL_HPP
0024 
0025 //
0026 //  This file is part of the internal implementation of the C++ XML DOM.
0027 //  It should NOT be included or used directly by application programs.
0028 //
0029 //  Applications should include the file <xercesc/dom/DOM.hpp> for the entire
0030 //  DOM API, or xercesc/dom/DOM*.hpp for individual DOM classes, where the class
0031 //  name is substituded for the *.
0032 //
0033 
0034 #include <xercesc/util/XercesDefs.hpp>
0035 
0036 XERCES_CPP_NAMESPACE_BEGIN
0037 
0038 class   DOMDocumentImpl;
0039 
0040 //
0041 //  DStringPoolEntry - one of these structs is allocated for each
0042 //                      XMLCh String in the pool.  Each slot in the
0043 //                      hash table array itself is a pointer to the head
0044 //                      of a singly-linked list of these structs.
0045 //
0046 //                      Although this struct is declared with a string length of one,
0047 //                      the factory method allocates enough storage to hold the full
0048 //                      string length.
0049 //
0050 struct DOMStringPoolEntry
0051 {
0052     DOMStringPoolEntry    *fNext;
0053     XMLSize_t             fLength;
0054     XMLCh                 fString[1];
0055 };
0056 
0057 //
0058 // DOMBuffer is a lightweight text buffer
0059 // The buffer is not nul terminated until some asks to see the raw buffer
0060 // contents. This also avoids overhead during append operations.
0061 class DOMBuffer
0062 {
0063 public :
0064     // -----------------------------------------------------------------------
0065     //  Constructors and Destructor
0066     // -----------------------------------------------------------------------
0067     DOMBuffer(DOMDocumentImpl *doc, XMLSize_t capacity = 31);
0068 
0069     ~DOMBuffer()
0070     {
0071     }
0072 
0073     // -----------------------------------------------------------------------
0074     //  Buffer Management
0075     // -----------------------------------------------------------------------
0076     void append (const XMLCh* const chars);
0077     void append (const XMLCh* const chars, const XMLSize_t count);
0078     void appendInPlace (const XMLCh* const chars, const XMLSize_t count);
0079 
0080     void set (const XMLCh* const chars);
0081     void set (const XMLCh* const chars, const XMLSize_t count);
0082 
0083     const XMLCh* getRawBuffer() const
0084     {
0085         fBuffer[fIndex] = 0;
0086         return fBuffer;
0087     }
0088 
0089     void reset()
0090     {
0091         fIndex = 0;
0092         fBuffer[0] = 0;
0093     }
0094 
0095     void chop
0096     (
0097         const XMLSize_t    count
0098     )
0099     {
0100         fBuffer[count] = 0;
0101         fIndex = count;
0102     }
0103 
0104 
0105     // -----------------------------------------------------------------------
0106     //  Getters
0107     // -----------------------------------------------------------------------
0108     XMLSize_t getLen() const
0109     {
0110         return fIndex;
0111     }
0112 
0113     XMLSize_t getCapacity() const
0114     {
0115         return fCapacity;
0116     }
0117 
0118     // -----------------------------------------------------------------------
0119     //  Private helpers
0120     // -----------------------------------------------------------------------
0121     void expandCapacity(const XMLSize_t extraNeeded, bool releasePrevious = false);
0122 
0123 
0124 private :
0125     // -----------------------------------------------------------------------
0126     //  Private data members
0127     //
0128     //  fBuffer
0129     //      The pointer to the buffer data. Its grown as needed. Its always
0130     //      one larger than fCapacity, to leave room for the null terminator.
0131     //
0132     //  fIndex
0133     //      The current index into the buffer, as characters are appended
0134     //      to it. If its zero, then the buffer is empty.
0135     //
0136     //  fCapacity
0137     //      The current capacity of the buffer. Its actually always one
0138     //      larger, to leave room for the null terminator.
0139     //
0140     //  fDoc
0141     //      For allocating memory
0142     // -----------------------------------------------------------------------
0143     XMLCh*           fBuffer;
0144     XMLSize_t        fIndex;
0145     XMLSize_t        fCapacity;
0146     DOMDocumentImpl* fDoc;
0147 
0148     // -----------------------------------------------------------------------
0149     // Unimplemented constructors and operators
0150     // -----------------------------------------------------------------------
0151     DOMBuffer(const DOMBuffer &);
0152     DOMBuffer & operator = (const DOMBuffer &);
0153 };
0154 
0155 inline void DOMBuffer::
0156 append (const XMLCh* const chars)
0157 {
0158   XMLSize_t count = XMLString::stringLen(chars);
0159   if (fIndex + count >= fCapacity)
0160     expandCapacity(count);
0161 
0162   memcpy(&fBuffer[fIndex], chars, count * sizeof(XMLCh));
0163   fIndex += count;
0164 
0165   // Keep it null terminated
0166   fBuffer[fIndex] = 0;
0167 }
0168 
0169 inline void DOMBuffer::
0170 append (const XMLCh* const chars, const XMLSize_t count)
0171 {
0172   if (fIndex + count >= fCapacity)
0173     expandCapacity(count);
0174 
0175   memcpy(&fBuffer[fIndex], chars, count * sizeof(XMLCh));
0176   fIndex += count;
0177 
0178   // Keep it null terminated
0179   fBuffer[fIndex] = 0;
0180 }
0181 
0182 inline void DOMBuffer::
0183 appendInPlace (const XMLCh* const chars, const XMLSize_t count)
0184 {
0185   if (fIndex + count >= fCapacity)
0186     expandCapacity(count, true);
0187 
0188   memcpy(&fBuffer[fIndex], chars, count * sizeof(XMLCh));
0189   fIndex += count;
0190 
0191   // Keep it null terminated
0192   fBuffer[fIndex] = 0;
0193 }
0194 
0195 inline void DOMBuffer::
0196 set (const XMLCh* const chars)
0197 {
0198   XMLSize_t count = XMLString::stringLen(chars);
0199   fIndex = 0;
0200   if (count >= fCapacity)
0201     expandCapacity(count);
0202 
0203   memcpy(fBuffer, chars, count * sizeof(XMLCh));
0204   fIndex = count;
0205 
0206   // Keep it null terminated
0207   fBuffer[fIndex] = 0;
0208 }
0209 
0210 inline void DOMBuffer::
0211 set (const XMLCh* const chars, const XMLSize_t count)
0212 {
0213   fIndex = 0;
0214   if (count >= fCapacity)
0215     expandCapacity(count);
0216 
0217   memcpy(fBuffer, chars, count * sizeof(XMLCh));
0218   fIndex = count;
0219 
0220   // Keep it null terminated
0221   fBuffer[fIndex] = 0;
0222 }
0223 
0224 XERCES_CPP_NAMESPACE_END
0225 
0226 #endif