diff options
author | deva <deva> | 2010-08-12 10:57:04 +0000 |
---|---|---|
committer | deva <deva> | 2010-08-12 10:57:04 +0000 |
commit | d9338083192084613e5530b02710b796252d342b (patch) | |
tree | e0ec2b36e0de62328e5fd5d3b597f6ee71d1b18f /server | |
parent | dbab8458dcce186e7eb7a114a83f759d7db5445a (diff) |
New scripting system part2.
Diffstat (limited to 'server')
-rw-r--r-- | server/TODO | 6 | ||||
-rw-r--r-- | server/src/luaresume.cc | 8 | ||||
-rw-r--r-- | server/src/luaresume.h | 2 | ||||
-rw-r--r-- | server/src/macroparser.cc | 65 | ||||
-rw-r--r-- | server/src/macroparser.h | 3 | ||||
-rw-r--r-- | server/src/resumeparser.cc | 122 | ||||
-rw-r--r-- | server/src/template.h | 2 | ||||
-rw-r--r-- | server/xml/macros/test_button.xml | 6 | ||||
-rw-r--r-- | server/xml/macros/test_lineedit.xml | 12 | ||||
-rw-r--r-- | server/xml/macros/test_metawidget.xml | 13 | ||||
-rw-r--r-- | server/xml/macros/test_resume.xml | 85 |
11 files changed, 175 insertions, 149 deletions
diff --git a/server/TODO b/server/TODO index 1c15b68..59b0513 100644 --- a/server/TODO +++ b/server/TODO @@ -1,3 +1,9 @@ +server: + params in configfile (all, but -c) + + + + client: sessionid skal lagres lokalt med template navn. diff --git a/server/src/luaresume.cc b/server/src/luaresume.cc index eb6d90b..a74f1f5 100644 --- a/server/src/luaresume.cc +++ b/server/src/luaresume.cc @@ -34,7 +34,7 @@ #define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten" -static int _getValue(lua_State *L) +static int _value(lua_State *L) { Pracro::checkParameters(L, Pracro::T_STRING, @@ -51,7 +51,7 @@ static int _getValue(lua_State *L) return 1; } - std::string value = lua->getValue(name); + std::string value = lua->value(name); lua_pushstring(L, value.c_str()); return 1; @@ -71,7 +71,7 @@ LUAResume::LUAResume(Commit &c) lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. - lua_register(L, "getValue", _getValue); + lua_register(L, "value", _value); } LUAResume::~LUAResume() @@ -79,7 +79,7 @@ LUAResume::~LUAResume() lua_close(L); } -std::string LUAResume::getValue(std::string name) +std::string LUAResume::value(std::string name) { if(commit.fields.find(name) == commit.fields.end()) { ERR(luaresume, "LUAResume: No such field '%s'\n", name.c_str()); diff --git a/server/src/luaresume.h b/server/src/luaresume.h index 5a1896e..1132f26 100644 --- a/server/src/luaresume.h +++ b/server/src/luaresume.h @@ -40,7 +40,7 @@ public: std::string run(std::string program); - std::string getValue(std::string name); + std::string value(std::string name); void error(std::string message); diff --git a/server/src/macroparser.cc b/server/src/macroparser.cc index fe7f6e3..2bd482e 100644 --- a/server/src/macroparser.cc +++ b/server/src/macroparser.cc @@ -84,6 +84,7 @@ MacroParser::MacroParser(std::string macrofile) m = NULL; current_map = NULL; current_script = NULL; + current_resume_script = NULL; file = macrofile; @@ -101,9 +102,9 @@ MacroParser::~MacroParser() void MacroParser::characterData(std::string &data) { - if(state == RESUME) { - assert(m); // No macro present! - m->resume.attributes["format"].append(data); + if(state == RESUME_SCRIPT) { + assert(current_resume_script); // No macro present! + current_resume_script->code.append(data); } if(state == MAP) { @@ -117,7 +118,8 @@ void MacroParser::characterData(std::string &data) } } -void MacroParser::startTag(std::string name, std::map< std::string, std::string> attributes) +void MacroParser::startTag(std::string name, + std::map< std::string, std::string> attributes) { // Create macro and enable parsing of queries, maps and widgets if(name == "macro") { @@ -203,18 +205,36 @@ void MacroParser::startTag(std::string name, std::map< std::string, std::string> return; } - // Create Query + // Create script if(name == "script") { - if(state != SCRIPTS) error("script found outside scripts."); - state = SCRIPT; - - assert(m); // No macro is currently available, cannot create map! - - Script s; - s.attributes = attributes; - m->scripts.push_back(s); - current_script = &(m->scripts.back()); + assert(m); // No macro is currently available, cannot create script! + + switch(state) { + case SCRIPTS: + { + state = SCRIPT; + + Script s; + s.attributes = attributes; + m->scripts.push_back(s); + current_script = &(m->scripts.back()); + } + break; + case RESUME: + { + state = RESUME_SCRIPT; + + Script s; + s.attributes = attributes; + m->resume_scripts.push_back(s); + current_resume_script = &(m->resume_scripts.back()); + } + break; + default: + error("<script> tag found outside <scripts> or <resume> tags."); + break; + } return; } @@ -278,8 +298,21 @@ void MacroParser::endTag(std::string name) } if(name == "scripts") state = MACRO; if(name == "script") { - current_script = NULL; - state = SCRIPTS; + switch(state) { + case SCRIPT: + current_script = NULL; + state = SCRIPTS; + break; + + case RESUME_SCRIPT: + current_resume_script = NULL; + state = RESUME; + break; + + default: + // tag mismatch? + break; + } } if(name == "widgets") state = MACRO; diff --git a/server/src/macroparser.h b/server/src/macroparser.h index 86a1487..eaa5638 100644 --- a/server/src/macroparser.h +++ b/server/src/macroparser.h @@ -35,13 +35,13 @@ class MacroParser : public SAXParser { UNDEFINED, MACRO, RESUME, + RESUME_SCRIPT, QUERIES, QUERY, MAPS, MAP, WIDGETS, SCRIPTS, - SCRIPT_INCLUDE, SCRIPT } ParserState; @@ -75,6 +75,7 @@ private: Macro *m; Map *current_map; Script *current_script; + Script *current_resume_script; std::vector< Widget* > widgetstack; // Error callback function. diff --git a/server/src/resumeparser.cc b/server/src/resumeparser.cc index 37b36f4..c80c2f0 100644 --- a/server/src/resumeparser.cc +++ b/server/src/resumeparser.cc @@ -31,110 +31,36 @@ #include "luaresume.h" #include "configuration.h" -static std::string resume_parser_format(Resume &r, Commit &commit) +std::string resume_parser(Macro ¯o, Commit &commit) { - const char* format = r.attributes["format"].c_str(); - - std::string resume; - std::string var; + LUAResume luaresume(commit); - const char *p = format; - const char *theend = p + strlen(format); - while(p < theend) { - switch(*p) { - case '$': - p++; - switch(*p) { - case '$': - resume.append(1, *p); - break; + std::string code; - case '{': - p++; - var = ""; - // Parser - while(p < theend && *p != '}') { - var.append(1, *p); - p++; + std::vector< Script >::iterator spi = macro.resume_scripts.begin(); + while(spi != macro.resume_scripts.end()) { + if(spi->attributes.find("src") != spi->attributes.end()) { + std::string src = spi->attributes["src"]; + std::string file = + Conf::xml_basedir + "/include/" + src; + FILE *fp = fopen(file.c_str(), "r"); + if(fp) { + char buf[64]; + size_t sz; + std::string inc; + while((sz = fread(buf, 1, sizeof(buf), fp)) != 0) { + inc.append(buf, sz); } - // p++; - //printf("[%s]\n", var.c_str()); - // resume += "var(" + var + ")"; - resume += commit.fields[var]; - break; - - default: - resume.append(1, *p); - // printf("Illigal $ command\n"); - break; - } - p++; - break; - - case '\\': - p++; - switch(*p) { - case 't': - resume.append(1, '\t'); - break; - case 'n': - resume.append(1, '\n'); - break; - case 'r': - resume.append(1, '\r'); - break; - case '\\': - default: - resume.append(1, *p); - break; + fclose(fp); + code += "\n-- BEGIN INCLUDE: '" + src + "'\n"; + code += inc; + code += "\n-- END INCLUDE: '" + src + "'\n"; } - p++; - break; - - default: - resume.append(1, *p); - p++; - break; + } else { + code += spi->code; } + spi++; } - return resume; -} - -static std::string resume_parser_lua(Macro ¯o, Commit &commit) -{ - LUAResume luaresume(commit); - std::string lua; - /* - std::vector< ScriptInclude >::iterator spii = - macro.script_includes.begin(); - while(spii != macro.script_includes.end()) { - std::string file = - Conf::xml_basedir + "/include/" + spii->attributes["file"]; - FILE *fp = fopen(file.c_str(), "r"); - if(fp) { - char buf[64]; - size_t sz; - std::string inc; - while((sz = fread(buf, 1, sizeof(buf), fp)) != 0) { - lua.append(buf, sz); - } - fclose(fp); - } - spii++; - } - */ - lua += macro.resume.attributes["format"]; - - return luaresume.run(lua); -} - -std::string resume_parser(Macro ¯o, Commit &commit) -{ - if(macro.resume.attributes["language"] == "lua") { - return resume_parser_lua(macro, commit); - } - - // Default to pracro format language. - return resume_parser_format(macro.resume, commit); + return luaresume.run(code); } diff --git a/server/src/template.h b/server/src/template.h index 1ca69ff..479e6ee 100644 --- a/server/src/template.h +++ b/server/src/template.h @@ -42,6 +42,7 @@ public: class Script { public: attr_t attributes; + std::string code; }; class Map { @@ -65,6 +66,7 @@ public: std::vector< Query > queries; maps_t maps; std::vector< Script > scripts; + std::vector< Script > resume_scripts; Widget widgets; attr_t attributes; Resume resume; diff --git a/server/xml/macros/test_button.xml b/server/xml/macros/test_button.xml index 9f01960..84f172e 100644 --- a/server/xml/macros/test_button.xml +++ b/server/xml/macros/test_button.xml @@ -1,6 +1,10 @@ <?xml version='1.0' encoding='UTF-8'?> <macro name="test_button" version="1.0"> - <resume></resume> + <resume> + <script> + return value('dims') + </script> + </resume> <queries> </queries> <maps> diff --git a/server/xml/macros/test_lineedit.xml b/server/xml/macros/test_lineedit.xml index 783ad57..d706b50 100644 --- a/server/xml/macros/test_lineedit.xml +++ b/server/xml/macros/test_lineedit.xml @@ -7,18 +7,14 @@ <maps> </maps> <scripts> - <script name="A" language="lua"> - if ( value == '4' ) - then - return true - end - return false - </script> + <script language="lua" src="regexp.lua"/> </scripts> <widgets caption="Test LineEdit" layout="vbox"> - <lineedit name="dims" value="tester" script="A" map="test.data" regexp="[0-9]"/> + <lineedit name="dims" value="tester" + map="test.data" + onChange="this:setValid(regexp('^%d?%d?%d$'))"/> <frame layout="hbox"> <spacer /> diff --git a/server/xml/macros/test_metawidget.xml b/server/xml/macros/test_metawidget.xml index a2d51ea..3e78ebe 100644 --- a/server/xml/macros/test_metawidget.xml +++ b/server/xml/macros/test_metawidget.xml @@ -10,14 +10,19 @@ <widgets caption="Test Metawidget" layout="vbox"> - <metawidget layout="vbox" name="dims" format="${test1}: ${test2}" storechildren="true"> + <metawidget layout="vbox" name="dims" format="${test1}: ${test2}" + storechildren="true"> <lineedit name="test1" value="test"/> - <checkbox name="test2" value="ja" truevalue="ja" falsevalue="nej" caption="Og svaret er?"/> + <checkbox name="test2" value="ja" truevalue="ja" falsevalue="nej" + caption="Og svaret er?"/> </metawidget> - <metawidget layout="vbox" name="dims2" format="${a}: ${b}" storechildren="false"> + <metawidget layout="vbox" name="dims2" format="${a}: ${b}" + storechildren="false"> <lineedit name="a" value="test"/> - <checkbox name="b" value="ja" truevalue="ja" falsevalue="nej" caption="Og svaret er?"/> + <checkbox name="b" value="ja" truevalue="ja" falsevalue="nej" + caption="Og svaret er?" + onChange="w=widget('dims') w:setValid(this:checked())"/> </metawidget> <frame layout="hbox"> diff --git a/server/xml/macros/test_resume.xml b/server/xml/macros/test_resume.xml index c3e3cbe..05afd3f 100644 --- a/server/xml/macros/test_resume.xml +++ b/server/xml/macros/test_resume.xml @@ -1,32 +1,85 @@ <?xml version='1.0' encoding='UTF-8'?> <macro name="test_resume" version="1.0"> - <resume language="lua"> - -- This is a LUA program! - if getValue('test2') == 'ja' - then - return getValue('dims') .. ' made out of ' .. getValue('test1') .. ' and ' .. getValue('test2') - else - return 'niksen' - end + + <resume> + <script src="regexp.lua"/> + <script> + -- inline code + if(regexp('.+', '')) + then + return 'a string' + else + return 'another string' + end + </script> </resume> + <queries> </queries> + <maps> </maps> + <scripts> + <script language="lua" src="test.lua"/> + <script language="lua"> + function bar(wdg) + w = widget(wdg) + w:setValid(this:checked()) + end + + function foo() + this:setValid((string.sub(this:value(), 1, 4) == 'test')) + end + </script> </scripts> + <widgets caption="Test Resume" layout="vbox"> - <metawidget layout="vbox" name="dims" - format="${test1}: ${test2}" - storechildren="true"> - <lineedit name="test1" value="test"/> - <checkbox caption="Og svaret er?" - name="test2" value="ja" - truevalue="ja" falsevalue="nej"/> - </metawidget> + <lineedit name="test1" value="test" + onChange="this:setValid(this:value()~='')"/> + <checkbox caption="Og svaret eer?" + name="test2" value="ja" + truevalue="ja" falsevalue="nej" + onChange="bar('test1')"/> + <textedit name="test3" value="tjae" + onChange="this:setValid(this:value() == 'a')"/> + <altcombobox name="dims" value="futtefejer" layout="vbox" + onChange="this:setValid((string.sub(this:value(), 1, 4) == 'test'))"> + <item caption="Test1" value="test1"/> + <item caption="Test2" value="test2"/> + <item caption="Test3" value="test3"/> + <altitem caption="Test[n]" value="test4" innerwidget="inner"> + <lineedit name="inner"/> + </altitem> + </altcombobox> + + <combobox name="combo1" type="select" + onChange="this:setValid((string.sub(this:value(), 1, 4) == 'test'))"> + <item caption="Test1" value="test1"/> + <item caption="Test2" value="test2"/> + <item caption="Test3" value="test3"/> + <item caption="Test4" value="test4"/> + </combobox> + + <combobox name="combo2" type="search" + onChange="this:setValid((string.sub(this:value(), 1, 4) == 'test'))"> + <item caption="Test1" value="test1"/> + <item caption="Test2" value="test2"/> + <item caption="Test3" value="test3"/> + <item caption="Test4" value="test4"/> + </combobox> + + <combobox name="combo3" type="edit" + onChange="this:setValid((string.sub(this:value(), 1, 4) == 'test'))"> + <item caption="Test1" value="test1"/> + <item caption="Test2" value="test2"/> + <item caption="Test3" value="test3"/> + <item caption="Test4" value="test4"/> + </combobox> + <frame layout="hbox"> <spacer /> <button caption="Reset" action="reset"/> |