From a619ccc300a00947207600e11fac848b7d37b26b Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 10 Jul 2009 09:59:41 +0000 Subject: Fieldname queries added macrotool. --- server/src/macrotool_fieldnames.cc | 119 +++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) (limited to 'server/src/macrotool_fieldnames.cc') diff --git a/server/src/macrotool_fieldnames.cc b/server/src/macrotool_fieldnames.cc index f2475f2..eeed648 100644 --- a/server/src/macrotool_fieldnames.cc +++ b/server/src/macrotool_fieldnames.cc @@ -27,9 +27,12 @@ */ #include "macrotool_fieldnames.h" +#include "macrotool_util.h" +#include "macroparser.h" + #include "debug.h" -#include "pracrodaopgsql.h" +#include "database.h" #include "configuration.h" static const char usage_str[] = @@ -41,15 +44,113 @@ static void add(std::string name, std::string desc) { #ifndef WITHOUT_DB PRACRO_DEBUG("SET (name: %s - desc: %s)\n", name.c_str(), desc.c_str()); - PracroDAOPgsql sql(Conf::database_addr, "", - Conf::database_user, - Conf::database_passwd, ""); #else /*WITHOUT_DB*/ printf("Project not compiled with db spport.") #endif/*WITHOUT_DB*/ } +static std::vector getWidgetNames(Widget &w) +{ + std::vector v; + if(w.attributes.find("name") != w.attributes.end()) v.push_back(w.attributes["name"]); + + std::vector< Widget >::iterator i = w.widgets.begin(); + while(i != w.widgets.end()) { + std::vector _v = getWidgetNames(*i); + v.insert(v.end(), _v.begin(), _v.end()); + i++; + } + + return v; +} + +static std::map > getMacroRefsList() +{ + std::map > reflist; + + std::vector macrofiles = getMacros(); + std::vector::iterator mfs = macrofiles.begin(); + while(mfs != macrofiles.end()) { + std::string name = mfs->substr(0, mfs->length() - 4); + + MacroParser parser(name); + parser.parse(); + Macro *macro = parser.getMacro(); + + std::string key = name; + reflist[key] = getWidgetNames(macro->window); + + mfs++; + } + + return reflist; +} + +static std::vector getMacroRefs(std::map > reflist, std::string name) +{ + std::vector macros; + std::map >::iterator macro = reflist.begin(); + while(macro != reflist.end()) { + std::vector::iterator field = macro->second.begin(); + while(field != macro->second.end()) { + if(*field == name) macros.push_back(macro->first); + field++; + } + macro++; + } + return macros; +} + +static void list() +{ + Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); + + std::vector fieldnames = db.getFieldnames(); + + std::map > reflist = getMacroRefsList(); + + size_t name_sz = 0; + size_t desc_sz = 0; + size_t time_sz = 0; + + std::vector::iterator i = fieldnames.begin(); + while(i != fieldnames.end()) { + if(i->name.length() > name_sz) name_sz = i->name.length(); + if(i->description.length() > desc_sz) desc_sz = i->description.length(); + char ts[32]; + sprintf(ts, "%u", (unsigned int)i->timestamp); + if(strlen(ts) > time_sz) time_sz = strlen(ts); + i++; + } + + printcolumn("Name:", name_sz); + printcolumn("Description:", desc_sz); + printcolumn("Timestamp:", time_sz); + printf("Macros:"); + printf("\n"); + + i = fieldnames.begin(); + while(i != fieldnames.end()) { + printcolumn(i->name, name_sz); + printcolumn(i->description, desc_sz); + char ts[32]; + sprintf(ts, "%u", (unsigned int)i->timestamp); + printcolumn(ts, time_sz); + + std::vector macros = getMacroRefs(reflist, i->name); + std::vector::iterator j = macros.begin(); + while(j != macros.end()) { + if(j != macros.begin()) printf(", "); + printf("%s", j->c_str()); + j++; + } + + printf("\n"); + i++; + } +} + void macrotool_fieldnames(std::vector params) { if(params.size() < 1) { @@ -59,6 +160,16 @@ void macrotool_fieldnames(std::vector params) PRACRO_DEBUG(fieldnames, "fieldnames: %s\n", params[0].c_str()); + if(params[0] == "list") { + if(params.size() != 1) { + printf("The command 'list' does not need any parameters.\n"); + printf(usage_str); + return; + } + list(); + return; + } + if(params[0] == "set") { if(params.size() != 3) { printf("The command 'set' needs 2 parameters.\n"); -- cgit v1.2.3