mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-16 03:59:56 +00:00
Reduce duplication for getting file name
This commit is contained in:
parent
3e804042c4
commit
376911f645
1 changed files with 11 additions and 23 deletions
|
@ -16,29 +16,16 @@
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
bool changeExtension(std::string& path, std::string_view ext)
|
||||||
struct MatchPathSeparator
|
|
||||||
{
|
{
|
||||||
bool operator()(char ch) const { return ch == '\\' || ch == '/'; }
|
std::string::size_type pos = path.rfind('.');
|
||||||
};
|
if (pos != std::string::npos && path.compare(pos, path.length() - pos, ext) != 0)
|
||||||
|
{
|
||||||
std::string getBasename(std::string const& pathname)
|
path.replace(pos, path.length(), ext);
|
||||||
{
|
return true;
|
||||||
return std::string(
|
}
|
||||||
std::find_if(pathname.rbegin(), pathname.rend(), MatchPathSeparator()).base(), pathname.end());
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool changeExtension(std::string& path, std::string_view ext)
|
|
||||||
{
|
|
||||||
std::string::size_type pos = path.rfind('.');
|
|
||||||
if (pos != std::string::npos && path.compare(pos, path.length() - pos, ext) != 0)
|
|
||||||
{
|
|
||||||
path.replace(pos, path.length(), ext);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Misc::ResourceHelpers::changeExtensionToDds(std::string& path)
|
bool Misc::ResourceHelpers::changeExtensionToDds(std::string& path)
|
||||||
|
@ -106,7 +93,8 @@ std::string Misc::ResourceHelpers::correctResourcePath(
|
||||||
// fall back to a resource in the top level directory if it exists
|
// fall back to a resource in the top level directory if it exists
|
||||||
std::string fallback{ topLevelDirectories.front() };
|
std::string fallback{ topLevelDirectories.front() };
|
||||||
fallback += '\\';
|
fallback += '\\';
|
||||||
fallback += getBasename(correctedPath);
|
fallback += Misc::getFileName(correctedPath);
|
||||||
|
|
||||||
if (vfs->exists(fallback))
|
if (vfs->exists(fallback))
|
||||||
return fallback;
|
return fallback;
|
||||||
|
|
||||||
|
@ -114,7 +102,7 @@ std::string Misc::ResourceHelpers::correctResourcePath(
|
||||||
{
|
{
|
||||||
fallback = topLevelDirectories.front();
|
fallback = topLevelDirectories.front();
|
||||||
fallback += '\\';
|
fallback += '\\';
|
||||||
fallback += getBasename(origExt);
|
fallback += Misc::getFileName(origExt);
|
||||||
if (vfs->exists(fallback))
|
if (vfs->exists(fallback))
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue