assimp/contrib/openddlparser/code/OpenDDLExport.cpp

362 lines
10 KiB
C++
Raw Normal View History

2015-10-01 22:38:50 +00:00
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)
2020-10-25 09:34:43 +00:00
Copyright (c) 2014-2020 Kim Kulling
2015-10-01 22:38:50 +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>
2020-10-25 09:34:43 +00:00
#include <openddlparser/OpenDDLExport.h>
2015-10-31 08:43:34 +00:00
#include <openddlparser/OpenDDLParser.h>
2020-10-25 09:34:43 +00:00
#include <openddlparser/Value.h>
2015-10-31 08:43:34 +00:00
#include <sstream>
2015-10-01 22:38:50 +00:00
BEGIN_ODDLPARSER_NS
struct DDLNodeIterator {
const DDLNode::DllNodeList &m_childs;
size_t m_idx;
2015-10-31 08:43:34 +00:00
2020-10-25 09:34:43 +00:00
DDLNodeIterator(const DDLNode::DllNodeList &childs) :
m_childs(childs), m_idx(0) {
2015-10-31 08:43:34 +00:00
// empty
}
2015-10-01 22:38:50 +00:00
2015-10-31 08:43:34 +00:00
~DDLNodeIterator() {
// empty
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
bool getNext(DDLNode **node) {
if (m_childs.size() > (m_idx + 1)) {
2015-10-01 22:38:50 +00:00
m_idx++;
2020-10-25 09:34:43 +00:00
*node = m_childs[m_idx];
2015-10-01 22:38:50 +00:00
return true;
}
return false;
}
private:
DDLNodeIterator() ddl_no_copy;
2020-10-25 09:34:43 +00:00
DDLNodeIterator &operator=(const DDLNodeIterator &) ddl_no_copy;
2015-10-01 22:38:50 +00:00
};
2020-10-25 09:34:43 +00:00
static void writeLineEnd(std::string &statement) {
2015-10-31 08:43:34 +00:00
statement += "\n";
}
2015-10-01 22:38:50 +00:00
2020-10-25 09:34:43 +00:00
OpenDDLExport::OpenDDLExport(IOStreamBase *stream) :
m_stream(stream) {
if (nullptr == m_stream) {
2015-10-31 08:43:34 +00:00
m_stream = new IOStreamBase();
}
2015-10-01 22:38:50 +00:00
}
OpenDDLExport::~OpenDDLExport() {
2020-10-25 09:34:43 +00:00
if (nullptr != m_stream) {
2015-10-31 08:43:34 +00:00
m_stream->close();
2015-10-01 22:38:50 +00:00
}
2015-10-31 08:43:34 +00:00
delete m_stream;
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::exportContext(Context *ctx, const std::string &filename) {
if (nullptr == ctx) {
2015-10-01 22:38:50 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
DDLNode *root(ctx->m_root);
if (nullptr == root) {
2015-10-01 22:38:50 +00:00
return true;
}
2015-10-31 08:43:34 +00:00
if (!filename.empty()) {
2020-10-25 09:34:43 +00:00
if (!m_stream->open(filename)) {
2015-10-31 08:43:34 +00:00
return false;
}
}
2020-10-25 09:34:43 +00:00
const bool retValue(handleNode(root));
2015-10-31 08:43:34 +00:00
return retValue;
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::handleNode(DDLNode *node) {
if (nullptr == node) {
2015-10-01 22:38:50 +00:00
return true;
}
const DDLNode::DllNodeList &childs = node->getChildNodeList();
2020-10-25 09:34:43 +00:00
if (childs.empty()) {
2015-10-01 22:38:50 +00:00
return true;
}
2020-10-25 09:34:43 +00:00
DDLNode *current(nullptr);
DDLNodeIterator it(childs);
2015-10-31 08:43:34 +00:00
std::string statement;
2020-10-25 09:34:43 +00:00
bool success(true);
while (it.getNext(&current)) {
if (nullptr != current) {
success |= writeNode(current, statement);
if (!handleNode(current)) {
2015-10-31 08:43:34 +00:00
success = false;
2015-10-01 22:38:50 +00:00
}
}
}
return success;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeToStream(const std::string &statement) {
if (nullptr == m_stream) {
2015-10-31 08:43:34 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
if (!statement.empty()) {
m_stream->write(statement);
2015-10-31 08:43:34 +00:00
}
return true;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeNode(DDLNode *node, std::string &statement) {
writeNodeHeader(node, statement);
2015-10-31 08:43:34 +00:00
if (node->hasProperties()) {
2021-08-11 23:33:10 +00:00
writeProperties(node, statement);
2015-10-31 08:43:34 +00:00
}
2020-10-25 09:34:43 +00:00
writeLineEnd(statement);
2015-10-31 08:43:34 +00:00
statement = "}";
2020-10-25 09:34:43 +00:00
DataArrayList *al(node->getDataArrayList());
if (nullptr != al) {
writeValueType(al->m_dataList->m_type, al->m_numItems, statement);
writeValueArray(al, statement);
2015-10-31 08:43:34 +00:00
}
2020-10-25 09:34:43 +00:00
Value *v(node->getValue());
if (nullptr != v) {
writeValueType(v->m_type, 1, statement);
2015-10-31 08:43:34 +00:00
statement = "{";
2020-10-25 09:34:43 +00:00
writeLineEnd(statement);
writeValue(v, statement);
2015-10-31 08:43:34 +00:00
statement = "}";
2020-10-25 09:34:43 +00:00
writeLineEnd(statement);
2015-10-31 08:43:34 +00:00
}
statement = "}";
2020-10-25 09:34:43 +00:00
writeLineEnd(statement);
2015-10-31 08:43:34 +00:00
2020-10-25 09:34:43 +00:00
writeToStream(statement);
2015-10-31 08:43:34 +00:00
return true;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeNodeHeader(DDLNode *node, std::string &statement) {
if (nullptr == node) {
2015-10-31 08:43:34 +00:00
return false;
}
statement += node->getType();
2020-10-25 09:34:43 +00:00
const std::string &name(node->getName());
if (!name.empty()) {
2015-10-31 08:43:34 +00:00
statement += " ";
statement += "$";
statement += name;
2015-10-01 22:38:50 +00:00
}
return true;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeProperties(DDLNode *node, std::string &statement) {
if (nullptr == node) {
2015-10-31 08:43:34 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
Property *prop(node->getProperties());
2015-10-31 08:43:34 +00:00
// if no properties are there, return
2020-10-25 09:34:43 +00:00
if (nullptr == prop) {
2015-10-31 08:43:34 +00:00
return true;
}
2020-10-25 09:34:43 +00:00
if (nullptr != prop) {
2015-10-31 08:43:34 +00:00
// for instance (attrib = "position", bla=2)
statement += "(";
2020-10-25 09:34:43 +00:00
bool first(true);
while (nullptr != prop) {
2015-10-31 08:43:34 +00:00
if (!first) {
statement += ", ";
} else {
first = false;
}
2020-10-25 09:34:43 +00:00
statement += std::string(prop->m_key->m_buffer);
2015-10-31 08:43:34 +00:00
statement += " = ";
2020-10-25 09:34:43 +00:00
writeValue(prop->m_value, statement);
2015-10-31 08:43:34 +00:00
prop = prop->m_next;
}
statement += ")";
}
return true;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeValueType(Value::ValueType type, size_t numItems, std::string &statement) {
if (Value::ValueType::ddl_types_max == type) {
2015-10-31 08:43:34 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
const std::string typeStr(getTypeToken(type));
2015-10-31 08:43:34 +00:00
statement += typeStr;
// if we have an array to write
2020-10-25 09:34:43 +00:00
if (numItems > 1) {
2015-10-31 08:43:34 +00:00
statement += "[";
2020-10-25 09:34:43 +00:00
char buffer[256];
::memset(buffer, '\0', 256 * sizeof(char));
snprintf(buffer, sizeof(buffer), "%d", static_cast<int>(numItems));
2015-10-31 08:43:34 +00:00
statement += buffer;
statement += "]";
}
return true;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeValue(Value *val, std::string &statement) {
if (nullptr == val) {
2015-10-31 08:43:34 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
switch (val->m_type) {
case Value::ValueType::ddl_bool:
if (true == val->getBool()) {
2015-10-31 08:43:34 +00:00
statement += "true";
} else {
statement += "false";
}
break;
2020-10-25 09:34:43 +00:00
case Value::ValueType::ddl_int8 : {
std::stringstream stream;
const int i = static_cast<int>(val->getInt8());
stream << i;
statement += stream.str();
} break;
case Value::ValueType::ddl_int16: {
std::stringstream stream;
char buffer[256];
::memset(buffer, '\0', 256 * sizeof(char));
snprintf(buffer, sizeof(buffer), "%d", val->getInt16());
2020-10-25 09:34:43 +00:00
statement += buffer;
} break;
case Value::ValueType::ddl_int32: {
std::stringstream stream;
char buffer[256];
::memset(buffer, '\0', 256 * sizeof(char));
const int i = static_cast<int>(val->getInt32());
snprintf(buffer, sizeof(buffer), "%d", i);
2020-10-25 09:34:43 +00:00
statement += buffer;
} break;
case Value::ValueType::ddl_int64: {
std::stringstream stream;
const int i = static_cast<int>(val->getInt64());
stream << i;
statement += stream.str();
} break;
case Value::ValueType::ddl_unsigned_int8: {
std::stringstream stream;
const int i = static_cast<unsigned int>(val->getUnsignedInt8());
stream << i;
statement += stream.str();
} break;
case Value::ValueType::ddl_unsigned_int16: {
std::stringstream stream;
const int i = static_cast<unsigned int>(val->getUnsignedInt16());
stream << i;
statement += stream.str();
} break;
case Value::ValueType::ddl_unsigned_int32: {
std::stringstream stream;
const int i = static_cast<unsigned int>(val->getUnsignedInt32());
stream << i;
statement += stream.str();
} break;
case Value::ValueType::ddl_unsigned_int64: {
std::stringstream stream;
const int i = static_cast<unsigned int>(val->getUnsignedInt64());
stream << i;
statement += stream.str();
} break;
case Value::ValueType::ddl_half:
2015-10-31 08:43:34 +00:00
break;
2020-10-25 09:34:43 +00:00
case Value::ValueType::ddl_float: {
std::stringstream stream;
stream << val->getFloat();
statement += stream.str();
} break;
case Value::ValueType::ddl_double: {
std::stringstream stream;
stream << val->getDouble();
statement += stream.str();
} break;
case Value::ValueType::ddl_string: {
std::stringstream stream;
stream << val->getString();
statement += "\"";
statement += stream.str();
statement += "\"";
} break;
case Value::ValueType::ddl_ref:
2015-10-31 08:43:34 +00:00
break;
2020-10-25 09:34:43 +00:00
case Value::ValueType::ddl_none:
case Value::ValueType::ddl_types_max:
2015-10-31 08:43:34 +00:00
default:
break;
}
return true;
}
2020-10-25 09:34:43 +00:00
bool OpenDDLExport::writeValueArray(DataArrayList *al, std::string &statement) {
if (nullptr == al) {
2015-10-31 08:43:34 +00:00
return false;
}
if (0 == al->m_numItems) {
return true;
}
2020-10-25 09:34:43 +00:00
DataArrayList *nextDataArrayList = al;
Value *nextValue(nextDataArrayList->m_dataList);
while (nullptr != nextDataArrayList) {
if (nullptr != nextDataArrayList) {
2015-10-31 08:43:34 +00:00
statement += "{ ";
2020-10-25 09:34:43 +00:00
nextValue = nextDataArrayList->m_dataList;
size_t idx(0);
while (nullptr != nextValue) {
2015-10-31 08:43:34 +00:00
if (idx > 0) {
statement += ", ";
}
2020-10-25 09:34:43 +00:00
writeValue(nextValue, statement);
2015-10-31 08:43:34 +00:00
nextValue = nextValue->m_next;
idx++;
}
statement += " }";
}
nextDataArrayList = nextDataArrayList->m_next;
}
2015-10-01 22:38:50 +00:00
return true;
}
END_ODDLPARSER_NS