-
Notifications
You must be signed in to change notification settings - Fork 288
/
FileSystem.h
400 lines (353 loc) · 9.2 KB
/
FileSystem.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#ifndef __FileSystem_h__
#define __FileSystem_h__
#include "StringTools.h"
#include "extern/md5/md5.h"
#include <sys/stat.h>
#ifdef WIN32
#include <direct.h>
#define NOMINMAX
#include "windows.h"
#include <commdlg.h>
#else
#include <unistd.h>
#include <dirent.h>
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#endif
namespace Utilities
{
/** \brief This class implements different file system functions.
*/
class FileSystem
{
public:
static std::string getFilePath(const std::string &path)
{
std::string npath = normalizePath(path);
std::string result = npath;
size_t i = result.rfind('.', result.length());
if (i != std::string::npos)
{
result = result.substr(0, i);
}
size_t p1 = result.rfind('\\', result.length());
size_t p2 = result.rfind('/', result.length());
if ((p1 != std::string::npos) && (p2 != std::string::npos))
result = result.substr(0, std::max(p1, p2));
else if (p1 != std::string::npos)
result = result.substr(0, p1);
else if (p2 != std::string::npos)
result = result.substr(0, p2);
return result;
}
static std::string getFileName(const std::string &path)
{
std::string npath = normalizePath(path);
std::string result = npath;
size_t i = result.rfind('.', result.length());
if (i != std::string::npos)
{
result = result.substr(0, i);
}
size_t p1 = result.rfind('\\', result.length());
size_t p2 = result.rfind('/', result.length());
if ((p1 != std::string::npos) && (p2 != std::string::npos))
result = result.substr(std::max(p1, p2) + 1, result.length());
else if (p1 != std::string::npos)
result = result.substr(p1 + 1, result.length());
else if (p2 != std::string::npos)
result = result.substr(p2 + 1, result.length());
return result;
}
static std::string getFileNameWithExt(const std::string &path)
{
std::string npath = normalizePath(path);
std::string result = npath;
size_t p1 = result.rfind('\\', result.length());
size_t p2 = result.rfind('/', result.length());
if ((p1 != std::string::npos) && (p2 != std::string::npos))
result = result.substr(std::max(p1, p2) + 1, result.length());
else if (p1 != std::string::npos)
result = result.substr(p1 + 1, result.length());
else if (p2 != std::string::npos)
result = result.substr(p2 + 1, result.length());
return result;
}
static std::string getFileExt(const std::string &path)
{
std::string npath = normalizePath(path);
std::string result = npath;
size_t i = result.rfind('.', result.length());
if (i != std::string::npos)
{
result = result.substr(i + 1, result.length());
}
return result;
}
static bool isRelativePath(const std::string &path)
{
std::string npath = normalizePath(path);
// Windows
size_t i = npath.find(":");
if (i != std::string::npos)
return false;
else if (npath[0] == '/')
return false;
return true;
}
static int makeDir(const std::string &path)
{
std::string npath = normalizePath(path);
struct stat st;
int status = 0;
if (stat(path.c_str(), &st) != 0)
{
#if WIN32
status = _mkdir(path.c_str());
#else
status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
if (status != 0 && errno != EEXIST)
status = -1;
}
else if (!(S_IFDIR & st.st_mode))
{
errno = ENOTDIR;
status = -1;
}
return status;
}
/** Make all subdirectories.
*/
static int makeDirs(const std::string &path)
{
char *pp;
char *sp;
int status;
#ifdef WIN32
char *copyOfPath = _strdup(path.c_str());
#else
char *copyOfPath = strdup(path.c_str());
#endif
status = 0;
pp = copyOfPath;
pp = pp + 3; // Cut away Drive:
while ((status == 0) && (((sp = strchr(pp, '/')) != 0) || ((sp = strchr(pp, '\\')) != 0)))
{
if (sp != pp)
{
*sp = '\0';
status = makeDir(copyOfPath);
*sp = '/';
}
pp = sp + 1;
}
if (status == 0)
status = makeDir(path);
free(copyOfPath);
return status;
}
static std::string normalizePath(const std::string &path)
{
if (path.size() == 0)
return path;
std::string result = path;
std::replace(result.begin(), result.end(), '\\', '/');
std::vector<std::string> tokens;
StringTools::tokenize(result, tokens, "/");
unsigned int index = 0;
while (index < tokens.size())
{
if ((tokens[index] == "..") && (index > 0))
{
tokens.erase(tokens.begin() + index - 1, tokens.begin() + index + 1);
index-=2;
}
index++;
}
result = "";
if (path[0] == '/')
result = "/";
result = result + tokens[0];
for (unsigned int i = 1; i < tokens.size(); i++)
result = result + "/" + tokens[i];
return result;
}
static bool fileExists(const std::string& fileName)
{
if (FILE *file = fopen(fileName.c_str(), "r"))
{
fclose(file);
return true;
}
else
return false;
}
static std::string getProgramPath()
{
char buffer[1000];
#ifdef WIN32
GetModuleFileName(NULL, buffer, 1000);
#else
char szTmp[32];
sprintf(szTmp, "/proc/%d/exe", getpid());
int bytes = std::min((int)readlink(szTmp, buffer, 1000), 999);
buffer[bytes] = '\0';
#endif
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
return std::string(buffer).substr(0, pos);
}
static bool copyFile(const std::string &source, const std::string &dest)
{
const size_t bufferSize = 8192;
char buffer[bufferSize];
size_t size;
FILE* sourceFile = fopen(source.c_str(), "rb");
FILE* destFile = fopen(dest.c_str(), "wb");
if ((sourceFile == NULL) || (destFile == NULL))
return false;
while (size = fread(buffer, 1, bufferSize, sourceFile))
{
fwrite(buffer, 1, size, destFile);
}
fclose(sourceFile);
fclose(destFile);
return true;
}
static bool isFile(const std::string &path)
{
struct stat st;
if (!stat(path.c_str(), &st))
return S_ISREG(st.st_mode);
return false;
}
static bool isDirectory(const std::string &path)
{
struct stat st;
if (!stat(path.c_str(), &st))
return S_ISDIR(st.st_mode);
return false;
}
static bool getFilesInDirectory(const std::string& path, std::vector<std::string> &res)
{
#ifdef WIN32
std::string p = path + "\\*";
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(p.c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
res.push_back(data.cFileName);
}
while (FindNextFile(hFind, &data) != 0);
FindClose(hFind);
return true;
}
return false;
#else
DIR* dir = opendir(path.c_str());
if (dir != NULL)
{
struct dirent *dp;
while ((dp = readdir(dir)) != NULL)
res.push_back(dp->d_name);
closedir(dir);
return true;
}
return false;
#endif
}
/** Compute the MD5 hash of a file.
*/
static std::string getFileMD5(const std::string &filename)
{
std::ifstream file(filename);
if (!file)
std::cerr << "Cannot open file: " << filename << std::endl;
else
{
MD5 context(file);
char *md5hex = context.hex_digest();
std::string res(md5hex);
delete[] md5hex;
return res;
}
return "";
}
/** Write the MD5 hash of a file to the md5File.
*/
static bool writeMD5File(const std::string& fileName, const std::string& md5File)
{
std::ofstream fstream;
fstream.open(md5File.c_str(), std::ios::out);
if (fstream.fail())
{
std::cerr << "Failed to open file: " << md5File << "\n";
return false;
}
std::string md5 = getFileMD5(fileName);
if (md5 != "")
fstream.write(md5.c_str(), md5.size());
fstream.close();
return true;
}
/** Compare an MD5 hash with the hash stored in an MD5 file.
*/
static bool checkMD5(const std::string& md5Hash, const std::string& md5File)
{
std::ifstream fstream;
fstream.open(md5File.c_str(), std::ios::in);
if (fstream.fail())
{
std::cerr << "Failed to open file: " << md5File << "\n";
return false;
}
std::string str((std::istreambuf_iterator<char>(fstream)),
std::istreambuf_iterator<char>());
fstream.close();
return str == md5Hash;
}
#ifdef WIN32
/** Open windows file dialog.\n
* dialogType 0 = Open file dialog\n
* dialogType 1 = Save file dialog\n
*/
static const std::string fileDialog(int dialogType,
const std::string &initialDir,
const std::string &filter)
{
std::string initDir = normalizePath(initialDir);
std::replace(initDir.begin(), initDir.end(), '/', '\\');
OPENFILENAME ofn; // common dialog box structure
char fileNameBuffer[512];
fileNameBuffer[0] = '\0';
const std::string filterWithEscape = filter + '\0';
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = fileNameBuffer;
ofn.nMaxFile = sizeof(fileNameBuffer);
ofn.lpstrFilter = filterWithEscape.c_str();
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = (LPSTR)initDir.c_str();
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
if (dialogType == 0)
{
if (GetOpenFileName(&ofn))
return std::string(fileNameBuffer);
}
else
{
if (GetSaveFileName(&ofn))
return std::string(fileNameBuffer);
}
return "";
}
#endif
};
}
#endif