2012-02-20 10:27:27 +00:00
|
|
|
|
2014-05-01 14:19:03 +00:00
|
|
|
#ifndef AI_OGREPARSINGUTILS_H_INC
|
|
|
|
#define AI_OGREPARSINGUTILS_H_INC
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
#include "ParsingUtils.h"
|
2012-02-20 10:27:27 +00:00
|
|
|
#include "irrXMLWrapper.h"
|
|
|
|
#include "fast_atof.h"
|
2014-05-04 14:02:52 +00:00
|
|
|
#include <functional>
|
2012-02-20 10:27:27 +00:00
|
|
|
namespace Assimp
|
|
|
|
{
|
|
|
|
namespace Ogre
|
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
|
2012-02-20 10:27:27 +00:00
|
|
|
typedef irr::io::IrrXMLReader XmlReader;
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
static void ThrowAttibuteError(const XmlReader* reader, const std::string &name, const std::string &error = "")
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
if (!error.empty())
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError(error + " in node '" + std::string(reader->getNodeName()) + "' and attribute '" + name + "'");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-04-30 02:43:13 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError("Attribute '" + name + "' does not exist in node '" + std::string(reader->getNodeName()) + "'");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
template<typename T>
|
|
|
|
inline T GetAttribute(const XmlReader* reader, const std::string &name);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline int GetAttribute<int>(const XmlReader* reader, const std::string &name)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
const char* value = reader->getAttributeValue(name.c_str());
|
|
|
|
if (value)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
return atoi(value);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
else
|
2014-05-01 22:38:13 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
ThrowAttibuteError(reader, name);
|
2014-05-01 22:38:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
template<>
|
|
|
|
inline unsigned int GetAttribute<unsigned int>(const XmlReader* reader, const std::string &name)
|
2012-05-23 09:23:34 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
const char* value = reader->getAttributeValue(name.c_str());
|
|
|
|
if (value)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
return static_cast<unsigned int>(atoi(value)); ///< @todo Find a better way...
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-05-23 09:23:34 +00:00
|
|
|
else
|
2014-05-01 22:38:13 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
ThrowAttibuteError(reader, name);
|
2014-05-01 22:38:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-05-23 09:23:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
template<>
|
|
|
|
inline float GetAttribute<float>(const XmlReader* reader, const std::string &name)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
const char* value = reader->getAttributeValue(name.c_str());
|
|
|
|
if (value)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
return fast_atof(value);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
else
|
2014-05-01 22:38:13 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
ThrowAttibuteError(reader, name);
|
2014-05-01 22:38:13 +00:00
|
|
|
return 0.f;
|
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
template<>
|
|
|
|
inline std::string GetAttribute<std::string>(const XmlReader* reader, const std::string &name)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
const char* value = reader->getAttributeValue(name.c_str());
|
|
|
|
if (value)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
return std::string(value);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
else
|
2014-05-01 22:38:13 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
ThrowAttibuteError(reader, name);
|
2014-05-01 22:38:13 +00:00
|
|
|
return "";
|
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
template<>
|
|
|
|
inline bool GetAttribute<bool>(const XmlReader* reader, const std::string &name)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
std::string value = GetAttribute<std::string>(reader, name);
|
|
|
|
if (ASSIMP_stricmp(value, "true") == 0)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
return true;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-04-30 02:43:13 +00:00
|
|
|
else if (ASSIMP_stricmp(value, "false") == 0)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
return false;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
else
|
2014-05-01 22:38:13 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
ThrowAttibuteError(reader, name, "Boolean value is expected to be 'true' or 'false', encountered '" + value + "'");
|
2014-05-01 22:38:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
inline bool NextNode(XmlReader* reader)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!reader->read()) {
|
2012-02-20 10:27:27 +00:00
|
|
|
return false;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
2014-04-30 02:43:13 +00:00
|
|
|
while(reader->getNodeType() != irr::io::EXN_ELEMENT);
|
2012-02-20 10:27:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
inline bool CurrentNodeNameEquals(const XmlReader* reader, const std::string &name)
|
|
|
|
{
|
|
|
|
return (ASSIMP_stricmp(std::string(reader->getNodeName()), name) == 0);
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:49:26 +00:00
|
|
|
/// Skips a line from current @ss position until a newline. Returns the skipped part.
|
|
|
|
static inline std::string SkipLine(std::stringstream &ss)
|
|
|
|
{
|
|
|
|
std::string skipped;
|
|
|
|
getline(ss, skipped);
|
|
|
|
return skipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Skips a line and reads next element from @c ss to @c nextElement.
|
|
|
|
/** @return Skipped line content until newline. */
|
|
|
|
static inline std::string NextAfterNewLine(std::stringstream &ss, std::string &nextElement)
|
|
|
|
{
|
|
|
|
std::string skipped = SkipLine(ss);
|
|
|
|
ss >> nextElement;
|
|
|
|
return skipped;
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
/// Returns a lower cased copy of @s.
|
|
|
|
static inline std::string ToLower(std::string s)
|
|
|
|
{
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:51:26 +00:00
|
|
|
/// Returns if @c s ends with @c suffix. If @c caseSensitive is false, both strings will be lower cased before matching.
|
|
|
|
static inline bool EndsWith(const std::string &s, const std::string &suffix, bool caseSensitive = true)
|
|
|
|
{
|
|
|
|
if (s.empty() || suffix.empty())
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 15:51:26 +00:00
|
|
|
return false;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 15:51:26 +00:00
|
|
|
else if (s.length() < suffix.length())
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 15:51:26 +00:00
|
|
|
return false;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 15:51:26 +00:00
|
|
|
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!caseSensitive) {
|
2014-05-01 15:51:26 +00:00
|
|
|
return EndsWith(ToLower(s), ToLower(suffix), true);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
|
2014-05-01 15:51:26 +00:00
|
|
|
size_t len = suffix.length();
|
|
|
|
std::string sSuffix = s.substr(s.length()-len, len);
|
|
|
|
return (ASSIMP_stricmp(sSuffix, suffix) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Below trim functions adapted from http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
|
|
|
|
|
|
|
|
/// Trim from start
|
2014-05-01 13:49:26 +00:00
|
|
|
static inline std::string &TrimLeft(std::string &s, bool newlines = true)
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
if (!newlines)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(Assimp::IsSpace<char>))));
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(Assimp::IsSpaceOrNewLine<char>))));
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:51:26 +00:00
|
|
|
/// Trim from end
|
2014-05-01 13:49:26 +00:00
|
|
|
static inline std::string &TrimRight(std::string &s, bool newlines = true)
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
if (!newlines)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(Assimp::IsSpace<char>))).base(),s.end());
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(Assimp::IsSpaceOrNewLine<char>))));
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
return s;
|
|
|
|
}
|
2014-05-01 15:51:26 +00:00
|
|
|
|
|
|
|
/// Trim from both ends
|
2014-05-01 13:49:26 +00:00
|
|
|
static inline std::string &Trim(std::string &s, bool newlines = true)
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
2014-05-01 13:49:26 +00:00
|
|
|
return TrimLeft(TrimRight(s, newlines), newlines);
|
2014-05-01 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
} // Ogre
|
|
|
|
} // Assimp
|
2014-05-01 14:19:03 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
#endif // AI_OGREPARSINGUTILS_H_INC
|