diff --git a/src/DOM.cpp b/src/DOM.cpp index 042314bb0..a4bbff466 100644 --- a/src/DOM.cpp +++ b/src/DOM.cpp @@ -25,7 +25,9 @@ // //////////////////////////////////////////////////////////////////////////////// +#include #include +#include #include #include "../cmake.h" @@ -48,36 +50,13 @@ DOM::~DOM () { } -//////////////////////////////////////////////////////////////////////////////// -const int DOM::getInteger (const std::string& name) -{ - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -const double DOM::getReal (const std::string& name) -{ - return 0.0; -} - -//////////////////////////////////////////////////////////////////////////////// -const bool DOM::getBoolean (const std::string& name) -{ - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -const time_t DOM::getDate (const std::string& name) -{ - return 0; -} - //////////////////////////////////////////////////////////////////////////////// // TODO . <-- context.tdb2 // TODO . <-- context.tdb2 // rc. <-- context.config // TODO report.. <-- context.reports // TODO stats. <-- context.stats +// TODO context. <-- args, ... // // system. <-- context.system // system.version @@ -94,6 +73,35 @@ const std::string DOM::get (const std::string& name) return context.config.get (name.substr (3)); } + else if (len > 8 && + name.substr (0, 8) == "context.") + { + if (name == "context.program") + return context.program; + + else if (name == "context.args") + { + std::string combined; + join (combined, " ", context.args); + return combined; + } + else if (name == "context.width") + { + std::stringstream s; + s << context.terminal_width; + return s.str (); + } + else if (name == "context.height") + { + std::stringstream s; + s << context.terminal_height; + return s.str (); + } + + else + throw std::string ("DOM: Cannot get unrecognized name '") + name + "'."; + } + // TODO . // TODO . // TODO report. @@ -104,17 +112,17 @@ const std::string DOM::get (const std::string& name) name.substr (0, 7) == "system.") { // Taskwarrior version number. - if (name.substr (7) == "version") + if (name == "system.version") return VERSION; #ifdef HAVE_LIBLUA // Lua version number. - else if (name.substr (7) == "lua.version") + else if (name == "system.lua.version") return LUA_RELEASE; #endif // OS type. - else if (name.substr (7) == "os") + else if (name == "system.os") #if defined (DARWIN) return "Darwin"; #elif defined (SOLARIS) @@ -132,34 +140,29 @@ const std::string DOM::get (const std::string& name) #else return ""; #endif + + else + throw std::string ("DOM: Cannot get unrecognized name '") + name + "'."; } return ""; } -//////////////////////////////////////////////////////////////////////////////// -void DOM::set (const std::string& name, const bool value) -{ -} - -//////////////////////////////////////////////////////////////////////////////// -void DOM::set (const std::string& name, const int value) -{ -} - -//////////////////////////////////////////////////////////////////////////////// -void DOM::set (const std::string& name, const double value) -{ -} - -//////////////////////////////////////////////////////////////////////////////// -void DOM::set (const std::string& name, const time_t value) -{ -} - //////////////////////////////////////////////////////////////////////////////// void DOM::set (const std::string& name, const std::string& value) { + int len = name.length (); + + // rc. --> context.config + if (len > 3 && + name.substr (0, 3) == "rc.") + { + return context.config.set (name.substr (3), value); + } + + // Unrecognized --> error. + else + throw std::string ("DOM: Cannot set '") + name + "'."; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/DOM.h b/src/DOM.h index 6c4da7c1f..394142946 100644 --- a/src/DOM.h +++ b/src/DOM.h @@ -36,16 +36,7 @@ public: DOM (); ~DOM (); - const int getInteger (const std::string&); - const double getReal (const std::string&); - const bool getBoolean (const std::string&); - const time_t getDate (const std::string&); - const std::string get (const std::string&); - - void set (const std::string&, const bool); - void set (const std::string&, const int); - void set (const std::string&, const double); - void set (const std::string&, const time_t); + const std::string get (const std::string&); void set (const std::string&, const std::string&); private: diff --git a/test/dom.t.cpp b/test/dom.t.cpp index 78bd6faac..1efaef05e 100644 --- a/test/dom.t.cpp +++ b/test/dom.t.cpp @@ -42,14 +42,22 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (2); + UnitTest t (7); try { DOM dom; + // TODO dom.get rc.name t.is (dom.get ("system.version"), VERSION, "DOM system.version -> VERSION"); t.is (dom.get ("system.lua.version"), LUA_RELEASE, "DOM system.lua.version -> LUA_RELEASE"); + t.ok (dom.get ("system.os") != "", "DOM system.os -> != Unknown"); + t.is (dom.get ("context.program"), "", "DOM context.program -> ''"); + t.is (dom.get ("context.args"), "", "DOM context.args -> ''"); + t.is (dom.get ("context.width"), "0", "DOM context.width -> '0'"); + t.is (dom.get ("context.height"), "0", "DOM context.height -> '0'"); + + // TODO dom.set rc.name } catch (std::string& error)