FreeCAD

Форк
0
/
XMLTools.cpp 
124 строки · 4.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de>              *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25

26
#include "XMLTools.h"
27

28
using namespace Base;
29
XERCES_CPP_NAMESPACE_USE
30

31
std::unique_ptr<XMLTranscoder> XMLTools::transcoder;  // NOLINT
32

33
void XMLTools::initialize()
34
{
35
    if (!transcoder) {
36
        XMLTransService::Codes res {};
37
        transcoder.reset(XMLPlatformUtils::fgTransService->makeNewTranscoderFor(
38
            XMLRecognizer::UTF_8,
39
            res,
40
            4096,
41
            XMLPlatformUtils::fgMemoryManager));
42
        if (res != XMLTransService::Ok) {
43
            throw Base::UnicodeError("Can't create transcoder");
44
        }
45
    }
46
}
47

48
std::string XMLTools::toStdString(const XMLCh* const toTranscode)
49
{
50
    std::string str;
51

52
    initialize();
53

54
    // char outBuff[128];
55
    static XMLByte outBuff[128];
56
    XMLSize_t outputLength = 0;
57
    XMLSize_t eaten = 0;
58
    XMLSize_t offset = 0;
59
    XMLSize_t inputLength = XMLString::stringLen(toTranscode);
60

61
    while (inputLength) {
62
        outputLength = transcoder->transcodeTo(toTranscode + offset,
63
                                               inputLength,
64
                                               outBuff,
65
                                               128,
66
                                               eaten,
67
                                               XMLTranscoder::UnRep_RepChar);
68
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
69
        str.append(reinterpret_cast<const char*>(outBuff), outputLength);
70
        offset += eaten;
71
        inputLength -= eaten;
72

73
        //  Bail out if nothing more was produced
74
        if (outputLength == 0) {
75
            break;
76
        }
77
    }
78

79
    return str;
80
}
81

82
std::basic_string<XMLCh> XMLTools::toXMLString(const char* const fromTranscode)
83
{
84
    std::basic_string<XMLCh> str;
85
    if (!fromTranscode) {
86
        return str;
87
    }
88

89
    initialize();
90

91
    static XMLCh outBuff[128];
92
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
93
    const XMLByte* xmlBytes = reinterpret_cast<const XMLByte*>(fromTranscode);
94
    XMLSize_t outputLength = 0;
95
    XMLSize_t eaten = 0;
96
    XMLSize_t offset = 0;
97
    XMLSize_t inputLength = std::string(fromTranscode).size();
98

99
    unsigned char* charSizes = new unsigned char[inputLength];
100
    while (inputLength) {
101
        outputLength = transcoder->transcodeFrom(xmlBytes + offset,
102
                                                 inputLength,
103
                                                 outBuff,
104
                                                 128,
105
                                                 eaten,
106
                                                 charSizes);
107
        str.append(outBuff, outputLength);
108
        offset += eaten;
109
        inputLength -= eaten;
110

111
        //  Bail out if nothing more was produced
112
        if (outputLength == 0) {
113
            break;
114
        }
115
    }
116

117
    delete[] charSizes;
118
    return str;
119
}
120

121
void XMLTools::terminate()
122
{
123
    transcoder.reset();
124
}
125

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.