forked from graphitemaster/neothyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u_file.cpp
282 lines (242 loc) · 6.32 KB
/
u_file.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdlib.h>
#include <string.h>
// These exist on Windows too
#include <sys/stat.h> // S_ISREG, stat
#include <sys/types.h>
#ifdef _WIN32
# define _WIN32_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
# include <direct.h> // rmdir, mkdir
#else
# include <dirent.h> // opendir, readir, DIR
# include <unistd.h> // rmdir, mkdir
#endif
#include "u_file.h"
#include "u_algorithm.h"
namespace u {
file::file()
: m_handle(nullptr)
{
}
file::file(FILE *fp)
: m_handle(fp)
{
}
file::~file() {
if (m_handle)
fclose(m_handle);
}
file::operator FILE*() {
return m_handle;
}
FILE *file::get() {
return m_handle;
}
u::string fixPath(const u::string &path) {
u::string fix = path;
for (auto &it : fix)
if (strchr("/\\", it))
it = u::kPathSep;
return fix;
}
// file system stuff
bool exists(const u::string &path, pathType type) {
if (type == kFile)
return dir::isFile(path);
// type == kDirectory
#ifndef _WIN32
struct stat info;
if (stat(path.c_str(), &info) != 0)
return false; // Couldn't stat directory
if (!(info.st_mode & S_IFDIR))
return false; // Not a directory
#else
// _stat does not like trailing path separators
u::string strip = path;
if (strip.end()[-1] == u::kPathSep)
strip.pop_back();
struct _stat info;
if (_stat(strip.c_str(), &info) != 0)
return false;
#ifndef S_IFDIR
# define S_IFDIR _S_IFDIR
#endif
if (!(info.st_mode & _S_IFDIR))
return false;
#endif
return true;
}
bool remove(const u::string &path, pathType type) {
u::string fix = fixPath(path);
if (type == kFile)
return ::remove(fix.c_str()) == 0;
// type == kDirectory
#ifdef _WIN32
return ::_rmdir(fix.c_str()) == 0;
#else
return ::rmdir(fix.c_str()) == 0;
#endif
}
bool mkdir(const u::string &dir) {
u::string fix = fixPath(dir);
const char *path = fix.c_str();
#ifdef _WIN32
return ::_mkdir(path) == 0;
#else
return ::mkdir(path, 0775);
#endif
}
u::file fopen(const u::string& infile, const char *type) {
return ::fopen(fixPath(infile).c_str(), type);
}
u::optional<u::string> getline(u::file &fp) {
u::string s;
for (;;) {
char buf[256];
if (!fgets(buf, sizeof(buf), fp.get())) {
if (feof(fp.get())) {
if (s.empty())
return u::none;
else
return u::move(s);
}
abort();
}
size_t n = strlen(buf);
if (n && buf[n - 1] == '\n')
--n;
s.append(buf, n);
if (n < sizeof(buf) - 1)
return u::move(s);
}
}
u::optional<u::vector<unsigned char>> read(const u::string &file, const char *mode) {
auto fp = u::fopen(file, mode);
if (!fp)
return u::none;
u::vector<unsigned char> data;
fseek(fp.get(), 0, SEEK_END);
auto size = ftell(fp.get());
if (size <= 0)
return u::none;
data.resize(size);
fseek(fp.get(), 0, SEEK_SET);
if (fread(&data[0], data.size(), 1, fp.get()) != 1)
return u::none;
return data;
}
bool write(const u::vector<unsigned char> &data, const u::string &file, const char *mode) {
auto fp = u::fopen(file, mode);
if (!fp)
return false;
if (fwrite(&data[0], data.size(), 1, fp.get()) != 1)
return false;
return true;
}
///! dir
#ifndef _WIN32
#include <unistd.h>
#include <dirent.h>
#define IS_IGNORE(X) (!strcmp((X)->d_name, ".") || !strcmp((X)->d_name, ".."))
///! dir::const_iterator
dir::const_iterator::const_iterator(void *handle)
: m_handle(handle)
, m_name(nullptr)
{
if (!m_handle)
return;
// Ignore "." and ".."
struct dirent *next = readdir((DIR *)m_handle);
while (next && IS_IGNORE(next))
next = readdir((DIR *)m_handle);
m_name = next ? next->d_name : nullptr;
}
dir::const_iterator &dir::const_iterator::operator++() {
struct dirent *next = readdir((DIR *)m_handle);
while (next && IS_IGNORE(next))
next = readdir((DIR *)m_handle);
m_name = next ? next->d_name : nullptr;
return *this;
}
///! dir
dir::dir(const char *where)
: m_handle((void *)opendir(where))
{
}
dir::~dir() {
if (m_handle)
closedir((DIR *)m_handle);
}
bool dir::isFile(const char *fileName) {
struct stat buff;
if (stat(fileName, &buff) != 0)
return false;
return S_ISREG(buff.st_mode);
}
#else
#include <assert.h>
#define IS_IGNORE(X) (!strcmp((X).cFileName, ".") || !strcmp((X).cFileName, ".."))
struct findContext {
findContext(const char *where);
~findContext();
HANDLE handle;
WIN32_FIND_DATA findData;
};
inline findContext::findContext(const char *where)
: handle(INVALID_HANDLE_VALUE)
{
static constexpr const char kPathExtra[] = "\\*";
const size_t length = strlen(where);
assert(length + sizeof(kPathExtra) < MAX_PATH);
char path[MAX_PATH];
memcpy((void *)path, (const void *)where, length);
memcpy((void *)&path[length], (const void *)kPathExtra, sizeof(kPathExtra));
if (!(handle = FindFirstFileA(path, &findData)))
return;
// Ignore "." and ".."
if (IS_IGNORE(findData)) {
BOOL next = FindNextFileA(handle, &findData);
while (next && IS_IGNORE(findData))
next = FindNextFileA(handle, &findData);
}
}
inline findContext::~findContext() {
if (handle != INVALID_HANDLE_VALUE)
FindClose(handle);
}
///! dir::const_iterator
dir::const_iterator::const_iterator(void *handle)
: m_handle(handle)
, m_name(((findContext*)m_handle)->findData.cFileName)
{
}
dir::const_iterator &dir::const_iterator::operator++() {
findContext *context = (findContext*)m_handle;
BOOL next = FindNextFileA(context->handle, &context->findData);
// Ignore "." and ".."
while (next && IS_IGNORE(context->findData))
next = FindNextFileA(context->handle, &context->findData);
m_name = next ? context->findData.cFileName : nullptr;
return *this;
}
///! dir
dir::dir(const char *where)
: m_handle(new findContext(where))
{
}
dir::~dir() {
delete (findContext*)m_handle;
}
bool dir::isFile(const char *fileName) {
struct _stat buff;
if (_stat(fileName, &buff) != 0)
return false;
#ifdef S_ISREG
return S_ISREG(buff.st_mode);
#else
return _S_ISREG(buff.st_mode);
#endif
}
#endif
}