Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / configmgr / source / partial.cxx
blobcba7147aa8bc0926879b3a18153037c4cf84d11e
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 <map>
24 #include <set>
26 #include <com/sun/star/uno/RuntimeException.hpp>
27 #include <com/sun/star/uno/XInterface.hpp>
28 #include <rtl/ustring.h>
29 #include <rtl/ustring.hxx>
30 #include <sal/types.h>
32 #include "data.hxx"
33 #include "partial.hxx"
35 namespace configmgr {
37 namespace {
39 bool parseSegment(
40 OUString const & path, sal_Int32 * index, OUString * segment)
42 assert(
43 index != nullptr && *index >= 0 && *index <= path.getLength() &&
44 segment != nullptr);
45 if (path[(*index)++] == '/') {
46 OUString name;
47 bool setElement;
48 OUString templateName;
49 *index = Data::parseSegment(
50 path, *index, &name, &setElement, &templateName);
51 if (*index != -1) {
52 *segment = Data::createSegment(templateName, name);
53 return *index == path.getLength();
56 throw css::uno::RuntimeException("bad path " + path);
61 Partial::Partial(
62 std::set< OUString > const & includedPaths,
63 std::set< OUString > const & excludedPaths)
65 // The Partial::Node tree built up here encodes the following information:
66 // * Inner node, startInclude: an include starts here that contains excluded
67 // sub-trees
68 // * Inner node, !startInclude: contains in-/excluded sub-trees
69 // * Leaf node, startInclude: an include starts here
70 // * Leaf node, !startInclude: an exclude starts here
71 for (std::set< OUString >::const_iterator i(includedPaths.begin());
72 i != includedPaths.end(); ++i)
74 sal_Int32 n = 0;
75 for (Node * p = &root_;;) {
76 OUString seg;
77 bool end = parseSegment(*i, &n, &seg);
78 p = &p->children[seg];
79 if (p->startInclude) {
80 break;
82 if (end) {
83 p->children.clear();
84 p->startInclude = true;
85 break;
89 for (std::set< OUString >::const_iterator i(excludedPaths.begin());
90 i != excludedPaths.end(); ++i)
92 sal_Int32 n = 0;
93 for (Node * p = &root_;;) {
94 OUString seg;
95 bool end = parseSegment(*i, &n, &seg);
96 if (end) {
97 p->children[seg].clear();
98 break;
100 Node::Children::iterator j(p->children.find(seg));
101 if (j == p->children.end()) {
102 break;
104 p = &j->second;
109 Partial::~Partial() {}
111 Partial::Containment Partial::contains(std::vector<OUString> const & path) const {
112 //TODO: For set elements, the segment names recorded in the node tree need
113 // not match the corresponding path segments, so this function can fail.
115 // * If path ends at a leaf node or goes past a leaf node:
116 // ** If that leaf node is startInclude: => CONTAINS_NODE
117 // ** If that leaf node is !startInclude: => CONTAINS_NOT
118 // * If path ends at inner node:
119 // ** If there is some startInclude along its trace: => CONTAINS_NODE
120 // ** If there is no startInclude along its trace: => CONTAINS_SUBNODES
121 Node const * p = &root_;
122 bool bIncludes = false;
123 for (auto i(path.begin()); i != path.end(); ++i) {
124 Node::Children::const_iterator j(p->children.find(*i));
125 if (j == p->children.end()) {
126 return p->startInclude ? CONTAINS_NODE : CONTAINS_NOT;
128 p = &j->second;
129 bIncludes |= p->startInclude;
131 return p->children.empty() && !p->startInclude
132 ? CONTAINS_NOT
133 : bIncludes ? CONTAINS_NODE : CONTAINS_SUBNODES;
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */