Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_utils.cpp
More file actions
Latest commit
96 lines (88 loc) · 1.83 KB
/
Copy pathfile_utils.cpp
File metadata and controls
96 lines (88 loc) · 1.83 KB
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
#include"file_utils.h"
#include<unistd.h>
boolremoveDir(const std::string& path)
{
if ( rmdir(path.c_str()) != 0 )
{
returnfalse;
}
returntrue;
}
boolfileExists(const std::string& path)
{
#if defined(_WIN32)
struct_stat info;
if (_stat(path.c_str(), &info) != 0)
{
returnfalse;
}
return (info.st_mode & _S_IFREG) != 0;
#else
structstat info;
if (stat(path.c_str(), &info) != 0)
{
returnfalse;
}
return (info.st_mode & S_IFREG) != 0;
#endif
}
booldirExists(const std::string& path)
{
returnisDirExist(path);
}
boolisDirExist(const std::string& path)
{
#if defined(_WIN32)
struct_stat info;
if (_stat(path.c_str(), &info) != 0)
{
returnfalse;
}
return (info.st_mode & _S_IFDIR) != 0;
#else
structstat info;
if (stat(path.c_str(), &info) != 0)
{
returnfalse;
}
return (info.st_mode & S_IFDIR) != 0;
#endif
}
boolmakePath(const std::string& path)
{
#if defined(_WIN32)
int ret = _mkdir(path.c_str());
#else
mode_t mode = 0755;
int ret = mkdir(path.c_str(), mode);
#endif
if (ret == 0)
returntrue;
switch (errno)
{
caseENOENT:
// parent didn't exist, try to create it
{
int pos = path.find_last_of('/');
if (pos == std::string::npos)
#if defined(_WIN32)
pos = path.find_last_of('\\');
if (pos == std::string::npos)
#endif
returnfalse;
if (!makePath( path.substr(0, pos) ))
returnfalse;
}
// now, try to create again
#if defined(_WIN32)
return0 == _mkdir(path.c_str());
#else
return0 == mkdir(path.c_str(), mode);
#endif
caseEEXIST:
// done!
returnisDirExist(path);
default:
returnfalse;
}
}