mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-21 08:53:52 +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
7 changed files with 24 additions and 33 deletions
|
@ -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)
|
for (const auto& [key, value] : mFiles)
|
||||||
out.emplace(VFS::Path::normalizeFilename(key), value);
|
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"; }
|
std::string getDescription() const override { return "TestData"; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include <components/files/istreamptr.hpp>
|
#include <components/files/istreamptr.hpp>
|
||||||
|
|
||||||
|
@ -19,16 +20,18 @@ namespace VFS
|
||||||
virtual std::filesystem::path getPath() = 0;
|
virtual std::filesystem::path getPath() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using FileMap = std::map<std::string, File*, std::less<>>;
|
||||||
|
|
||||||
class Archive
|
class Archive
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Archive() = default;
|
virtual ~Archive() = default;
|
||||||
|
|
||||||
/// List all resources contained in this archive.
|
/// 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.
|
/// 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;
|
virtual std::string getDescription() const = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -48,7 +48,7 @@ namespace VFS
|
||||||
|
|
||||||
virtual ~BsaArchive() {}
|
virtual ~BsaArchive() {}
|
||||||
|
|
||||||
void listResources(std::map<std::string, File*>& out) override
|
void listResources(FileMap& out) override
|
||||||
{
|
{
|
||||||
for (auto& resource : mResources)
|
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)
|
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)
|
if (!mBuiltIndex)
|
||||||
{
|
{
|
||||||
|
@ -51,14 +51,12 @@ namespace VFS
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (index::iterator it = mIndex.begin(); it != mIndex.end(); ++it)
|
for (auto& [k, v] : mIndex)
|
||||||
{
|
out[k] = &v;
|
||||||
out[it->first] = &it->second;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileSystemArchive::contains(const std::string& file) const
|
bool FileSystemArchive::contains(std::string_view file) const
|
||||||
{
|
{
|
||||||
return mIndex.find(file) != mIndex.end();
|
return mIndex.find(file) != mIndex.end();
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,16 +27,14 @@ namespace VFS
|
||||||
public:
|
public:
|
||||||
FileSystemArchive(const std::filesystem::path& path);
|
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;
|
std::string getDescription() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::map<std::string, FileSystemArchiveFile> index;
|
std::map<std::string, FileSystemArchiveFile, std::less<>> mIndex;
|
||||||
index mIndex;
|
|
||||||
|
|
||||||
bool mBuiltIndex;
|
bool mBuiltIndex;
|
||||||
std::filesystem::path mPath;
|
std::filesystem::path mPath;
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,11 +35,11 @@ namespace VFS
|
||||||
return getNormalized(Path::normalizeFilename(name));
|
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())
|
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();
|
return found->second->open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,21 +70,13 @@ namespace VFS
|
||||||
return found->second->getPath();
|
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
|
Manager::RecursiveDirectoryRange Manager::getRecursiveDirectoryIterator(std::string_view path) const
|
||||||
{
|
{
|
||||||
if (path.empty())
|
if (path.empty())
|
||||||
return { mIndex.begin(), mIndex.end() };
|
return { mIndex.begin(), mIndex.end() };
|
||||||
std::string normalized = Path::normalizeFilename(path);
|
std::string normalized = Path::normalizeFilename(path);
|
||||||
const auto it = mIndex.lower_bound(normalized);
|
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 };
|
return { it, it };
|
||||||
++normalized.back();
|
++normalized.back();
|
||||||
return { it, mIndex.lower_bound(normalized) };
|
return { it, mIndex.lower_bound(normalized) };
|
||||||
|
|
|
@ -41,7 +41,7 @@ namespace VFS
|
||||||
class RecursiveDirectoryIterator
|
class RecursiveDirectoryIterator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RecursiveDirectoryIterator(std::map<std::string, File*>::const_iterator it)
|
RecursiveDirectoryIterator(FileMap::const_iterator it)
|
||||||
: mIt(it)
|
: mIt(it)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ namespace VFS
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, File*>::const_iterator mIt;
|
FileMap::const_iterator mIt;
|
||||||
};
|
};
|
||||||
|
|
||||||
using RecursiveDirectoryRange = IteratorPair<RecursiveDirectoryIterator>;
|
using RecursiveDirectoryRange = IteratorPair<RecursiveDirectoryIterator>;
|
||||||
|
@ -83,7 +83,7 @@ namespace VFS
|
||||||
/// Retrieve a file by name (name is already normalized).
|
/// Retrieve a file by name (name is already normalized).
|
||||||
/// @note Throws an exception if the file can not be found.
|
/// @note Throws an exception if the file can not be found.
|
||||||
/// @note May be called from any thread once the index has been built.
|
/// @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;
|
std::string getArchive(std::string_view name) const;
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ namespace VFS
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<Archive>> mArchives;
|
std::vector<std::unique_ptr<Archive>> mArchives;
|
||||||
|
|
||||||
std::map<std::string, File*> mIndex;
|
FileMap mIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue