summaryrefslogtreecommitdiff
path: root/server/src/luaquerymapper.cc
diff options
context:
space:
mode:
authordeva <deva>2008-05-06 15:26:39 +0000
committerdeva <deva>2008-05-06 15:26:39 +0000
commit27af2d0a32351de63977280e6deaa6d3aea8edd5 (patch)
tree43a9cf5c2cc6014edcf82b0527a8a265fc3d18db /server/src/luaquerymapper.cc
parentcb3f60dbe46166b22ef4060c7d3d6c9073e95e27 (diff)
Added LUA parser for the mapping of external data query results.
Diffstat (limited to 'server/src/luaquerymapper.cc')
-rw-r--r--server/src/luaquerymapper.cc134
1 files changed, 134 insertions, 0 deletions
diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc
new file mode 100644
index 0000000..f4f6f20
--- /dev/null
+++ b/server/src/luaquerymapper.cc
@@ -0,0 +1,134 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * luaquerymapper.cc
+ *
+ * Mon May 5 15:43:52 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
+ * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "luaquerymapper.h"
+
+static std::string loadresultstring(QueryResult &res, std::string group = "")
+{
+ std::string s;
+
+ std::map< std::string, std::string >::iterator v = res.values.begin();
+ while(v != res.values.end()) {
+ s += group + (*v).first + " = \"" + (*v).second + "\"\n";
+ v++;
+ }
+
+ std::map< std::string, QueryResult >::iterator g = res.groups.begin();
+ while(g != res.groups.end()) {
+ s += group + (*g).first + " = {}\n";
+ s += loadresultstring((*g).second, group + (*g).first + ".");
+ g++;
+ }
+
+ return s;
+}
+
+LUAQueryMapper::LUAQueryMapper(QueryResult &res)
+{
+ L = luaL_newstate();
+ if(L == NULL) {
+ // throw LUADataParserException("Could not create LUA state.");
+ }
+
+ luaL_openlibs(L);
+
+ // FIXME: Insert these variables directly into the LUA stack instead of parsing them.
+ std::string preload = loadresultstring(res);
+
+ int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload");
+ switch(s) {
+ case 0: //no errors;
+ break;
+ case LUA_ERRSYNTAX: //syntax error during pre-compilation;
+ case LUA_ERRMEM: //memory allocation error.
+ case LUA_ERRFILE: //cannot open/read the file.
+ //throw LUADataParserException(lua_tostring(L, lua_gettop(L)));
+ break;
+ default:
+ //throw LUADataParserException("Unknown return value of luaL_loadfile.");
+ break;
+ }
+
+ // Run program (init)
+ lua_pcall(L, 0, LUA_MULTRET, 0);
+}
+
+LUAQueryMapper::~LUAQueryMapper()
+{
+ lua_close(L);
+}
+
+std::string LUAQueryMapper::map(const std::string &mapper)
+{
+ int s = luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper");
+ switch(s) {
+ case 0: //no errors;
+ break;
+ case LUA_ERRSYNTAX: //syntax error during pre-compilation;
+ case LUA_ERRMEM: //memory allocation error.
+ case LUA_ERRFILE: //cannot open/read the file.
+ //throw LUADataParserException(lua_tostring(L, lua_gettop(L)));
+ break;
+ default:
+ //throw LUADataParserException("Unknown return value of luaL_loadfile.");
+ break;
+ }
+
+ // Run the loaded code
+ lua_pcall(L, 0, LUA_MULTRET, 0);
+
+ return lua_tostring(L, lua_gettop(L));
+}
+
+#ifdef TEST_LUAQUERYMAPPER
+
+int main()
+{
+ QueryResult res;
+
+ QueryResult group1;
+ group1.values["dims"] = "42";
+
+ QueryResult group2;
+ group2.values["foo"] = "bar";
+ group2.values["bar"] = "foo";
+ group2.groups["fnuller"] = group1;
+
+ res.values["dims"] = "42";
+ res.groups["dimmer"] = group2;
+
+ LUAQueryMapper mapper(res);
+
+ std::string luamap = "return dimmer.fnuller.dims * 2 + dims";
+ printf("%s\n", mapper.map(luamap).c_str());
+
+ luamap = "return math.sin(dimmer.fnuller.dims * 2 + dims)";
+ printf("%s\n", mapper.map(luamap).c_str());
+
+ return 0;
+}
+
+#endif/*TEST_LUAQUERYMAPPER*/