Skip to content

Commit

Permalink
Incorporate cloud version string stripping.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuerste committed Jul 5, 2023
1 parent dc6c5ca commit f000bbc
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions code/Common/BaseImporter.cpp
Expand Up @@ -59,6 +59,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
#include <sstream>

namespace {
// Removes a possible version hash from a string, as found for example in
// gcs uris (e.g. `gs://bucket/model.glb#1234`) or aws s3 uris
// (e.g. `s3://bucket/model.glb#NULL`), see also
// https://github.com/GoogleCloudPlatform/gsutil/blob/c80f329bc3c4011236c78ce8910988773b2606cb/gslib/storage_url.py#L39-L42.
std::string StripVersionHash(const std::string &s) {
const std::string::size_type pos = s.find_last_of('#');
// Only strip if the part behind the hash is a version string.
if (pos != std::string::npos &&
std::find_if_not(s.begin() + pos + 1, s.end(), [](const char c) {
return std::isalnum(static_cast<int>(c));
}) != s.end()) {
return s.substr(0, pos);
}
return s;
}
} // namespace

using namespace Assimp;

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -240,16 +258,17 @@ void BaseImporter::GetExtensionList(std::set<std::string> &extensions) {
// ------------------------------------------------------------------------------------------------
// Check for file extension
/*static*/ bool BaseImporter::HasExtension(const std::string &pFile, const std::set<std::string> &extensions) {
const std::string file = StripVersionHash(pFile);
// 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;
if (dotExt.length() > file.length()) continue;
// Possible optimization: Fetch the lowercase filename!
if (0 == ASSIMP_stricmp(pFile.c_str() + pFile.length() - dotExt.length(), dotExt.c_str())) {
if (0 == ASSIMP_stricmp(file.c_str() + file.length() - dotExt.length(), dotExt.c_str())) {
return true;
}
}
Expand All @@ -258,7 +277,8 @@ void BaseImporter::GetExtensionList(std::set<std::string> &extensions) {

// ------------------------------------------------------------------------------------------------
// Get file extension from path
std::string BaseImporter::GetExtension(const std::string &file) {
std::string BaseImporter::GetExtension(const std::string &pFile) {
const std::string file = StripVersionHash(pFile);
std::string::size_type pos = file.find_last_of('.');

// no file extension at all
Expand Down

0 comments on commit f000bbc

Please sign in to comment.