XmlFile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "core/XmlFile.h"
  2. #include <OgreStringConverter.h>
  3. #include "core/Logger.h"
  4. XmlFile::XmlFile( const Ogre::String& file ):
  5. m_File( file )
  6. {
  7. m_File.SetCondenseWhiteSpace( false );
  8. m_NormalFile = m_File.LoadFile();
  9. if( m_NormalFile == false )
  10. {
  11. LOG_ERROR( "Can't open " + file + ". TinyXml Error: " + m_File.ErrorDesc() );
  12. }
  13. }
  14. XmlFile::~XmlFile()
  15. {
  16. }
  17. bool
  18. XmlFile::GetBool( TiXmlNode* node, const Ogre::String& tag, bool def ) const
  19. {
  20. bool ret = def;
  21. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  22. {
  23. const std::string* string = node->ToElement()->Attribute( tag );
  24. if( string != NULL )
  25. {
  26. ret = Ogre::StringConverter::parseBool( *string );
  27. }
  28. }
  29. return ret;
  30. }
  31. int
  32. XmlFile::GetInt( TiXmlNode* node, const Ogre::String& tag, int def ) const
  33. {
  34. int ret = def;
  35. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  36. {
  37. const std::string* string = node->ToElement()->Attribute( tag );
  38. if( string != NULL )
  39. {
  40. ret = Ogre::StringConverter::parseInt( *string );
  41. }
  42. }
  43. return ret;
  44. }
  45. float
  46. XmlFile::GetFloat( TiXmlNode* node, const Ogre::String& tag, float def ) const
  47. {
  48. float ret = def;
  49. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  50. {
  51. const std::string* string = node->ToElement()->Attribute( tag );
  52. if( string != NULL )
  53. {
  54. ret = Ogre::StringConverter::parseReal( *string );
  55. }
  56. }
  57. return ret;
  58. }
  59. const Ogre::String
  60. XmlFile::GetString( TiXmlNode* node, const Ogre::String& tag, const Ogre::String& def ) const
  61. {
  62. Ogre::String ret = def;
  63. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  64. {
  65. const std::string* string = node->ToElement()->Attribute( tag );
  66. if( string != NULL )
  67. {
  68. ret = *string;
  69. }
  70. }
  71. return ret;
  72. }
  73. const Ogre::UTFString
  74. XmlFile::GetUTFString( TiXmlNode* node, const Ogre::String& tag, const Ogre::UTFString& def ) const
  75. {
  76. Ogre::UTFString ret = def;
  77. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  78. {
  79. const std::string* string = node->ToElement()->Attribute( tag );
  80. if( string != NULL )
  81. {
  82. ret = ( *string ).c_str();
  83. }
  84. }
  85. return ret;
  86. }
  87. const Ogre::Vector2
  88. XmlFile::GetVector2( TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector2& def ) const
  89. {
  90. Ogre::Vector2 ret = def;
  91. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  92. {
  93. const std::string* string = node->ToElement()->Attribute( tag );
  94. if( string != NULL )
  95. {
  96. ret = Ogre::StringConverter::parseVector2( *string );
  97. }
  98. }
  99. return ret;
  100. }
  101. const Ogre::Vector3
  102. XmlFile::GetVector3( TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector3& def ) const
  103. {
  104. Ogre::Vector3 ret = def;
  105. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  106. {
  107. const std::string* string = node->ToElement()->Attribute( tag );
  108. if( string != NULL )
  109. {
  110. ret = Ogre::StringConverter::parseVector3( *string );
  111. }
  112. }
  113. return ret;
  114. }
  115. const Ogre::Vector4
  116. XmlFile::GetVector4( TiXmlNode* node, const Ogre::String& tag, const Ogre::Vector4& def ) const
  117. {
  118. Ogre::Vector4 ret = def;
  119. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  120. {
  121. const std::string* string = node->ToElement()->Attribute( tag );
  122. if( string != NULL )
  123. {
  124. ret = Ogre::StringConverter::parseVector4( *string );
  125. }
  126. }
  127. return ret;
  128. }
  129. const Ogre::Matrix4
  130. XmlFile::GetMatrix4( TiXmlNode* node, const Ogre::String& tag, const Ogre::Matrix4& def ) const
  131. {
  132. Ogre::Matrix4 ret = def;
  133. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  134. {
  135. const std::string* string = node->ToElement()->Attribute( tag );
  136. if( string != NULL )
  137. {
  138. ret = Ogre::StringConverter::parseMatrix4( *string );
  139. }
  140. }
  141. return ret;
  142. }
  143. const Ogre::Quaternion
  144. XmlFile::GetQuaternion( TiXmlNode* node, const Ogre::String& tag, const Ogre::Quaternion& def ) const
  145. {
  146. Ogre::Quaternion ret = def;
  147. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  148. {
  149. const std::string* string = node->ToElement()->Attribute( tag );
  150. if( string != NULL )
  151. {
  152. ret = Ogre::StringConverter::parseQuaternion( *string );
  153. }
  154. }
  155. return ret;
  156. }
  157. const Ogre::ColourValue
  158. XmlFile::GetColourValue( TiXmlNode* node, const Ogre::String& tag, const Ogre::ColourValue& def ) const
  159. {
  160. Ogre::ColourValue ret = def;
  161. if( node->Type() == TiXmlNode::TINYXML_ELEMENT )
  162. {
  163. const std::string* string = node->ToElement()->Attribute( tag );
  164. if( string != NULL )
  165. {
  166. ret = Ogre::StringConverter::parseColourValue( *string );
  167. }
  168. }
  169. return ret;
  170. }