XmlFile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreStringConverter.h>
  16. #include "common/Logger.h"
  17. #include "common/XmlFile.h"
  18. XmlFile::XmlFile(const Ogre::String& file): file_(file){
  19. file_.SetCondenseWhiteSpace(false);
  20. normal_file_ = file_.LoadFile();
  21. if (normal_file_ == false)
  22. LOG_ERROR("Can't open " + file + ". TinyXml Error: " + file_.ErrorDesc());
  23. }
  24. XmlFile::~XmlFile(){}
  25. bool XmlFile::GetBool(TiXmlNode* node, const Ogre::String& tag, bool def) const{
  26. bool ret = def;
  27. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  28. const std::string* string = node->ToElement()->Attribute(tag);
  29. if (string != nullptr) ret = Ogre::StringConverter::parseBool(*string);
  30. }
  31. return ret;
  32. }
  33. int XmlFile::GetInt(TiXmlNode* node, const Ogre::String& tag, int def) const{
  34. int ret = def;
  35. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  36. const std::string* string = node->ToElement()->Attribute(tag);
  37. if (string != nullptr) ret = Ogre::StringConverter::parseInt(*string);
  38. }
  39. return ret;
  40. }
  41. float XmlFile::GetFloat(TiXmlNode* node, const Ogre::String& tag, float def) const{
  42. float ret = def;
  43. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  44. const std::string* string = node->ToElement()->Attribute(tag);
  45. if (string != nullptr) ret = Ogre::StringConverter::parseReal(*string);
  46. }
  47. return ret;
  48. }
  49. const Ogre::String XmlFile::GetString(
  50. TiXmlNode* node, const Ogre::String& tag, const Ogre::String& def
  51. ) const{
  52. Ogre::String ret = def;
  53. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  54. const std::string* string = node->ToElement()->Attribute(tag);
  55. if (string != nullptr) ret = *string;
  56. }
  57. return ret;
  58. }
  59. const Ogre::UTFString XmlFile::GetUTFString(
  60. TiXmlNode* node, const Ogre::String& tag, const Ogre::UTFString& def
  61. ) const{
  62. Ogre::UTFString ret = def;
  63. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  64. const std::string* string = node->ToElement()->Attribute(tag);
  65. if (string != nullptr) ret = (*string).c_str();
  66. }
  67. return ret;
  68. }
  69. const Ogre::Vector2 XmlFile::GetVector2(
  70. TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector2& def
  71. ) const{
  72. Ogre::Vector2 ret = def;
  73. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  74. const std::string* string = node->ToElement()->Attribute(tag);
  75. if (string != nullptr) ret = Ogre::StringConverter::parseVector2(*string);
  76. }
  77. return ret;
  78. }
  79. const Ogre::Vector3 XmlFile::GetVector3(
  80. TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector3& def
  81. ) const{
  82. Ogre::Vector3 ret = def;
  83. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  84. const std::string* string = node->ToElement()->Attribute(tag);
  85. if (string != nullptr) ret = Ogre::StringConverter::parseVector3(*string);
  86. }
  87. return ret;
  88. }
  89. const Ogre::Vector4 XmlFile::GetVector4(
  90. TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector4& def
  91. ) const{
  92. Ogre::Vector4 ret = def;
  93. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  94. const std::string* string = node->ToElement()->Attribute(tag);
  95. if (string != nullptr) ret = Ogre::StringConverter::parseVector4(*string);
  96. }
  97. return ret;
  98. }
  99. const Ogre::Matrix4 XmlFile::GetMatrix4(
  100. TiXmlNode* node, const Ogre::String& tag, const Ogre::Matrix4& def
  101. ) const{
  102. Ogre::Matrix4 ret = def;
  103. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  104. const std::string* string = node->ToElement()->Attribute(tag);
  105. if (string != nullptr) ret = Ogre::StringConverter::parseMatrix4(*string);
  106. }
  107. return ret;
  108. }
  109. const Ogre::Quaternion XmlFile::GetQuaternion(
  110. TiXmlNode* node, const Ogre::String& tag, const Ogre::Quaternion& def
  111. ) const{
  112. Ogre::Quaternion ret = def;
  113. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  114. const std::string* string = node->ToElement()->Attribute(tag);
  115. if (string != nullptr) ret = Ogre::StringConverter::parseQuaternion(*string);
  116. }
  117. return ret;
  118. }
  119. const Ogre::ColourValue XmlFile::GetColourValue(
  120. TiXmlNode* node, const Ogre::String& tag, const Ogre::ColourValue& def
  121. ) const{
  122. Ogre::ColourValue ret = def;
  123. if (node->Type() == TiXmlNode::TINYXML_ELEMENT){
  124. const std::string* string = node->ToElement()->Attribute(tag);
  125. if (string != nullptr) ret = Ogre::StringConverter::parseColourValue(*string);
  126. }
  127. return ret;
  128. }