source: NonGTP/Xerces/xerces/include/xercesc/framework/XMLBuffer.hpp @ 358

Revision 358, 8.7 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: XMLBuffer.hpp,v $
19 * Revision 1.8  2004/09/29 18:59:18  peiyongz
20 * [jira1207] --patch from Dan Rosen
21 *
22 * Revision 1.7  2004/09/08 13:55:58  peiyongz
23 * Apache License Version 2.0
24 *
25 * Revision 1.6  2004/01/29 11:46:29  cargilld
26 * Code cleanup changes to get rid of various compiler diagnostic messages.
27 *
28 * Revision 1.5  2003/05/16 21:36:55  knoaman
29 * Memory manager implementation: Modify constructors to pass in the memory manager.
30 *
31 * Revision 1.4  2003/05/15 18:26:07  knoaman
32 * Partial implementation of the configurable memory manager.
33 *
34 * Revision 1.3  2002/11/04 15:00:21  tng
35 * C++ Namespace Support.
36 *
37 * Revision 1.2  2002/08/21 18:54:52  tng
38 * [Bug 11869] Add the const modifier (XMLBuffer.hpp).
39 *
40 * Revision 1.1.1.1  2002/02/01 22:21:51  peiyongz
41 * sane_include
42 *
43 * Revision 1.6  2001/06/27 20:29:09  tng
44 * [Bug 2365] Huge performance problem with the parser in XMLScanner::sendCharData() .  By David Bertoni.
45 *
46 * Revision 1.5  2000/03/02 19:54:24  roddey
47 * This checkin includes many changes done while waiting for the
48 * 1.1.0 code to be finished. I can't list them all here, but a list is
49 * available elsewhere.
50 *
51 * Revision 1.4  2000/02/24 20:00:22  abagchi
52 * Swat for removing Log from API docs
53 *
54 * Revision 1.3  2000/02/15 01:21:30  roddey
55 * Some initial documentation improvements. More to come...
56 *
57 * Revision 1.2  2000/02/06 07:47:47  rahulj
58 * Year 2K copyright swat.
59 *
60 * Revision 1.1.1.1  1999/11/09 01:08:29  twl
61 * Initial checkin
62 *
63 * Revision 1.2  1999/11/08 20:44:36  rahul
64 * Swat for adding in Product name and CVS comment log variable.
65 *
66 */
67
68
69#if !defined(XMLBUFFER_HPP)
70#define XMLBUFFER_HPP
71
72#include <xercesc/util/XMemory.hpp>
73#include <xercesc/util/PlatformUtils.hpp>
74#include <xercesc/framework/MemoryManager.hpp>
75
76XERCES_CPP_NAMESPACE_BEGIN
77
78class XMLBufferFullHandler;
79
80/**
81 *  XMLBuffer is a lightweight, expandable Unicode text buffer. Since XML is
82 *  inherently theoretically unbounded in terms of the sizes of things, we
83 *  very often need to have expandable buffers. The primary concern here is
84 *  that appends of characters and other buffers or strings be very fast, so
85 *  it always maintains the current buffer size.
86 *
87 *  The buffer is not nul terminated until some asks to see the raw buffer
88 *  contents. This also avoids overhead during append operations.
89 */
90 class XMLPARSER_EXPORT XMLBuffer : public XMemory
91{
92public :
93    // -----------------------------------------------------------------------
94    //  Constructors and Destructor
95    // -----------------------------------------------------------------------
96
97    /** @name Constructor */
98    //@{
99    XMLBuffer(const unsigned int capacity = 1023
100              , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) :
101
102        fUsed(false)
103        , fIndex(0)
104        , fCapacity(capacity)
105        , fMemoryManager(manager)
106        , fFullHandler(0)
107        , fFullSize(0)
108        , fBuffer(0)
109    {
110        // Buffer is one larger than capacity, to allow for zero term
111        fBuffer = (XMLCh*) manager->allocate((capacity+1) * sizeof(XMLCh)); //new XMLCh[fCapacity+1];
112
113        // Keep it null terminated
114        fBuffer[0] = XMLCh(0);
115    }
116    //@}
117
118    /** @name Destructor */
119    //@{
120    ~XMLBuffer()
121    {
122        fMemoryManager->deallocate(fBuffer); //delete [] fBuffer;
123    }
124    //@}
125
126    // -----------------------------------------------------------------------
127    //  Buffer Full Handler Management
128    // -----------------------------------------------------------------------
129    void setFullHandler(XMLBufferFullHandler* handler, const unsigned int fullSize)
130    {
131        fFullHandler = handler;
132        fFullSize = fullSize;
133    }
134
135    // -----------------------------------------------------------------------
136    //  Buffer Management
137    // -----------------------------------------------------------------------
138    void append(const XMLCh toAppend)
139    {
140        // Put in char and bump the index
141        if (fIndex == fCapacity)
142            insureCapacity(1);
143        fBuffer[fIndex++] = toAppend;
144    }
145
146    void append (const XMLCh* const chars, const unsigned int count = 0);
147
148    void set (const XMLCh* const chars, const unsigned int count = 0)
149    {
150        fIndex = 0;
151        append(chars, count);
152    }
153
154    const XMLCh* getRawBuffer() const
155    {
156        fBuffer[fIndex] = 0;
157        return fBuffer;
158    }
159
160    XMLCh* getRawBuffer()
161    {
162        fBuffer[fIndex] = 0;
163        return fBuffer;
164    }
165
166    void reset()
167    {
168        fIndex = 0;
169        fBuffer[0] = 0;
170    }
171
172    // -----------------------------------------------------------------------
173    //  Getters
174    // -----------------------------------------------------------------------
175    bool getInUse() const
176    {
177        return fUsed;
178    }
179
180    unsigned int getLen() const
181    {
182        return fIndex;
183    }
184
185    bool isEmpty() const
186    {
187        return (fIndex == 0);
188    }
189
190    // -----------------------------------------------------------------------
191    //  Setters
192    // -----------------------------------------------------------------------
193    void setInUse(const bool newValue)
194    {
195        fUsed = newValue;
196    }
197
198private :
199    // -----------------------------------------------------------------------
200    //  Unimplemented constructors and operators
201    // -----------------------------------------------------------------------
202    XMLBuffer(const XMLBuffer&);
203    XMLBuffer& operator=(const XMLBuffer&);
204
205    // -----------------------------------------------------------------------
206    //  Declare our friends
207    // -----------------------------------------------------------------------
208    friend class XMLBufBid;
209
210    // -----------------------------------------------------------------------
211    //  Private helpers
212    // -----------------------------------------------------------------------
213    void insureCapacity(const unsigned int extraNeeded);
214
215
216    // -----------------------------------------------------------------------
217    //  Private data members
218    //
219    //  fBuffer
220    //      The pointer to the buffer data. Its grown as needed. Its always
221    //      one larger than fCapacity, to leave room for the null terminator.
222    //
223    //  fIndex
224    //      The current index into the buffer, as characters are appended
225    //      to it. If its zero, then the buffer is empty.
226    //
227    //  fCapacity
228    //      The current capacity of the buffer. Its actually always one
229    //      larger, to leave room for the null terminator.
230    //
231    //  fUsed
232    //      Indicates whether this buffer is in use or not.
233    //
234    //  fFullHandler, fFullSize
235    //      If fFullHandler is non-null, the buffer has a maximum size
236    //      indicated by fFullSize. If writing to the buffer would exceed the
237    //      buffer's maximum size, fFullHandler's bufferFull callback is
238    //      invoked, to empty the buffer.
239    // -----------------------------------------------------------------------
240    bool            fUsed;
241    unsigned int    fIndex;
242    unsigned int    fCapacity;
243    MemoryManager* const        fMemoryManager;
244    XMLBufferFullHandler*       fFullHandler;
245    unsigned int                fFullSize;
246    XMLCh*          fBuffer;
247};
248
249/**
250 *  XMLBufferFullHandler is a callback interface for clients of
251 *  XMLBuffers that impose a size restriction (e.g. XMLScanner).
252 *  Note that this is intended solely as a mix-in for internal
253 *  use, and therefore does not derive from XMemory (to avoid
254 *  the ambiguous base class problem).
255 */
256class XMLPARSER_EXPORT XMLBufferFullHandler
257{
258public :
259
260    /**
261     * Callback method, intended to allow clients of an XMLBuffer which has
262     * become full to empty it appropriately.
263     * @return true if the handler was able to empty the buffer (either
264     * partially or completely), otherwise false to indicate an error.
265     */
266    virtual bool bufferFull(XMLBuffer&) = 0;
267
268};
269
270XERCES_CPP_NAMESPACE_END
271
272#endif
Note: See TracBrowser for help on using the repository browser.