Merge pull request #5157 from feuerste/file_extension
Unify extension check for importers.pull/5159/head^2
commit
53846430ce
|
@ -230,27 +230,30 @@ void BaseImporter::GetExtensionList(std::set<std::string> &extensions) {
|
||||||
const char *ext0,
|
const char *ext0,
|
||||||
const char *ext1,
|
const char *ext1,
|
||||||
const char *ext2) {
|
const char *ext2) {
|
||||||
std::string::size_type pos = pFile.find_last_of('.');
|
std::set<std::string> extensions;
|
||||||
|
for (const char* ext : {ext0, ext1, ext2}) {
|
||||||
// no file extension - can't read
|
if (ext == nullptr) continue;
|
||||||
if (pos == std::string::npos) {
|
extensions.emplace(ext);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return HasExtension(pFile, extensions);
|
||||||
|
}
|
||||||
|
|
||||||
const char *ext_real = &pFile[pos + 1];
|
// ------------------------------------------------------------------------------------------------
|
||||||
if (!ASSIMP_stricmp(ext_real, ext0)) {
|
// Check for file extension
|
||||||
return true;
|
/*static*/ bool BaseImporter::HasExtension(const std::string &pFile, const std::set<std::string> &extensions) {
|
||||||
|
// CAUTION: Do not just search for the extension!
|
||||||
|
// GetExtension() returns the part after the *last* dot, but some extensions
|
||||||
|
// have dots inside them, e.g. ogre.mesh.xml. Compare the entire end of the
|
||||||
|
// string.
|
||||||
|
for (const std::string& ext : extensions) {
|
||||||
|
// Yay for C++<20 not having std::string::ends_with()
|
||||||
|
const std::string dotExt = "." + ext;
|
||||||
|
if (dotExt.length() > pFile.length()) continue;
|
||||||
|
// Possible optimization: Fetch the lowercase filename!
|
||||||
|
if (0 == ASSIMP_stricmp(pFile.c_str() + pFile.length() - dotExt.length(), dotExt.c_str())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for other, optional, file extensions
|
|
||||||
if (ext1 && !ASSIMP_stricmp(ext_real, ext1)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ext2 && !ASSIMP_stricmp(ext_real, ext2)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -637,24 +637,10 @@ const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) {
|
||||||
std::set<std::string> extensions;
|
std::set<std::string> extensions;
|
||||||
pimpl->mImporter[a]->GetExtensionList(extensions);
|
pimpl->mImporter[a]->GetExtensionList(extensions);
|
||||||
|
|
||||||
// CAUTION: Do not just search for the extension!
|
if (BaseImporter::HasExtension(pFile, extensions)) {
|
||||||
// GetExtension() returns the part after the *last* dot, but some extensions have dots
|
ImporterAndIndex candidate = { pimpl->mImporter[a], a };
|
||||||
// inside them, e.g. ogre.mesh.xml. Compare the entire end of the string.
|
possibleImporters.push_back(candidate);
|
||||||
for (std::set<std::string>::const_iterator it = extensions.cbegin(); it != extensions.cend(); ++it) {
|
|
||||||
|
|
||||||
// Yay for C++<20 not having std::string::ends_with()
|
|
||||||
std::string extension = "." + *it;
|
|
||||||
if (extension.length() <= pFile.length()) {
|
|
||||||
// Possible optimization: Fetch the lowercase filename!
|
|
||||||
if (0 == ASSIMP_stricmp(pFile.c_str() + pFile.length() - extension.length(), extension.c_str())) {
|
|
||||||
ImporterAndIndex candidate = { pimpl->mImporter[a], a };
|
|
||||||
possibleImporters.push_back(candidate);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If just one importer supports this extension, pick it and close the case.
|
// If just one importer supports this extension, pick it and close the case.
|
||||||
|
|
|
@ -275,6 +275,16 @@ public: // static utilities
|
||||||
const char *ext1 = nullptr,
|
const char *ext1 = nullptr,
|
||||||
const char *ext2 = nullptr);
|
const char *ext2 = nullptr);
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------
|
||||||
|
/** @brief Check whether a file has one of the passed file extensions
|
||||||
|
* @param pFile Input file
|
||||||
|
* @param extensions Extensions to check for. Lowercase characters only, no dot!
|
||||||
|
* @note Case-insensitive
|
||||||
|
*/
|
||||||
|
static bool HasExtension(
|
||||||
|
const std::string &pFile,
|
||||||
|
const std::set<std::string> &extensions);
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
/** @brief Extract file extension from a string
|
/** @brief Extract file extension from a string
|
||||||
* @param pFile Input file
|
* @param pFile Input file
|
||||||
|
|
Loading…
Reference in New Issue