mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-06 00:55:50 +00:00
Merge branch 'vfs_string_view' into 'master'
Use string_view for VFS (#6125) See merge request OpenMW/openmw!3688
This commit is contained in:
commit
a497d40689
@ -57,13 +57,13 @@ namespace TestingOpenMW
|
||||
{
|
||||
}
|
||||
|
||||
void listResources(std::map<std::string, VFS::File*>& out) override
|
||||
void listResources(VFS::FileMap& out) override
|
||||
{
|
||||
for (const auto& [key, value] : mFiles)
|
||||
out.emplace(VFS::Path::normalizeFilename(key), value);
|
||||
}
|
||||
|
||||
bool contains(const std::string& file) const override { return mFiles.count(file) != 0; }
|
||||
bool contains(std::string_view file) const override { return mFiles.contains(file); }
|
||||
|
||||
std::string getDescription() const override { return "TestData"; }
|
||||
};
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <string_view>
|
||||
|
||||
#include <components/files/istreamptr.hpp>
|
||||
|
||||
@ -19,16 +20,18 @@ namespace VFS
|
||||
virtual std::filesystem::path getPath() = 0;
|
||||
};
|
||||
|
||||
using FileMap = std::map<std::string, File*, std::less<>>;
|
||||
|
||||
class Archive
|
||||
{
|
||||
public:
|
||||
virtual ~Archive() = default;
|
||||
|
||||
/// List all resources contained in this archive.
|
||||
virtual void listResources(std::map<std::string, File*>& out) = 0;
|
||||
virtual void listResources(FileMap& out) = 0;
|
||||
|
||||
/// True if this archive contains the provided normalized file.
|
||||
virtual bool contains(const std::string& file) const = 0;
|
||||
virtual bool contains(std::string_view file) const = 0;
|
||||
|
||||
virtual std::string getDescription() const = 0;
|
||||
};
|
||||
|
@ -48,7 +48,7 @@ namespace VFS
|
||||
|
||||
virtual ~BsaArchive() {}
|
||||
|
||||
void listResources(std::map<std::string, File*>& out) override
|
||||
void listResources(FileMap& out) override
|
||||
{
|
||||
for (auto& resource : mResources)
|
||||
{
|
||||
@ -59,7 +59,7 @@ namespace VFS
|
||||
}
|
||||
}
|
||||
|
||||
bool contains(const std::string& file) const override
|
||||
bool contains(std::string_view file) const override
|
||||
{
|
||||
for (const auto& it : mResources)
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ namespace VFS
|
||||
{
|
||||
}
|
||||
|
||||
void FileSystemArchive::listResources(std::map<std::string, File*>& out)
|
||||
void FileSystemArchive::listResources(FileMap& out)
|
||||
{
|
||||
if (!mBuiltIndex)
|
||||
{
|
||||
@ -51,14 +51,12 @@ namespace VFS
|
||||
}
|
||||
else
|
||||
{
|
||||
for (index::iterator it = mIndex.begin(); it != mIndex.end(); ++it)
|
||||
{
|
||||
out[it->first] = &it->second;
|
||||
}
|
||||
for (auto& [k, v] : mIndex)
|
||||
out[k] = &v;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileSystemArchive::contains(const std::string& file) const
|
||||
bool FileSystemArchive::contains(std::string_view file) const
|
||||
{
|
||||
return mIndex.find(file) != mIndex.end();
|
||||
}
|
||||
|
@ -27,16 +27,14 @@ namespace VFS
|
||||
public:
|
||||
FileSystemArchive(const std::filesystem::path& path);
|
||||
|
||||
void listResources(std::map<std::string, File*>& out) override;
|
||||
void listResources(FileMap& out) override;
|
||||
|
||||
bool contains(const std::string& file) const override;
|
||||
bool contains(std::string_view file) const override;
|
||||
|
||||
std::string getDescription() const override;
|
||||
|
||||
private:
|
||||
typedef std::map<std::string, FileSystemArchiveFile> index;
|
||||
index mIndex;
|
||||
|
||||
std::map<std::string, FileSystemArchiveFile, std::less<>> mIndex;
|
||||
bool mBuiltIndex;
|
||||
std::filesystem::path mPath;
|
||||
};
|
||||
|
@ -35,11 +35,11 @@ namespace VFS
|
||||
return getNormalized(Path::normalizeFilename(name));
|
||||
}
|
||||
|
||||
Files::IStreamPtr Manager::getNormalized(const std::string& normalizedName) const
|
||||
Files::IStreamPtr Manager::getNormalized(std::string_view normalizedName) const
|
||||
{
|
||||
std::map<std::string, File*>::const_iterator found = mIndex.find(normalizedName);
|
||||
const auto found = mIndex.find(normalizedName);
|
||||
if (found == mIndex.end())
|
||||
throw std::runtime_error("Resource '" + normalizedName + "' not found");
|
||||
throw std::runtime_error("Resource '" + std::string(normalizedName) + "' not found");
|
||||
return found->second->open();
|
||||
}
|
||||
|
||||
@ -70,21 +70,13 @@ namespace VFS
|
||||
return found->second->getPath();
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
bool startsWith(std::string_view text, std::string_view start)
|
||||
{
|
||||
return text.rfind(start, 0) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
Manager::RecursiveDirectoryRange Manager::getRecursiveDirectoryIterator(std::string_view path) const
|
||||
{
|
||||
if (path.empty())
|
||||
return { mIndex.begin(), mIndex.end() };
|
||||
std::string normalized = Path::normalizeFilename(path);
|
||||
const auto it = mIndex.lower_bound(normalized);
|
||||
if (it == mIndex.end() || !startsWith(it->first, normalized))
|
||||
if (it == mIndex.end() || !it->first.starts_with(normalized))
|
||||
return { it, it };
|
||||
++normalized.back();
|
||||
return { it, mIndex.lower_bound(normalized) };
|
||||
|
@ -41,7 +41,7 @@ namespace VFS
|
||||
class RecursiveDirectoryIterator
|
||||
{
|
||||
public:
|
||||
RecursiveDirectoryIterator(std::map<std::string, File*>::const_iterator it)
|
||||
RecursiveDirectoryIterator(FileMap::const_iterator it)
|
||||
: mIt(it)
|
||||
{
|
||||
}
|
||||
@ -55,7 +55,7 @@ namespace VFS
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, File*>::const_iterator mIt;
|
||||
FileMap::const_iterator mIt;
|
||||
};
|
||||
|
||||
using RecursiveDirectoryRange = IteratorPair<RecursiveDirectoryIterator>;
|
||||
@ -83,7 +83,7 @@ namespace VFS
|
||||
/// Retrieve a file by name (name is already normalized).
|
||||
/// @note Throws an exception if the file can not be found.
|
||||
/// @note May be called from any thread once the index has been built.
|
||||
Files::IStreamPtr getNormalized(const std::string& normalizedName) const;
|
||||
Files::IStreamPtr getNormalized(std::string_view normalizedName) const;
|
||||
|
||||
std::string getArchive(std::string_view name) const;
|
||||
|
||||
@ -101,7 +101,7 @@ namespace VFS
|
||||
private:
|
||||
std::vector<std::unique_ptr<Archive>> mArchives;
|
||||
|
||||
std::map<std::string, File*> mIndex;
|
||||
FileMap mIndex;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user