CppunitTest_sc_tiledrendering2: move to tiledrendering folder
[LibreOffice.git] / l10ntools / source / helper.cxx
blob4726234b19dd1d3d8a3acfbe1ba05766b89dc081
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <libxml/parser.h>
14 #include <o3tl/safeint.hxx>
15 #include <o3tl/string_view.hxx>
16 #include <rtl/strbuf.hxx>
18 #include <helper.hxx>
20 namespace helper {
22 OString escapeAll(
23 std::string_view rText, std::string_view rUnEscaped, std::string_view rEscaped )
25 assert( rEscaped.size() == 2*rUnEscaped.size() );
26 OStringBuffer sReturn;
27 for ( size_t nIndex = 0; nIndex < rText.size(); ++nIndex )
29 size_t nUnEscapedOne = rUnEscaped.find(rText[nIndex]);
30 if( nUnEscapedOne != std::string_view::npos )
32 sReturn.append(rEscaped.substr(nUnEscapedOne*2,2));
34 else
35 sReturn.append(rText[nIndex]);
37 return sReturn.makeStringAndClear();
41 OString unEscapeAll(
42 std::string_view rText, std::string_view rEscaped, std::string_view rUnEscaped)
44 assert( rEscaped.size() == 2*rUnEscaped.length() );
45 OStringBuffer sReturn;
46 const size_t nLength = rText.size();
47 for ( size_t nIndex = 0; nIndex < nLength; ++nIndex )
49 if( rText[nIndex] == '\\' && nIndex+1 < nLength )
51 size_t nEscapedOne = rEscaped.find(rText.substr(nIndex,2));
52 if( nEscapedOne != std::string_view::npos )
54 sReturn.append(rUnEscaped[nEscapedOne/2]);
55 ++nIndex;
57 else
59 sReturn.append(rText[nIndex]);
62 else
63 sReturn.append(rText[nIndex]);
65 return sReturn.makeStringAndClear();
69 OString QuotHTML(std::string_view rString)
71 OStringBuffer sReturn;
72 for (size_t i = 0; i < rString.size(); ++i)
74 switch (rString[i])
76 case '<':
77 sReturn.append("&lt;");
78 break;
79 case '>':
80 sReturn.append("&gt;");
81 break;
82 case '"':
83 sReturn.append("&quot;");
84 break;
85 case '\'':
86 sReturn.append("&apos;");
87 break;
88 case '&':
89 if (o3tl::starts_with(rString.substr(i), "&amp;"))
90 sReturn.append('&');
91 else
92 sReturn.append("&amp;");
93 break;
94 default:
95 sReturn.append(rString[i]);
96 break;
99 return sReturn.makeStringAndClear();
102 OString UnQuotHTML( std::string_view rString )
104 OStringBuffer sReturn;
105 for (size_t i = 0; i != rString.size();) {
106 auto tmp = rString.substr(i);
107 if (o3tl::starts_with(tmp, "&amp;")) {
108 sReturn.append('&');
109 i += RTL_CONSTASCII_LENGTH("&amp;");
110 } else if (o3tl::starts_with(tmp, "&lt;")) {
111 sReturn.append('<');
112 i += RTL_CONSTASCII_LENGTH("&lt;");
113 } else if (o3tl::starts_with(tmp, "&gt;")) {
114 sReturn.append('>');
115 i += RTL_CONSTASCII_LENGTH("&gt;");
116 } else if (o3tl::starts_with(tmp, "&quot;")) {
117 sReturn.append('"');
118 i += RTL_CONSTASCII_LENGTH("&quot;");
119 } else if (o3tl::starts_with(tmp, "&apos;")) {
120 sReturn.append('\'');
121 i += RTL_CONSTASCII_LENGTH("&apos;");
122 } else {
123 sReturn.append(rString[i]);
124 ++i;
127 return sReturn.makeStringAndClear();
130 bool isWellFormedXML( std::string_view text )
132 xmlDocPtr doc;
133 bool result = true;
135 OString content = OString::Concat("<root>") + text + "</root>";
136 doc = xmlParseMemory(content.getStr(),static_cast<int>(content.getLength()));
137 if (doc == nullptr) {
138 result = false;
140 xmlFreeDoc(doc);
141 xmlCleanupParser();
142 return result;
145 //Convert xmlChar* to OString
146 OString xmlStrToOString( const xmlChar* pString )
148 xmlChar* pTemp = xmlStrdup( pString );
149 OString sResult = reinterpret_cast<char*>( pTemp );
150 xmlFree( pTemp );
151 return sResult;
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */