From 884bb7c285c29cd640bf77423bdebd28bc8468b3 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 19 Jul 2011 23:55:08 +0000 Subject: Push defined columns stuff into its own class, used from streamRows Also use it in new class regexRows for filtering and splitting rows by regex Move all code over to using Glib regexes now they're in Glibmm, as they work properly with UTF8 strings --- project2/Jamfile.jam | 7 ++-- project2/cgi/cgiAppEngine.cpp | 6 ++-- project2/definedColumns.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++ project2/definedColumns.h | 50 ++++++++++++++++++++++++++ project2/regexCheck.cpp | 11 ++---- project2/regexCheck.h | 2 -- project2/regexRows.cpp | 47 ++++++++++++++++++++++++ project2/regexRows.h | 22 ++++++++++++ project2/sqlCheck.cpp | 1 - project2/streamRows.cpp | 79 +--------------------------------------- project2/streamRows.h | 39 +++----------------- 11 files changed, 216 insertions(+), 132 deletions(-) create mode 100644 project2/definedColumns.cpp create mode 100644 project2/definedColumns.h create mode 100644 project2/regexRows.cpp create mode 100644 project2/regexRows.h diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 1b8c17d..5d7186d 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -16,7 +16,6 @@ alias libxslt : : : : lib dl : : dl ; lib fcgi : : fcgi ; lib fcgi++ : : fcgi++ ; -lib boost_regex : : boost_regex ; lib boost_system : : boost_system ; lib boost_filesystem : : boost_filesystem ; lib boost_date_time : : boost_date_time ; @@ -60,7 +59,7 @@ lib p2common : sourceObject.cpp task.cpp variables.cpp variableConvert.cpp view.cpp xmlObjectLoader.cpp exceptions.cpp sessionClearTask.cpp session.cpp sessionSetTask.cpp commonObjects.cpp xmlPresenter.cpp taskHost.cpp checkHost.cpp rowView.cpp rowSet.cpp rowUser.cpp rowProcessor.cpp config.cpp fileStrmVarWriter.cpp noOutputExecute.cpp - transform.cpp transformHtml.cpp transformText.cpp + transform.cpp transformHtml.cpp transformText.cpp definedColumns.cpp ../libmisc/buffer.cpp ../libmisc/misc.cpp : @@ -111,10 +110,9 @@ lib p2files : ; lib p2regex : - regexCheck.cpp + regexCheck.cpp regexRows.cpp : ../libmisc - boost_regex libxmlpp p2common ; @@ -193,7 +191,6 @@ lib p2web : p2common p2uuid boost_program_options - boost_regex boost_filesystem p2xmlSession : : diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index f4eb134..ee19331 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -5,11 +5,11 @@ #include "../iterate.h" #include "../logger.h" #include -#include #include #include "../sessionXml.h" #include "../ostreamWrapper.h" #include +#include const std::string SESSIONID = "sessionID"; typedef UUID SIDKey; @@ -182,8 +182,8 @@ CgiApplicationEngine::session() const bool CgiApplicationEngine::checkDomain(const DomainPlatforms::value_type & i) const { - const std::string & h = _env->getServerName(); - return boost::regex_match(h.begin(), h.end(), boost::regex(i.first.raw())); + Glib::RefPtr reg = Glib::Regex::create(i.first, Glib::REGEX_CASELESS | Glib::REGEX_DOTALL); + return reg->match(_env->getServerName()); } Glib::ustring diff --git a/project2/definedColumns.cpp b/project2/definedColumns.cpp new file mode 100644 index 0000000..1361453 --- /dev/null +++ b/project2/definedColumns.cpp @@ -0,0 +1,84 @@ +#include "definedColumns.h" +#include +#include + +DefinedColumns::DefinedColumns(const xmlpp::Element * p) : + RowSet(p) +{ + unsigned int colNo = 0; + BOOST_FOREACH(const xmlpp::Node * node, p->find("columns/column")) { + const xmlpp::Element * elem = dynamic_cast(node); + if (elem) { + columns.insert(Column(colNo++, elem)); + } + } +} + +DefinedColumns::Column::Column(unsigned int i, const Glib::ustring & c) : + idx(i), + col(c), + defValue(VariableType()) +{ +} + +DefinedColumns::Column::Column(unsigned int i, const xmlpp::Element * p) : + idx(i), + col(p->get_child_text()->get_content()), + defValue(p, "default", false) +{ +} + +void +DefinedColumns::Column::operator=(const VariableType & v) const +{ + value = v; +} + +const Glib::ustring & +DefinedColumns::getColumnName(unsigned int col) const +{ + Columns::index::type::iterator i = columns.get().find(col); + if (i != columns.get().end()) { + return i->col; + } + throw RowSet::FieldOutOfRange(col); +} + +unsigned int +DefinedColumns::columnCount() const +{ + return columns.size(); +} + +VariableType +DefinedColumns::getCurrentValue(unsigned int col) const +{ + Columns::index::type::iterator i = columns.get().find(col); + if (i != columns.get().end()) { + return i->value; + } + throw RowSet::FieldOutOfRange(col); +} + +bool +DefinedColumns::isNull(unsigned int col) const +{ + return (columns.get().find(col) == columns.get().end()); +} + +bool +DefinedColumns::isNull(const Glib::ustring & col) const +{ + return (columns.get().find(col) == columns.get().end()); +} + +VariableType +DefinedColumns::getCurrentValue(const Glib::ustring & col) const +{ + Columns::const_iterator i = columns.get().find(col); + if (i != columns.end()) { + return i->value; + } + throw RowSet::FieldDoesNotExist(col); +} + diff --git a/project2/definedColumns.h b/project2/definedColumns.h new file mode 100644 index 0000000..d6f2ba3 --- /dev/null +++ b/project2/definedColumns.h @@ -0,0 +1,50 @@ +#ifndef DEFINEDCOLUMNS_H +#define DEFINEDCOLUMNS_H + +#include +#include +#include +#include +#include "variables.h" +#include "rowSet.h" + +class DefinedColumns : public RowSet { + public: + DefinedColumns(const xmlpp::Element *); + + unsigned int columnCount() const; + const Glib::ustring & getColumnName(unsigned int col) const; + VariableType getCurrentValue(const Glib::ustring & id) const; + VariableType getCurrentValue(unsigned int col) const; + bool isNull(unsigned int col) const; + bool isNull(const Glib::ustring & id) const; + + protected: + class Column { + public: + Column(unsigned int idx, const Glib::ustring &); + Column(unsigned int idx, const xmlpp::Element * p); + + void operator=(const VariableType &) const; + + const unsigned int idx; + const Glib::ustring col; + mutable VariableType value; + const Variable defValue; + }; + struct byColIdx {}; + struct byColName {}; + typedef boost::multi_index::multi_index_container< + Column, + boost::multi_index::indexed_by< + boost::multi_index::ordered_unique< + boost::multi_index::tag, BOOST_MULTI_INDEX_MEMBER(Column, const Glib::ustring, col)>, + boost::multi_index::ordered_unique< + boost::multi_index::tag, BOOST_MULTI_INDEX_MEMBER(Column, const unsigned int, idx)> + > > Columns; + mutable Columns columns; +}; + +#endif + + diff --git a/project2/regexCheck.cpp b/project2/regexCheck.cpp index b8504c8..b88a44c 100644 --- a/project2/regexCheck.cpp +++ b/project2/regexCheck.cpp @@ -1,7 +1,7 @@ #include "regexCheck.h" #include "xmlObjectLoader.h" #include "commonObjects.h" -#include +#include DECLARE_LOADER("regexcheck", RegexCheck); @@ -24,12 +24,7 @@ RegexCheck::loadComplete(const CommonObjects *) bool RegexCheck::performCheck() const { - return checkString(applyTo()); -} - -bool -RegexCheck::checkString(const Glib::ustring & str) const -{ - return boost::regex_match(str.begin(), str.end(), boost::regex(regex())); + Glib::RefPtr reg = Glib::Regex::create(regex()); + return reg->match(applyTo()); } diff --git a/project2/regexCheck.h b/project2/regexCheck.h index 602f822..17033de 100644 --- a/project2/regexCheck.h +++ b/project2/regexCheck.h @@ -15,8 +15,6 @@ class RegexCheck : public ParamChecker { const Variable applyTo; const Variable regex; - private: - bool checkString(const Glib::ustring & str) const; }; #endif diff --git a/project2/regexRows.cpp b/project2/regexRows.cpp new file mode 100644 index 0000000..b950125 --- /dev/null +++ b/project2/regexRows.cpp @@ -0,0 +1,47 @@ +#include "regexRows.h" +#include "xmlObjectLoader.h" +#include "rowProcessor.h" +#include +#include + +DECLARE_LOADER("regexrows", RegexRows); + +RegexRows::RegexRows(const xmlpp::Element * p) : + DefinedColumns(p), + sourceText(p, "sourceText"), + regex(p, "regex") +{ +} + +RegexRows::~RegexRows() +{ +} + +void +RegexRows::loadComplete(const CommonObjects*) +{ +} + +void +RegexRows::setFilter(const Glib::ustring&) +{ +} + +void +RegexRows::execute(const RowProcessor * rp) const +{ + rowNum = 1; + Glib::RefPtr reg = Glib::Regex::create(regex(), Glib::REGEX_CASELESS | Glib::REGEX_DOTALL); + Glib::MatchInfo matches; + if (reg->match(sourceText(), matches)) { + do { + unsigned int cols = std::min(matches.get_match_count(), DefinedColumns::columnCount() + 1); + for (unsigned int n = 1; n < cols; n += 1) { + *DefinedColumns::columns.get().find(n - 1) = matches.fetch(n); + } + rp->rowReady(); + rowNum += 1; + } while (matches.next()); + } +} + diff --git a/project2/regexRows.h b/project2/regexRows.h new file mode 100644 index 0000000..b4b6446 --- /dev/null +++ b/project2/regexRows.h @@ -0,0 +1,22 @@ +#ifndef REGEXROWS_H +#define REGEXROWS_H + +#include "definedColumns.h" +#include "variables.h" + +/// Base class for Project2 components that create a row set based on the contents of a byte stream +class RegexRows : public DefinedColumns { + public: + RegexRows(const xmlpp::Element * p); + ~RegexRows(); + void loadComplete(const CommonObjects*); + void setFilter(const Glib::ustring&); + void execute(const RowProcessor*) const; + + private: + const Variable sourceText; + const Variable regex; +}; + +#endif + diff --git a/project2/sqlCheck.cpp b/project2/sqlCheck.cpp index c36c229..c89608b 100644 --- a/project2/sqlCheck.cpp +++ b/project2/sqlCheck.cpp @@ -3,7 +3,6 @@ #include "selectcommand.h" #include "column.h" #include "rdbmsDataSource.h" -#include #include "commonObjects.h" #include "sqlVariableBinder.h" #include diff --git a/project2/streamRows.cpp b/project2/streamRows.cpp index 62d0d0f..4cf29e6 100644 --- a/project2/streamRows.cpp +++ b/project2/streamRows.cpp @@ -1,10 +1,8 @@ #include "streamRows.h" #include "rowProcessor.h" -#include -#include StreamRows::StreamRows(const xmlpp::Element * p) : - RowSet(p), + DefinedColumns(p), fieldSep(p->get_attribute_value("fieldSep")[0]), quoteChar(p->get_attribute_value("quoteChar")[0]), keepBlankRows(p->get_attribute_value("keepBlankRows") == "true"), @@ -16,13 +14,6 @@ StreamRows::StreamRows(const xmlpp::Element * p) : inQuotes(false), prevWasQuote(false) { - unsigned int colNo = 0; - BOOST_FOREACH(const xmlpp::Node * node, p->find("columns/column")) { - const xmlpp::Element * elem = dynamic_cast(node); - if (elem) { - columns.insert(Column(colNo++, elem)); - } - } mkCols = columns.empty(); } @@ -30,54 +21,6 @@ StreamRows::~StreamRows() { } -const Glib::ustring & -StreamRows::getColumnName(unsigned int col) const -{ - Columns::index::type::iterator i = columns.get().find(col); - if (i != columns.get().end()) { - return i->col; - } - throw RowSet::FieldOutOfRange(col); -} - -unsigned int -StreamRows::columnCount() const -{ - return columns.size(); -} - -VariableType -StreamRows::getCurrentValue(unsigned int col) const -{ - Columns::index::type::iterator i = columns.get().find(col); - if (i != columns.get().end()) { - return i->value; - } - throw RowSet::FieldOutOfRange(col); -} - -bool -StreamRows::isNull(unsigned int col) const -{ - return (columns.get().find(col) == columns.get().end()); -} - -bool -StreamRows::isNull(const Glib::ustring & col) const -{ - return (columns.get().find(col) == columns.get().end()); -} - -VariableType -StreamRows::getCurrentValue(const Glib::ustring & col) const -{ - Columns::const_iterator i = columns.get().find(col); - if (i != columns.end()) { - return i->value; - } - throw RowSet::FieldDoesNotExist(col); -} - void StreamRows::addColumn(Glib::ustring & tok) const { @@ -92,26 +35,6 @@ StreamRows::addColumn(Glib::ustring & tok) const columns.insert(Column(columns.size(), tok)); } -StreamRows::Column::Column(unsigned int i, const Glib::ustring & c) : - idx(i), - col(c), - defValue(VariableType()) -{ -} - -StreamRows::Column::Column(unsigned int i, const xmlpp::Element * p) : - idx(i), - col(p->get_child_text()->get_content()), - defValue(p, "default", false) -{ -} - -void -StreamRows::Column::operator=(const VariableType & v) const -{ - value = v; -} - void StreamRows::begin() const { diff --git a/project2/streamRows.h b/project2/streamRows.h index 449a44b..338d997 100644 --- a/project2/streamRows.h +++ b/project2/streamRows.h @@ -1,25 +1,17 @@ #ifndef STREAMROWS_H #define STREAMROWS_H -#include "rowSet.h" #include "variables.h" -#include -#include -#include +#include "definedColumns.h" + +class RowProcessor; /// Base class for Project2 components that create a row set based on the contents of a byte stream -class StreamRows : public RowSet { +class StreamRows : public DefinedColumns { public: StreamRows(const xmlpp::Element * p); ~StreamRows(); - unsigned int columnCount() const; - const Glib::ustring & getColumnName(unsigned int col) const; - VariableType getCurrentValue(const Glib::ustring & id) const; - VariableType getCurrentValue(unsigned int col) const; - bool isNull(unsigned int col) const; - bool isNull(const Glib::ustring & id) const; - protected: void begin() const; void pushChar(gunichar ch, const RowProcessor *) const; @@ -27,29 +19,6 @@ class StreamRows : public RowSet { private: void addColumn(Glib::ustring & rawtok) const; - class Column { - public: - Column(unsigned int idx, const Glib::ustring &); - Column(unsigned int idx, const xmlpp::Element * p); - - void operator=(const VariableType &) const; - - const unsigned int idx; - const Glib::ustring col; - mutable VariableType value; - const Variable defValue; - }; - struct byColIdx {}; - struct byColName {}; - typedef boost::multi_index::multi_index_container< - Column, - boost::multi_index::indexed_by< - boost::multi_index::ordered_unique< - boost::multi_index::tag, BOOST_MULTI_INDEX_MEMBER(Column, const Glib::ustring, col)>, - boost::multi_index::ordered_unique< - boost::multi_index::tag, BOOST_MULTI_INDEX_MEMBER(Column, const unsigned int, idx)> - > > Columns; - mutable Columns columns; public: const gunichar fieldSep; -- cgit v1.2.3