From 79ac4d97bc05b5d85f41957f53cfc1fe01ae8a7c Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 6 Jul 2010 08:27:54 +0000 Subject: File renames --- server/src/macrotool/Makefile.am | 16 +- server/src/macrotool/dump.cc | 341 ++++++++++++++++++++++++++ server/src/macrotool/dump.h | 36 +++ server/src/macrotool/fieldnames.cc | 208 ++++++++++++++++ server/src/macrotool/fieldnames.h | 36 +++ server/src/macrotool/filehandler.cc | 227 +++++++++++++++++ server/src/macrotool/filehandler.h | 36 +++ server/src/macrotool/macrotool.cc | 6 +- 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 --- server/src/macrotool/util.cc | 77 ++++++ server/src/macrotool/util.h | 39 +++ 18 files changed, 1011 insertions(+), 1011 deletions(-) create mode 100644 server/src/macrotool/dump.cc create mode 100644 server/src/macrotool/dump.h create mode 100644 server/src/macrotool/fieldnames.cc create mode 100644 server/src/macrotool/fieldnames.h create mode 100644 server/src/macrotool/filehandler.cc create mode 100644 server/src/macrotool/filehandler.h delete mode 100644 server/src/macrotool/macrotool_dump.cc delete mode 100644 server/src/macrotool/macrotool_dump.h delete mode 100644 server/src/macrotool/macrotool_fieldnames.cc delete mode 100644 server/src/macrotool/macrotool_fieldnames.h delete mode 100644 server/src/macrotool/macrotool_filehandler.cc delete mode 100644 server/src/macrotool/macrotool_filehandler.h delete mode 100644 server/src/macrotool/macrotool_util.cc delete mode 100644 server/src/macrotool/macrotool_util.h create mode 100644 server/src/macrotool/util.cc create mode 100644 server/src/macrotool/util.h (limited to 'server/src') diff --git a/server/src/macrotool/Makefile.am b/server/src/macrotool/Makefile.am index e6fdc94..32e8aee 100644 --- a/server/src/macrotool/Makefile.am +++ b/server/src/macrotool/Makefile.am @@ -9,10 +9,10 @@ macrotool_CXXFLAGS = -I.. $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) \ macrotool_SOURCES = \ macrotool.cc \ - macrotool_dump.cc \ - macrotool_fieldnames.cc \ - macrotool_filehandler.cc \ - macrotool_util.cc \ + dump.cc \ + fieldnames.cc \ + filehandler.cc \ + util.cc \ ../debug.cc \ ../configuration.cc \ ../configurationparser.cc \ @@ -33,10 +33,10 @@ macrotool_SOURCES = \ ../versionstr.cc EXTRA_DIST = \ - macrotool_dump.h \ - macrotool_fieldnames.h \ - macrotool_filehandler.h \ - macrotool_util.h + dump.h \ + fieldnames.h \ + filehandler.h \ + util.h ################ # Test Section # diff --git a/server/src/macrotool/dump.cc b/server/src/macrotool/dump.cc new file mode 100644 index 0000000..6a5b018 --- /dev/null +++ b/server/src/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 "dump.h" + +#include +#include +#include + +#include + +#include + +#include "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; + } + + 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/dump.h b/server/src/macrotool/dump.h new file mode 100644 index 0000000..1f2101d --- /dev/null +++ b/server/src/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/fieldnames.cc b/server/src/macrotool/fieldnames.cc new file mode 100644 index 0000000..f348ef4 --- /dev/null +++ b/server/src/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 "fieldnames.h" + +#include +#include + +#include "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; + } + + 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/fieldnames.h b/server/src/macrotool/fieldnames.h new file mode 100644 index 0000000..0ac2759 --- /dev/null +++ b/server/src/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/filehandler.cc b/server/src/macrotool/filehandler.cc new file mode 100644 index 0000000..3c80a1c --- /dev/null +++ b/server/src/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 "filehandler.h" + +#include +#include + +#include + +#include "macroheaderparser.h" +#include "macroparser.h" +#include "template.h" + +#include "debug.h" + +#include "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; + } + + 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/filehandler.h b/server/src/macrotool/filehandler.h new file mode 100644 index 0000000..70777fc --- /dev/null +++ b/server/src/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.cc b/server/src/macrotool/macrotool.cc index c41bc9a..6d484ad 100644 --- a/server/src/macrotool/macrotool.cc +++ b/server/src/macrotool/macrotool.cc @@ -42,9 +42,9 @@ #include "debug.h" -#include "macrotool_dump.h" -#include "macrotool_fieldnames.h" -#include "macrotool_filehandler.h" +#include "dump.h" +#include "fieldnames.h" +#include "filehandler.h" static const char version_str[] = "Pracro server v" VERSION "\n" diff --git a/server/src/macrotool/macrotool_dump.cc b/server/src/macrotool/macrotool_dump.cc deleted file mode 100644 index 30d0fd5..0000000 --- a/server/src/macrotool/macrotool_dump.cc +++ /dev/null @@ -1,341 +0,0 @@ -/* -*- 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; - } - - 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 deleted file mode 100644 index 1f2101d..0000000 --- a/server/src/macrotool/macrotool_dump.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- 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 deleted file mode 100644 index 8d90323..0000000 --- a/server/src/macrotool/macrotool_fieldnames.cc +++ /dev/null @@ -1,208 +0,0 @@ -/* -*- 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; - } - - 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 deleted file mode 100644 index 0ac2759..0000000 --- a/server/src/macrotool/macrotool_fieldnames.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- 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 deleted file mode 100644 index f9a9b45..0000000 --- a/server/src/macrotool/macrotool_filehandler.cc +++ /dev/null @@ -1,227 +0,0 @@ -/* -*- 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; - } - - 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 deleted file mode 100644 index 70777fc..0000000 --- a/server/src/macrotool/macrotool_filehandler.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- 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 deleted file mode 100644 index 075a9e0..0000000 --- a/server/src/macrotool/macrotool_util.cc +++ /dev/null @@ -1,77 +0,0 @@ -/* -*- 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) { - 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 deleted file mode 100644 index 2e41f6b..0000000 --- a/server/src/macrotool/macrotool_util.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -*- 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__*/ diff --git a/server/src/macrotool/util.cc b/server/src/macrotool/util.cc new file mode 100644 index 0000000..8be5435 --- /dev/null +++ b/server/src/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 "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) { + 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/util.h b/server/src/macrotool/util.h new file mode 100644 index 0000000..2e41f6b --- /dev/null +++ b/server/src/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