2015-01-31 10:07:48 +00:00
|
|
|
/*-----------------------------------------------------------------------------------------------
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Copyright (c) 2014-2020 Kim Kulling
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
-----------------------------------------------------------------------------------------------*/
|
|
|
|
#include <openddlparser/DDLNode.h>
|
|
|
|
#include <openddlparser/OpenDDLParser.h>
|
2020-10-25 09:34:43 +00:00
|
|
|
#include <openddlparser/OpenDDLStream.h>
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
BEGIN_ODDLPARSER_NS
|
|
|
|
|
|
|
|
DDLNode::DllNodeList DDLNode::s_allocatedNodes;
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
template <class T>
|
|
|
|
inline static void releaseDataType(T *ptr) {
|
|
|
|
if (nullptr == ptr) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
T *current(nullptr);
|
|
|
|
while (ptr) {
|
2015-01-31 10:07:48 +00:00
|
|
|
current = ptr;
|
|
|
|
ptr = ptr->m_next;
|
|
|
|
delete current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void releaseReferencedNames(Reference *ref) {
|
|
|
|
if (nullptr == ref) {
|
2015-03-17 13:25:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:29:15 +00:00
|
|
|
delete ref;
|
2015-03-17 13:25:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
DDLNode::DDLNode(const std::string &type, const std::string &name, size_t idx, DDLNode *parent) :
|
|
|
|
m_type(type),
|
|
|
|
m_name(name),
|
|
|
|
m_parent(parent),
|
|
|
|
m_children(),
|
|
|
|
m_properties(nullptr),
|
|
|
|
m_value(nullptr),
|
|
|
|
m_dtArrayList(nullptr),
|
|
|
|
m_references(nullptr),
|
|
|
|
m_idx(idx) {
|
|
|
|
if (m_parent) {
|
|
|
|
m_parent->m_children.push_back(this);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLNode::~DDLNode() {
|
2017-06-19 19:07:18 +00:00
|
|
|
delete m_properties;
|
|
|
|
delete m_value;
|
2020-10-25 09:34:43 +00:00
|
|
|
releaseReferencedNames(m_references);
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
delete m_dtArrayList;
|
2020-10-25 09:34:43 +00:00
|
|
|
m_dtArrayList = nullptr;
|
|
|
|
if (s_allocatedNodes[m_idx] == this) {
|
|
|
|
s_allocatedNodes[m_idx] = nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
for (size_t i = 0; i < m_children.size(); i++) {
|
|
|
|
delete m_children[i];
|
2017-06-19 19:07:18 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::attachParent(DDLNode *parent) {
|
|
|
|
if (m_parent == parent) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_parent = parent;
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr != m_parent) {
|
|
|
|
m_parent->m_children.push_back(this);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DDLNode::detachParent() {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr != m_parent) {
|
|
|
|
DDLNodeIt it = std::find(m_parent->m_children.begin(), m_parent->m_children.end(), this);
|
|
|
|
if (m_parent->m_children.end() != it) {
|
|
|
|
m_parent->m_children.erase(it);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
m_parent = nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLNode *DDLNode::getParent() const {
|
|
|
|
return m_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DDLNode::DllNodeList &DDLNode::getChildNodeList() const {
|
|
|
|
return m_children;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::setType(const std::string &type) {
|
2015-01-31 10:07:48 +00:00
|
|
|
m_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &DDLNode::getType() const {
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::setName(const std::string &name) {
|
2015-01-31 10:07:48 +00:00
|
|
|
m_name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &DDLNode::getName() const {
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::setProperties(Property *prop) {
|
|
|
|
if (m_properties != nullptr)
|
2017-06-19 19:07:18 +00:00
|
|
|
delete m_properties;
|
2015-02-07 11:47:53 +00:00
|
|
|
m_properties = prop;
|
|
|
|
}
|
|
|
|
|
|
|
|
Property *DDLNode::getProperties() const {
|
|
|
|
return m_properties;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
bool DDLNode::hasProperty(const std::string &name) {
|
|
|
|
const Property *prop(findPropertyByName(name));
|
|
|
|
return (nullptr != prop);
|
2015-04-16 09:09:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 18:29:15 +00:00
|
|
|
bool DDLNode::hasProperties() const {
|
2020-10-25 09:34:43 +00:00
|
|
|
return (nullptr != m_properties);
|
2015-10-01 18:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Property *DDLNode::findPropertyByName(const std::string &name) {
|
|
|
|
if (name.empty()) {
|
|
|
|
return nullptr;
|
2015-04-16 09:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr == m_properties) {
|
|
|
|
return nullptr;
|
2015-04-16 09:09:54 +00:00
|
|
|
}
|
2015-10-31 08:43:34 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Property *current(m_properties);
|
|
|
|
while (nullptr != current) {
|
|
|
|
int res = strncmp(current->m_key->m_buffer, name.c_str(), name.size());
|
|
|
|
if (0 == res) {
|
2015-04-16 09:09:54 +00:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
current = current->m_next;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
return nullptr;
|
2015-04-16 09:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::setValue(Value *val) {
|
2015-01-31 10:07:48 +00:00
|
|
|
m_value = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *DDLNode::getValue() const {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::setDataArrayList(DataArrayList *dtArrayList) {
|
2015-01-31 10:07:48 +00:00
|
|
|
m_dtArrayList = dtArrayList;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataArrayList *DDLNode::getDataArrayList() const {
|
|
|
|
return m_dtArrayList;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::setReferences(Reference *refs) {
|
2015-03-09 09:17:23 +00:00
|
|
|
m_references = refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference *DDLNode::getReferences() const {
|
|
|
|
return m_references;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void DDLNode::dump(IOStreamBase &stream) {
|
|
|
|
if (!stream.isOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &type = this->getType();
|
|
|
|
stream.write("type = " + type);
|
|
|
|
Value::Iterator it(getValue());
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Value *v = it.getNext();
|
|
|
|
v->dump(stream);
|
|
|
|
}
|
2017-06-25 11:21:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
DDLNode *DDLNode::create(const std::string &type, const std::string &name, DDLNode *parent) {
|
|
|
|
const size_t idx(s_allocatedNodes.size());
|
|
|
|
DDLNode *node = new DDLNode(type, name, idx, parent);
|
|
|
|
s_allocatedNodes.push_back(node);
|
|
|
|
|
2015-01-31 10:07:48 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DDLNode::releaseNodes() {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (s_allocatedNodes.size() > 0) {
|
|
|
|
for (DDLNodeIt it = s_allocatedNodes.begin(); it != s_allocatedNodes.end(); it++) {
|
|
|
|
if (*it) {
|
2015-01-31 10:07:48 +00:00
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s_allocatedNodes.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
END_ODDLPARSER_NS
|