Finish abstract Vfs classes

This commit is contained in:
Zach Hilman
2018-06-17 20:19:10 -04:00
parent 767e6f56e5
commit 6e9322987d
2 changed files with 127 additions and 47 deletions

View File

@@ -2,6 +2,62 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <numeric>
#include "core/file_sys/vfs.h"
namespace FileSys {}
namespace FileSys {
VfsFile::operator bool() {
return IsGood();
}
boost::optional<u8> VfsFile::ReadByte(u64 offset) {
auto vec = ReadBytes(offset, 1);
if (vec.empty())
return boost::none;
return vec[0];
}
std::vector<u8> VfsFile::ReadAllBytes() {
return ReadBytes(0, GetSize());
}
u64 VfsFile::ReplaceBytes(const std::vector<u8>& data) {
if (!Resize(data.size()))
return 0;
return WriteBytes(data, 0);
}
boost::optional<VfsFile> VfsDirectory::GetFile(const std::string& name) {
auto files = GetFiles();
auto iter = std::find_if(files.begin(), files.end(),
[&name](auto file1) { return name == file1.GetName(); });
return iter == files.end() ? boost::none : boost::make_optional(*iter);
}
boost::optional<VfsDirectory> VfsDirectory::GetSubdirectory(const std::string& name) {
auto subs = GetSubdirectories();
auto iter = std::find_if(subs.begin(), subs.end(),
[&name](auto file1) { return name == file1.GetName(); });
return iter == subs.end() ? boost::none : boost::make_optional(*iter);
}
u64 VfsDirectory::GetSize() {
auto files = GetFiles();
auto file_total = std::accumulate(files.begin(), files.end(), 0);
auto sub_dir = GetSubdirectories();
auto subdir_total = std::accumulate(sub_dir.begin(), sub_dir.end(), 0);
return file_total + subdir_total;
}
bool VfsDirectory::Copy(const std::string& src, const std::string& dest) {
auto f1 = CreateFile(src), f2 = CreateFile(dest);
if (f1 == boost::none || f2 == boost::none)
return false;
return f2->ReplaceBytes(f1->ReadAllBytes()) == f1->GetSize();
}
} // namespace FileSys