OpenDDLCommon.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*-----------------------------------------------------------------------------------------------
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Kim Kulling
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. -----------------------------------------------------------------------------------------------*/
  19. #include <openddlparser/DDLNode.h>
  20. #include <openddlparser/OpenDDLCommon.h>
  21. #include <openddlparser/Value.h>
  22. BEGIN_ODDLPARSER_NS
  23. Text::Text(const char *buffer, size_t numChars) :
  24. m_capacity(0),
  25. m_len(0),
  26. m_buffer(nullptr) {
  27. set(buffer, numChars);
  28. }
  29. Text::~Text() {
  30. clear();
  31. }
  32. void Text::clear() {
  33. delete[] m_buffer;
  34. m_buffer = nullptr;
  35. m_capacity = 0;
  36. m_len = 0;
  37. }
  38. void Text::set(const char *buffer, size_t numChars) {
  39. clear();
  40. if (numChars > 0) {
  41. m_len = numChars;
  42. m_capacity = m_len + 1;
  43. m_buffer = new char[m_capacity];
  44. strncpy(m_buffer, buffer, numChars);
  45. m_buffer[numChars] = '\0';
  46. }
  47. }
  48. bool Text::operator==(const std::string &name) const {
  49. if (m_len != name.size()) {
  50. return false;
  51. }
  52. const int res(strncmp(m_buffer, name.c_str(), name.size()));
  53. return (0 == res);
  54. }
  55. bool Text::operator==(const Text &rhs) const {
  56. if (m_len != rhs.m_len) {
  57. return false;
  58. }
  59. const int res(strncmp(m_buffer, rhs.m_buffer, m_len));
  60. return (0 == res);
  61. }
  62. Name::Name(NameType type, Text *id) :
  63. m_type(type), m_id(id) {
  64. // empty
  65. }
  66. Name::~Name() {
  67. delete m_id;
  68. m_id = nullptr;
  69. }
  70. Name::Name(const Name &name) {
  71. m_type = name.m_type;
  72. m_id = new Text(name.m_id->m_buffer, name.m_id->m_len);
  73. }
  74. Reference::Reference() :
  75. m_numRefs(0), m_referencedName(nullptr) {
  76. // empty
  77. }
  78. Reference::Reference(size_t numrefs, Name **names) :
  79. m_numRefs(numrefs), m_referencedName(nullptr) {
  80. if (numrefs > 0) {
  81. m_referencedName = new Name *[numrefs];
  82. for (size_t i = 0; i < numrefs; i++) {
  83. m_referencedName[i] = names[i];
  84. }
  85. }
  86. }
  87. Reference::Reference(const Reference &ref) {
  88. m_numRefs = ref.m_numRefs;
  89. if (m_numRefs != 0) {
  90. m_referencedName = new Name *[m_numRefs];
  91. for (size_t i = 0; i < m_numRefs; i++) {
  92. m_referencedName[i] = new Name(*ref.m_referencedName[i]);
  93. }
  94. }
  95. }
  96. Reference::~Reference() {
  97. for (size_t i = 0; i < m_numRefs; i++) {
  98. delete m_referencedName[i];
  99. }
  100. m_numRefs = 0;
  101. delete[] m_referencedName;
  102. m_referencedName = nullptr;
  103. }
  104. size_t Reference::sizeInBytes() {
  105. if (0 == m_numRefs) {
  106. return 0;
  107. }
  108. size_t size(0);
  109. for (size_t i = 0; i < m_numRefs; i++) {
  110. Name *name(m_referencedName[i]);
  111. if (nullptr != name) {
  112. size += name->m_id->m_len;
  113. }
  114. }
  115. return size;
  116. }
  117. Property::Property(Text *id) :
  118. m_key(id), m_value(nullptr), m_ref(nullptr), m_next(nullptr) {
  119. // empty
  120. }
  121. Property::~Property() {
  122. delete m_key;
  123. if (m_value != nullptr)
  124. delete m_value;
  125. if (m_ref != nullptr)
  126. delete (m_ref);
  127. if (m_next != nullptr)
  128. delete m_next;
  129. }
  130. DataArrayList::DataArrayList() :
  131. m_numItems(0), m_dataList(nullptr), m_next(nullptr), m_refs(nullptr), m_numRefs(0) {
  132. // empty
  133. }
  134. DataArrayList::~DataArrayList() {
  135. delete m_dataList;
  136. if (m_next != nullptr)
  137. delete m_next;
  138. if (m_refs != nullptr)
  139. delete m_refs;
  140. }
  141. size_t DataArrayList::size() {
  142. size_t result(0);
  143. if (nullptr == m_next) {
  144. if (m_dataList != nullptr) {
  145. result = 1;
  146. }
  147. return result;
  148. }
  149. DataArrayList *n(m_next);
  150. while (nullptr != n) {
  151. result++;
  152. n = n->m_next;
  153. }
  154. return result;
  155. }
  156. Context::Context() :
  157. m_root(nullptr) {
  158. // empty
  159. }
  160. Context::~Context() {
  161. clear();
  162. }
  163. void Context::clear() {
  164. delete m_root;
  165. m_root = nullptr;
  166. }
  167. END_ODDLPARSER_NS