-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environment.cpp
119 lines (88 loc) · 2.23 KB
/
Environment.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
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
/*
* Environment.cpp
*
* Created on: Oct 10, 2016
* Author: paul
*/
#include "Environment.h"
#include "Directory.h"
#include <string>
#include <cctype>
#include <cassert>
using namespace std;
Environment::Environment (string appName)
{
m_builtins = {"IASH_APP_NAME", "CWD", "?"};
setProtected("IASH_APP_NAME", appName);
setProtected("CWD", (Directory::getWorkingDir()).getAbsPath());
setProtected("?", "0");
}
Environment::~Environment () {}
void Environment::set (const string &key, const string &value)
{
assert(key.find(' ') == string::npos);
string keyInCaps = convName(key);
if (!isBuiltinVar(key)) setProtected(key, value);
}
void Environment::set (const string &key, const bool value)
{
if (value) set(key, "true");
else set(key, "false");
}
void Environment::set (const string &key, const int value)
{
set(key, to_string(value));
}
void Environment::set (const string &key, const double value)
{
set(key, to_string(value));
}
const string& Environment::getString (const string &key) const
{
assert(key.find(' ') == string::npos);
return m_envMap.at(convName(key));
}
bool Environment::getBool (const string &key) const
{
string envString = getString(key);
assert(envString == "true" || envString == "false");
return envString == "true";
}
int Environment::getInt (const string &key) const
{
string envString = getString(key);
assert(envString.length() > 0);
return stoi(envString);
}
double Environment::getDouble (const string &key) const
{
string envString = getString(key);
assert(envString.length() > 0);
return stod(envString);
}
void Environment::rm (const string &key)
{
map<string,string>::iterator it = m_envMap.find(key);
if (it != m_envMap.end()) m_envMap.erase(it);
}
void Environment::setProtected (const string &key, const string &value)
{
assert(key.find(' ') == string::npos);
string keyInCaps = convName (key);
m_envMap[keyInCaps] = value;
}
bool Environment::isBuiltinVar (const string &key) const
{
for (unsigned i = 0; i < m_builtins.size(); ++i)
{
if (key == m_builtins.at(i)) return true;
}
return false;
}
string Environment::convName (const string &key)
{
string keyInCaps = key;
for (size_t i = 0; i < key.length(); ++i)
keyInCaps[i] = toupper(key[i]);
return keyInCaps;
}