forked from SpectacularAI/sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.hpp
51 lines (45 loc) · 1.17 KB
/
helpers.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
#include <sys/stat.h>
#ifdef _MSC_VER
#include <direct.h>
#endif
int makeDir(const std::string &dir) {
#ifdef _MSC_VER
return _mkdir(dir.c_str());
#else
mode_t mode = 0755;
return mkdir(dir.c_str(), mode);
#endif
}
bool folderExists(const std::string &folder) {
#ifdef _MSC_VER
struct _stat info;
if (_stat(folder.c_str(), &info) != 0) return false;
return (info.st_mode & _S_IFDIR) != 0;
#else
struct stat info;
if (stat(folder.c_str(), &info) != 0) return false;
return (info.st_mode & S_IFDIR) != 0;
#endif
}
bool createFolders(const std::string &folder) {
int ret = makeDir(folder);
if (ret == 0) return true;
switch (errno) {
case ENOENT: {
size_t pos = folder.find_last_of('/');
if (pos == std::string::npos)
#ifdef _MSC_VER
pos = folder.find_last_of('\\');
if (pos == std::string::npos)
#endif
return false;
if (!createFolders(folder.substr(0, pos)))
return false;
return 0 == makeDir(folder);
}
case EEXIST:
return folderExists(folder);
default:
return false;
}
}