assimp/contrib/openddlparser/code/OpenDDLCommon.cpp

201 lines
4.8 KiB
C++
Raw Permalink 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/OpenDDLCommon.h>
#include <openddlparser/Value.h>
2015-10-01 22:38:50 +00:00
BEGIN_ODDLPARSER_NS
2020-10-25 09:34:43 +00:00
Text::Text(const char *buffer, size_t numChars) :
m_capacity(0),
m_len(0),
m_buffer(nullptr) {
set(buffer, numChars);
2015-10-01 22:38:50 +00:00
}
Text::~Text() {
clear();
}
void Text::clear() {
delete[] m_buffer;
2020-10-25 09:34:43 +00:00
m_buffer = nullptr;
2015-10-01 22:38:50 +00:00
m_capacity = 0;
m_len = 0;
}
2020-10-25 09:34:43 +00:00
void Text::set(const char *buffer, size_t numChars) {
2015-10-01 22:38:50 +00:00
clear();
2020-10-25 09:34:43 +00:00
if (numChars > 0) {
2015-10-01 22:38:50 +00:00
m_len = numChars;
m_capacity = m_len + 1;
2020-10-25 09:34:43 +00:00
m_buffer = new char[m_capacity];
strncpy(m_buffer, buffer, numChars);
m_buffer[numChars] = '\0';
2015-10-01 22:38:50 +00:00
}
}
2020-10-25 09:34:43 +00:00
bool Text::operator==(const std::string &name) const {
if (m_len != name.size()) {
2015-10-01 22:38:50 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
const int res(strncmp(m_buffer, name.c_str(), name.size()));
2015-10-01 22:38:50 +00:00
2020-10-25 09:34:43 +00:00
return (0 == res);
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
bool Text::operator==(const Text &rhs) const {
if (m_len != rhs.m_len) {
2015-10-01 22:38:50 +00:00
return false;
}
2020-10-25 09:34:43 +00:00
const int res(strncmp(m_buffer, rhs.m_buffer, m_len));
2015-10-01 22:38:50 +00:00
2020-10-25 09:34:43 +00:00
return (0 == res);
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
Name::Name(NameType type, Text *id) :
m_type(type), m_id(id) {
2015-10-01 22:38:50 +00:00
// empty
}
Name::~Name() {
delete m_id;
2020-10-25 09:34:43 +00:00
m_id = nullptr;
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
Name::Name(const Name &name) {
m_type = name.m_type;
m_id = new Text(name.m_id->m_buffer, name.m_id->m_len);
}
2020-10-25 09:34:43 +00:00
Reference::Reference() :
m_numRefs(0), m_referencedName(nullptr) {
2015-10-01 22:38:50 +00:00
// empty
}
2020-10-25 09:34:43 +00:00
Reference::Reference(size_t numrefs, Name **names) :
m_numRefs(numrefs), m_referencedName(nullptr) {
if (numrefs > 0) {
m_referencedName = new Name *[numrefs];
for (size_t i = 0; i < numrefs; i++) {
m_referencedName[i] = names[i];
}
}
}
Reference::Reference(const Reference &ref) {
2020-10-25 09:34:43 +00:00
m_numRefs = ref.m_numRefs;
if (m_numRefs != 0) {
m_referencedName = new Name *[m_numRefs];
for (size_t i = 0; i < m_numRefs; i++) {
m_referencedName[i] = new Name(*ref.m_referencedName[i]);
}
2015-10-01 22:38:50 +00:00
}
}
Reference::~Reference() {
2020-10-25 09:34:43 +00:00
for (size_t i = 0; i < m_numRefs; i++) {
delete m_referencedName[i];
2015-10-01 22:38:50 +00:00
}
m_numRefs = 0;
2020-10-25 09:34:43 +00:00
delete[] m_referencedName;
m_referencedName = nullptr;
2015-10-01 22:38:50 +00:00
}
size_t Reference::sizeInBytes() {
2020-10-25 09:34:43 +00:00
if (0 == m_numRefs) {
return 0;
}
2020-10-25 09:34:43 +00:00
size_t size(0);
for (size_t i = 0; i < m_numRefs; i++) {
Name *name(m_referencedName[i]);
if (nullptr != name) {
size += name->m_id->m_len;
}
}
return size;
}
2020-10-25 09:34:43 +00:00
Property::Property(Text *id) :
m_key(id), m_value(nullptr), m_ref(nullptr), m_next(nullptr) {
2015-10-01 22:38:50 +00:00
// empty
}
Property::~Property() {
delete m_key;
2020-10-25 09:34:43 +00:00
if (m_value != nullptr)
delete m_value;
2020-10-25 09:34:43 +00:00
if (m_ref != nullptr)
delete (m_ref);
if (m_next != nullptr)
delete m_next;
2015-10-01 22:38:50 +00:00
}
2020-10-25 09:34:43 +00:00
DataArrayList::DataArrayList() :
m_numItems(0), m_dataList(nullptr), m_next(nullptr), m_refs(nullptr), m_numRefs(0) {
2015-10-01 22:38:50 +00:00
// empty
}
DataArrayList::~DataArrayList() {
delete m_dataList;
2020-10-25 09:34:43 +00:00
if (m_next != nullptr)
delete m_next;
2020-10-25 09:34:43 +00:00
if (m_refs != nullptr)
delete m_refs;
2015-10-01 22:38:50 +00:00
}
size_t DataArrayList::size() {
2020-10-25 09:34:43 +00:00
size_t result(0);
if (nullptr == m_next) {
if (m_dataList != nullptr) {
2016-06-28 11:13:08 +00:00
result = 1;
}
return result;
}
2020-10-25 09:34:43 +00:00
DataArrayList *n(m_next);
while (nullptr != n) {
result++;
2016-06-28 11:13:08 +00:00
n = n->m_next;
}
return result;
}
2020-10-25 09:34:43 +00:00
Context::Context() :
m_root(nullptr) {
2015-10-01 22:38:50 +00:00
// empty
}
Context::~Context() {
clear();
2015-10-01 22:38:50 +00:00
}
void Context::clear() {
delete m_root;
2020-10-25 09:34:43 +00:00
m_root = nullptr;
2015-10-01 22:38:50 +00:00
}
END_ODDLPARSER_NS