-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_dir.cpp
46 lines (36 loc) · 967 Bytes
/
check_dir.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
#include <sys/stat.h>
static inline bool is_dir(const char *path)
{
struct stat st;
return (0 == stat(path, &st)) && (st.st_mode & S_IFDIR);
}
static bool check_dir(string &path)
{
while (!path.empty() && path.back() == '/')
path.erase(path.size() - 1, 1);
if (false == parse_env(path))
return false;
mint4 len = path.size();
if (0 == mkdir(path.c_str(), 0777) || is_dir(path.c_str()) )
return true;
bool sep_state = false;
for (mint4 i = 0; i < len; i++)
{
if (path[i] != '/')
{
if (sep_state)
sep_state = false;
continue;
}
if (sep_state) // 忽略多余的分隔符
continue;
path[i] = '\0';
if (0 != mkdir(path.c_str(), 0777) && !is_dir(path.c_str()))
return false;
path[i] = '/';
sep_state = true;
}
if (0 != mkdir(path.c_str(), 0777) && !is_dir(path.c_str()))
return false;
return true;
}