From: g0dil Date: Wed, 26 Nov 2008 15:51:58 +0000 (+0000) Subject: Scheduler/Console: Add optional file support to FileConfig X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=e2d104a5f998f474c3f0afcc3a5dc04f620c49c7;p=senf.git Scheduler/Console: Add optional file support to FileConfig git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@983 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Scheduler/Console/ConfigFile.cc b/Scheduler/Console/ConfigFile.cc index 45e0eee..bbe3b3f 100644 --- a/Scheduler/Console/ConfigFile.cc +++ b/Scheduler/Console/ConfigFile.cc @@ -37,9 +37,15 @@ prefix_ void senf::console::detail::ConfigFileSource::v_parse(RestrictedExecutor & executor) { - parser_.parseFile(filename_, boost::bind( boost::ref(executor), - boost::ref(std::cerr), - _1 )); + try { + parser_.parseFile(filename_, boost::bind( boost::ref(executor), + boost::ref(std::cerr), + _1 )); + } + catch (SystemException & ex) { + if (! (ignoreMissing_ && ex.anyOf(ENOENT))) + throw; + } } /////////////////////////////////////////////////////////////////////////// diff --git a/Scheduler/Console/ConfigFile.cci b/Scheduler/Console/ConfigFile.cci index 8e80db8..a655592 100644 --- a/Scheduler/Console/ConfigFile.cci +++ b/Scheduler/Console/ConfigFile.cci @@ -39,8 +39,15 @@ senf::console::detail::ConfigFileSource::create(std::string const & filename) return ptr(new ConfigFileSource(filename)); } +prefix_ senf::console::detail::ConfigFileSource::ptr +senf::console::detail::ConfigFileSource::ignoreMissing() +{ + ignoreMissing_ = true; + return ptr(this); +} + prefix_ senf::console::detail::ConfigFileSource::ConfigFileSource(std::string const & filename) - : filename_ (filename) + : filename_ (filename), ignoreMissing_ (true) {} /////////////////////////////////////////////////////////////////////////// @@ -48,9 +55,12 @@ prefix_ senf::console::detail::ConfigFileSource::ConfigFileSource(std::string co prefix_ senf::console::ConfigFile::ConfigFile(std::string const & filename, DirectoryNode & root) - : detail::BundleMixin(root) + : detail::BundleMixin(root), self_ (add(detail::ConfigFileSource::create(filename))) +{} + +prefix_ void senf::console::ConfigFile::ignoreMissing() { - add(detail::ConfigFileSource::create(filename)); + self_.ignoreMissing(); } /////////////////////////////////////////////////////////////////////////// diff --git a/Scheduler/Console/ConfigFile.hh b/Scheduler/Console/ConfigFile.hh index 15414fb..c3bea53 100644 --- a/Scheduler/Console/ConfigFile.hh +++ b/Scheduler/Console/ConfigFile.hh @@ -73,6 +73,11 @@ namespace console { ///@} /////////////////////////////////////////////////////////////////////////// + + void ignoreMissing(); ///< Call to ignore missing files + + private: + detail::ConfigFileSource & self_; }; /** \brief Read configuration file diff --git a/Scheduler/Console/ConfigFile.ih b/Scheduler/Console/ConfigFile.ih index 80191d0..f37ca6e 100644 --- a/Scheduler/Console/ConfigFile.ih +++ b/Scheduler/Console/ConfigFile.ih @@ -43,12 +43,15 @@ namespace detail { static ptr create(std::string const & filename); + ptr ignoreMissing(); + private: ConfigFileSource(std::string const & filename); virtual void v_parse(RestrictedExecutor & executor); std::string filename_; + bool ignoreMissing_; CommandParser parser_; }; diff --git a/Scheduler/Console/ConfigFile.test.cc b/Scheduler/Console/ConfigFile.test.cc index b1976c2..0d0a34a 100644 --- a/Scheduler/Console/ConfigFile.test.cc +++ b/Scheduler/Console/ConfigFile.test.cc @@ -78,18 +78,24 @@ BOOST_AUTO_UNIT_TEST(configFile) senf::console::ConfigFile cfg (cfgf.name()); var1 = 0; - SENF_CHECK_NO_THROW( cfg.parse() ) + SENF_CHECK_NO_THROW( cfg.parse() ); BOOST_CHECK_EQUAL( var1, 10 ); var1 = 0; - SENF_CHECK_NO_THROW( cfg.parse() ) + SENF_CHECK_NO_THROW( cfg.parse() ); BOOST_CHECK_EQUAL( var1, 0 ); var1 = 0; cfg.reset(); - SENF_CHECK_NO_THROW( cfg.parse() ) + SENF_CHECK_NO_THROW( cfg.parse() ); BOOST_CHECK_EQUAL( var1, 10 ); } + + { + senf::console::ConfigFile cfg ("i don't exist"); + cfg.ignoreMissing(); + SENF_CHECK_NO_THROW( cfg.parse() ); + } } BOOST_AUTO_UNIT_TEST(configFileRestrict) diff --git a/Scheduler/Console/Mainpage.dox b/Scheduler/Console/Mainpage.dox index ce49820..1314b5d 100644 --- a/Scheduler/Console/Mainpage.dox +++ b/Scheduler/Console/Mainpage.dox @@ -201,6 +201,8 @@ \code senf::console::ConfigFile cf ("some.conf"); + // The following line is optional: Call to ignore mussing files + cf.ignoreMissing(); cf.parse(); \endcode @@ -410,7 +412,7 @@ std::vector args; senf::console::ConfigBundle conf (senf::console::root()["config"]); conf.add( senf::console::FileConfig("/etc/myserver.conf") ); - conf.add( senf::console::FileConfig(".myserver.conf") ); + conf.add( senf::console::FileConfig(".myserver.conf")->ignoreMissing() ); conf.add( senf::console::OptionsConfig(senf::Daemon::instance().argc(), senf::Daemon::instance().argv()) ) .nonOptions(args)