Merge pull request #5157 from feuerste/file_extension

Unify extension check for importers.
pull/5159/head^2
Kim Kulling 2023-07-11 10:18:49 +02:00 committed by GitHub
commit 53846430ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 35 deletions

View File

@ -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
/*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; 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;
} }

View File

@ -637,26 +637,12 @@ 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
// inside them, e.g. ogre.mesh.xml. Compare the entire end of the string.
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 }; ImporterAndIndex candidate = { pimpl->mImporter[a], a };
possibleImporters.push_back(candidate); 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.
BaseImporter* imp = nullptr; BaseImporter* imp = nullptr;
if (1 == possibleImporters.size()) { if (1 == possibleImporters.size()) {

View File

@ -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