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 | #include "NamespaceNode.h" |
---|
31 | #include "CoreIncludes.h" |
---|
32 | #include "XMLPort.h" |
---|
33 | #include "util/SubString.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | CreateFactory(Namespace); |
---|
38 | |
---|
39 | Namespace::Namespace() : |
---|
40 | bAutogeneratedFileRootNamespace_(false), |
---|
41 | bRoot_(false), |
---|
42 | operator_("or") |
---|
43 | { |
---|
44 | RegisterObject(Namespace); |
---|
45 | } |
---|
46 | |
---|
47 | Namespace::~Namespace() |
---|
48 | { |
---|
49 | if (this->bRoot_) |
---|
50 | for (std::set<NamespaceNode*>::iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); ++it) |
---|
51 | delete (*it); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | @brief XML loading and saving. |
---|
56 | @param xmlelement The XML-element |
---|
57 | @param loading Loading (true) or saving (false) |
---|
58 | @return The XML-element |
---|
59 | */ |
---|
60 | void Namespace::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
61 | { |
---|
62 | // BaseObject::XMLPort(xmlelement, mode); |
---|
63 | SUPER(Namespace, XMLPort, xmlelement, mode); |
---|
64 | |
---|
65 | std::string name = this->getName(); |
---|
66 | unsigned int pos = 0; |
---|
67 | while ((pos = name.find(',')) != std::string::npos) |
---|
68 | name.replace(pos, 1, " "); |
---|
69 | while ((pos = name.find(';')) != std::string::npos) |
---|
70 | name.replace(pos, 1, " "); |
---|
71 | while ((pos = name.find('\n')) != std::string::npos) |
---|
72 | name.replace(pos, 1, " "); |
---|
73 | while ((pos = name.find('\t')) != std::string::npos) |
---|
74 | name.replace(pos, 1, " "); |
---|
75 | SubString tokens(name, " ", "", false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
76 | if (this->bRoot_) |
---|
77 | { |
---|
78 | this->representingNamespaces_.insert(new NamespaceNode(this->getName())); |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | for (unsigned int i = 0; i < tokens.size(); i++) |
---|
83 | { |
---|
84 | for (std::set<NamespaceNode*>::iterator it = this->getNamespace()->representingNamespaces_.begin(); it != this->getNamespace()->representingNamespaces_.end(); ++it) |
---|
85 | { |
---|
86 | std::set<NamespaceNode*> temp = (*it)->getNodeRelative(tokens[i]); |
---|
87 | this->representingNamespaces_.insert(temp.begin(), temp.end()); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | XMLPortParam(Namespace, "operator", setOperator, getOperator, xmlelement, mode); |
---|
93 | XMLPortParam(Namespace, "bAutogenerated", setAutogenerated, isAutogenerated, xmlelement, mode); |
---|
94 | |
---|
95 | if (this->bAutogeneratedFileRootNamespace_) |
---|
96 | { |
---|
97 | for (std::set<NamespaceNode*>::iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); ++it) |
---|
98 | { |
---|
99 | (*it)->setRoot(true); |
---|
100 | (*it)->setHidden(true); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | XMLPortObject(Namespace, BaseObject, "", loadObjects, saveObjects, xmlelement, mode, true, false); |
---|
105 | } |
---|
106 | |
---|
107 | void Namespace::loadObjects(BaseObject* object) |
---|
108 | { |
---|
109 | object->setNamespace(this); |
---|
110 | } |
---|
111 | |
---|
112 | const BaseObject* Namespace::saveObjects(unsigned int index) const |
---|
113 | { |
---|
114 | return 0; // todo |
---|
115 | } |
---|
116 | |
---|
117 | bool Namespace::includes(const Namespace* ns) const |
---|
118 | { |
---|
119 | for (std::set<NamespaceNode*>::const_iterator it1 = this->representingNamespaces_.begin(); it1 != this->representingNamespaces_.end(); ++it1) |
---|
120 | { |
---|
121 | for (std::set<NamespaceNode*>::const_iterator it2 = ns->representingNamespaces_.begin(); it2 != ns->representingNamespaces_.end(); ++it2) |
---|
122 | { |
---|
123 | if ((*it1)->includes(*it2)) |
---|
124 | { |
---|
125 | if (this->operator_ == "or") |
---|
126 | return true; |
---|
127 | |
---|
128 | if (this->operator_ == "not") |
---|
129 | return false; |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | if (this->operator_ == "and") |
---|
134 | return false; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if (this->operator_ == "or") |
---|
140 | return false; |
---|
141 | else if (this->operator_ == "and") |
---|
142 | return true; |
---|
143 | else if (this->operator_ == "not") |
---|
144 | return true; |
---|
145 | |
---|
146 | return false; |
---|
147 | } |
---|
148 | |
---|
149 | std::string Namespace::toString() const |
---|
150 | { |
---|
151 | std::string output; |
---|
152 | |
---|
153 | int i = 0; |
---|
154 | for (std::set<NamespaceNode*>::const_iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); i++, ++it) |
---|
155 | { |
---|
156 | if (i > 0) |
---|
157 | output += " / "; |
---|
158 | |
---|
159 | output += (*it)->toString(); |
---|
160 | } |
---|
161 | |
---|
162 | return output; |
---|
163 | } |
---|
164 | |
---|
165 | std::string Namespace::toString(const std::string& indentation) const |
---|
166 | { |
---|
167 | std::string output; |
---|
168 | |
---|
169 | int i = 0; |
---|
170 | for (std::set<NamespaceNode*>::const_iterator it = this->representingNamespaces_.begin(); it != this->representingNamespaces_.end(); i++, ++it) |
---|
171 | { |
---|
172 | if (i > 0) |
---|
173 | output += "\n"; |
---|
174 | |
---|
175 | output += (*it)->toString(indentation); |
---|
176 | } |
---|
177 | |
---|
178 | return output; |
---|
179 | } |
---|
180 | } |
---|