-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.h
190 lines (153 loc) · 3.4 KB
/
file.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef FILE_H
#define FILE_H
#include "string_view.h"
#include "stream.h"
#include "path.h"
#include <stdint.h>
class file
{
public:
file();
file(string_view path, bool readonly);
file(file && o);
~file();
file & operator=(file && o);
void open(string_view path, bool readonly);
bool try_open(string_view path, bool readonly);
void close();
bool is_open() const;
class ifile
: public istream
{
public:
ifile(file * f, file_offset_t pos)
: m_file(f), m_pos(pos)
{
}
size_t read(uint8_t * p, size_t capacity) override
{
size_t r = m_file->read_abs(m_pos, p, capacity);
m_pos += r;
return r;
}
private:
file * m_file;
file_offset_t m_pos;
};
class ofile
: public ostream
{
public:
ofile(file * f, file_offset_t pos)
: m_file(f), m_pos(pos)
{
}
size_t write(uint8_t const * p, size_t size) override
{
size_t r = m_file->write_abs(m_pos, p, size);
m_pos += r;
return r;
}
void flush() override
{
}
private:
file * m_file;
file_offset_t m_pos;
};
size_t read_abs(file_offset_t pos, uint8_t * p, size_t capacity);
size_t write_abs(file_offset_t pos, uint8_t const * p, size_t capacity);
ifile seekg(file_offset_t pos);
ofile seekp(file_offset_t pos);
static void create(string_view path, string_view content);
static bool exists(string_view path);
static bool is_file(string_view path);
static bool is_directory(string_view path);
private:
intptr_t m_fd;
file(file const &);
file & operator=(file const &);
};
enum class dir_entry_type
{
directory,
file,
gitlink,
};
struct directory_entry
{
std::string name;
std::string cannon_name;
uint32_t mtime;
uint32_t mode;
directory_entry()
{
}
directory_entry(std::string name, uint32_t mtime, uint32_t mode)
: name(std::move(name)), cannon_name(cannonical_path(this->name)), mtime(mtime), mode(mode)
{
}
directory_entry(directory_entry && o)
: name(std::move(o.name)), cannon_name(std::move(o.cannon_name)),
mtime(o.mtime), mode(o.mode)
{
}
directory_entry & operator=(directory_entry && o)
{
name = std::move(o.name);
cannon_name = std::move(o.cannon_name);
mtime = o.mtime;
mode = o.mode;
return *this;
}
friend void swap(directory_entry & lhs, directory_entry & rhs)
{
lhs.name.swap(rhs.name);
lhs.cannon_name.swap(rhs.cannon_name);
std::swap(lhs.mtime, rhs.mtime);
std::swap(lhs.mode, rhs.mode);
}
dir_entry_type type() const;
};
class dir_enum_proxy
{
public:
class iterator
{
public:
explicit iterator(dir_enum_proxy * pimpl);
directory_entry operator*() const;
iterator & operator++();
iterator operator++(int);
bool operator==(iterator rhs)
{
auto * l = m_pimpl? m_pimpl->m_pimpl: 0;
auto * r = rhs.m_pimpl? rhs.m_pimpl->m_pimpl: 0;
return l == r;
}
bool operator!=(iterator rhs)
{
return !(*this == rhs);
}
private:
dir_enum_proxy * m_pimpl;
};
dir_enum_proxy(string_view path, string_view mask);
dir_enum_proxy(dir_enum_proxy && o);
~dir_enum_proxy();
iterator begin();
iterator end();
private:
struct impl;
impl * m_pimpl;
static bool find_next(impl & pimpl);
dir_enum_proxy(dir_enum_proxy const &);
dir_enum_proxy & operator=(dir_enum_proxy const &);
};
inline dir_enum_proxy enumdir(string_view path, string_view mask = "*")
{
return dir_enum_proxy(path, mask);
}
std::vector<directory_entry> listdir(string_view path, string_view mask = "*");
bool make_directory(string_view path);
#endif // FILE_H