OpenDDLCommon.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*-----------------------------------------------------------------------------------------------
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2015 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/OpenDDLCommon.h>
  20. #include <openddlparser/DDLNode.h>
  21. BEGIN_ODDLPARSER_NS
  22. Text::Text( const char *buffer, size_t numChars )
  23. : m_capacity( 0 )
  24. , m_len( 0 )
  25. , m_buffer( ddl_nullptr ) {
  26. set( buffer, numChars );
  27. }
  28. Text::~Text() {
  29. clear();
  30. }
  31. void Text::clear() {
  32. delete[] m_buffer;
  33. m_buffer = ddl_nullptr;
  34. m_capacity = 0;
  35. m_len = 0;
  36. }
  37. void Text::set( const char *buffer, size_t numChars ) {
  38. clear();
  39. if( numChars > 0 ) {
  40. m_len = numChars;
  41. m_capacity = m_len + 1;
  42. m_buffer = new char[ m_capacity ];
  43. strncpy( m_buffer, buffer, numChars );
  44. m_buffer[ numChars ] = '\0';
  45. }
  46. }
  47. bool Text::operator == ( const std::string &name ) const {
  48. if( m_len != name.size() ) {
  49. return false;
  50. }
  51. const int res( strncmp( m_buffer, name.c_str(), name.size() ) );
  52. return ( 0 == res );
  53. }
  54. bool Text::operator == ( const Text &rhs ) const {
  55. if( m_len != rhs.m_len ) {
  56. return false;
  57. }
  58. const int res( strncmp( m_buffer, rhs.m_buffer, m_len ) );
  59. return ( 0 == res );
  60. }
  61. Identifier::Identifier( const char buffer[], size_t len )
  62. : m_text( buffer, len ) {
  63. // empty
  64. }
  65. Identifier::Identifier( const char buffer[] )
  66. : m_text( buffer, strlen( buffer ) ) {
  67. // empty
  68. }
  69. Identifier::~Identifier() {
  70. // empty
  71. }
  72. bool Identifier::operator == ( const Identifier &rhs ) const {
  73. return m_text == rhs.m_text;
  74. }
  75. Name::Name( NameType type, Identifier *id )
  76. : m_type( type )
  77. , m_id( id ) {
  78. // empty
  79. }
  80. Name::~Name() {
  81. m_id = ddl_nullptr;
  82. }
  83. Reference::Reference()
  84. : m_numRefs( 0 )
  85. , m_referencedName( ddl_nullptr ) {
  86. // empty
  87. }
  88. Reference::Reference( size_t numrefs, Name **names )
  89. : m_numRefs( numrefs )
  90. , m_referencedName( ddl_nullptr ) {
  91. m_referencedName = new Name *[ numrefs ];
  92. for( size_t i = 0; i < numrefs; i++ ) {
  93. Name *name = new Name( names[ i ]->m_type, names[ i ]->m_id );
  94. m_referencedName[ i ] = name;
  95. }
  96. }
  97. Reference::~Reference() {
  98. for( size_t i = 0; i < m_numRefs; i++ ) {
  99. delete m_referencedName[ i ];
  100. }
  101. m_numRefs = 0;
  102. m_referencedName = ddl_nullptr;
  103. }
  104. Property::Property( Identifier *id )
  105. : m_key( id )
  106. , m_value( ddl_nullptr )
  107. , m_ref( ddl_nullptr )
  108. , m_next( ddl_nullptr ) {
  109. // empty
  110. }
  111. Property::~Property() {
  112. m_key = ddl_nullptr;
  113. m_value = ddl_nullptr;
  114. m_ref = ddl_nullptr;;
  115. m_next = ddl_nullptr;;
  116. }
  117. DataArrayList::DataArrayList()
  118. : m_numItems( 0 )
  119. , m_dataList( ddl_nullptr )
  120. , m_next( ddl_nullptr ) {
  121. // empty
  122. }
  123. DataArrayList::~DataArrayList() {
  124. // empty
  125. }
  126. Context::Context()
  127. : m_root( ddl_nullptr ) {
  128. // empty
  129. }
  130. Context::~Context() {
  131. m_root = ddl_nullptr;
  132. }
  133. void Context::clear() {
  134. delete m_root;
  135. m_root = ddl_nullptr;
  136. }
  137. END_ODDLPARSER_NS