From 500fa9784a6a667f4af97c8928ba3387a7d5ebca Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 27 Apr 2016 18:01:35 -0400 Subject: [PATCH] Exclusion: New syntax - Removed ::serialize which is unused. - Moved to a two-arg ctor, rather than a 2-step init. --- src/Exclusion.cpp | 87 +++++++++++++++++++++++------------------------ src/Exclusion.h | 5 +-- 2 files changed, 44 insertions(+), 48 deletions(-) diff --git a/src/Exclusion.cpp b/src/Exclusion.cpp index 3aba13ef..d5271771 100644 --- a/src/Exclusion.cpp +++ b/src/Exclusion.cpp @@ -30,41 +30,46 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////// // An exclusion represents untrackable time such as holidays, weekends, evenings // and lunch. By default there are none, but they may be configured. Once there // are exclusions defined, the :fill functionality is enabled. // -// Exclusions are instantiated from the 'define exclusions:' rule. This method -// simply validates. +// Exclusions are instantiated from configuration, and are passed here as name/ +// value strings. // -// Syntax: -// exc monday [ ...] -// exc day on -// exc day off +// Name Value +// -------------------------- ------------------------ +// exclusions.days.2016_01_01 on +// exclusions.days.2016_01_02 off +// exclusions.friday <8:00 12:00-12:45 >17:30 +// exclusions.monday <8:00 12:00-12:45 >17:30 +// exclusions.thursday <8:00 12:00-12:45 >17:30 +// exclusions.tuesday <8:00 12:00-12:45 >18:30 +// exclusions.wednesday <8:00 12:00-13:30 >17:30 // -// block: -// HH:MM:SS -// -void Exclusion::initialize (const std::string& line) +Exclusion::Exclusion (const std::string& name, const std::string& value) +//void Exclusion::initialize (const std::string& line) { - _tokens = split (line); + _tokens = split (name, '.'); + for (auto& token : split (value)) + _tokens.push_back (token); // Validate syntax only. Do nothing with the data. if (_tokens.size () >= 2 && - _tokens[0] == "exc") + _tokens[0] == "exclusions") { if (_tokens.size () == 4 && - _tokens[1] == "day" && - _tokens[2] == "on") - { + _tokens[1] == "days" && + _tokens[3] == "on") { _additive = true; return; } if (_tokens.size () == 4 && - _tokens[1] == "day" && - _tokens[2] == "off") + _tokens[1] == "days" && + _tokens[3] == "off") { _additive = false; return; @@ -76,7 +81,7 @@ void Exclusion::initialize (const std::string& line) } } - throw format ("Unrecognized exclusion syntax: '{1}'.", line); + throw format ("Unrecognized exclusion syntax: '{1}' '{2}'.", name, value); } //////////////////////////////////////////////////////////////////////////////// @@ -98,16 +103,18 @@ std::vector Exclusion::ranges (const Range& range) const std::vector results; int dayOfWeek; - if (_tokens[1] == "day" && - (_tokens[2] == "on" || - _tokens[2] == "off")) + if (_tokens[1] == "days" && + (_tokens[3] == "on" || + _tokens[3] == "off")) { - Datetime start (_tokens[3]); + auto day = _tokens[2]; + std::replace (day.begin (), day.end (), '_', '-'); + Datetime start (day); Datetime end (start); ++end; - Range day (start, end); - if (range.overlap (day)) - results.push_back (day); + Range all_day (start, end); + if (range.overlap (all_day)) + results.push_back (all_day); } else if ((dayOfWeek = Datetime::dayOfWeek (_tokens[1])) != -1) @@ -139,12 +146,6 @@ bool Exclusion::additive () const return _additive; } -//////////////////////////////////////////////////////////////////////////////// -std::string Exclusion::serialize () const -{ - return std::string ("exc") + join (" ", _tokens); -} - //////////////////////////////////////////////////////////////////////////////// std::string Exclusion::dump () const { @@ -164,26 +165,24 @@ Range Exclusion::rangeFromTimeBlock ( int hh, mm, ss; if (pig.getHMS (hh, mm, ss)) return Range (start, Datetime (start.year (), start.month (), start.day (), hh, mm, ss)); - - throw format ("Malformed time block '{1}'.", block); } else if (pig.skip ('>')) { int hh, mm, ss; if (pig.getHMS (hh, mm, ss)) return Range (Datetime (start.year (), start.month (), start.day (), hh, mm, ss), end); - - throw format ("Malformed time block '{1}'.", block); } - - int hh1, mm1, ss1; - int hh2, mm2, ss2; - if (pig.getHMS (hh1, mm1, ss1) && - pig.skip ('-') && - pig.getHMS (hh2, mm2, ss2)) - return Range ( - Datetime (start.year (), start.month (), start.day (), hh1, mm1, ss1), - Datetime (start.year (), start.month (), start.day (), hh2, mm2, ss2)); + else + { + int hh1, mm1, ss1; + int hh2, mm2, ss2; + if (pig.getHMS (hh1, mm1, ss1) && + pig.skip ('-') && + pig.getHMS (hh2, mm2, ss2)) + return Range ( + Datetime (start.year (), start.month (), start.day (), hh1, mm1, ss1), + Datetime (start.year (), start.month (), start.day (), hh2, mm2, ss2)); + } throw format ("Malformed time block '{1}'.", block); } diff --git a/src/Exclusion.h b/src/Exclusion.h index 676c5efb..e30894ac 100644 --- a/src/Exclusion.h +++ b/src/Exclusion.h @@ -35,13 +35,10 @@ class Exclusion { public: - Exclusion () = default; - void initialize (const std::string&); + Exclusion (const std::string&, const std::string&); std::vector tokens () const; std::vector ranges (const Range&) const; bool additive () const; - - std::string serialize () const; std::string dump () const; private: