diff options
author | randomdan <randomdan@localhost> | 2011-10-15 01:40:24 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-10-15 01:40:24 +0000 |
commit | 4223db35df5400187e9d640e204df669dbcc655c (patch) | |
tree | fd9652f163de3e8e217484cc6ae45a8cb18de09a | |
parent | Allow view/columns to be (functional) variables (diff) | |
download | project2-4223db35df5400187e9d640e204df669dbcc655c.tar.bz2 project2-4223db35df5400187e9d640e204df669dbcc655c.tar.xz project2-4223db35df5400187e9d640e204df669dbcc655c.zip |
Add some functional variables for date handling and string manipulation
-rw-r--r-- | project2/common/Jamfile.jam | 1 | ||||
-rw-r--r-- | project2/common/functions/dates.cpp | 95 | ||||
-rw-r--r-- | project2/common/functions/strings.cpp | 29 |
3 files changed, 125 insertions, 0 deletions
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 <pch.hpp> +#include "../variables.h" +#include "../xmlObjectLoader.h" +#include "../xmlStorage.h" +#include "../logger.h" +#include <locale> +#include <iostream> +#include <boost/date_time.hpp> + +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<boost::posix_time::ptime, char> * ft = new boost::date_time::time_facet<boost::posix_time::ptime, char>(); + ss.imbue(std::locale(ss.getloc(), ft)); + ft->format(format()); + ss << boost::get<boost::posix_time::ptime>(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<boost::posix_time::ptime>(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 <pch.hpp> +#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); + + |