Variables work. Added trimline support.

This commit is contained in:
Brad Arant 2024-09-24 12:13:06 -07:00
parent 3679644f64
commit 933c400922
5 changed files with 26 additions and 25 deletions

View File

@ -33,14 +33,15 @@ namespace jet {
coreutils::ZString Global::getVariable(coreutils::ZString &variable) { coreutils::ZString Global::getVariable(coreutils::ZString &variable) {
if(variable.ifNext("$[")) { if(variable.ifNext("$[")) {
coreutils::MString name;
if(variable.ifNext("!")) { if(variable.ifNext("!")) {
return variables[renderVariableName(variable)]; return variables[renderVariableName(name, variable)];
} if(variable.ifNext(":")) { } if(variable.ifNext(":")) {
// TODO: should only allow CGI variable name. Allow variable substitution. // TODO: should only allow CGI variable name. Allow variable substitution.
} if(variable.ifNext("@")) { } if(variable.ifNext("@")) {
// TODO: should only allow environment variables. Allow substitution. // TODO: should only allow environment variables. Allow substitution.
} else { } else {
return variables[renderVariableName(variable)]; return variables[renderVariableName(name, variable)];
} }
throw coreutils::Exception("expected variable name or type designator."); throw coreutils::Exception("expected variable name or type designator.");
} if(variable.ifNext("#[")) { } if(variable.ifNext("#[")) {
@ -51,15 +52,17 @@ namespace jet {
throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[')."); throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
} }
coreutils::MString Global::renderVariableName(coreutils::ZString &variable) { coreutils::MString Global::renderVariableName(coreutils::MString &name, coreutils::ZString &variable) {
coreutils::MString name = variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(variable.ifNext("]")) if(variable.ifNext("]"))
return name; return name;
if(variable.startsWith("$[")) { if(variable.startsWith("$[")) {
name << getVariable(variable); name << getVariable(variable);
if(variable.ifNext("]"))
return name; return name;
} }
return coreutils::MString(""); renderVariableName(name, variable);
return name;
} }
} }

View File

@ -19,12 +19,11 @@ namespace jet {
void addSession(coreutils::MString sessionId, __mysql *mysql); void addSession(coreutils::MString sessionId, __mysql *mysql);
void removeSession(coreutils::MString sessionId); void removeSession(coreutils::MString sessionId);
coreutils::ZString getVariable(coreutils::ZString &variable); coreutils::ZString getVariable(coreutils::ZString &variable);
coreutils::MString renderVariableName(coreutils::MString &name, coreutils::ZString &variable);
std::map<coreutils::MString, coreutils::MString> variables; std::map<coreutils::MString, coreutils::MString> variables;
std::map<coreutils::MString, __mysql *> sessions; std::map<coreutils::MString, __mysql *> sessions;
private:
coreutils::MString renderVariableName(coreutils::ZString &variable);
}; };

View File

@ -5,10 +5,8 @@ namespace jet {
KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() { KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() {
while(!data.eod()) { while(!data.eod()) {
if(data.ifNext("$[")) { if(data.startsWith("$[")) {
coreutils::ZString varName(data.getTokenExclude("]")); write(global.getVariable(data));
data.ifNext("]");
write(global.variables[varName]);
} else { } else {
write(data.charAt(0)); write(data.charAt(0));
data.nextChar(); data.nextChar();

View File

@ -50,6 +50,9 @@ namespace jet {
if(variableDefined("filterblanklines")) { if(variableDefined("filterblanklines")) {
filterBlankLines = variables["filterblanklines"] == "true" ? true: false; filterBlankLines = variables["filterblanklines"] == "true" ? true: false;
} }
if(variableDefined("trimlines")) {
trimLines = variables["trimlines"] == "true" ? true: false;
}
if(hasContainer) { if(hasContainer) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "has Container: " << hasContainer; coreutils::Log(coreutils::LOG_DEBUG_2) << "has Container: " << hasContainer;
bool hasSplitTag = splitTagName == "" ? false: true; bool hasSplitTag = splitTagName == "" ? false: true;
@ -239,6 +242,9 @@ namespace jet {
while(!in.eod()) { while(!in.eod()) {
if(filterBlankLines) { if(filterBlankLines) {
if(!in.lineIsWhitespace()) { if(!in.lineIsWhitespace()) {
if(trimLines)
out.write(in.goeol().trim());
else
out.write(in.goeol()); out.write(in.goeol());
out.write('\n'); out.write('\n');
} }

View File

@ -6,7 +6,7 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
coreutils::ZString data2("<jet name1=\"localname\" filterblanklines=\"true\">\n" coreutils::ZString data("<jet name1=\"localname\" filterblanklines=\"true\" trimlines=\"true\">\n"
" <comment>This is a comment and should not show up in the output.</comment>\n" " <comment>This is a comment and should not show up in the output.</comment>\n"
" <html>\n" " <html>\n"
" <set name=\"ix\" value=\"1\" />\n" " <set name=\"ix\" value=\"1\" />\n"
@ -20,7 +20,10 @@ int main(int argc, char **argv) {
" <set name=\"newname\" scope=\"global\">another container value</set>\n" " <set name=\"newname\" scope=\"global\">another container value</set>\n"
" >>>$[noeval]<<<\n" " >>>$[noeval]<<<\n"
" >>>$[thename]<<<\n" " >>>$[thename]<<<\n"
" local: >>>#[name$[ix]]<<<\n" " <set name=\"iz\" value=\"data\" />\n"
" <set name=\"ix1\" value=\"1\" />\n"
" <set name=\"test$[ix$[ix1]]$[iz]\" value=\"0123456789\" />\n"
" $[test$[ix$[ix1]]$[iz]]\n"
" <mysql key=\"uu\">\n" " <mysql key=\"uu\">\n"
" <if value1=\"X\" value2=\"Y\" type=\"ne\">\n" " <if value1=\"X\" value2=\"Y\" type=\"ne\">\n"
" 789\n" " 789\n"
@ -41,14 +44,6 @@ int main(int argc, char **argv) {
" </html>\n" " </html>\n"
"</jet>\n"); "</jet>\n");
coreutils::ZString data("<jet filterblanklines=\"true\">\n"
" <set name=\"test1\" value=\"0123456789\" />\n"
" <set name=\"ix\" value=\"1\" />\n"
" $[test$[ix]]\n"
"</jet>\n");
// coreutils::ZString data("<jet>ABC<jet>HIJ</jet>XYZ</jet>\n");
std::cout << "---------\n" << data << "----------\n" << std::endl; std::cout << "---------\n" << data << "----------\n" << std::endl;
try { try {
@ -56,7 +51,7 @@ int main(int argc, char **argv) {
coreutils::MString out; coreutils::MString out;
jet::__jet *jet = new jet::__jet(data, out, global); jet::__jet *jet = new jet::__jet(data, out, global);
delete jet; delete jet;
std::cout << ">>-------" << std::endl << out << std::endl << "<<------" << std::endl; std::cout << ">>-------" << std::endl << out << "<<------" << std::endl;
// global.dump(); // global.dump();
} }
catch(coreutils::Exception e) { catch(coreutils::Exception e) {