Move getFileExtension to common header and use instead of repeating same code

pull/3117/head
Cédric Mocquillon 3 years ago
parent c2df0949e2
commit 6817282097

@ -13,6 +13,7 @@
#include <MyGUI_Gui.h>
#include <MyGUI_TextBox.h>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/rng.hpp>
#include <components/debug/debuglog.hpp>
#include <components/myguiplatform/myguitexture.hpp>
@ -66,23 +67,15 @@ namespace MWGui
void LoadingScreen::findSplashScreens()
{
/* priority given to the left */
const std::array<std::string, 7> supported_extensions {{".tga", ".dds", ".ktx", ".png", ".bmp", ".jpeg", ".jpg"}};
auto isSupportedExtension = [](const std::string_view& ext) {
static const std::array<std::string, 7> supported_extensions{ {"tga", "dds", "ktx", "png", "bmp", "jpeg", "jpg"} };
return !ext.empty() && std::find(supported_extensions.begin(), supported_extensions.end(), ext) != supported_extensions.end();
};
for (const auto& name : mResourceSystem->getVFS()->getRecursiveDirectoryIterator("Splash/"))
{
size_t pos = name.find_last_of('.');
if (pos != std::string::npos)
{
for (auto const& extension : supported_extensions)
{
if (name.compare(pos, name.size() - pos, extension) == 0)
{
mSplashScreens.push_back(name);
break; /* based on priority */
}
}
}
if (isSupportedExtension(Misc::getFileExtension(name)))
mSplashScreens.push_back(name);
}
if (mSplashScreens.empty())
Log(Debug::Warning) << "Warning: no splash screens found!";

@ -17,6 +17,7 @@
#include <components/resource/keyframemanager.hpp>
#include <components/misc/constants.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/sceneutil/keyframe.hpp>
@ -600,8 +601,7 @@ namespace MWRender
for (const auto& name : mResourceSystem->getVFS()->getRecursiveDirectoryIterator(animationPath))
{
size_t pos = name.find_last_of('.');
if (pos != std::string::npos && name.compare(pos, name.size() - pos, ".kf") == 0)
if (Misc::getFileExtension(name) == "kf")
addSingleAnimSource(name, baseModel);
}
}
@ -1292,8 +1292,7 @@ namespace MWRender
for (const auto& name : resourceSystem->getVFS()->getRecursiveDirectoryIterator(animationPath))
{
size_t pos = name.find_last_of('.');
if (pos != std::string::npos && name.compare(pos, name.size() - pos, ".nif") == 0)
if (Misc::getFileExtension(name) == "nif")
loadBonesFromFile(node, name, resourceSystem);
}
}

@ -17,6 +17,7 @@
#include <components/vfs/manager.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/stringops.hpp>
#include <components/myguiplatform/myguitexture.hpp>
@ -193,8 +194,7 @@ namespace Gui
{
for (const auto& name : mVFS->getRecursiveDirectoryIterator("Fonts/"))
{
size_t pos = name.find_last_of('.');
if (pos != std::string::npos && name.compare(pos, name.size() - pos, ".fnt") == 0)
if (Misc::getFileExtension(name) == "fnt")
loadBitmapFont(name, exportToFile);
}
}

@ -0,0 +1,19 @@
#ifndef OPENMW_COMPONENTS_MISC_PATHHELPERS_H
#define OPENMW_COMPONENTS_MISC_PATHHELPERS_H
#include <string>
namespace Misc
{
inline std::string_view getFileExtension(std::string_view file)
{
if (auto extPos = file.find_last_of('.'); extPos != std::string::npos)
{
file.remove_prefix(extPos + 1);
return file;
}
return {};
}
}
#endif

@ -8,6 +8,7 @@
#include <BulletCollision/CollisionShapes/btTriangleMesh.h>
#include <components/misc/pathhelpers.hpp>
#include <components/sceneutil/visitor.hpp>
#include <components/vfs/manager.hpp>
@ -129,12 +130,7 @@ osg::ref_ptr<const BulletShape> BulletShapeManager::getShape(const std::string &
shape = osg::ref_ptr<BulletShape>(static_cast<BulletShape*>(obj.get()));
else
{
size_t extPos = normalized.find_last_of('.');
std::string ext;
if (extPos != std::string::npos && extPos+1 < normalized.size())
ext = normalized.substr(extPos+1);
if (ext == "nif")
if (Misc::getFileExtension(normalized) == "nif")
{
NifBullet::BulletNifLoader loader;
shape = loader.load(*mNifFileManager->get(normalized));

@ -4,6 +4,7 @@
#include <osgDB/Registry>
#include <components/debug/debuglog.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/vfs/manager.hpp>
#include "objectcache.hpp"
@ -102,10 +103,7 @@ namespace Resource
return mWarningImage;
}
size_t extPos = normalized.find_last_of('.');
std::string ext;
if (extPos != std::string::npos && extPos+1 < normalized.size())
ext = normalized.substr(extPos+1);
const std::string ext(Misc::getFileExtension(normalized));
osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension(ext);
if (!reader)
{

@ -9,6 +9,7 @@
#include <components/nifosg/nifloader.hpp>
#include <components/sceneutil/keyframe.hpp>
#include <components/sceneutil/osgacontroller.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/stringops.hpp>
#include "animation.hpp"
@ -141,8 +142,7 @@ namespace Resource
else
{
osg::ref_ptr<SceneUtil::KeyframeHolder> loaded (new SceneUtil::KeyframeHolder);
std::string ext = Resource::getFileExtension(normalized);
if (ext == "kf")
if (Misc::getFileExtension(normalized) == "kf")
{
NifOsg::Loader::loadKf(Nif::NIFFilePtr(new Nif::NIFFile(mVFS->getNormalized(normalized), normalized)), *loaded.get());
}

@ -17,6 +17,7 @@
#include <components/nifosg/nifloader.hpp>
#include <components/nif/niffile.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/stringops.hpp>
#include <components/vfs/manager.hpp>
@ -388,12 +389,12 @@ namespace Resource
osg::ref_ptr<osg::Node> load (const std::string& normalizedFilename, const VFS::Manager* vfs, Resource::ImageManager* imageManager, Resource::NifFileManager* nifFileManager)
{
std::string ext = Resource::getFileExtension(normalizedFilename);
auto ext = Misc::getFileExtension(normalizedFilename);
if (ext == "nif")
return NifOsg::Loader::load(nifFileManager->get(normalizedFilename), imageManager);
else
{
osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension(ext);
osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension(std::string(ext));
if (!reader)
{
std::stringstream errormsg;
@ -816,12 +817,4 @@ namespace Resource
shaderVisitor->setTranslucentFramebuffer(translucentFramebuffer);
return shaderVisitor;
}
std::string getFileExtension(const std::string& file)
{
size_t extPos = file.find_last_of('.');
if (extPos != std::string::npos && extPos+1 < file.size())
return file.substr(extPos+1);
return std::string();
}
}

Loading…
Cancel
Save