DOMStringPool.cpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:8k
源码类别:

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2001-2002 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) 2001, 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.  * $Id: DOMStringPool.cpp,v 1.5 2002/11/04 15:07:34 tng Exp $
  58.  */
  59. #include <xercesc/util/XMLString.hpp>
  60. #include <xercesc/util/PlatformUtils.hpp>
  61. #include "DOMStringPool.hpp"
  62. #include "DOMDocumentImpl.hpp"
  63. XERCES_CPP_NAMESPACE_BEGIN
  64. //
  65. //  DStringPoolEntry - one of these structs is allocated for each
  66. //                      XMLCh String in the pool.  Each slot in the
  67. //                      hash table array itself is a pointer to the head
  68. //                      of a singly-linked list of these structs.
  69. //
  70. //                      Although this struct is delcared with a string length of one,
  71. //                      the factory method allocates enough storage to hold the full
  72. //                      string length.
  73. //
  74. struct DOMStringPoolEntry
  75. {
  76.     DOMStringPoolEntry    *fNext;
  77.     XMLCh                 fString[1];
  78. };
  79. //
  80. // createSPE - factory method for creating sting pool entry structs.
  81. //             Allocates sufficient storage to hold the entire string
  82. //
  83. static DOMStringPoolEntry *createSPE(const XMLCh *str, DOMDocumentImpl *doc)
  84. {
  85.     //  Compute size to allocate.  Note that there's 1 char of string declared in the
  86.     //       struct, so we don't need to add one again to account for the trailing null.
  87.     //
  88.     size_t sizeToAllocate = sizeof(DOMStringPoolEntry) + XMLString::stringLen(str)*sizeof(XMLCh);
  89.     DOMStringPoolEntry *newSPE = (DOMStringPoolEntry *)doc->allocate(sizeToAllocate);
  90.     newSPE->fNext = 0;
  91.     XMLCh * nonConstStr = (XMLCh *)newSPE->fString;
  92.     XMLString::copyString(nonConstStr, str);
  93.     return newSPE;
  94. }
  95. DOMStringPool::DOMStringPool(int hashTableSize, DOMDocumentImpl *doc)
  96. {
  97.     fDoc           = doc;          // needed to get access to the doc's storage allocator.
  98.     fHashTableSize = hashTableSize;
  99.     //fHashTable = new (fDoc) DOMStringPoolEntry *[hashTableSize];
  100.     void* p = doc->allocate(sizeof(DOMStringPoolEntry*) * hashTableSize);
  101.     fHashTable = (DOMStringPoolEntry**) p;
  102.     for (int i=0; i<fHashTableSize; i++)
  103.         fHashTable[i] = 0;
  104. };
  105. //  Destructor.    Nothing to do, since storage all belongs to the document.
  106. //
  107. DOMStringPool::~DOMStringPool()
  108. {
  109. };
  110. const XMLCh *DOMStringPool::getPooledString(const XMLCh *in)
  111. {
  112.     DOMStringPoolEntry    **pspe;
  113.     DOMStringPoolEntry    *spe;
  114.     int    inHash     = XMLString::hash(in, fHashTableSize);
  115.     pspe = &fHashTable[inHash];
  116.     while (*pspe != 0)
  117.     {
  118.         if (XMLString::equals((*pspe)->fString, in))
  119.             return (*pspe)->fString;
  120.         pspe = &((*pspe)->fNext);
  121.     }
  122.     // This string hasn't been seen before.  Add it to the pool.
  123.     *pspe = spe = createSPE(in, fDoc);
  124.     return spe->fString;
  125. };
  126. // -----------------------------------------------------------------------
  127. //  DOMBuffer: Constructors
  128. // -----------------------------------------------------------------------
  129. DOMBuffer::DOMBuffer(DOMDocumentImpl *doc, int capacity) :
  130.     fBuffer(0)
  131.     , fIndex(0)
  132.     , fCapacity(capacity)
  133.     , fDoc(doc)
  134. {
  135.     // Buffer is one larger than capacity, to allow for zero term
  136.     fBuffer = (XMLCh*) doc->allocate((fCapacity+1)*sizeof(XMLCh));
  137.     // Keep it null terminated
  138.     fBuffer[0] = XMLCh(0);
  139. }
  140. DOMBuffer::DOMBuffer(DOMDocumentImpl *doc, const XMLCh* string) :
  141.     fBuffer(0)
  142.     , fIndex(0)
  143.     , fCapacity(0)
  144.     , fDoc(doc)
  145. {
  146.     unsigned int actualCount = XMLString::stringLen(string);
  147.     fCapacity = actualCount + 15;
  148.     // Buffer is one larger than capacity, to allow for zero term
  149.     fBuffer = (XMLCh*) doc->allocate((fCapacity+1)*sizeof(XMLCh));
  150.     memcpy(fBuffer, string, actualCount * sizeof(XMLCh));
  151.     fIndex = actualCount;
  152.     // Keep it null terminated
  153.     fBuffer[fIndex] = 0;
  154. }
  155. // ---------------------------------------------------------------------------
  156. //  DOMBuffer: Buffer management
  157. // ---------------------------------------------------------------------------
  158. void DOMBuffer::append(const XMLCh* const chars, const unsigned int count)
  159. {
  160.     unsigned int actualCount = count;
  161.     if (!count)
  162.         actualCount = XMLString::stringLen(chars);
  163.     if (fIndex + actualCount >= fCapacity)
  164.         expandCapacity(actualCount);
  165.     memcpy(&fBuffer[fIndex], chars, actualCount * sizeof(XMLCh));
  166.     fIndex += actualCount;
  167. }
  168. void DOMBuffer::set(const XMLCh* const chars, const unsigned int count)
  169. {
  170.     unsigned int actualCount = count;
  171.     if (!count)
  172.         actualCount = XMLString::stringLen(chars);
  173.     fIndex = 0;
  174.     if (fIndex + actualCount >= fCapacity)
  175.         expandCapacity(actualCount);
  176.     memcpy(fBuffer, chars, actualCount * sizeof(XMLCh));
  177.     fIndex = actualCount;
  178. }
  179. // ---------------------------------------------------------------------------
  180. //  DOMBuffer: Private helper methods
  181. // ---------------------------------------------------------------------------
  182. void DOMBuffer::expandCapacity(const unsigned int extraNeeded)
  183. {
  184.     //not enough room. Calc new capacity and allocate new buffer
  185.     const unsigned int newCap = (unsigned int)((fIndex + extraNeeded) * 1.25);
  186.     XMLCh* newBuf = (XMLCh*) fDoc->allocate((newCap+1)*sizeof(XMLCh));
  187.     // Copy over the old stuff
  188.     memcpy(newBuf, fBuffer, fCapacity * sizeof(XMLCh));
  189.     // revisit: Leave the old buffer in document heap, yes, this is a leak, but live with it!
  190.     // store new stuff
  191.     fBuffer = newBuf;
  192.     fCapacity = newCap;
  193. }
  194. XERCES_CPP_NAMESPACE_END