Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / config / configpaths.cxx
blob4c626d96b4e09666b4ce77589adf163ba55f3ccb
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <string_view>
25 #include <unotools/configpaths.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <osl/diagnose.h>
29 #include <o3tl/string_view.hxx>
31 namespace utl
34 static
35 void lcl_resolveCharEntities(OUString & aLocalString)
37 sal_Int32 nEscapePos=aLocalString.indexOf('&');
38 if (nEscapePos < 0) return;
40 OUStringBuffer aResult;
41 sal_Int32 nStart = 0;
45 sal_Unicode ch = 0;
46 if (aLocalString.match("&amp;",nEscapePos))
47 ch = '&';
49 else if (aLocalString.match("&apos;",nEscapePos))
50 ch = '\'';
52 else if (aLocalString.match("&quot;",nEscapePos))
53 ch = '"';
55 OSL_ENSURE(ch,"Configuration path contains '&' that is not part of a valid character escape");
56 if (ch)
58 aResult.append(aLocalString.subView(nStart,nEscapePos-nStart) + OUStringChar(ch));
60 sal_Int32 nEscapeEnd=aLocalString.indexOf(';',nEscapePos);
61 nStart = nEscapeEnd+1;
62 nEscapePos=aLocalString.indexOf('&',nStart);
64 else
66 nEscapePos=aLocalString.indexOf('&',nEscapePos+1);
69 while ( nEscapePos > 0);
71 aResult.append(aLocalString.subView(nStart));
73 aLocalString = aResult.makeStringAndClear();
76 bool splitLastFromConfigurationPath(std::u16string_view _sInPath,
77 OUString& _rsOutPath,
78 OUString& _rsLocalName)
80 size_t nStart,nEnd;
82 size_t nPos = _sInPath.size();
84 // for backwards compatibility, strip trailing slash
85 if (nPos > 1 && _sInPath[ nPos - 1 ] == '/')
87 --nPos;
90 // check for set element ['xxx'] or ["yyy"]
91 bool decode;
92 if (nPos > 0 && _sInPath[ nPos - 1 ] == ']')
94 decode = true;
95 if (nPos < 3) { // expect at least chQuote + chQuote + ']' at _sInPath[nPos-3..nPos-1]
96 goto invalid;
98 nPos -= 2;
99 sal_Unicode chQuote = _sInPath[nPos];
101 if (chQuote == '\'' || chQuote == '\"')
103 nEnd = nPos;
104 nPos = _sInPath.rfind(chQuote,nEnd - 1);
105 if (nPos == std::u16string_view::npos) {
106 goto invalid;
108 nStart = nPos + 1;
110 else
112 goto invalid;
115 OSL_ENSURE(nPos > 0 && _sInPath[nPos - 1] == '[', "Invalid config path: unmatched quotes or brackets");
116 if (nPos > 1 && _sInPath[nPos - 1] == '[')
117 // expect at least '/' + '[' at _sInPath[nPos-2..nPos-1]
119 nPos = _sInPath.rfind('/',nPos - 2);
120 if (nPos == std::u16string_view::npos) {
121 goto invalid;
124 else
126 goto invalid;
130 else
132 decode = false;
133 nEnd = nPos;
134 if (nEnd == 0) {
135 goto invalid;
137 nPos = _sInPath.rfind('/',nEnd - 1);
138 if (nPos == std::u16string_view::npos) {
139 goto invalid;
141 nStart = nPos + 1;
143 assert( nPos != std::u16string_view::npos &&
144 nPos < nStart &&
145 nStart <= nEnd &&
146 nEnd <= _sInPath.size() );
148 assert(_sInPath[nPos] == '/');
149 OSL_ENSURE(nPos != 0 , "Invalid config child path: immediate child of root");
151 _rsLocalName = _sInPath.substr(nStart, nEnd-nStart);
152 _rsOutPath = (nPos > 0) ? OUString(_sInPath.substr(0,nPos)) : OUString();
153 if (decode) {
154 lcl_resolveCharEntities(_rsLocalName);
157 return true;
159 invalid:
160 _rsOutPath.clear();
161 _rsLocalName = _sInPath;
162 return false;
165 OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath)
167 sal_Int32 nSep = _sInPath.indexOf('/');
168 sal_Int32 nBracket = _sInPath.indexOf('[');
170 sal_Int32 nStart = nBracket + 1;
171 sal_Int32 nEnd = nSep;
173 if (0 <= nBracket) // found a bracket-quoted relative path
175 if (nSep < 0 || nBracket < nSep) // and the separator comes after it
177 sal_Unicode chQuote = _sInPath[nStart];
178 if (chQuote == '\'' || chQuote == '\"')
180 ++nStart;
181 nEnd = _sInPath.indexOf(chQuote, nStart+1);
182 nBracket = nEnd+1;
184 else
186 nEnd = _sInPath.indexOf(']',nStart);
187 nBracket = nEnd;
189 OSL_ENSURE(nEnd > nStart && _sInPath[nBracket] == ']', "Invalid config path: improper mismatch of quote or bracket");
190 OSL_ENSURE((nBracket+1 == _sInPath.getLength() && nSep == -1) || (_sInPath[nBracket+1] == '/' && nSep == nBracket+1), "Invalid config path: brackets not followed by slash");
192 else // ... but our initial element name is in simple form
193 nStart = 0;
196 OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath;
197 lcl_resolveCharEntities(sResult);
199 if (_sOutPath != nullptr)
201 *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString();
204 return sResult;
207 // find the position after the prefix in the nested path
208 static sal_Int32 lcl_findPrefixEnd(std::u16string_view _sNestedPath, std::u16string_view _sPrefixPath)
210 // TODO: currently handles only exact prefix matches
211 size_t nPrefixLength = _sPrefixPath.size();
213 OSL_ENSURE(nPrefixLength == 0 || _sPrefixPath[nPrefixLength-1] != '/',
214 "Cannot handle slash-terminated prefix paths");
216 bool bIsPrefix;
217 if (_sNestedPath.size() > nPrefixLength)
219 bIsPrefix = _sNestedPath[nPrefixLength] == '/' &&
220 o3tl::starts_with(_sNestedPath, _sPrefixPath);
221 ++nPrefixLength;
223 else if (_sNestedPath.size() == nPrefixLength)
225 bIsPrefix = _sNestedPath == _sPrefixPath;
227 else
229 bIsPrefix = false;
232 return bIsPrefix ? nPrefixLength : 0;
235 bool isPrefixOfConfigurationPath(std::u16string_view _sNestedPath,
236 std::u16string_view _sPrefixPath)
238 return _sPrefixPath.empty() || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0;
241 OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath,
242 std::u16string_view _sPrefixPath)
244 if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) )
246 return _sNestedPath.copy(nPrefixEnd);
248 else
250 OSL_ENSURE(_sPrefixPath.empty(), "Path does not start with expected prefix");
252 return _sNestedPath;
256 static
257 OUString lcl_wrapName(std::u16string_view _sContent, const OUString& _sType)
259 const sal_Unicode * const pBeginContent = _sContent.data();
260 const sal_Unicode * const pEndContent = pBeginContent + _sContent.size();
262 OSL_PRECOND(!_sType.isEmpty(), "Unexpected config type name: empty");
263 OSL_PRECOND(pBeginContent <= pEndContent, "Invalid config name: empty");
265 if (pBeginContent == pEndContent)
266 return _sType;
268 OUStringBuffer aNormalized(_sType.getLength() + _sContent.size() + 4); // reserve approximate size initially
270 // prefix: type, opening bracket and quote
271 aNormalized.append( _sType + "['" );
273 // content: copy over each char and handle escaping
274 for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur)
276 // append (escape if needed)
277 switch(*pCur)
279 case u'&' : aNormalized.append( "&amp;" ); break;
280 case u'\'': aNormalized.append( "&apos;" ); break;
281 case u'\"': aNormalized.append( "&quot;" ); break;
283 default: aNormalized.append( *pCur );
287 // suffix: closing quote and bracket
288 aNormalized.append( "']" );
290 return aNormalized.makeStringAndClear();
293 OUString wrapConfigurationElementName(std::u16string_view _sElementName)
295 return lcl_wrapName(_sElementName, "*" );
298 OUString wrapConfigurationElementName(std::u16string_view _sElementName,
299 OUString const& _sTypeName)
301 // todo: check that _sTypeName is valid
302 return lcl_wrapName(_sElementName, _sTypeName);
305 } // namespace utl
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */