From 78c50e7261df6632a1d40380d225be29491f932d Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 14 Feb 2011 10:54:32 +0000 Subject: Convert dumpTask into a generic purpose file* writer Fully implement view support in p2console --- project2/Jamfile.jam | 4 +- project2/console/consoleAppEngine.cpp | 48 +++++++++++++++---- project2/console/consoleAppEngine.h | 26 ++++++----- project2/dumpTask.cpp | 88 ----------------------------------- project2/dumpTask.h | 17 ------- project2/fileStrmVarWriter.cpp | 60 ++++++++++++++++++++++++ project2/fileStrmVarWriter.h | 32 +++++++++++++ project2/presenter.cpp | 12 +---- project2/presenter.h | 4 +- project2/xmlPresenter.cpp | 12 ++++- project2/xmlPresenter.h | 7 +++ 11 files changed, 167 insertions(+), 143 deletions(-) delete mode 100644 project2/dumpTask.cpp delete mode 100644 project2/dumpTask.h create mode 100644 project2/fileStrmVarWriter.cpp create mode 100644 project2/fileStrmVarWriter.h diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 53ff5ef..96a0797 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -52,7 +52,7 @@ lib p2common : iterate.cpp paramChecker.cpp presenter.cpp rawView.cpp logger.cpp if.cpp sourceObject.cpp task.cpp variables.cpp variableConvert.cpp view.cpp xmlObjectLoader.cpp exceptions.cpp sessionClearTask.cpp session.cpp sessionSetTask.cpp commonObjects.cpp xmlPresenter.cpp - rowView.cpp rowSet.cpp rowUser.cpp rowProcessor.cpp config.cpp + rowView.cpp rowSet.cpp rowUser.cpp rowProcessor.cpp config.cpp fileStrmVarWriter.cpp : ../libmisc//misc libxmlpp @@ -168,7 +168,7 @@ exe p2fcgi : ; exe p2console : - [ glob dumpTask.cpp console/*.cpp ] + [ glob console/*.cpp ] : p2parts ; diff --git a/project2/console/consoleAppEngine.cpp b/project2/console/consoleAppEngine.cpp index bd7c6c6..6053ca9 100644 --- a/project2/console/consoleAppEngine.cpp +++ b/project2/console/consoleAppEngine.cpp @@ -54,7 +54,9 @@ class ConsoleSession : public Session { ConsoleApplicationEngine::ConsoleApplicationEngine(const ConsoleEnvironment * env, const boost::filesystem::path & f) : ApplicationEngine("console/environment"), _env(env), - runtime(new ConsoleSession()) + indent(0), + runtime(new ConsoleSession()), + out(stdout, true) { xmlpp::DomParser request(f.string()); while (xmlXIncludeProcessFlags(request.get_document()->cobj(), XML_PARSE_NOXINCNODE) > 0); @@ -64,6 +66,7 @@ ConsoleApplicationEngine::ConsoleApplicationEngine(const ConsoleEnvironment * en loader.supportedStorers.insert(Storer::into(¶meterChecks)); loader.supportedStorers.insert(Storer::into(&tasks)); loader.supportedStorers.insert(Storer::into(&rowSets)); + loader.supportedStorers.insert(Storer::into(&views)); loader.collectAll(this, request.get_document()->get_root_node(), true); } @@ -95,6 +98,9 @@ ConsoleApplicationEngine::process() const } throw; } + BOOST_FOREACH(Views::value_type v, views) { + v.second->execute(this); + } } const Environment * @@ -114,31 +120,55 @@ ConsoleApplicationEngine::addAppData(const Presenter *) const { } -ConsoleApplicationEngine::ConsolePresenter::ConsolePresenter(const ConsoleApplicationEngine *, - const std::string & group, const std::string & id) : - Presenter(group, id) +void +ConsoleApplicationEngine::pushSub(const Glib::ustring & name, const Glib::ustring & ns) const { + fprintf(stdout, "%*s", indent, ""); + fprintf(stdout, ">>> "); + if (!ns.empty()) { + fprintf(stdout, "%s::", ns.c_str()); + } + fprintf(stdout, "%s\n", name.c_str()); + indent += 2; } -ConsoleApplicationEngine::ConsolePresenter::~ConsolePresenter() +void +ConsoleApplicationEngine::addAttr(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const { + fprintf(stdout, "%*s", indent, ""); + if (!ns.empty()) { + fprintf(stdout, "%s::", ns.c_str()); + } + fprintf(stdout, "@%s = ", name.c_str()); + out(value); + fprintf(stdout, "\n"); } void -ConsoleApplicationEngine::ConsolePresenter::pushSub(const Glib::ustring &, const Glib::ustring &) const +ConsoleApplicationEngine::addField(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const { - indent += 2; + fprintf(stdout, "%*s", indent, ""); + if (!ns.empty()) { + fprintf(stdout, "%s::", ns.c_str()); + } + fprintf(stdout, "%s = ", name.c_str()); + out(value); + fprintf(stdout, "\n"); } void -ConsoleApplicationEngine::ConsolePresenter::addField(const Glib::ustring &, const Glib::ustring &, const Glib::ustring &) const +ConsoleApplicationEngine::setText(const VariableType & value) const { + fprintf(stdout, "%*s = ", indent, ""); + out(value); + fprintf(stdout, "\n"); } void -ConsoleApplicationEngine::ConsolePresenter::popSub() const +ConsoleApplicationEngine::popSub() const { indent -= 2; + fprintf(stdout, "%*s<<<\n", indent, ""); } Glib::ustring diff --git a/project2/console/consoleAppEngine.h b/project2/console/consoleAppEngine.h index abc471f..c18956f 100644 --- a/project2/console/consoleAppEngine.h +++ b/project2/console/consoleAppEngine.h @@ -6,13 +6,15 @@ #include "../paramChecker.h" #include "../presenter.h" #include "../commonObjects.h" +#include "../view.h" +#include "../fileStrmVarWriter.h" #include #include #include class ConsoleEnvironment; -class ConsoleApplicationEngine : public ApplicationEngine, public CommonObjects { +class ConsoleApplicationEngine : public ApplicationEngine, public Presenter { public: ConsoleApplicationEngine(const ConsoleEnvironment *, const boost::filesystem::path &); virtual ~ConsoleApplicationEngine(); @@ -31,22 +33,22 @@ class ConsoleApplicationEngine : public ApplicationEngine, public CommonObjects mutable ConsolePlatforms conplat; void loadEngineSection(const xmlpp::Element *) const; - class ConsolePresenter : public Presenter { - public: - ConsolePresenter(const ConsoleApplicationEngine *, const std::string & id); - ConsolePresenter(const ConsoleApplicationEngine *, const std::string & group, const std::string & id); - virtual ~ConsolePresenter(); - void pushSub(const Glib::ustring & ns, const Glib::ustring & name) const; - void addField(const Glib::ustring & name, const Glib::ustring & ns, const Glib::ustring & value) const; - void popSub() const; - private: - mutable unsigned int indent; - }; + // Presenter interface + public: + void pushSub(const Glib::ustring & ns, const Glib::ustring & name) const; + void addAttr(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const; + void addField(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const; + void setText(const VariableType & value) const; + void popSub() const; + private: + mutable unsigned int indent; OrderedParamCheckers parameterChecks; NoOutputExecutes tasks; + Views views; SessionPtr runtime; + FileStreamVariableWriter out; }; #endif diff --git a/project2/dumpTask.cpp b/project2/dumpTask.cpp deleted file mode 100644 index b266821..0000000 --- a/project2/dumpTask.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "dumpTask.h" -#include "rowSet.h" -#include "xmlObjectLoader.h" -#include -#include -#include - -DECLARE_LOADER("dumptask", DumpTask); - -DumpTask::DumpTask(const xmlpp::Element * p) : - SourceObject(p), - Task(p) -{ -} - -DumpTask::~DumpTask() -{ -} - -void -DumpTask::loadComplete(const CommonObjects *) -{ -} - -class Printer : public boost::static_visitor<> { - public: - void operator()(const long long int & i) const { - fprintf(stderr, "%lld", i); - } - void operator()(const long int & i) const { - fprintf(stderr, "%ld", i); - } - void operator()(const int & i) const { - fprintf(stderr, "%d", i); - } - void operator()(const short int & i) const { - fprintf(stderr, "%hd", i); - } - void operator()(const long long unsigned int & i) const { - fprintf(stderr, "%llu", i); - } - void operator()(const long unsigned int & i) const { - fprintf(stderr, "%lu", i); - } - void operator()(const unsigned int & i) const { - fprintf(stderr, "%u", i); - } - void operator()(const short unsigned int & i) const { - fprintf(stderr, "%hu", i); - } - void operator()(const float & i) const { - fprintf(stderr, "%g", i); - } - void operator()(const double & i) const { - fprintf(stderr, "%g", i); - } - void operator()(const Glib::ustring & i) const { - fprintf(stderr, "'%.*s'", i.length(), i.c_str()); - } - void operator()(const boost::shared_ptr & i) const { - fprintf(stderr, "'%.*s'", i->length(), i->c_str()); - } - void operator()(const boost::posix_time::ptime & i) const { - fprintf(stderr, "[%s]", boost::posix_time::to_iso_extended_string(i).c_str()); - } - void operator()(const boost::shared_ptr & i) const { - fprintf(stderr, "[%s]", boost::posix_time::to_iso_extended_string(*i).c_str()); - } -}; - -void -DumpTask::execute() const -{ - const RowSet::RowValuesStack::value_type & r = *++RowSet::Stack().rbegin(); - unsigned int cols = r->columnCount(); - Printer printer; - for (unsigned int c = 0; c < cols; c += 1) { - if (c > 0) { - fprintf(stderr, ", "); - } - fprintf(stderr, "%s = ", r->getColumnName(c).c_str()); - if (!r->isNull(c)) { - boost::apply_visitor(printer, r->getCurrentValue(c)); - } - } - fprintf(stderr, "\n"); -} - diff --git a/project2/dumpTask.h b/project2/dumpTask.h deleted file mode 100644 index b3dc906..0000000 --- a/project2/dumpTask.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef DUMPTASK_H -#define DUMPTASK_H - -#include "task.h" - -class DumpTask : public Task { - public: - DumpTask(const xmlpp::Element * p); - virtual ~DumpTask(); - virtual void loadComplete(const CommonObjects *); - virtual void execute() const; - -}; - -#endif - - diff --git a/project2/fileStrmVarWriter.cpp b/project2/fileStrmVarWriter.cpp new file mode 100644 index 0000000..f3812ce --- /dev/null +++ b/project2/fileStrmVarWriter.cpp @@ -0,0 +1,60 @@ +#include "fileStrmVarWriter.h" +#include "rowSet.h" +#include "xmlObjectLoader.h" +#include +#include +#include + +FileStreamVariableWriter::FileStreamVariableWriter(FILE * o, bool q) : + out(o), + quoting(q) +{ +} + +FileStreamVariableWriter::~FileStreamVariableWriter() +{ +} + +void FileStreamVariableWriter::operator()(const long long int & i) const { + fprintf(out, "%lld", i); +} +void FileStreamVariableWriter::operator()(const long int & i) const { + fprintf(out, "%ld", i); +} +void FileStreamVariableWriter::operator()(const int & i) const { + fprintf(out, "%d", i); +} +void FileStreamVariableWriter::operator()(const short int & i) const { + fprintf(out, "%hd", i); +} +void FileStreamVariableWriter::operator()(const long long unsigned int & i) const { + fprintf(out, "%llu", i); +} +void FileStreamVariableWriter::operator()(const long unsigned int & i) const { + fprintf(out, "%lu", i); +} +void FileStreamVariableWriter::operator()(const unsigned int & i) const { + fprintf(out, "%u", i); +} +void FileStreamVariableWriter::operator()(const short unsigned int & i) const { + fprintf(out, "%hu", i); +} +void FileStreamVariableWriter::operator()(const float & i) const { + fprintf(out, "%g", i); +} +void FileStreamVariableWriter::operator()(const double & i) const { + fprintf(out, "%g", i); +} +void FileStreamVariableWriter::operator()(const Glib::ustring & i) const { + fprintf(out, "'%.*s'", i.length(), i.c_str()); +} +void FileStreamVariableWriter::operator()(const boost::shared_ptr & i) const { + fprintf(out, "'%.*s'", i->length(), i->c_str()); +} +void FileStreamVariableWriter::operator()(const boost::posix_time::ptime & i) const { + fprintf(out, "[%s]", boost::posix_time::to_iso_extended_string(i).c_str()); +} +void FileStreamVariableWriter::operator()(const boost::shared_ptr & i) const { + fprintf(out, "[%s]", boost::posix_time::to_iso_extended_string(*i).c_str()); +} + diff --git a/project2/fileStrmVarWriter.h b/project2/fileStrmVarWriter.h new file mode 100644 index 0000000..ecd65b6 --- /dev/null +++ b/project2/fileStrmVarWriter.h @@ -0,0 +1,32 @@ +#ifndef FILESTREAMVARWRITER_H +#define FILESTREAMVARWRITER_H + +#include "variables.h" + +class FileStreamVariableWriter : public boost::static_visitor<> { + public: + FileStreamVariableWriter(FILE *, bool quoting); + ~FileStreamVariableWriter(); + + void operator()(const long long int & i) const; + void operator()(const long int & i) const; + void operator()(const int & i) const; + void operator()(const short int & i) const; + void operator()(const long long unsigned int & i) const; + void operator()(const long unsigned int & i) const; + void operator()(const unsigned int & i) const; + void operator()(const short unsigned int & i) const; + void operator()(const float & i) const; + void operator()(const double & i) const; + void operator()(const Glib::ustring & i) const; + void operator()(const boost::shared_ptr & i) const; + void operator()(const boost::posix_time::ptime & i) const; + void operator()(const boost::shared_ptr & i) const; + + private: + FILE * out; + bool quoting; +}; + +#endif + diff --git a/project2/presenter.cpp b/project2/presenter.cpp index c19846e..888a577 100644 --- a/project2/presenter.cpp +++ b/project2/presenter.cpp @@ -1,19 +1,9 @@ #include "presenter.h" -#include #include "dataSource.h" #include "appEngine.h" -Presenter::Presenter(const std::string & group, const std::string & file) : - present(group + "/" + file + ".xml") +Presenter::Presenter() { - while (xmlXIncludeProcessFlags(present.get_document()->cobj(), XML_PARSE_NOXINCNODE) > 0); - - LoaderBase loader("http://project2.randomdan.homeip.net", true); - loader.supportedStorers.insert(Storer::into(&datasources)); - loader.supportedStorers.insert(Storer::into(&rowSets)); - loader.supportedStorers.insert(Storer::into(&views)); - loader.supportedStorers.insert(Storer::into(¶meterChecks)); - loader.collectAll(this, present.get_document()->get_root_node(), true); } Presenter::~Presenter() diff --git a/project2/presenter.h b/project2/presenter.h index 88807ae..4c54a7b 100644 --- a/project2/presenter.h +++ b/project2/presenter.h @@ -11,7 +11,7 @@ class Presenter : public virtual CommonObjects, public virtual IntrusivePtrBase { public: - Presenter(const std::string & group, const std::string & file); + Presenter(); virtual ~Presenter() = 0; virtual void pushSub(const Glib::ustring & name) const; @@ -26,8 +26,6 @@ class Presenter : public virtual CommonObjects, public virtual IntrusivePtrBase protected: Views views; - OrderedParamCheckers parameterChecks; - xmlpp::DomParser present; }; typedef boost::intrusive_ptr PresenterCPtr; diff --git a/project2/xmlPresenter.cpp b/project2/xmlPresenter.cpp index 9cf7a73..dcee298 100644 --- a/project2/xmlPresenter.cpp +++ b/project2/xmlPresenter.cpp @@ -1,16 +1,26 @@ #include "xmlPresenter.h" +#include XmlPresenter::~XmlPresenter() { } XmlPresenter::XmlPresenter(const std::string & group, const std::string & file) : - Presenter(group, file), + present(group + "/" + file + ".xml"), responseRootNodeName(present.get_document()->get_root_node()->get_attribute_value("root")), responseStyle(present.get_document()->get_root_node()->get_attribute_value("style")), contentType(present.get_document()->get_root_node()->get_attribute_value("contenttype")), responseDoc(XmlDocumentPtr(new xmlpp::Document("1.0"))) { + while (xmlXIncludeProcessFlags(present.get_document()->cobj(), XML_PARSE_NOXINCNODE) > 0); + + LoaderBase loader("http://project2.randomdan.homeip.net", true); + loader.supportedStorers.insert(Storer::into(&datasources)); + loader.supportedStorers.insert(Storer::into(&rowSets)); + loader.supportedStorers.insert(Storer::into(&views)); + loader.supportedStorers.insert(Storer::into(¶meterChecks)); + loader.collectAll(this, present.get_document()->get_root_node(), true); + nodeStack.push_back(responseDoc->create_root_node(responseRootNodeName)); xmlNewNs(nodeStack.back()->cobj(), BAD_CAST "http://project2.randomdan.homeip.net", BAD_CAST "project2"); // XSLT Style diff --git a/project2/xmlPresenter.h b/project2/xmlPresenter.h index ade2aaa..72fc3aa 100644 --- a/project2/xmlPresenter.h +++ b/project2/xmlPresenter.h @@ -16,10 +16,17 @@ class XmlPresenter : public Presenter { virtual XmlDocumentPtr getDataDocument() const; + protected: + xmlpp::DomParser present; + + public: const Glib::ustring responseRootNodeName; const Glib::ustring responseStyle; const Glib::ustring contentType; + protected: + OrderedParamCheckers parameterChecks; + private: XmlDocumentPtr responseDoc; mutable std::vector nodeStack; -- cgit v1.2.3