-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_env.cc
73 lines (65 loc) · 1.73 KB
/
parse_env.cc
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
static bool parse_env(string &path)
{
MLOG_TRACE("parse envrion for %s", path.c_str());
size_t pos = 0;
std::string strenv;
const std::string strsep(" \t/\r\n");
while ( std::string::npos != (pos = path.find('$', 0))
|| std::string::npos != (pos = path.find('@', 0)))
{
const char left = path[pos + 1];
char right = 0;
size_t env_start = pos + 1;
size_t env_end = 0;
if (left == '(' )
{
right = ')';
}
else if (left == '{')
{
right = '}';
}
if (0 == right)
{
env_end = path.find_first_of(strsep, env_start);
}
else
{
env_start++;
env_end = path.find_first_of(right, env_start);
}
if (std::string::npos != env_end)
{
strenv = path.substr(env_start, env_end - env_start);
}
else if (0 == right)
{
// "$WORKPATH"
strenv = path.substr(env_start);
}
else
{
MLOG_INFO("can not get env string, path=%s", path.c_str());
return false;
}
MLOG_TRACE("strenv is %s", strenv.c_str());
const char *env = getenv(strenv.c_str());
if (NULL == env)
{
MLOG_INFO("can not find env for %s", strenv.c_str() );
return false;
}
if (std::string::npos != env_end)
{
path.replace(pos, env_end - pos + (right ? 1 : 0), env);
}
else
{
// "$WORKPATH"
path = path.substr(0, --env_start);
path += env;
}
MLOG_TRACE("env replace after:%s", path.c_str());
}
return true;
}