[895] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #include "NamespaceNode.h" |
---|
| 29 | #include "Debug.h" |
---|
| 30 | |
---|
| 31 | namespace orxonox |
---|
| 32 | { |
---|
| 33 | NamespaceNode::NamespaceNode(const std::string& name, NamespaceNode* parent) |
---|
| 34 | { |
---|
| 35 | this->name_ = name; |
---|
| 36 | this->parent_ = parent; |
---|
| 37 | this->bRoot_ = false; |
---|
| 38 | this->bHidden_ = false; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | NamespaceNode::~NamespaceNode() |
---|
| 42 | { |
---|
| 43 | for (std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ) |
---|
| 44 | delete ((*(it++)).second); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | std::set<NamespaceNode*> NamespaceNode::getNodeRelative(const std::string& name) |
---|
| 48 | { |
---|
| 49 | std::set<NamespaceNode*> nodes; |
---|
| 50 | |
---|
[901] | 51 | if ((name.size() == 0) || (name == "")) |
---|
[895] | 52 | { |
---|
| 53 | nodes.insert(this); |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | { |
---|
| 57 | unsigned int pos = name.find("::"); |
---|
[901] | 58 | std::string firstPart = name; |
---|
| 59 | std::string secondPart; |
---|
[895] | 60 | |
---|
[901] | 61 | if (pos != std::string::npos) |
---|
| 62 | { |
---|
| 63 | firstPart = name.substr(0, pos); |
---|
| 64 | secondPart = name.substr(pos + 2, std::string::npos); |
---|
| 65 | } |
---|
| 66 | |
---|
[895] | 67 | if (firstPart == "..") |
---|
| 68 | { |
---|
| 69 | if (this->bRoot_) |
---|
| 70 | { |
---|
| 71 | COUT(2) << "Warning: Can't go to enclosing namespace with '..' operator in namespace " << this->name_ << ", namespace is root." << std::endl; |
---|
| 72 | nodes = this->getNodeRelative(secondPart); |
---|
| 73 | } |
---|
[901] | 74 | else if (!this->parent_) |
---|
| 75 | { |
---|
| 76 | COUT(2) << "Warning: Can't go to enclosing namespace with '..' operator in namespace " << this->name_ << ", no parent namespace set." << std::endl; |
---|
| 77 | nodes = this->getNodeRelative(secondPart); |
---|
| 78 | } |
---|
[895] | 79 | else |
---|
| 80 | { |
---|
| 81 | nodes = this->parent_->getNodeRelative(secondPart); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | else if (name.find('@') != 0) |
---|
| 85 | { |
---|
| 86 | std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.find(firstPart); |
---|
| 87 | if (it == this->subnodes_.end()) |
---|
| 88 | it = this->subnodes_.insert(this->subnodes_.begin(), std::pair<std::string, NamespaceNode*>(firstPart, new NamespaceNode(firstPart, this))); |
---|
| 89 | |
---|
| 90 | if ((*it).second->isHidden()) |
---|
[901] | 91 | { |
---|
[895] | 92 | COUT(2) << "Warning: Subnamespace '" << firstPart << "' in namespace '" << this->name_ << "' is hidden and can't be accessed." << std::endl; |
---|
[901] | 93 | nodes.insert(this); |
---|
| 94 | } |
---|
[895] | 95 | else |
---|
[901] | 96 | { |
---|
[895] | 97 | nodes = (*it).second->getNodeRelative(secondPart); |
---|
[901] | 98 | } |
---|
[895] | 99 | } |
---|
| 100 | else |
---|
| 101 | { |
---|
[901] | 102 | bool bFoundMatchingNamespace = false; |
---|
| 103 | |
---|
[895] | 104 | for (std::map<std::string, NamespaceNode*>::iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it) |
---|
| 105 | { |
---|
| 106 | if ((*it).first.find(firstPart) == ((*it).first.size() - firstPart.size())) |
---|
| 107 | { |
---|
| 108 | std::set<NamespaceNode*> temp2 = (*it).second->getNodeRelative(secondPart); |
---|
| 109 | nodes.insert(temp2.begin(), temp2.end()); |
---|
[901] | 110 | bFoundMatchingNamespace = true; |
---|
[895] | 111 | } |
---|
| 112 | } |
---|
[901] | 113 | |
---|
| 114 | if (!bFoundMatchingNamespace) |
---|
| 115 | { |
---|
| 116 | COUT(2) << "Warning: No file included with name '" << firstPart.substr(1, std::string::npos) << "' at this part of the level file, using parent namespace instead." << std::endl; |
---|
| 117 | nodes = this->getNodeRelative(secondPart); |
---|
| 118 | } |
---|
[895] | 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return nodes; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | bool NamespaceNode::includes(const NamespaceNode* ns) const |
---|
| 126 | { |
---|
| 127 | if (ns == this) |
---|
| 128 | { |
---|
| 129 | return true; |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it) |
---|
| 134 | if ((*it).second->includes(ns)) |
---|
| 135 | return true; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return false; |
---|
| 139 | } |
---|
[901] | 140 | |
---|
| 141 | std::string NamespaceNode::toString() const |
---|
| 142 | { |
---|
| 143 | std::string output = this->name_; |
---|
| 144 | |
---|
| 145 | if (this->subnodes_.size() > 0) |
---|
| 146 | { |
---|
| 147 | output += " ("; |
---|
| 148 | |
---|
| 149 | int i = 0; |
---|
| 150 | for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); i++, ++it) |
---|
| 151 | { |
---|
| 152 | if (i > 0) |
---|
| 153 | output += ", "; |
---|
| 154 | |
---|
| 155 | output += (*it).second->toString(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | output += ")"; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return output; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | std::string NamespaceNode::toString(const std::string& indentation) const |
---|
| 165 | { |
---|
| 166 | std::string output = (indentation + this->name_ + "\n"); |
---|
| 167 | |
---|
| 168 | for (std::map<std::string, NamespaceNode*>::const_iterator it = this->subnodes_.begin(); it != this->subnodes_.end(); ++it) |
---|
| 169 | output += (*it).second->toString(indentation + " "); |
---|
| 170 | |
---|
| 171 | return output; |
---|
| 172 | } |
---|
[895] | 173 | } |
---|