-
Notifications
You must be signed in to change notification settings - Fork 1
/
variables.cpp
91 lines (76 loc) · 2.05 KB
/
variables.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
#include <cstring>
#include "variables.h"
using namespace std;
extern char **environ; //set in the c library - magic; $ man environ
VariableStore varstore; //for use with `extern`
VariableStore::VariableStore(void):
electricVars({
{string("DATE"),[](void){
time_t tim=time(NULL);
struct tm *timeinfo=localtime(&tim);
char buf[64];
strftime(buf,63,"%D",timeinfo);
return string(buf);
}},
{string("TIME"),[](void){
time_t tim=time(NULL);
struct tm *timeinfo=localtime(&tim);
char buf[64];
strftime(buf,63,"%T",timeinfo);
return string(buf);
}},
{string("PWD"),[](void){
char *buf=getcwd(NULL,0);
string s=buf;
free(buf);
return s;
}},
{string("KAASH"),[](void){
return "kaas.";
}}
}){
const char *envvar=*environ;
const char *found;
int i;
for(i=0;envvar;envvar=environ[++i]){
found=strchr(envvar,'=');
if(!found)continue; //no '=' in the line, wtf?
store(string(envvar,(int)(found-envvar)),string(found+1));
}
store("~", [this] (void) { // Dynamic 'alias' to `~`.
return get("HOME");
});
}
string VariableStore::get(string name){
auto elec_it = electricVars.find(name);
if (elec_it != electricVars.cend()) return elec_it->second();
auto vars_it = variables.find(name);
if (vars_it != variables.cend()) {
return vars_it->second;
}
return "";
}
bool VariableStore::exists(string name){
unordered_map<string,string>::const_iterator it=variables.find(name);
return it!=variables.cend();
}
void VariableStore::store(const string &name, const string &val){
if(currentScope&¤tScope->find(name)==currentScope->end()){
currentScope->insert(name);
}
variables[name]=val;
}
void VariableStore::store(const string &name, function<string(void)> func) {
electricVars.emplace(name, func);
}
void VariableStore::enterScope(void){
scopes.emplace_back();
currentScope=&scopes[scopes.size()-1];
}
void VariableStore::leaveScope(void){
for(const string &name : *currentScope){
variables.erase(variables.find(name));
}
scopes.pop_back();
if(scopes.size()!=0)currentScope=&scopes[scopes.size()-1];
}