source: icXML/icXML-devel/src/xercesc/framework/LocalFileFormatTarget.cpp @ 2722

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

Original Xerces files with import mods for icxercesc

File size: 4.4 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: LocalFileFormatTarget.cpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22#include <xercesc/framework/LocalFileFormatTarget.hpp>
23#include <xercesc/framework/MemoryManager.hpp>
24#include <xercesc/util/IOException.hpp>
25#include <xercesc/util/OutOfMemoryException.hpp>
26#include <assert.h>
27#include <string.h>
28
29XERCES_CPP_NAMESPACE_BEGIN
30
31const XMLSize_t MAX_BUFFER_SIZE = 65536;
32
33LocalFileFormatTarget::LocalFileFormatTarget( const XMLCh* const   fileName
34                                            , MemoryManager* const manager)
35: fSource(0)
36, fDataBuf(0)
37, fIndex(0)
38, fCapacity(1024)
39, fMemoryManager(manager)
40{
41    fSource = XMLPlatformUtils::openFileToWrite(fileName, manager);
42
43    if (fSource == (FileHandle) XERCES_Invalid_File_Handle)
44        ThrowXMLwithMemMgr1(IOException, XMLExcepts::File_CouldNotOpenFile, fileName, fMemoryManager);
45
46    fDataBuf = (XMLByte*) fMemoryManager->allocate (
47      fCapacity * sizeof(XMLByte));
48}
49
50LocalFileFormatTarget::LocalFileFormatTarget( const char* const    fileName
51                                            , MemoryManager* const manager)
52: fSource(0)
53, fDataBuf(0)
54, fIndex(0)
55, fCapacity(1024)
56, fMemoryManager(manager)
57{
58    fSource = XMLPlatformUtils::openFileToWrite(fileName, manager);
59
60    if (fSource == (FileHandle) XERCES_Invalid_File_Handle)
61        ThrowXMLwithMemMgr1(IOException, XMLExcepts::File_CouldNotOpenFile, fileName, fMemoryManager);
62
63    fDataBuf = (XMLByte*) fMemoryManager->allocate (
64      fCapacity * sizeof(XMLByte));
65}
66
67LocalFileFormatTarget::~LocalFileFormatTarget()
68{
69    try
70    {
71        // flush remaining buffer before destroy
72        XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
73
74        if (fSource)
75          XMLPlatformUtils::closeFile(fSource, fMemoryManager);
76    }
77    catch (...)
78    {
79      // There is nothing we can do about it here.
80    }
81
82    fMemoryManager->deallocate(fDataBuf);//delete [] fDataBuf;
83}
84
85void LocalFileFormatTarget::flush()
86{
87  XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
88  fIndex = 0;
89}
90
91void LocalFileFormatTarget::writeChars(const XMLByte* const toWrite
92                                     , const XMLSize_t count
93                                     , XMLFormatter * const)
94{
95    if (count)
96    {
97      if (count < MAX_BUFFER_SIZE)
98      {
99        // If we don't have enough space, see if we can grow the buffer.
100        //
101        if (fIndex + count > fCapacity && fCapacity < MAX_BUFFER_SIZE)
102          ensureCapacity (count);
103
104        // If still not enough space, flush the buffer.
105        //
106        if (fIndex + count > fCapacity)
107        {
108          XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
109          fIndex = 0;
110        }
111
112        memcpy(&fDataBuf[fIndex], toWrite, count * sizeof(XMLByte));
113        fIndex += count;
114      }
115      else
116      {
117        if (fIndex)
118        {
119          XMLPlatformUtils::writeBufferToFile(fSource, fIndex, fDataBuf, fMemoryManager);
120          fIndex = 0;
121        }
122
123        XMLPlatformUtils::writeBufferToFile(fSource, count, toWrite, fMemoryManager);
124      }
125    }
126
127    return;
128}
129
130void LocalFileFormatTarget::ensureCapacity(const XMLSize_t extraNeeded)
131{
132    XMLSize_t newCap = fCapacity * 2;
133
134    while (fIndex + extraNeeded > newCap)
135      newCap *= 2;
136
137    XMLByte* newBuf  = (XMLByte*) fMemoryManager->allocate (
138      newCap * sizeof(XMLByte));
139
140    // Copy over the old stuff
141    memcpy(newBuf, fDataBuf, fIndex * sizeof(XMLByte));
142
143    // Clean up old buffer and store new stuff
144    fMemoryManager->deallocate(fDataBuf);
145    fDataBuf = newBuf;
146    fCapacity = newCap;
147}
148
149XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.