1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
25 #include <com/sun/star/uno/RuntimeException.hpp>
26 #include <rtl/ustring.hxx>
27 #include <sal/types.h>
30 #include "partial.hxx"
37 OUString
const & path
, sal_Int32
* index
, OUString
* segment
)
40 index
!= nullptr && *index
>= 0 && *index
<= path
.getLength() &&
42 if (path
[(*index
)++] == '/') {
45 OUString templateName
;
46 *index
= Data::parseSegment(
47 path
, *index
, &name
, &setElement
, &templateName
);
49 *segment
= Data::createSegment(templateName
, name
);
50 return *index
== path
.getLength();
53 throw css::uno::RuntimeException("bad path " + path
);
59 std::set
< OUString
> const & includedPaths
,
60 std::set
< OUString
> const & excludedPaths
)
62 // The Partial::Node tree built up here encodes the following information:
63 // * Inner node, startInclude: an include starts here that contains excluded
65 // * Inner node, !startInclude: contains in-/excluded sub-trees
66 // * Leaf node, startInclude: an include starts here
67 // * Leaf node, !startInclude: an exclude starts here
68 for (auto const& includedPath
: includedPaths
)
71 for (Node
* p
= &root_
;;) {
73 bool end
= parseSegment(includedPath
, &n
, &seg
);
74 p
= &p
->children
[seg
];
75 if (p
->startInclude
) {
80 p
->startInclude
= true;
85 for (auto const& excludedPath
: excludedPaths
)
88 for (Node
* p
= &root_
;;) {
90 bool end
= parseSegment(excludedPath
, &n
, &seg
);
92 p
->children
[seg
].clear();
95 Node::Children::iterator
j(p
->children
.find(seg
));
96 if (j
== p
->children
.end()) {
104 Partial::~Partial() {}
106 Partial::Containment
Partial::contains(std::vector
<OUString
> const & path
) const {
107 //TODO: For set elements, the segment names recorded in the node tree need
108 // not match the corresponding path segments, so this function can fail.
110 // * If path ends at a leaf node or goes past a leaf node:
111 // ** If that leaf node is startInclude: => CONTAINS_NODE
112 // ** If that leaf node is !startInclude: => CONTAINS_NOT
113 // * If path ends at inner node:
114 // ** If there is some startInclude along its trace: => CONTAINS_NODE
115 // ** If there is no startInclude along its trace: => CONTAINS_SUBNODES
116 Node
const * p
= &root_
;
117 bool bIncludes
= false;
118 for (auto const& elemPath
: path
)
120 Node::Children::const_iterator
j(p
->children
.find(elemPath
));
121 if (j
== p
->children
.end()) {
122 return p
->startInclude
? CONTAINS_NODE
: CONTAINS_NOT
;
125 bIncludes
|= p
->startInclude
;
127 return p
->children
.empty() && !p
->startInclude
129 : bIncludes
? CONTAINS_NODE
: CONTAINS_SUBNODES
;
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */