forked from gliptic/liero
/
filesystem.hpp
162 lines (129 loc) · 3.34 KB
/
filesystem.hpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef LIERO_FILESYSTEM_HPP
#define LIERO_FILESYSTEM_HPP
#include <string>
#include <cstdio>
#include <memory>
#include <map>
#include <gvl/support/platform.hpp>
#include <gvl/io2/stream.hpp>
#include <gvl/zlib/zlib2.h>
#include <gvl/resman/shared_ptr.hpp>
std::string changeLeaf(std::string const& path, std::string const& newLeaf);
std::string getRoot(std::string const& path);
std::string getLeaf(std::string const& path);
std::string getBasename(std::string const& path);
std::string getExtension(std::string const& path);
std::string toUpperCase(std::string str);
std::string joinPath(std::string const& root, std::string const& leaf);
FILE* tolerantFOpen(std::string const& name, char const* mode);
std::size_t fileLength(FILE* f);
bool create_directories(std::string const& dir);
struct NodeName
{
NodeName(std::string nameInit, bool isDirInit)
: name(std::move(nameInit))
, isDir(isDirInit)
{
}
std::string name;
bool isDir;
};
struct DirectoryListing
{
std::vector<NodeName> subs;
DirectoryListing(DirectoryListing&& other)
: subs(std::move(other.subs))
{
}
DirectoryListing& operator=(DirectoryListing&& other)
{
subs = std::move(other.subs);
return *this;
}
DirectoryListing(std::string const& dir);
DirectoryListing(std::vector<NodeName>&& subsInit);
~DirectoryListing();
DirectoryListing operator|(DirectoryListing const& other)
{
DirectoryListing ret(std::move(subs));
ret.subs.insert(ret.subs.end(), other.subs.begin(), other.subs.end());
ret.sort();
return std::move(ret);
}
std::vector<NodeName>::iterator begin()
{
return subs.begin();
}
std::vector<NodeName>::iterator end()
{
return subs.end();
}
void sort();
};
struct FsNodeImp : gvl::shared
{
virtual std::string const& fullPath() = 0;
virtual DirectoryListing iter() = 0;
virtual gvl::shared_ptr<FsNodeImp> go(std::string const& name) = 0;
virtual gvl::source tryToSource() = 0;
virtual gvl::sink tryToSink() = 0;
virtual bool exists() const = 0;
};
struct FsNode
{
gvl::shared_ptr<FsNodeImp> imp;
FsNode()
{
}
explicit FsNode(std::string const& path);
FsNode(FsNode const& other) = default;
FsNode& operator=(FsNode const& other) = default;
FsNode(FsNode&& other)
: imp(std::move(other.imp))
{
}
FsNode(gvl::shared_ptr<FsNodeImp> imp)
: imp(std::move(imp))
{
}
FsNode& operator=(FsNode&& other)
{
imp = std::move(other.imp);
return *this;
}
operator void*() const
{
return imp.get();
}
std::string const& fullPath() const
{ return imp->fullPath(); }
DirectoryListing iter() const
{ return imp->iter(); }
FsNode operator/(std::string const& name) const
{ return FsNode(imp->go(name)); }
bool exists() const
{ return imp && imp->exists(); }
gvl::octet_reader toOctetReader() const
{
return gvl::octet_reader(toSource());
}
gvl::source toSource() const
{
auto s = imp->tryToSource();
if (!s)
throw std::runtime_error("Could not read " + fullPath());
return std::move(s);
}
gvl::octet_writer toOctetWriter() const
{
return gvl::octet_writer(toSink());
}
gvl::sink toSink() const
{
auto s = imp->tryToSink();
if (!s)
throw std::runtime_error("Could not write " + fullPath());
return std::move(s);
}
};
#endif // LIERO_FILESYSTEM_HPP