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