Scheduler/Console: Add optional file support to FileConfig
g0dil [Wed, 26 Nov 2008 15:51:58 +0000 (15:51 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@983 270642c3-0616-0410-b53a-bc976706d245

Scheduler/Console/ConfigFile.cc
Scheduler/Console/ConfigFile.cci
Scheduler/Console/ConfigFile.hh
Scheduler/Console/ConfigFile.ih
Scheduler/Console/ConfigFile.test.cc
Scheduler/Console/Mainpage.dox

index 45e0eee..bbe3b3f 100644 (file)
 
 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;
+    }
 }
 
 ///////////////////////////////////////////////////////////////////////////
index 8e80db8..a655592 100644 (file)
@@ -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();
 }
 
 ///////////////////////////////////////////////////////////////////////////
index 15414fb..c3bea53 100644 (file)
@@ -73,6 +73,11 @@ namespace console {
 
         ///@}
         ///////////////////////////////////////////////////////////////////////////
+
+        void ignoreMissing();           ///< Call to ignore missing files
+
+    private:
+        detail::ConfigFileSource & self_;
     };
 
     /** \brief Read configuration file
index 80191d0..f37ca6e 100644 (file)
@@ -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_;
     };
 
index b1976c2..0d0a34a 100644 (file)
@@ -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)
index ce49820..1314b5d 100644 (file)
     
     \code
     senf::console::ConfigFile cf ("some.conf");
+    // The following line is optional: Call to ignore mussing files
+    cf.ignoreMissing();
     cf.parse();
     \endcode
 
     std::vector<std::string> 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)