69 lines
2.0 KiB
C++
69 lines
2.0 KiB
C++
#include "Global.h"
|
|
#include "Exception.h"
|
|
#include <iostream>
|
|
|
|
namespace jet {
|
|
|
|
Global::Global() {
|
|
|
|
}
|
|
|
|
Global::~Global() {
|
|
|
|
}
|
|
|
|
void Global::dump() {
|
|
for (auto i = variables.begin(); i != variables.end(); i++)
|
|
std::cout << i->first << "=[" << i->second << "]" << std::endl;
|
|
}
|
|
|
|
bool Global::sessionExists(coreutils::MString sessionId) {
|
|
return sessions.find(sessionId) != sessions.end();
|
|
}
|
|
|
|
void Global::addSession(coreutils::MString sessionId, __mysql *mysql) {
|
|
if(sessionExists(sessionId))
|
|
coreutils::Exception("sessionid already exists.");
|
|
sessions[sessionId] = mysql;
|
|
}
|
|
|
|
void Global::removeSession(coreutils::MString sessionId) {
|
|
sessions.erase(sessionId);
|
|
}
|
|
|
|
coreutils::ZString Global::getVariable(coreutils::ZString &variable) {
|
|
if(variable.ifNext("$[")) {
|
|
coreutils::MString name;
|
|
if(variable.ifNext("!")) {
|
|
return variables[renderVariableName(name, variable)];
|
|
} if(variable.ifNext(":")) {
|
|
// TODO: should only allow CGI variable name. Allow variable substitution.
|
|
} if(variable.ifNext("@")) {
|
|
// TODO: should only allow environment variables. Allow substitution.
|
|
} else {
|
|
return variables[renderVariableName(name, variable)];
|
|
}
|
|
throw coreutils::Exception("expected variable name or type designator.");
|
|
} if(variable.ifNext("#[")) {
|
|
// TODO: this local variable type has no special naming conventions. We
|
|
// just need to do nested variable substitution for the entry.
|
|
|
|
}
|
|
throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
|
|
}
|
|
|
|
coreutils::MString Global::renderVariableName(coreutils::MString &name, coreutils::ZString &variable) {
|
|
name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
|
|
if(variable.ifNext("]"))
|
|
return name;
|
|
if(variable.startsWith("$[")) {
|
|
name << getVariable(variable);
|
|
if(variable.ifNext("]"))
|
|
return name;
|
|
}
|
|
renderVariableName(name, variable);
|
|
return name;
|
|
}
|
|
|
|
}
|