mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-10 21:16:24 +02:00
Use std::string_view in VFS
This commit is contained in:
1 parent
801d379f74
commit
de702ced45
6 files changed
+59
-56
No files matched your search
@@ -8,12 +8,12 @@ DirectoryReader::DirectoryReader(const Path &path) {
|
||||
path_ = path;
|
||||
}
|
||||
|
||||
uint8_t *DirectoryReader::ReadFile(const char *path, size_t *size) {
|
||||
uint8_t *DirectoryReader::ReadFile(std::string_view path, size_t *size) {
|
||||
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
|
||||
return File::ReadLocalFile(new_path, size);
|
||||
}
|
||||
|
||||
bool DirectoryReader::GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) {
|
||||
bool DirectoryReader::GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) {
|
||||
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
|
||||
|
||||
File::FileInfo info;
|
||||
@@ -27,12 +27,12 @@ bool DirectoryReader::GetFileListing(const char *path, std::vector<File::FileInf
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DirectoryReader::GetFileInfo(const char *path, File::FileInfo *info) {
|
||||
bool DirectoryReader::GetFileInfo(std::string_view path, File::FileInfo *info) {
|
||||
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
|
||||
return File::GetFileInfo(new_path, info);
|
||||
}
|
||||
|
||||
bool DirectoryReader::Exists(const char *path) {
|
||||
bool DirectoryReader::Exists(std::string_view path) {
|
||||
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
|
||||
return File::Exists(new_path);
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
FILE *file = nullptr;
|
||||
};
|
||||
|
||||
VFSFileReference *DirectoryReader::GetFile(const char *path) {
|
||||
VFSFileReference *DirectoryReader::GetFile(std::string_view path) {
|
||||
Path filePath = path_ / path;
|
||||
if (!File::Exists(filePath)) {
|
||||
return nullptr;
|
||||
|
||||
@@ -8,9 +8,9 @@ class DirectoryReader : public VFSBackend {
|
||||
public:
|
||||
explicit DirectoryReader(const Path &path);
|
||||
// use delete[] on the returned value.
|
||||
uint8_t *ReadFile(const char *path, size_t *size) override;
|
||||
uint8_t *ReadFile(std::string_view path, size_t *size) override;
|
||||
|
||||
VFSFileReference *GetFile(const char *path) override;
|
||||
VFSFileReference *GetFile(std::string_view path) override;
|
||||
bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) override;
|
||||
void ReleaseFile(VFSFileReference *vfsReference) override;
|
||||
|
||||
@@ -19,9 +19,9 @@ public:
|
||||
size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) override;
|
||||
void CloseFile(VFSOpenFile *vfsOpenFile) override;
|
||||
|
||||
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) override;
|
||||
bool GetFileInfo(const char *path, File::FileInfo *info) override;
|
||||
bool Exists(const char *path) override;
|
||||
bool GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter) override;
|
||||
bool GetFileInfo(std::string_view path, File::FileInfo *info) override;
|
||||
bool Exists(std::string_view path) override;
|
||||
std::string toString() const override {
|
||||
return path_.ToString();
|
||||
}
|
||||
|
||||
+24
-23
@@ -1,5 +1,6 @@
|
||||
#include <cstring>
|
||||
|
||||
#include "Common/Common.h"
|
||||
#include "Common/Log.h"
|
||||
#include "Common/File/VFS/VFS.h"
|
||||
#include "Common/File/FileUtil.h"
|
||||
@@ -8,12 +9,12 @@
|
||||
|
||||
VFS g_VFS;
|
||||
|
||||
void VFS::Register(const char *prefix, VFSBackend *reader) {
|
||||
void VFS::Register(std::string_view prefix, VFSBackend *reader) {
|
||||
if (reader) {
|
||||
entries_.push_back(VFSEntry{ prefix, reader });
|
||||
DEBUG_LOG(Log::IO, "Registered VFS for prefix %s: %s", prefix, reader->toString().c_str());
|
||||
DEBUG_LOG(Log::IO, "Registered VFS for prefix %.*s: %s", STR_VIEW(prefix), reader->toString().c_str());
|
||||
} else {
|
||||
ERROR_LOG(Log::IO, "Trying to register null VFS backend for prefix %s", prefix);
|
||||
ERROR_LOG(Log::IO, "Trying to register null VFS backend for prefix %.*s", STR_VIEW(prefix));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,22 +38,22 @@ static bool IsLocalAbsolutePath(std::string_view path) {
|
||||
}
|
||||
|
||||
// The returned data should be free'd with delete[].
|
||||
uint8_t *VFS::ReadFile(const char *filename, size_t *size) {
|
||||
uint8_t *VFS::ReadFile(std::string_view filename, size_t *size) {
|
||||
if (IsLocalAbsolutePath(filename)) {
|
||||
// Local path, not VFS.
|
||||
// INFO_LOG(Log::IO, "Not a VFS path: %s . Reading local file.", filename);
|
||||
return File::ReadLocalFile(Path(filename), size);
|
||||
}
|
||||
|
||||
int fn_len = (int)strlen(filename);
|
||||
const int fn_len = (int)filename.length();
|
||||
bool fileSystemFound = false;
|
||||
for (const auto &entry : entries_) {
|
||||
int prefix_len = (int)strlen(entry.prefix);
|
||||
int prefix_len = (int)entry.prefix.length();
|
||||
if (prefix_len >= fn_len) continue;
|
||||
if (0 == memcmp(filename, entry.prefix, prefix_len)) {
|
||||
if (0 == memcmp(filename.data(), entry.prefix.data(), prefix_len)) {
|
||||
fileSystemFound = true;
|
||||
// INFO_LOG(Log::IO, "Prefix match: %s (%s) -> %s", entries[i].prefix, filename, filename + prefix_len);
|
||||
uint8_t *data = entry.reader->ReadFile(filename + prefix_len, size);
|
||||
uint8_t *data = entry.reader->ReadFile(filename.substr(prefix_len), size);
|
||||
if (data)
|
||||
return data;
|
||||
else
|
||||
@@ -66,7 +67,7 @@ uint8_t *VFS::ReadFile(const char *filename, size_t *size) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool VFS::GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) {
|
||||
bool VFS::GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter) {
|
||||
if (IsLocalAbsolutePath(path)) {
|
||||
// Local path, not VFS.
|
||||
// INFO_LOG(Log::IO, "Not a VFS path: %s . Reading local directory.", path);
|
||||
@@ -74,14 +75,14 @@ bool VFS::GetFileListing(const char *path, std::vector<File::FileInfo> *listing,
|
||||
return true;
|
||||
}
|
||||
|
||||
int fn_len = (int)strlen(path);
|
||||
int fn_len = (int)path.length();
|
||||
bool fileSystemFound = false;
|
||||
for (const auto &entry : entries_) {
|
||||
int prefix_len = (int)strlen(entry.prefix);
|
||||
int prefix_len = (int)entry.prefix.length();
|
||||
if (prefix_len >= fn_len) continue;
|
||||
if (0 == memcmp(path, entry.prefix, prefix_len)) {
|
||||
if (0 == memcmp(path.data(), entry.prefix.data(), prefix_len)) {
|
||||
fileSystemFound = true;
|
||||
if (entry.reader->GetFileListing(path + prefix_len, listing, filter)) {
|
||||
if (entry.reader->GetFileListing(path.substr(prefix_len), listing, filter)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -93,7 +94,7 @@ bool VFS::GetFileListing(const char *path, std::vector<File::FileInfo> *listing,
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VFS::GetFileInfo(const char *path, File::FileInfo *info) {
|
||||
bool VFS::GetFileInfo(std::string_view path, File::FileInfo *info) {
|
||||
if (IsLocalAbsolutePath(path)) {
|
||||
// Local path, not VFS.
|
||||
// INFO_LOG(Log::IO, "Not a VFS path: %s . Getting local file info.", path);
|
||||
@@ -101,13 +102,13 @@ bool VFS::GetFileInfo(const char *path, File::FileInfo *info) {
|
||||
}
|
||||
|
||||
bool fileSystemFound = false;
|
||||
int fn_len = (int)strlen(path);
|
||||
int fn_len = (int)path.length();
|
||||
for (const auto &entry : entries_) {
|
||||
int prefix_len = (int)strlen(entry.prefix);
|
||||
int prefix_len = (int)entry.prefix.length();
|
||||
if (prefix_len >= fn_len) continue;
|
||||
if (0 == memcmp(path, entry.prefix, prefix_len)) {
|
||||
if (0 == memcmp(path.data(), entry.prefix.data(), prefix_len)) {
|
||||
fileSystemFound = true;
|
||||
if (entry.reader->GetFileInfo(path + prefix_len, info))
|
||||
if (entry.reader->GetFileInfo(path.substr(prefix_len), info))
|
||||
return true;
|
||||
else
|
||||
continue;
|
||||
@@ -119,7 +120,7 @@ bool VFS::GetFileInfo(const char *path, File::FileInfo *info) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VFS::Exists(const char *path) {
|
||||
bool VFS::Exists(std::string_view path) {
|
||||
if (IsLocalAbsolutePath(path)) {
|
||||
// Local path, not VFS.
|
||||
// INFO_LOG(Log::IO, "Not a VFS path: %s . Getting local file info.", path);
|
||||
@@ -127,13 +128,13 @@ bool VFS::Exists(const char *path) {
|
||||
}
|
||||
|
||||
bool fileSystemFound = false;
|
||||
int fn_len = (int)strlen(path);
|
||||
int fn_len = (int)path.length();
|
||||
for (const auto &entry : entries_) {
|
||||
int prefix_len = (int)strlen(entry.prefix);
|
||||
int prefix_len = (int)entry.prefix.length();
|
||||
if (prefix_len >= fn_len) continue;
|
||||
if (0 == memcmp(path, entry.prefix, prefix_len)) {
|
||||
if (0 == memcmp(path.data(), entry.prefix.data(), prefix_len)) {
|
||||
fileSystemFound = true;
|
||||
if (entry.reader->Exists(path + prefix_len))
|
||||
if (entry.reader->Exists(path.substr(prefix_len)))
|
||||
return true;
|
||||
else
|
||||
continue;
|
||||
|
||||
+12
-11
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
|
||||
#include "Common/File/DirListing.h"
|
||||
|
||||
@@ -36,14 +37,14 @@ public:
|
||||
class VFSInterface {
|
||||
public:
|
||||
virtual ~VFSInterface() {}
|
||||
virtual uint8_t *ReadFile(const char *path, size_t *size) = 0;
|
||||
virtual uint8_t *ReadFile(std::string_view path, size_t *size) = 0;
|
||||
// If listing already contains files, it'll be cleared.
|
||||
virtual bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) = 0;
|
||||
virtual bool GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) = 0;
|
||||
};
|
||||
|
||||
class VFSBackend : public VFSInterface {
|
||||
public:
|
||||
virtual VFSFileReference *GetFile(const char *path) = 0;
|
||||
virtual VFSFileReference *GetFile(std::string_view path) = 0;
|
||||
virtual bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) = 0;
|
||||
virtual void ReleaseFile(VFSFileReference *vfsReference) = 0;
|
||||
|
||||
@@ -55,8 +56,8 @@ public:
|
||||
virtual void CloseFile(VFSOpenFile *vfsOpenFile) = 0;
|
||||
|
||||
// Filter support is optional but nice to have
|
||||
virtual bool GetFileInfo(const char *path, File::FileInfo *info) = 0;
|
||||
virtual bool Exists(const char *path) {
|
||||
virtual bool GetFileInfo(std::string_view path, File::FileInfo *info) = 0;
|
||||
virtual bool Exists(std::string_view path) {
|
||||
File::FileInfo info{};
|
||||
return GetFileInfo(path, &info) && info.exists;
|
||||
}
|
||||
@@ -67,21 +68,21 @@ public:
|
||||
class VFS : public VFSInterface {
|
||||
public:
|
||||
~VFS() { Clear(); }
|
||||
void Register(const char *prefix, VFSBackend *reader);
|
||||
void Register(std::string_view prefix, VFSBackend *reader);
|
||||
void Clear();
|
||||
|
||||
// Use delete [] to release the returned memory.
|
||||
// Always allocates an extra zero byte at the end, so that it
|
||||
// can be used for text like shader sources.
|
||||
uint8_t *ReadFile(const char *filename, size_t *size) override;
|
||||
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) override;
|
||||
uint8_t *ReadFile(std::string_view filename, size_t *size) override;
|
||||
bool GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) override;
|
||||
|
||||
bool GetFileInfo(const char *filename, File::FileInfo *fileInfo);
|
||||
bool Exists(const char *path);
|
||||
bool GetFileInfo(std::string_view filename, File::FileInfo *fileInfo);
|
||||
bool Exists(std::string_view path);
|
||||
|
||||
private:
|
||||
struct VFSEntry {
|
||||
const char *prefix;
|
||||
std::string_view prefix;
|
||||
VFSBackend *reader;
|
||||
};
|
||||
std::vector<VFSEntry> entries_;
|
||||
|
||||
@@ -160,8 +160,8 @@ ZipFileReader::~ZipFileReader() {
|
||||
zip_file_.close();
|
||||
}
|
||||
|
||||
uint8_t *ZipFileReader::ReadFile(const char *path, size_t *size) {
|
||||
std::string temp_path = inZipPath_ + path;
|
||||
uint8_t *ZipFileReader::ReadFile(std::string_view path, size_t *size) {
|
||||
std::string temp_path = join(inZipPath_, path);
|
||||
|
||||
std::lock_guard<std::mutex> guard(lock_);
|
||||
// Figure out the file size first. TODO: Can this part be done without locking the mutex?
|
||||
@@ -185,8 +185,8 @@ uint8_t *ZipFileReader::ReadFile(const char *path, size_t *size) {
|
||||
return contents;
|
||||
}
|
||||
|
||||
bool ZipFileReader::GetFileListing(const char *orig_path, std::vector<File::FileInfo> *listing, const char *filter = 0) {
|
||||
std::string path = std::string(inZipPath_) + orig_path;
|
||||
bool ZipFileReader::GetFileListing(std::string_view orig_path, std::vector<File::FileInfo> *listing, const char *filter = 0) {
|
||||
std::string path = join(inZipPath_, orig_path);
|
||||
if (!path.empty() && path.back() != '/') {
|
||||
path.push_back('/');
|
||||
}
|
||||
@@ -293,9 +293,9 @@ bool ZipFileReader::GetZipListings(const std::string &path, std::set<std::string
|
||||
return anyPrefixMatched;
|
||||
}
|
||||
|
||||
bool ZipFileReader::GetFileInfo(const char *path, File::FileInfo *info) {
|
||||
bool ZipFileReader::GetFileInfo(std::string_view path, File::FileInfo *info) {
|
||||
struct zip_stat zstat;
|
||||
std::string temp_path = inZipPath_ + path;
|
||||
std::string temp_path = join(inZipPath_, path);
|
||||
|
||||
// Clear some things to start.
|
||||
info->isDirectory = false;
|
||||
@@ -340,8 +340,9 @@ public:
|
||||
zip_file_t *zf = nullptr;
|
||||
};
|
||||
|
||||
VFSFileReference *ZipFileReader::GetFile(const char *path) {
|
||||
int zi = zip_name_locate(zip_file_, path, ZIP_FL_NOCASE); // this is EXPENSIVE
|
||||
VFSFileReference *ZipFileReader::GetFile(std::string_view path) {
|
||||
std::string p(path);
|
||||
int zi = zip_name_locate(zip_file_, p.c_str(), ZIP_FL_NOCASE); // this is EXPENSIVE
|
||||
if (zi < 0) {
|
||||
// Not found.
|
||||
return nullptr;
|
||||
|
||||
@@ -45,9 +45,9 @@ public:
|
||||
bool IsValid() const { return zip_file_ != nullptr; }
|
||||
|
||||
// use delete[] on the returned value.
|
||||
uint8_t *ReadFile(const char *path, size_t *size) override;
|
||||
uint8_t *ReadFile(std::string_view path, size_t *size) override;
|
||||
|
||||
VFSFileReference *GetFile(const char *path) override;
|
||||
VFSFileReference *GetFile(std::string_view path) override;
|
||||
bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) override;
|
||||
void ReleaseFile(VFSFileReference *vfsReference) override;
|
||||
|
||||
@@ -56,8 +56,8 @@ public:
|
||||
size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) override;
|
||||
void CloseFile(VFSOpenFile *vfsOpenFile) override;
|
||||
|
||||
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) override;
|
||||
bool GetFileInfo(const char *path, File::FileInfo *info) override;
|
||||
bool GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter) override;
|
||||
bool GetFileInfo(std::string_view path, File::FileInfo *info) override;
|
||||
std::string toString() const override {
|
||||
std::string retval = zipPath_.ToVisualString();
|
||||
if (!inZipPath_.empty()) {
|
||||
|
||||
Reference in new issue
Block a user