File system base Implementation #11

Merged
XiaoQiDigipen merged 4 commits from SP3-14-FileSystem into main 2022-09-13 11:42:30 +08:00
4 changed files with 123 additions and 0 deletions
Showing only changes of commit 9a223098f6 - Show all commits

View File

@ -103,10 +103,12 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="src\Engine\SHEngine.h" />
<ClInclude Include="src\Filesystem\SHFileSystem.h" />
<ClInclude Include="src\SHpch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Engine\SHEngine.cpp" />
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
<ClCompile Include="src\SHpch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>

View File

@ -10,11 +10,13 @@
<Filter>Engine</Filter>
</ClInclude>
<ClInclude Include="src\SHpch.h" />
<ClInclude Include="src\Filesystem\SHFileSystem.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Engine\SHEngine.cpp">
<Filter>Engine</Filter>
</ClCompile>
<ClCompile Include="src\SHpch.cpp" />
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,75 @@
#include "SHFileSystem.h"
#include "fileapi.h"
#include <cassert>
namespace SHADE
{
char const FOLDER_MAX_COUNT {15};
std::unordered_map<FolderLocation, std::unique_ptr<SHFolder>> SHFileSystem::folders;
SHFolder::SHFolder(FolderHandle id, FolderName name)
:id{id}, name{name}, subFolders(0)
{
}
FolderLocation SHFileSystem::CreateNewFolderHere(FolderName name, FolderLocation here) noexcept
{
if (here == 0)
{
if (!folders.contains(0))
{
folders[0] = std::make_unique<SHFolder>(0, "root");
}
FolderCounter const count = static_cast<FolderCounter>(folders[here]->subFolders.size());
assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n");
FolderHandle const location = static_cast<FolderLocation>(count);
CreateFolderHelper(folders[0]->path, here, location, name);
return location;
}
assert(folders.contains(here), "Folder creation location does not exist/invalid\n");
FolderCounter const count = static_cast<FolderCounter>(folders[here]->subFolders.size());
FolderHandle location = here;
location <<= 4;
location |= count;
assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n");
CreateFolderHelper(folders[0]->path, here, location, name);
return location;
}
bool SHFileSystem::DeleteFolder(FolderLocation location) noexcept
{
assert(folders.contains(location), "Delete target does not exist/invalid.\n");
for (auto const& subFolder : folders[location]->subFolders)
{
DeleteFolder(subFolder);
}
RemoveDirectoryA(folders[location]->path.string().c_str());
return true;
}
bool SHFileSystem::CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept
{
assert(
CreateDirectoryA(path.string().c_str(), nullptr),
"Failed to create folder\n"
);
folders[location] = std::make_unique<SHFolder>(location, name);
folders[parent]->subFolders.push_back(location);
return true;
}
}

View File

@ -0,0 +1,44 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <filesystem>
#include <unordered_map>
namespace SHADE
{
typedef unsigned char FolderCounter;
typedef unsigned char FileCounter;
typedef uint32_t FolderLocation;
typedef uint32_t FolderHandle;
typedef std::string FolderName;
typedef std::filesystem::path FolderPath;
class SHFolder
{
public:
SHFolder(FolderHandle id, FolderName name);
FolderHandle id;
FolderName name;
std::vector<FolderHandle> subFolders;
private:
FolderPath path;
friend class SHFileSystem;
};
class SHFileSystem
{
public:
static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept;
static bool DeleteFolder(FolderLocation location) noexcept;
private:
static std::unordered_map<FolderLocation, std::unique_ptr<SHFolder>> folders;
static bool CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept;
};
}