OgreXmlHelper.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "irrXMLWrapper.h"
  2. #include "fast_atof.h"
  3. namespace Assimp
  4. {
  5. namespace Ogre
  6. {
  7. typedef irr::io::IrrXMLReader XmlReader;
  8. //------------Helper Funktion to Get a Attribute Save---------------
  9. template<typename t> inline t GetAttribute(XmlReader* Reader, std::string Name);
  10. /*
  11. {
  12. BOOST_STATIC_ASSERT(false);
  13. return t();
  14. }
  15. */
  16. template<> inline int GetAttribute<int>(XmlReader* Reader, std::string Name)
  17. {
  18. const char* Value=Reader->getAttributeValue(Name.c_str());
  19. if(Value)
  20. return atoi(Value);
  21. else
  22. throw DeadlyImportError(std::string("Attribute "+Name+" does not exist in "+Reader->getNodeName()).c_str());
  23. }
  24. template<> inline unsigned int GetAttribute<unsigned int>(XmlReader* Reader, std::string Name)
  25. {
  26. const char* Value=Reader->getAttributeValue(Name.c_str());
  27. if(Value)
  28. return static_cast<unsigned int>(atoi(Value));//yes, ugly, but pfff
  29. else
  30. throw DeadlyImportError(std::string("Attribute "+Name+" does not exist in "+Reader->getNodeName()).c_str());
  31. }
  32. template<> inline float GetAttribute<float>(XmlReader* Reader, std::string Name)
  33. {
  34. const char* Value=Reader->getAttributeValue(Name.c_str());
  35. if(Value)
  36. return fast_atof(Value);
  37. else
  38. throw DeadlyImportError(std::string("Attribute "+Name+" does not exist in "+Reader->getNodeName()).c_str());
  39. }
  40. template<> inline std::string GetAttribute<std::string>(XmlReader* Reader, std::string Name)
  41. {
  42. const char* Value=Reader->getAttributeValue(Name.c_str());
  43. if(Value)
  44. return std::string(Value);
  45. else
  46. throw DeadlyImportError(std::string("Attribute "+Name+" does not exist in "+Reader->getNodeName()).c_str());
  47. }
  48. template<> inline bool GetAttribute<bool>(XmlReader* Reader, std::string Name)
  49. {
  50. const char* Value=Reader->getAttributeValue(Name.c_str());
  51. if(Value)
  52. {
  53. if(Value==std::string("true"))
  54. return true;
  55. else if(Value==std::string("false"))
  56. return false;
  57. else
  58. throw DeadlyImportError(std::string("Bool value has invalid value: "+Name+" / "+Value+" / "+Reader->getNodeName()));
  59. }
  60. else
  61. throw DeadlyImportError(std::string("Attribute "+Name+" does not exist in "+Reader->getNodeName()).c_str());
  62. }
  63. //__________________________________________________________________
  64. inline bool XmlRead(XmlReader* Reader)
  65. {
  66. do
  67. {
  68. if(!Reader->read())
  69. return false;
  70. }
  71. while(Reader->getNodeType()!=irr::io::EXN_ELEMENT);
  72. return true;
  73. }
  74. }//namespace Ogre
  75. }//namespace Assimp