From 99a18f27356e3c3b0f73fead4e2011dacddad8aa Mon Sep 17 00:00:00 2001 From: randomdan Date: Sat, 15 Oct 2011 01:40:24 +0000 Subject: Add some functional variables for date handling and string manipulation --- project2/common/Jamfile.jam | 1 + project2/common/functions/dates.cpp | 95 +++++++++++++++++++++++++++++++++++ project2/common/functions/strings.cpp | 29 +++++++++++ 3 files changed, 125 insertions(+) create mode 100644 project2/common/functions/dates.cpp create mode 100644 project2/common/functions/strings.cpp diff --git a/project2/common/Jamfile.jam b/project2/common/Jamfile.jam index 517be8e..9f8f012 100644 --- a/project2/common/Jamfile.jam +++ b/project2/common/Jamfile.jam @@ -25,6 +25,7 @@ lib p2common : variables-modparam.cpp variables-modsession.cpp variables-moduri.cpp + [ glob functions/*.cpp ] ../../libmisc/buffer.cpp ../../libmisc/misc.cpp : diff --git a/project2/common/functions/dates.cpp b/project2/common/functions/dates.cpp new file mode 100644 index 0000000..da169cf --- /dev/null +++ b/project2/common/functions/dates.cpp @@ -0,0 +1,95 @@ +#include +#include "../variables.h" +#include "../xmlObjectLoader.h" +#include "../xmlStorage.h" +#include "../logger.h" +#include +#include +#include + +class ParseError { }; +/// Variable implementation to access platform configuration values +class ParseDate : public VariableImplDyn { + public: + ParseDate(const xmlpp::Element * e) : + VariableImplDyn(e), + string(e, "string"), + format(e, "format") + { + } + VariableType value() const + { + const char * s = string(); + const char * f = format(); + boost::posix_time::ptime t; + struct tm tm; + memset(&tm, 0, sizeof(struct tm)); + mktime(&tm); + const char * e = strptime(s, f, &tm); + if (!e) { + Logger()->messagef(LOG_ERR, "%s: check failed (parse) for '%s' against '%s' (remaining chars='%s')", + __PRETTY_FUNCTION__, s, f, e); + throw ParseError(); + } + return boost::posix_time::ptime(boost::posix_time::ptime_from_tm(tm)); + } + private: + Variable string; + Variable format; +}; +DECLARE_COMPONENT_LOADER("parsedate", ParseDate, VariableLoader); + +class FormatDate : public VariableImplDyn { + public: + FormatDate(const xmlpp::Element * e) : + VariableImplDyn(e), + date(e, "date"), + format(e, "format") + { + } + VariableType value() const + { + std::stringstream ss; + boost::date_time::time_facet * ft = new boost::date_time::time_facet(); + ss.imbue(std::locale(ss.getloc(), ft)); + ft->format(format()); + ss << boost::get(date()); + return ss.str(); + } + private: + Variable date; + Variable format; +}; +DECLARE_COMPONENT_LOADER("formatdate", FormatDate, VariableLoader); + +class AdjustDate : public VariableImplDyn { + public: + AdjustDate(const xmlpp::Element * e) : + VariableImplDyn(e), + date(e, "date"), + offset(e, "offset") + { + } + VariableType value() const + { + return boost::get(date()) + boost::posix_time::duration_from_string(offset()); + } + private: + Variable date; + Variable offset; +}; +DECLARE_COMPONENT_LOADER("adjustdate", AdjustDate, VariableLoader); + +class CurrentDate : public VariableImplDyn { + public: + CurrentDate(const xmlpp::Element * e) : + VariableImplDyn(e) + { + } + VariableType value() const + { + return boost::posix_time::microsec_clock::universal_time(); + } +}; +DECLARE_COMPONENT_LOADER("currentdate", CurrentDate, VariableLoader); + diff --git a/project2/common/functions/strings.cpp b/project2/common/functions/strings.cpp new file mode 100644 index 0000000..1272c6c --- /dev/null +++ b/project2/common/functions/strings.cpp @@ -0,0 +1,29 @@ +#include +#include "../variables.h" +#include "../xmlObjectLoader.h" +#include "../xmlStorage.h" + +/// Variable implementation to access platform configuration values +class Trim : public VariableImplDyn { + public: + Trim(const xmlpp::Element * e) : + VariableImplDyn(e), + string(e, "string") + { + } + VariableType value() const + { + Glib::ustring str = string(); + Glib::ustring::const_iterator b = str.begin(); + while (Glib::Unicode::isspace(*b)) b++; + Glib::ustring::const_iterator e = str.end(); + while (Glib::Unicode::isspace(*--e)) ; + e++; + return Glib::ustring(b, e); + } + private: + Variable string; +}; +DECLARE_COMPONENT_LOADER("trim", Trim, VariableLoader); + + -- cgit v1.2.3