Merge branch 'fileview' into 'master'

Use std::string_view in file collections

See merge request OpenMW/openmw!4909
This commit is contained in:
Evil Eye 2025-09-19 21:50:42 +00:00
commit aedd1da9e3
10 changed files with 51 additions and 56 deletions

View File

@ -42,7 +42,7 @@ namespace
const Files::PathContainer mDataDirs{ { std::filesystem::path{ OPENMW_DATA_DIR } } }; const Files::PathContainer mDataDirs{ { std::filesystem::path{ OPENMW_DATA_DIR } } };
const Files::Collections mFileCollections{ mDataDirs }; const Files::Collections mFileCollections{ mDataDirs };
const std::string mContentFile = "template.omwgame"; const std::string mContentFile = "template.omwgame";
const std::filesystem::path mContentFilePath = mFileCollections.getCollection(".omwgame").getPath(mContentFile); const std::filesystem::path mContentFilePath = mFileCollections.getCollection("omwgame").getPath(mContentFile);
}; };
TEST_F(ESM3ReadersCacheWithContentFile, shouldKeepOpenReleasedOpenReader) TEST_F(ESM3ReadersCacheWithContentFile, shouldKeepOpenReleasedOpenReader)

View File

@ -190,8 +190,8 @@ void readVFS(std::unique_ptr<VFS::Archive>&& archive, const std::filesystem::pat
if (!archivePath.empty() && !isBSA(archivePath)) if (!archivePath.empty() && !isBSA(archivePath))
{ {
const Files::Collections fileCollections({ archivePath }); const Files::Collections fileCollections({ archivePath });
const Files::MultiDirCollection& bsaCol = fileCollections.getCollection(".bsa"); const Files::MultiDirCollection& bsaCol = fileCollections.getCollection("bsa");
const Files::MultiDirCollection& ba2Col = fileCollections.getCollection(".ba2"); const Files::MultiDirCollection& ba2Col = fileCollections.getCollection("ba2");
for (const Files::MultiDirCollection& collection : { bsaCol, ba2Col }) for (const Files::MultiDirCollection& collection : { bsaCol, ba2Col })
{ {
for (auto& file : collection) for (auto& file : collection)

View File

@ -32,6 +32,7 @@
#include <components/misc/constants.hpp> #include <components/misc/constants.hpp>
#include <components/misc/convert.hpp> #include <components/misc/convert.hpp>
#include <components/misc/mathutil.hpp> #include <components/misc/mathutil.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/resourcehelpers.hpp> #include <components/misc/resourcehelpers.hpp>
#include <components/misc/rng.hpp> #include <components/misc/rng.hpp>
@ -2885,9 +2886,7 @@ namespace MWWorld
int idx = 0; int idx = 0;
for (const std::string& file : content) for (const std::string& file : content)
{ {
const auto filename = Files::pathFromUnicodeString(file); const Files::MultiDirCollection& col = fileCollections.getCollection(Misc::getFileExtension(file));
const Files::MultiDirCollection& col
= fileCollections.getCollection(Files::pathToUnicodeString(filename.extension()));
if (col.doesExist(file)) if (col.doesExist(file))
{ {
gameContentLoader.load(col.getPath(file), idx, listener); gameContentLoader.load(col.getPath(file), idx, listener);

View File

@ -18,6 +18,7 @@
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/files/multidircollection.hpp> #include <components/files/multidircollection.hpp>
#include <components/loadinglistener/loadinglistener.hpp> #include <components/loadinglistener/loadinglistener.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/resourcehelpers.hpp> #include <components/misc/resourcehelpers.hpp>
#include <components/misc/strings/lower.hpp> #include <components/misc/strings/lower.hpp>
@ -211,21 +212,20 @@ namespace EsmLoader
{ {
ShallowContent result; ShallowContent result;
const std::set<std::string> supportedFormats{ const std::set<std::string_view, Misc::StringUtils::CiComp> supportedFormats{
".esm", "esm",
".esp", "esp",
".omwgame", "omwgame",
".omwaddon", "omwaddon",
".project", "project",
}; };
for (std::size_t i = 0; i < contentFiles.size(); ++i) for (std::size_t i = 0; i < contentFiles.size(); ++i)
{ {
const std::string& file = contentFiles[i]; const std::string& file = contentFiles[i];
const std::string extension const std::string_view extension = Misc::getFileExtension(file);
= Misc::StringUtils::lowerCase(Files::pathToUnicodeString(std::filesystem::path(file).extension()));
if (supportedFormats.find(extension) == supportedFormats.end()) if (!supportedFormats.contains(extension))
{ {
Log(Debug::Warning) << "Skipping unsupported content file: " << file; Log(Debug::Warning) << "Skipping unsupported content file: " << file;
continue; continue;

View File

@ -2,7 +2,6 @@
#include "conversion.hpp" #include "conversion.hpp"
#include <components/misc/strings/algorithm.hpp> #include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/lower.hpp>
namespace Files namespace Files
{ {
@ -18,14 +17,12 @@ namespace Files
{ {
} }
const MultiDirCollection& Collections::getCollection(const std::string& extension) const const MultiDirCollection& Collections::getCollection(std::string_view extension) const
{ {
std::string ext = Misc::StringUtils::lowerCase(extension); auto iter = mCollections.find(extension);
auto iter = mCollections.find(ext);
if (iter == mCollections.end()) if (iter == mCollections.end())
{ {
std::pair<MultiDirCollectionContainer::iterator, bool> result auto result = mCollections.emplace(extension, MultiDirCollection(mDirectories, extension));
= mCollections.emplace(ext, MultiDirCollection(mDirectories, ext));
iter = result.first; iter = result.first;
} }
@ -33,7 +30,7 @@ namespace Files
return iter->second; return iter->second;
} }
std::filesystem::path Collections::getPath(const std::string& file) const std::filesystem::path Collections::getPath(std::string_view file) const
{ {
for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++) for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++)
{ {
@ -47,10 +44,10 @@ namespace Files
} }
} }
throw std::runtime_error("file " + file + " not found"); throw std::runtime_error("file " + std::string(file) + " not found");
} }
bool Collections::doesExist(const std::string& file) const bool Collections::doesExist(std::string_view file) const
{ {
for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++) for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++)
{ {

View File

@ -15,27 +15,25 @@ namespace Files
///< Directories are listed with increasing priority. ///< Directories are listed with increasing priority.
Collections(const Files::PathContainer& directories); Collections(const Files::PathContainer& directories);
///< Return a file collection for the given extension. Extension must contain the ///< Return a file collection for the given extension.
/// leading dot and must be all lower-case. const MultiDirCollection& getCollection(std::string_view extension) const;
const MultiDirCollection& getCollection(const std::string& extension) const;
std::filesystem::path getPath(const std::string& file) const; std::filesystem::path getPath(std::string_view file) const;
///< Return full path (including filename) of \a file. ///< Return full path (including filename) of \a file.
/// ///
/// If the file does not exist in any of the collection's /// If the file does not exist in any of the collection's
/// directories, an exception is thrown. \a file must include the /// directories, an exception is thrown. \a file must include the
/// extension. /// extension.
bool doesExist(const std::string& file) const; bool doesExist(std::string_view file) const;
///< \return Does a file with the given name exist? ///< \return Does a file with the given name exist?
const Files::PathContainer& getPaths() const; const Files::PathContainer& getPaths() const;
private: private:
typedef std::map<std::string, MultiDirCollection> MultiDirCollectionContainer;
Files::PathContainer mDirectories; Files::PathContainer mDirectories;
mutable MultiDirCollectionContainer mCollections; mutable std::map<std::string, MultiDirCollection, Misc::StringUtils::CiComp> mCollections;
}; };
} }

View File

@ -8,7 +8,7 @@
namespace Files namespace Files
{ {
MultiDirCollection::MultiDirCollection(const Files::PathContainer& directories, const std::string& extension) MultiDirCollection::MultiDirCollection(const Files::PathContainer& directories, std::string_view extension)
{ {
for (const auto& directory : directories) for (const auto& directory : directories)
{ {
@ -22,7 +22,8 @@ namespace Files
{ {
const auto& path = dirIter.path(); const auto& path = dirIter.path();
if (!Misc::StringUtils::ciEqual(extension, Files::pathToUnicodeString(path.extension()))) std::string ext = Files::pathToUnicodeString(path.extension());
if (ext.size() != extension.size() + 1 || !Misc::StringUtils::ciEndsWith(ext, extension))
continue; continue;
const auto filename = Files::pathToUnicodeString(path.filename()); const auto filename = Files::pathToUnicodeString(path.filename());
@ -41,25 +42,25 @@ namespace Files
{ {
// handle case folding // handle case folding
mFiles.erase(result->first); mFiles.erase(result->first);
mFiles.insert(std::make_pair(filename, path)); mFiles.emplace(filename, path);
} }
} }
} }
} }
std::filesystem::path MultiDirCollection::getPath(const std::string& file) const std::filesystem::path MultiDirCollection::getPath(std::string_view file) const
{ {
TIter iter = mFiles.find(file); TIter iter = mFiles.find(file);
if (iter == mFiles.end()) if (iter == mFiles.end())
throw std::runtime_error("file " + file + " not found"); throw std::runtime_error("file " + std::string(file) + " not found");
return iter->second; return iter->second;
} }
bool MultiDirCollection::doesExist(const std::string& file) const bool MultiDirCollection::doesExist(std::string_view file) const
{ {
return mFiles.find(file) != mFiles.end(); return mFiles.contains(file);
} }
MultiDirCollection::TIter MultiDirCollection::begin() const MultiDirCollection::TIter MultiDirCollection::begin() const

View File

@ -28,19 +28,18 @@ namespace Files
TContainer mFiles; TContainer mFiles;
public: public:
MultiDirCollection(const Files::PathContainer& directories, const std::string& extension); MultiDirCollection(const Files::PathContainer& directories, std::string_view extension);
///< Directories are listed with increasing priority. ///< Directories are listed with increasing priority.
/// \param extension The extension that should be listed in this collection. Must /// \param extension The extension that should be listed in this collection. Must
/// contain the leading dot. /// contain the leading dot.
/// \param foldCase Ignore filename case
std::filesystem::path getPath(const std::string& file) const; std::filesystem::path getPath(std::string_view file) const;
///< Return full path (including filename) of \a file. ///< Return full path (including filename) of \a file.
/// ///
/// If the file does not exist, an exception is thrown. \a file must include /// If the file does not exist, an exception is thrown. \a file must include
/// the extension. /// the extension.
bool doesExist(const std::string& file) const; bool doesExist(std::string_view file) const;
///< \return Does a file with the given name exist? ///< \return Does a file with the given name exist?
TIter begin() const; TIter begin() const;

View File

@ -2,6 +2,8 @@
#include <fstream> #include <fstream>
#include <components/misc/pathhelpers.hpp>
namespace Translation namespace Translation
{ {
Storage::Storage() Storage::Storage()
@ -11,25 +13,24 @@ namespace Translation
void Storage::loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName) void Storage::loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName)
{ {
std::string esmNameNoExtension(Misc::StringUtils::lowerCase(esmFileName)); std::string_view esmNameNoExtension = Misc::stemFile(esmFileName);
// changing the extension
size_t dotPos = esmNameNoExtension.rfind('.');
if (dotPos != std::string::npos)
esmNameNoExtension.resize(dotPos);
loadData(mCellNamesTranslations, esmNameNoExtension, ".cel", dataFileCollections); loadData(mCellNamesTranslations, esmNameNoExtension, "cel", dataFileCollections);
loadData(mPhraseForms, esmNameNoExtension, ".top", dataFileCollections); loadData(mPhraseForms, esmNameNoExtension, "top", dataFileCollections);
loadData(mTopicIDs, esmNameNoExtension, ".mrk", dataFileCollections); loadData(mTopicIDs, esmNameNoExtension, "mrk", dataFileCollections);
} }
void Storage::loadData(ContainerType& container, const std::string& fileNameNoExtension, void Storage::loadData(ContainerType& container, std::string_view fileNameNoExtension, std::string_view extension,
const std::string& extension, const Files::Collections& dataFileCollections) const Files::Collections& dataFileCollections)
{ {
std::string fileName = fileNameNoExtension + extension; std::string fileName(fileNameNoExtension);
fileName += '.';
fileName += extension;
if (dataFileCollections.getCollection(extension).doesExist(fileName)) const Files::MultiDirCollection& collection = dataFileCollections.getCollection(extension);
if (collection.doesExist(fileName))
{ {
std::ifstream stream(dataFileCollections.getCollection(extension).getPath(fileName).c_str()); std::ifstream stream(collection.getPath(fileName));
if (!stream.is_open()) if (!stream.is_open())
throw std::runtime_error("failed to open translation file: " + fileName); throw std::runtime_error("failed to open translation file: " + fileName);

View File

@ -26,7 +26,7 @@ namespace Translation
private: private:
typedef std::map<std::string, std::string, std::less<>> ContainerType; typedef std::map<std::string, std::string, std::less<>> ContainerType;
void loadData(ContainerType& container, const std::string& fileNameNoExtension, const std::string& extension, void loadData(ContainerType& container, std::string_view fileNameNoExtension, std::string_view extension,
const Files::Collections& dataFileCollections); const Files::Collections& dataFileCollections);
void loadDataFromStream(ContainerType& container, std::istream& stream); void loadDataFromStream(ContainerType& container, std::istream& stream);