| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | *                    > www.orxonox.net < | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *   License notice: | 
|---|
| 7 | * | 
|---|
| 8 | *   This program is free software; you can redistribute it and/or | 
|---|
| 9 | *   modify it under the terms of the GNU General Public License | 
|---|
| 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 | *   of the License, or (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | *   GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | *   You should have received a copy of the GNU General Public License | 
|---|
| 19 | *   along with this program; if not, write to the Free Software | 
|---|
| 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 | * | 
|---|
| 22 | *   Author: | 
|---|
| 23 | *      Fabian 'x3n' Landau | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "Namespace.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include <set> | 
|---|
| 32 |  | 
|---|
| 33 | #include "NamespaceNode.h" | 
|---|
| 34 | #include "CoreIncludes.h" | 
|---|
| 35 | #include "XMLPort.h" | 
|---|
| 36 | #include "util/SubString.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 | CreateFactory(Namespace); | 
|---|
| 41 |  | 
|---|
| 42 | Namespace::Namespace(BaseObject* creator) : BaseObject(creator), | 
|---|
| 43 | bAutogeneratedFileRootNamespace_(false), | 
|---|
| 44 | bRoot_(false), | 
|---|
| 45 | operator_("or") | 
|---|
| 46 | { | 
|---|
| 47 | RegisterObject(Namespace); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | Namespace::~Namespace() | 
|---|
| 51 | { | 
|---|
| 52 | if (this->bRoot_) | 
|---|
| 53 | for (std::set<NamespaceNode*>::iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); ++it) | 
|---|
| 54 | delete (*it); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void Namespace::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 58 | { | 
|---|
| 59 | SUPER(Namespace, XMLPort, xmlelement, mode); | 
|---|
| 60 |  | 
|---|
| 61 | std::string name = this->getName(); | 
|---|
| 62 | size_t pos = 0; | 
|---|
| 63 | while ((pos = name.find(',')) != std::string::npos) | 
|---|
| 64 | name.replace(pos, 1, " "); | 
|---|
| 65 | while ((pos = name.find(';')) != std::string::npos) | 
|---|
| 66 | name.replace(pos, 1, " "); | 
|---|
| 67 | while ((pos = name.find('\n')) != std::string::npos) | 
|---|
| 68 | name.replace(pos, 1, " "); | 
|---|
| 69 | while ((pos = name.find('\t')) != std::string::npos) | 
|---|
| 70 | name.replace(pos, 1, " "); | 
|---|
| 71 | SubString tokens(name, " ", "", false, '\\', true, '"', true, '\0', '\0', true, '\0'); | 
|---|
| 72 | if (this->bRoot_) | 
|---|
| 73 | { | 
|---|
| 74 | this->representingNamespaces_.insert(new NamespaceNode(this->getName())); | 
|---|
| 75 | } | 
|---|
| 76 | else | 
|---|
| 77 | { | 
|---|
| 78 | for (unsigned int i = 0; i < tokens.size(); i++) | 
|---|
| 79 | { | 
|---|
| 80 | for (std::set<NamespaceNode*>::iterator it = this->getNamespace()->representingNamespaces_.begin(); it != this->getNamespace()->representingNamespaces_.end(); ++it) | 
|---|
| 81 | { | 
|---|
| 82 | std::set<NamespaceNode*> temp = (*it)->getNodeRelative(tokens[i]); | 
|---|
| 83 | this->representingNamespaces_.insert(temp.begin(), temp.end()); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | XMLPortParam(Namespace, "operator", setOperator, getOperator, xmlelement, mode); | 
|---|
| 89 | XMLPortParam(Namespace, "bAutogenerated", setAutogenerated, isAutogenerated, xmlelement, mode); | 
|---|
| 90 |  | 
|---|
| 91 | if (this->bAutogeneratedFileRootNamespace_) | 
|---|
| 92 | { | 
|---|
| 93 | for (std::set<NamespaceNode*>::iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); ++it) | 
|---|
| 94 | { | 
|---|
| 95 | (*it)->setRoot(true); | 
|---|
| 96 | (*it)->setHidden(true); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | XMLPortObjectExtended(Namespace, BaseObject, "", loadObjects, saveObjects, xmlelement, mode, true, false); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | void Namespace::loadObjects(BaseObject* object) | 
|---|
| 104 | { | 
|---|
| 105 | object->setNamespace(this); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | const BaseObject* Namespace::saveObjects(unsigned int index) const | 
|---|
| 109 | { | 
|---|
| 110 | return 0; // todo | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | bool Namespace::includes(const Namespace* ns) const | 
|---|
| 114 | { | 
|---|
| 115 | for (std::set<NamespaceNode*>::const_iterator it1 = this->representingNamespaces_.begin(); it1 != this->representingNamespaces_.end(); ++it1) | 
|---|
| 116 | { | 
|---|
| 117 | for (std::set<NamespaceNode*>::const_iterator it2 = ns->representingNamespaces_.begin(); it2 != ns->representingNamespaces_.end(); ++it2) | 
|---|
| 118 | { | 
|---|
| 119 | if ((*it1)->includes(*it2)) | 
|---|
| 120 | { | 
|---|
| 121 | if (this->operator_ == "or") | 
|---|
| 122 | return true; | 
|---|
| 123 |  | 
|---|
| 124 | if (this->operator_ == "not") | 
|---|
| 125 | return false; | 
|---|
| 126 | } | 
|---|
| 127 | else | 
|---|
| 128 | { | 
|---|
| 129 | if (this->operator_ == "and") | 
|---|
| 130 | return false; | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | if (this->operator_ == "or") | 
|---|
| 136 | return false; | 
|---|
| 137 | else if (this->operator_ == "and") | 
|---|
| 138 | return true; | 
|---|
| 139 | else if (this->operator_ == "not") | 
|---|
| 140 | return true; | 
|---|
| 141 |  | 
|---|
| 142 | return false; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | std::string Namespace::toString() const | 
|---|
| 146 | { | 
|---|
| 147 | std::string output; | 
|---|
| 148 |  | 
|---|
| 149 | int i = 0; | 
|---|
| 150 | for (std::set<NamespaceNode*>::const_iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); i++, ++it) | 
|---|
| 151 | { | 
|---|
| 152 | if (i > 0) | 
|---|
| 153 | output += " / "; | 
|---|
| 154 |  | 
|---|
| 155 | output += (*it)->toString(); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | return output; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | std::string Namespace::toString(const std::string& indentation) const | 
|---|
| 162 | { | 
|---|
| 163 | std::string output; | 
|---|
| 164 |  | 
|---|
| 165 | int i = 0; | 
|---|
| 166 | for (std::set<NamespaceNode*>::const_iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); i++, ++it) | 
|---|
| 167 | { | 
|---|
| 168 | if (i > 0) | 
|---|
| 169 | output += '\n'; | 
|---|
| 170 |  | 
|---|
| 171 | output += (*it)->toString(indentation); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | return output; | 
|---|
| 175 | } | 
|---|
| 176 | } | 
|---|