First implementation of custom path type.
This commit is contained in:
@@ -5,30 +5,31 @@
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
std::vector<FileInfo> MemoryFileSystemAdapter::listFiles(const fs::path& folder)
|
||||
std::vector<FolderEntry> MemoryFileSystemAdapter::listFiles(PathView folder)
|
||||
{
|
||||
const detail::MemoryFolder* folderObj = findFolder(folder);
|
||||
if (folderObj == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
std::vector<FileInfo> result;
|
||||
std::vector<FolderEntry> result;
|
||||
result.reserve(folderObj->folders.size() + folderObj->files.size());
|
||||
|
||||
const Path folderPath(folder);
|
||||
for (const auto& [name, subFolder] : folderObj->folders)
|
||||
{
|
||||
result.push_back(folderInfo(folder / name, subFolder));
|
||||
result.emplace_back(folderPath / name, folderInfo(subFolder));
|
||||
}
|
||||
|
||||
for (const auto& [name, file] : folderObj->files)
|
||||
{
|
||||
result.push_back(fileInfo(folder / name, file));
|
||||
result.emplace_back(folderPath / name, fileInfo(file));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file)
|
||||
FileInfo MemoryFileSystemAdapter::getFileInfo(PathView file)
|
||||
{
|
||||
#if 0 // shouldn't be necessary
|
||||
// empty means root
|
||||
@@ -43,38 +44,38 @@ FileInfo MemoryFileSystemAdapter::getFileInfo(const fs::path& file)
|
||||
}
|
||||
#endif
|
||||
|
||||
const detail::MemoryFolder* folderObj = findFolder(file.parent_path());
|
||||
const detail::MemoryFolder* folderObj = findFolder(file.getParent());
|
||||
if (folderObj == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
const std::string filename = file.filename().generic_string();
|
||||
const std::string_view filename = file.getName();
|
||||
|
||||
if (auto itFolder = folderObj->folders.find(filename); itFolder != folderObj->folders.end())
|
||||
{
|
||||
return folderInfo(file, itFolder->second);
|
||||
return folderInfo(itFolder->second);
|
||||
}
|
||||
if (auto itFile = folderObj->files.find(filename); itFile != folderObj->files.end())
|
||||
{
|
||||
return fileInfo(file, itFile->second);
|
||||
return fileInfo(itFile->second);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
StreamError MemoryFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
{
|
||||
if (mode != FileOpenMode::READ)
|
||||
{
|
||||
return StreamError::IO_ERROR;
|
||||
}
|
||||
|
||||
const detail::MemoryFolder* folderObj = findFolder(path.parent_path());
|
||||
const detail::MemoryFolder* folderObj = findFolder(path.getParent());
|
||||
if (folderObj == nullptr)
|
||||
{
|
||||
return StreamError::IO_ERROR;
|
||||
}
|
||||
|
||||
auto itFile = folderObj->files.find(path.filename().generic_string());
|
||||
auto itFile = folderObj->files.find(path.getName());
|
||||
if (itFile == folderObj->files.end())
|
||||
{
|
||||
return StreamError::IO_ERROR;
|
||||
@@ -87,10 +88,10 @@ StreamError MemoryFileSystemAdapter::open(const fs::path& path, FileOpenMode mod
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span<const std::uint8_t> data, Overwrite overwrite, CopyData copyData)
|
||||
bool MemoryFileSystemAdapter::addFile(PathView path, std::span<const std::uint8_t> data, Overwrite overwrite, CopyData copyData)
|
||||
{
|
||||
detail::MemoryFolder& folder = *findFolder(path.parent_path(), true);
|
||||
std::string filename = path.filename().generic_string();
|
||||
detail::MemoryFolder& folder = *findFolder(path.getParent(), true);
|
||||
const std::string_view filename = path.getName();
|
||||
|
||||
if (folder.folders.contains(filename))
|
||||
{
|
||||
@@ -107,29 +108,28 @@ bool MemoryFileSystemAdapter::addFile(const fs::path& path, std::span<const std:
|
||||
data = fileData_.emplace_back(data.begin(), data.end());
|
||||
}
|
||||
|
||||
folder.files.emplace(std::move(filename), detail::MemoryFile{.data = data});
|
||||
folder.files.emplace(filename, detail::MemoryFile{.data = data});
|
||||
return true;
|
||||
}
|
||||
|
||||
void MemoryFileSystemAdapter::addFolder(const fs::path& path)
|
||||
void MemoryFileSystemAdapter::addFolder(PathView path)
|
||||
{
|
||||
(void) findFolder(path, true);
|
||||
}
|
||||
|
||||
detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path, bool create) MIJIN_NOEXCEPT
|
||||
detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(PathView path, bool create) MIJIN_NOEXCEPT
|
||||
{
|
||||
detail::MemoryFolder* folder = &root_;
|
||||
for (const fs::path& part : path)
|
||||
for (const std::string_view part : path)
|
||||
{
|
||||
std::string partname = part.generic_string();
|
||||
auto it = folder->folders.find(partname);
|
||||
auto it = folder->folders.find(part);
|
||||
if (it == folder->folders.end())
|
||||
{
|
||||
if (!create)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
folder = &folder->folders[std::move(partname)];
|
||||
folder = &folder->folders[std::string(part)];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -139,20 +139,18 @@ detail::MemoryFolder* MemoryFileSystemAdapter::findFolder(const fs::path& path,
|
||||
return folder;
|
||||
}
|
||||
|
||||
FileInfo MemoryFileSystemAdapter::folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
|
||||
FileInfo MemoryFileSystemAdapter::folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT
|
||||
{
|
||||
return {
|
||||
.path = path,
|
||||
.size = folder.folders.size() + folder.files.size(),
|
||||
.exists = true,
|
||||
.isFolder = true
|
||||
};
|
||||
}
|
||||
|
||||
FileInfo MemoryFileSystemAdapter::fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT
|
||||
FileInfo MemoryFileSystemAdapter::fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT
|
||||
{
|
||||
return {
|
||||
.path = path,
|
||||
.size = file.data.size(),
|
||||
.exists = true
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user