Debian Codegraph @

src/Base/XMLTools.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2011 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <xercesc/framework/MemoryManager.hpp>
26 #include <xercesc/util/OutOfMemoryException.hpp>
27
28 #include "Exception.h"
29 #include "XMLTools.h"
30
31
32 #ifndef XERCES_CPP_NAMESPACE_BEGIN
33 # define XERCES_CPP_NAMESPACE_QUALIFIER
34 using namespace XERCES_CPP_NAMESPACE;
35 #else
36 XERCES_CPP_NAMESPACE_USE
37 #endif
38
39 std::unique_ptr<XMLTranscoder> XMLTools::transcoder; // NOLINT
40
41 void XMLTools::initialize()
42 {
43 if (!transcoder) {
44 XMLTransService::Codes res {};
45 transcoder.reset(
46 XMLPlatformUtils::fgTransService
47 ->makeNewTranscoderFor(XMLRecognizer::UTF_8, res, 4096, XMLPlatformUtils::fgMemoryManager)
48 );
49 if (res != XMLTransService::Ok) {
50 throw Base::UnicodeError("Can't create transcoder");
51 }
52 }
53 }
54
55 std::string XMLTools::toStdString(const XMLCh* const toTranscode)
56 {
57 std::string str;
58
59 initialize();
60
61 // char outBuff[128];
62 static XMLByte outBuff[128];
63 XMLSize_t outputLength = 0;
64 XMLSize_t eaten = 0;
65 XMLSize_t offset = 0;
66 XMLSize_t inputLength = XMLString::stringLen(toTranscode);
67
68 while (inputLength) {
69 outputLength = transcoder->transcodeTo(
70 toTranscode + offset,
71 inputLength,
72 outBuff,
73 128,
74 eaten,
75 XMLTranscoder::UnRep_RepChar
76 );
77 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
78 str.append(reinterpret_cast<const char*>(outBuff), outputLength);
79 offset += eaten;
80 inputLength -= eaten;
81
82 // Bail out if nothing more was produced
83 if (outputLength == 0) {
84 break;
85 }
86 }
87
88 return str;
89 }
90
91 std::basic_string<XMLCh> XMLTools::toXMLString(const char* const fromTranscode)
92 {
93 std::basic_string<XMLCh> str;
94 if (!fromTranscode) {
95 return str;
96 }
97
98 initialize();
99
100 static XMLCh outBuff[128];
101 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
102 const XMLByte* xmlBytes = reinterpret_cast<const XMLByte*>(fromTranscode);
103 XMLSize_t outputLength = 0;
104 XMLSize_t eaten = 0;
105 XMLSize_t offset = 0;
106 XMLSize_t inputLength = std::string(fromTranscode).size();
107
108 unsigned char* charSizes = new unsigned char[inputLength];
109 while (inputLength) {
110 outputLength
111 = transcoder->transcodeFrom(xmlBytes + offset, inputLength, outBuff, 128, eaten, charSizes);
112 str.append(outBuff, outputLength);
113 offset += eaten;
114 inputLength -= eaten;
115
116 // Bail out if nothing more was produced
117 if (outputLength == 0) {
118 break;
119 }
120 }
121
122 delete[] charSizes;
123 return str;
124 }
125
126 void XMLTools::terminate()
127 {
128 transcoder.reset();
129 }
130
131 void* XStrMemoryManager::allocate(XMLSize_t size)
132 {
133 auto ptr = ::operator new(
134 static_cast<size_t>(size),
135 static_cast<std::align_val_t>(alignof(XMLCh)),
136 std::nothrow
137 );
138 if (ptr == nullptr && size != 0) {
139 throw XERCES_CPP_NAMESPACE::OutOfMemoryException();
140 }
141 return ptr;
142 }
143
144 void XStrMemoryManager::deallocate(void* p)
145 {
146 ::operator delete(p);
147 }