-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtinyvfs.cpp
More file actions
85 lines (62 loc) · 1.67 KB
/
tinyvfs.cpp
File metadata and controls
85 lines (62 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "tinyvfs.hpp"
TinyVFS* TinyVFS::FS()
{
static TinyVFS vfs;
return &vfs;
}
VFSPath TinyVFS::GetPathAndFile(std::string filePath)
{
VFSPath result;
int endOfPathIndex = 0;
for (int c = 0; c < filePath.size(); c++)
{
if (filePath[c] == '/')
endOfPathIndex = c;
}
result.path = filePath.substr(0, endOfPathIndex + 1);
result.fileName = filePath.substr(result.path.length(), filePath.length());
return result;
}
bool TinyVFS::DoesFileExist(std::string filePath)
{
std::ifstream checker(filePath);
return checker.good();
}
void TinyVFS::MountDir(std::string virtualDir, std::string physicalDir)
{
mappedDirs[virtualDir].push_back(physicalDir);
}
void TinyVFS::UnmountDir(std::string virtualDir)
{
mappedDirs[virtualDir].clear();
}
std::string TinyVFS::ResolvePhysicalDir(std::string virtualDir)
{
std::string physicalDir;
auto pathAndFile = GetPathAndFile(virtualDir);
auto virtualPath = pathAndFile.path;
auto fileName = pathAndFile.fileName;
for (auto &item : mappedDirs[virtualPath])
{
auto pathAndFileName = item + fileName;
if (DoesFileExist(pathAndFileName))
physicalDir = pathAndFileName;
}
return physicalDir;
}
bool TinyVFS::ReadTextFile(std::string virtualDir, std::string &outFileContents)
{
auto resolved = ResolvePhysicalDir(virtualDir);
std::ifstream fileHandle(resolved);
if (!fileHandle)
return false;
std::string data;
std::string contents;
while(getline(fileHandle, data))
{
contents += (data + "\n");
}
outFileContents = contents;
fileHandle.close();
return true;
}