-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFakeFileSystem.cpp
More file actions
144 lines (123 loc) · 3.44 KB
/
FakeFileSystem.cpp
File metadata and controls
144 lines (123 loc) · 3.44 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "FakeFileSystem.h"
#include <iterator>
FakeFileSystem::FakeFileSystem(const FileSystemObjects &rootObjects)
: m_root(root.string(), rootObjects)
{ }
std::set<std::string> FakeFileSystem::GetChildren(const path &dir)
{
const auto& fsos = GoTo(dir).GetChildren();
std::set<std::string> children;
std::transform(fsos.begin(), fsos.end(), std::inserter(children, children.begin()), [](const auto& fso) { return fso.GetName(); });
return children;
}
bool FakeFileSystem::IsDir(const path &obj)
{
return GoTo(obj).IsDir();
}
void FakeFileSystem::MakeDir(const path &dir)
{
GoToDir(dir.parent_path()).AddChild(Folder(dir.filename().string()));
}
void FakeFileSystem::CopyFile(const path &src, const path &dst)
{
src; // unused
CreateFile(dst);
}
bool FakeFileSystem::FileExists(const path &file)
{
auto fso = GoToSafe(file);
return fso != nullptr && !fso->IsDir();
}
bool FakeFileSystem::DirExists(const path &dir)
{
auto fso = GoToSafe(dir);
return fso != nullptr && fso->IsDir();
}
void FakeFileSystem::CreateFile(const path &file)
{
GoToDir(file.parent_path()).AddChild(File(file.filename().string()));
}
FileSystemObject &FakeFileSystem::GoTo(const path &obj)
{
FileSystemObject* current = &m_root;
path::iterator it = obj.begin();
if (it == obj.end())
{
throw std::runtime_error("Path is empty");
}
for (++it; it != obj.end(); ++it)
{
auto child = std::find_if(current->GetChildren().begin(), current->GetChildren().end(), [&it](const auto& fso)
{ return fso.GetName() == it->string(); }
);
if (child == current->GetChildren().end())
{
throw std::runtime_error("Path does not exist");
}
current = (FileSystemObject*)&(*child);
}
return *current;
}
Folder &FakeFileSystem::GoToDir(const path &dir)
{
auto& target = GoTo(dir);
if (!target.IsDir())
{
throw std::runtime_error("Target path is not a directory");
}
return (Folder&)target;
}
FileSystemObject *FakeFileSystem::GoToSafe(const path &path)
{
FileSystemObject* fso = nullptr;
try
{
fso = &GoTo(path);
}
catch(const std::exception&) { }
return fso;
}
FileSystemObject::FileSystemObject(const std::string& name, bool isDir)
: name(name)
, isDir(isDir)
{ }
FileSystemObject::FileSystemObject(const std::string &name, const FileSystemObjects &children)
: name(name)
, children(children)
, isDir(true)
{ }
const bool FileSystemObject::operator==(const FileSystemObject &other) const
{
return name == other.name;
}
const bool FileSystemObject::operator!=(const FileSystemObject &other) const
{
return !operator==(other);
}
const bool FileSystemObject::operator<(const FileSystemObject &other) const
{
return name < other.name;
}
const std::string &FileSystemObject::GetName() const
{
return name;
}
const FileSystemObjects &FileSystemObject::GetChildren() const
{
return children;
}
bool FileSystemObject::IsDir() const
{
return isDir;
}
void Folder::AddChild(const FileSystemObject &child, bool overwrite /*= false*/)
{
FileSystemObjects::iterator existant = std::find_if(children.begin(), children.end(), [&child](const auto& fso)
{ return fso.GetName() == child.GetName(); }
);
if (existant != children.end() && overwrite)
{
throw std::runtime_error("Child with specified name is already exist");
}
children.insert(child);
}