From da6bfc301203b0181dcf70a9798a7d840d514126 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 5 Jul 2010 06:57:54 +0000 Subject: Partial commit: Move macrotool to subfolder. --- server/src/macrotool/Makefile.am | 50 ++++ server/src/macrotool/macrotool.cc | 166 +++++++++++++ server/src/macrotool/macrotool_dump.cc | 341 ++++++++++++++++++++++++++ server/src/macrotool/macrotool_dump.h | 36 +++ server/src/macrotool/macrotool_fieldnames.cc | 208 ++++++++++++++++ server/src/macrotool/macrotool_fieldnames.h | 36 +++ server/src/macrotool/macrotool_filehandler.cc | 227 +++++++++++++++++ server/src/macrotool/macrotool_filehandler.h | 36 +++ server/src/macrotool/macrotool_util.cc | 77 ++++++ server/src/macrotool/macrotool_util.h | 39 +++ 10 files changed, 1216 insertions(+) create mode 100644 server/src/macrotool/Makefile.am create mode 100644 server/src/macrotool/macrotool.cc create mode 100644 server/src/macrotool/macrotool_dump.cc create mode 100644 server/src/macrotool/macrotool_dump.h create mode 100644 server/src/macrotool/macrotool_fieldnames.cc create mode 100644 server/src/macrotool/macrotool_fieldnames.h create mode 100644 server/src/macrotool/macrotool_filehandler.cc create mode 100644 server/src/macrotool/macrotool_filehandler.h create mode 100644 server/src/macrotool/macrotool_util.cc create mode 100644 server/src/macrotool/macrotool_util.h (limited to 'server/src/macrotool') diff --git a/server/src/macrotool/Makefile.am b/server/src/macrotool/Makefile.am new file mode 100644 index 0000000..e6fdc94 --- /dev/null +++ b/server/src/macrotool/Makefile.am @@ -0,0 +1,50 @@ + +bin_PROGRAMS = macrotool + +macrotool_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) \ + $(LUA_LIBS) $(PTHREAD_LIBS) $(EXPAT_LIBS) + +macrotool_CXXFLAGS = -I.. $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) \ + $(LUA_CXXFLAGS) $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) + +macrotool_SOURCES = \ + macrotool.cc \ + macrotool_dump.cc \ + macrotool_fieldnames.cc \ + macrotool_filehandler.cc \ + macrotool_util.cc \ + ../debug.cc \ + ../configuration.cc \ + ../configurationparser.cc \ + ../database.cc \ + ../entitylist.cc \ + ../exception.cc \ + ../inotify.cc \ + ../log.cc \ + ../macroheaderparser.cc \ + ../macrolist.cc \ + ../macroparser.cc \ + ../mutex.cc \ + ../pracrodao.cc \ + ../pracrodaopgsql.cc \ + ../pracrodaotest.cc \ + ../saxparser.cc \ + ../templateparser.cc \ + ../versionstr.cc + +EXTRA_DIST = \ + macrotool_dump.h \ + macrotool_fieldnames.h \ + macrotool_filehandler.h \ + macrotool_util.h + +################ +# Test Section # +################ + +#TEST_SOURCE_DEPS = ${macrotool_SOURCES} ${EXTRA_DIST} +#TEST_SCRIPT_DIR = $(top_srcdir)/../../tools + +#include ${TEST_SCRIPT_DIR}/Makefile.am.test + +#include Makefile.am.test \ No newline at end of file diff --git a/server/src/macrotool/macrotool.cc b/server/src/macrotool/macrotool.cc new file mode 100644 index 0000000..40ca845 --- /dev/null +++ b/server/src/macrotool/macrotool.cc @@ -0,0 +1,166 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool.cc + * + * Mon Jul 6 08:25:28 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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. + */ +// For ETC +#include + +#include +#include + +// For getopt_long and friends +#include + +#include +#include + +#include "configurationparser.h" +#include "configuration.h" + +#include "debug.h" + +#include "macrotool_dump.h" +#include "macrotool_fieldnames.h" +#include "macrotool_filehandler.h" + +static const char version_str[] = +"Pracro server v" VERSION "\n" +; + +static const char copyright_str[] = +"Copyright (C) 2006-2009 Bent Bisballe Nyeng - Aasimon.org.\n" +"This is free software. You may redistribute copies of it under the terms of\n" +"the GNU General Public License .\n" +"There is NO WARRANTY, to the extent permitted by law.\n" +"\n" +"Written by Bent Bisballe Nyeng (deva@aasimon.org)\n" +; + +static const char usage_str[] = +"Usage: %s [options] command\n" +"Options:\n" +" -c, --config file Read configfile from 'file'\n" +" -x, --xml-basedir d Use 'd' as basedir for finding template- and macro-files (default "XML").\n" +" -v, --version Print version information and exit.\n" +" -h, --help Print this message and exit.\n" +" -D, --debug ddd Enable debug messages on 'ddd'; see documentation for details\n" +"\n" +"Commands:\n" +" dump entity Dumps 'entity' to screen ('dump help' to see list of entities).\n" +" fieldnames entity Add/delete/update entries in the fieldnames database\n" +" ('fieldnames help' to see list of entities).\n" +" filehandler entity Handle macro files ('macrohandler help' to see list of entities).\n" +; + +ConfigurationParser *configparser = NULL; + +int main(int argc, char *argv[]) +{ + int c; + char *configfile = NULL; + char *xml_basedir = NULL; + char *debugstr = NULL; + + pracro_debug_init(); + + int option_index = 0; + while(1) { + static struct option long_options[] = { + {"config", required_argument, 0, 'c'}, + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {"xml-basedir", required_argument, 0, 'x'}, + {"debug", required_argument, 0, 'D'}, + {0, 0, 0, 0} + }; + + c = getopt_long (argc, argv, "D:hvc:x:", long_options, &option_index); + + if (c == -1) + break; + + switch(c) { + case 'c': + configfile = strdup(optarg); + break; + + case 'x': + xml_basedir = strdup(optarg); + break; + + case 'D': + debugstr = strdup(optarg); + break; + + case '?': + case 'h': + printf("%s", version_str); + printf(usage_str, argv[0]); + return 0; + + case 'v': + printf("%s", version_str); + printf("%s", copyright_str); + return 0; + + default: + break; + } + } + + if(optind >= argc) { + fprintf(stderr, "Missing command\n"); + printf(usage_str, argv[0]); + exit(EXIT_FAILURE); + } + + if(debugstr) { + pracro_debug_parse(debugstr); + } + + // Load config + if(configfile) configparser = new ConfigurationParser(configfile); + else configparser = new ConfigurationParser(ETC"/pracrod.conf"); + + if(xml_basedir) { + Conf::xml_basedir = xml_basedir; + } + + std::string command = argv[optind++]; + std::vector params; + while(optind < argc) { + params.push_back(argv[optind++]); + } + + if(command == "dump") macrotool_dump(params); + if(command == "fieldnames") macrotool_fieldnames(params); + if(command == "filehandler") macrotool_filehandler(params); + + // Clean up + if(configfile) free(configfile); + + return 0; +} diff --git a/server/src/macrotool/macrotool_dump.cc b/server/src/macrotool/macrotool_dump.cc new file mode 100644 index 0000000..7e7d06f --- /dev/null +++ b/server/src/macrotool/macrotool_dump.cc @@ -0,0 +1,341 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_dump.cc + * + * Mon Jul 6 08:37:22 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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 "macrotool_dump.h" + +#include +#include +#include + +#include + +#include + +#include "macrotool_util.h" + +#include "debug.h" + +#include "macroparser.h" +#include "templateparser.h" + +#include "configuration.h" + +#include "exception.h" + +#include "database.h" + +struct _macro { + std::string name; + std::string title; + std::set templates; + std::vector fields; + std::string file; + std::string version; +}; + +struct _template { + std::string name; + std::string title; + std::vector macros; + std::string file; + std::string version; +}; + +static const char usage_str[] = +" help Prints this helptext.\n" +" macros Writes macro names, versions, filenames, titles and template references to stdout.\n" +" templates Writes template names, versions, filenames, titles and macro references to stdout.\n" +" fields Outputs all fields sorted by macro, with indication of those in the fieldnames table.\n" +; + +static std::vector getFields(Widget &widget) +{ + std::vector fields; + + std::vector< Widget >::iterator w = widget.widgets.begin(); + while(w != widget.widgets.end()) { + std::vector fs = getFields(*w); + fields.insert(fields.end(), fs.begin(), fs.end()); + w++; + } + + if(widget.attributes.find("name") != widget.attributes.end()) + fields.push_back(widget.attributes["name"]); + + return fields; +} + +static std::map macroList() +{ + std::map macros; + + 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(Conf::xml_basedir + "/macros/" + *mfs); + try { + parser.parse(); + Macro *macro = parser.getMacro(); + + std::string key = macro->attributes["name"];// + "-" + macro->attributes["version"]; + macros[key].name = macro->attributes["name"]; + macros[key].file = *mfs; + macros[key].title = macro->widgets.attributes["caption"]; + macros[key].fields = getFields(macro->widgets); + macros[key].version = macro->attributes["version"]; + } catch( Exception &e ) { + printf("Skipping: %s: %s\n", mfs->c_str(), e.what()); + } + + mfs++; + } + + std::vector templatefiles = getTemplates(); + std::vector::iterator tfs = templatefiles.begin(); + while(tfs != templatefiles.end()) { + std::string templ = tfs->substr(0, tfs->length() - 4); + TemplateParser parser(Conf::xml_basedir + "/templates/" + *tfs); + try { + parser.parse(); + Template *t = parser.getTemplate(); + std::vector::iterator ms = t->macros.begin(); + while(ms != t->macros.end()) { + if(ms->attributes.find("header") == ms->attributes.end()) + macros[ms->attributes["name"]].templates.insert(templ); + ms++; + } + } catch( Exception &e ) { + printf("Skipping: %s: %s\n", tfs->c_str(), e.what()); + } + + tfs++; + } + + return macros; +} + +static void dump_fields() +{ + Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); + std::vector fieldnames = db.getFieldnames(); + + std::map macros = macroList(); + std::map::iterator ms = macros.begin(); + while(ms != macros.end()) { + printf("Macro: %s\n", ms->second.name.c_str()); + + std::vector::iterator ts = ms->second.fields.begin(); + while(ts != ms->second.fields.end()) { + bool reg = false; + + std::vector::iterator fs = fieldnames.begin(); + while(fs != fieldnames.end()) { + if(*ts == fs->name) { + reg = true; + break; + } + fs++; + } + + printf("\t%s %s\n", reg?"(*)":" ", ts->c_str()); + ts++; + } + + printf("\n"); + ms++; + } + + printf("----\n(*) Indicates that the field is registered in the fieldnames table.\n"); +} + +static void dump_macros() +{ + std::map macros = macroList(); + macros[""].title = "Title:"; + macros[""].file = "File:"; + macros[""].version = "Version:"; + macros[""].name = "Macro:"; + macros[""].templates.insert("Templates:"); + + size_t name_sz = 0; + size_t version_sz = 0; + size_t file_sz = 0; + size_t title_sz = 0; + + std::map::iterator ms = macros.begin(); + while(ms != macros.end()) { + if(ms->second.name.length() > name_sz) name_sz = ms->second.name.length(); + if(ms->second.version.length() > version_sz) version_sz = ms->second.version.length(); + if(ms->second.file.length() > file_sz) file_sz = ms->second.file.length(); + if(ms->second.title.length() > title_sz) title_sz = ms->second.title.length(); + ms++; + } + + ms = macros.begin(); + while(ms != macros.end()) { + printcolumn(ms->second.name, name_sz); + printcolumn(ms->second.version, version_sz); + printcolumn(ms->second.file, file_sz); + printcolumn(ms->second.title, title_sz); + + std::set::iterator ts = ms->second.templates.begin(); + while(ts != ms->second.templates.end()) { + if(ts != ms->second.templates.begin()) printf(", "); + printf("%s", ts->c_str() ); + ts++; + } + + printf("\n"); + ms++; + } +} + +static std::map templateList() +{ + std::map templates; + + std::vector templatefiles = getTemplates(); + std::vector::iterator tfs = templatefiles.begin(); + while(tfs != templatefiles.end()) { + TemplateParser parser(Conf::xml_basedir + "/templates/" + *tfs); + try { + parser.parse(); + Template *t = parser.getTemplate(); + + std::string key = t->attributes["name"]; + + templates[key].file = *tfs; + templates[key].name = t->attributes["name"]; + templates[key].title = t->attributes["title"]; + templates[key].version = t->attributes["version"]; + + std::vector::iterator ms = t->macros.begin(); + while(ms != t->macros.end()) { + if(ms->attributes.find("header") == ms->attributes.end()) + templates[key].macros.push_back(ms->attributes["name"]); + ms++; + } + } catch( Exception &e ) { + printf("Skipping: %s: %s\n", tfs->c_str(), e.what()); + } + tfs++; + } + + return templates; +} + +static void dump_templates() +{ + std::map templates = templateList(); + templates[""].title = "Title:"; + templates[""].file = "File:"; + templates[""].version = "Version:"; + templates[""].name = "Template:"; + + size_t name_sz = 0; + size_t version_sz = 0; + size_t file_sz = 0; + size_t title_sz = 0; + + std::map::iterator ts = templates.begin(); + while(ts != templates.end()) { + if(ts->second.name.length() > name_sz) name_sz = ts->second.name.length(); + if(ts->second.version.length() > version_sz) version_sz = ts->second.version.length(); + if(ts->second.file.length() > file_sz) file_sz = ts->second.file.length(); + if(ts->second.title.length() > title_sz) title_sz = ts->second.title.length(); + ts++; + } + + ts = templates.begin(); + while(ts != templates.end()) { + printcolumn(ts->second.name, name_sz); + printcolumn(ts->second.version, version_sz); + printcolumn(ts->second.file, file_sz); + printcolumn(ts->second.title, title_sz); + + printf("\n"); + std::vector::iterator ms = ts->second.macros.begin(); + while(ms != ts->second.macros.end()) { + printf("\t%s\n", ms->c_str() ); + ms++; + } + + printf("\n"); + ts++; + } +} + +void macrotool_dump(std::vector params) +{ + if(params.size() < 1) { + printf("%s", usage_str); + return; + } + + PRACRO_DEBUG(fieldnames, "dump: %s\n", params[0].c_str()); + + if(params[0] == "fields") { + if(params.size() != 1) { + printf("The command 'fields' doen't take any parameters.\n"); + printf("%s", usage_str); + return; + } + dump_fields(); + return; + } + + if(params[0] == "macros") { + if(params.size() != 1) { + printf("The command 'macro' doen't take any parameters.\n"); + printf("%s", usage_str); + return; + } + dump_macros(); + return; + } + + if(params[0] == "templates") { + if(params.size() != 1) { + printf("The command 'templates' doen't take any parameters.\n"); + printf("%s", usage_str); + return; + } + dump_templates(); + return; + } + + if(params[0] == "help") { + printf("%s", usage_str); + return; + } + + printf("Unknown command '%s'\n", params[0].c_str()); + printf("%s", usage_str); + return; +} diff --git a/server/src/macrotool/macrotool_dump.h b/server/src/macrotool/macrotool_dump.h new file mode 100644 index 0000000..1f2101d --- /dev/null +++ b/server/src/macrotool/macrotool_dump.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_dump.h + * + * Mon Jul 6 08:37:22 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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. + */ +#ifndef __PRACRO_MACROTOOL_DUMP_H__ +#define __PRACRO_MACROTOOL_DUMP_H__ + +#include +#include + +void macrotool_dump(std::vector params); + +#endif/*__PRACRO_MACROTOOL_DUMP_H__*/ diff --git a/server/src/macrotool/macrotool_fieldnames.cc b/server/src/macrotool/macrotool_fieldnames.cc new file mode 100644 index 0000000..2a9681a --- /dev/null +++ b/server/src/macrotool/macrotool_fieldnames.cc @@ -0,0 +1,208 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_fieldnames.cc + * + * Mon Jul 6 14:15:05 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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 "macrotool_fieldnames.h" + +#include +#include + +#include "macrotool_util.h" +#include "macroparser.h" + +#include "debug.h" + +#include "database.h" +#include "configuration.h" + +static const char usage_str[] = +" help Prints this helptext.\n" +" add name desc Add a field called 'name', described as 'desc'\n" +" del name Delete a field called 'name'\n" +" list List all fieldnames as well as their macro references.\n" +; + +static void add(std::string name, std::string desc) +{ + Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); + db.addFieldname(name, desc); +} + +static void del(std::string name) +{ + Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); + db.delFieldname(name); +} + +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(Conf::xml_basedir + "/macros/" + *mfs); + parser.parse(); + Macro *macro = parser.getMacro(); + + std::string key = name; + reflist[key] = getWidgetNames(macro->widgets); + + 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) { + printf("%s", usage_str); + return; + } + + 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("%s", usage_str); + return; + } + list(); + return; + } + + if(params[0] == "add") { + if(params.size() != 3) { + printf("The command 'add' needs 2 parameters.\n"); + printf("%s", usage_str); + return; + } + add(params[1], params[2]); + return; + } + + if(params[0] == "del") { + if(params.size() != 2) { + printf("The command 'del' needs 1 parameter.\n"); + printf("%s", usage_str); + return; + } + del(params[1]); + return; + } + + if(params[0] == "help") { + printf("%s", usage_str); + return; + } + + printf("Unknown command '%s'\n", params[0].c_str()); + printf("%s", usage_str); + return; +} diff --git a/server/src/macrotool/macrotool_fieldnames.h b/server/src/macrotool/macrotool_fieldnames.h new file mode 100644 index 0000000..0ac2759 --- /dev/null +++ b/server/src/macrotool/macrotool_fieldnames.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_fieldnames.h + * + * Mon Jul 6 14:15:05 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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. + */ +#ifndef __PRACRO_MACROTOOL_FIELDNAMES_H__ +#define __PRACRO_MACROTOOL_FIELDNAMES_H__ + +#include +#include + +void macrotool_fieldnames(std::vector params); + +#endif/*__PRACRO_MACROTOOL_FIELDNAMES_H__*/ diff --git a/server/src/macrotool/macrotool_filehandler.cc b/server/src/macrotool/macrotool_filehandler.cc new file mode 100644 index 0000000..c52921a --- /dev/null +++ b/server/src/macrotool/macrotool_filehandler.cc @@ -0,0 +1,227 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_filehandler.cc + * + * Fri Jul 17 08:48:09 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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 "macrotool_filehandler.h" + +#include +#include + +#include + +#include "macroheaderparser.h" +#include "macroparser.h" +#include "template.h" + +#include "debug.h" + +#include "macrotool_util.h" + +#include "configuration.h" + +static const char usage_str[] = +" help Prints this helptext.\n" +" add file Add a file called 'file' to the macro or template folder, according\n" +" to its contents.\n" +" check file Check if a file is valid, and print a resume of its contents.\n" +; + +/** +class Macro { +public: + std::vector< Query > queries; + std::vector< Map > maps; + std::vector< Script > scripts; + Widget widgets; + std::map< std::string, std::string > attributes; + Resume resume; +}; + **/ +static bool check(std::string file, std::string *name = NULL, std::string *version = NULL); +static bool check(std::string file, std::string *name, std::string *version) +{ + try { + MacroHeaderParser parser(file); + parser.parse(); + Macro *macro = parser.getMacro(); + + if(!macro) { + printf("Macro malformed!\n"); + return false; + } + + printf("Parsing of %s was succesful.\n", file.c_str()); + printf("Name: %s\n", macro->attributes["name"].c_str()); + printf("Version: %s\n", macro->attributes["version"].c_str()); + + if(name) *name = macro->attributes["name"]; + if(version) *version = macro->attributes["version"]; + + } catch( std::exception &e ) { + printf("%s\n", e.what()); + return false; + } + return true; +} + +#define SZ 1 // For stress test purposes set this to a large number (1000) +static bool macro_exists(std::string name, std::string version, std::string &clashfile) +{ + std::vector macrofiles = getMacros(); + + for(int prut = 0; prut < SZ; prut++) { + std::vector::iterator mfs = macrofiles.begin(); + while(mfs != macrofiles.end()) { + std::string macroname = mfs->substr(0, mfs->length() - 4); + + MacroHeaderParser parser(Conf::xml_basedir + "/macros/" + *mfs); + //MacroParser parser(macroname); + parser.parse(); + Macro *macro = parser.getMacro(); + + if(name == macro->attributes["name"] && + version == macro->attributes["version"]) { + clashfile = *mfs; + return true; + } + + mfs++; + } + } + printf("Parsed %d files\n", macrofiles.size() * SZ); + return false; +} + +static std::string strippath(std::string filename) +{ + if(filename.find('/') == std::string::npos) return filename; + return filename.substr(filename.rfind('/')+1); +} + +static bool file_exist(std::string file) +{ + FILE *fp = fopen(file.c_str(), "r"); + if(!fp) return false; + fclose(fp); + return true; +} + +static void add(std::string file) +{ + std::string name; + std::string version; + std::string clashfile; + std::string target; + + if(!check(file, &name, &version)) { + printf("File not a valid macro file.\nAborting...\n"); + return; + } + + if(macro_exists(name, version, clashfile)) { + printf("WARNING: A macro with that name and version already exists." + " THE EXISTING FILE WILL BE OVERWRITTEN!\n"); + printf("File: %s\n", clashfile.c_str()); + char answer[32]; + answer[0] = '\0'; + while(std::string(answer) != "yes\n" && std::string(answer) != "no\n") { + if(answer[0] == '\0') printf("Are you sure you want to put the file in the macro directory? [yes/no]\n"); + else printf("Please answer 'yes' or 'no'\n"); + fgets(answer, sizeof(answer), stdin); + } + + if(std::string(answer) == "no\n") { + printf("Aborting...\n"); + return; + } + target = Conf::xml_basedir + "/macros/" + clashfile; + } else { + target = Conf::xml_basedir + "/macros/" + strippath(file); + + size_t cnt = 0; + while(file_exist(target)) { + char *num; + if(cnt) asprintf(&num, "-%d", cnt); + else num = strdup(""); + target = Conf::xml_basedir + "/macros/" + name + "-" + version + num + ".xml"; + printf("Trying: %d %s\n", cnt, target.c_str()); + free(num); + cnt++; + } + } + + printf("Copying '%s' to '%s' ...\n", file.c_str(), target.c_str()); + + { + std::ifstream ifs(file.c_str(), std::ios::binary); + if(!ifs) { + printf("Could read source file.\nAborting...\n"); + return; + } + std::ofstream ofs(target.c_str(), std::ios::binary); + ofs << ifs.rdbuf(); + } + printf("done\n"); +} + +void macrotool_filehandler(std::vector params) +{ + if(params.size() < 1) { + printf("%s", usage_str); + return; + } + + PRACRO_DEBUG(filehandler, "filehandler: %s\n", params[0].c_str()); + + if(params[0] == "add") { + if(params.size() != 2) { + printf("The command 'add' needs 1 parameter.\n"); + printf("%s", usage_str); + return; + } + add(params[1]); + return; + } + + if(params[0] == "check") { + if(params.size() != 2) { + printf("The command 'check' needs 1 parameter.\n"); + printf("%s", usage_str); + return; + } + check(params[1]); + return; + } + + if(params[0] == "help") { + printf("%s", usage_str); + return; + } + + printf("Unknown command '%s'\n", params[0].c_str()); + printf("%s", usage_str); + return; +} diff --git a/server/src/macrotool/macrotool_filehandler.h b/server/src/macrotool/macrotool_filehandler.h new file mode 100644 index 0000000..70777fc --- /dev/null +++ b/server/src/macrotool/macrotool_filehandler.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_filehandler.h + * + * Fri Jul 17 08:48:09 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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. + */ +#ifndef __PRACRO_MACROTOOL_FILEHANDLER_H__ +#define __PRACRO_MACROTOOL_FILEHANDLER_H__ + +#include +#include + +void macrotool_filehandler(std::vector params); + +#endif/*__PRACRO_MACROTOOL_FILEHANDLER_H__*/ diff --git a/server/src/macrotool/macrotool_util.cc b/server/src/macrotool/macrotool_util.cc new file mode 100644 index 0000000..3c18ab7 --- /dev/null +++ b/server/src/macrotool/macrotool_util.cc @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_util.cc + * + * Fri Jul 10 09:11:28 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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 "macrotool_util.h" + +#include +#include +#include + +#include "debug.h" +#include "configuration.h" + +static std::vector listdir(std::string path) +{ + std::vector files; + + DIR* dir = opendir(path.c_str()); + if(!dir) { + PRACRO_ERR(dump, "Could not open directory: %s\n", path.c_str()); + return files; + } + + struct dirent *d; + while((d = readdir(dir)) != 0) { + //if(d->d_type == DT_DIR) { + std::string name = d->d_name; + if(name.length() >= 4 && name.substr(name.length() - 4) == ".xml") + files.push_back(name); + //} + } + closedir(dir); + + return files; +} + +std::vector getMacros() +{ + std::vector macros = listdir(Conf::xml_basedir + "/macros"); + return macros; +} + +std::vector getTemplates() +{ + std::vector templates = listdir(Conf::xml_basedir + "/templates"); + return templates; +} + +void printcolumn(std::string text, size_t width) +{ + printf("%s", text.c_str()); + for(size_t i = text.length(); i < width; i++) printf(" "); + printf("\t"); +} diff --git a/server/src/macrotool/macrotool_util.h b/server/src/macrotool/macrotool_util.h new file mode 100644 index 0000000..2e41f6b --- /dev/null +++ b/server/src/macrotool/macrotool_util.h @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * macrotool_util.h + * + * Fri Jul 10 09:11:28 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * 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. + */ +#ifndef __PRACRO_MACROTOOL_UTIL_H__ +#define __PRACRO_MACROTOOL_UTIL_H__ + +#include +#include + +std::vector getMacros(); +std::vector getTemplates(); + +void printcolumn(std::string text, size_t width); + +#endif/*__PRACRO_MACROTOOL_UTIL_H__*/ -- cgit v1.2.3