Divided load_config_from_files into smaller ones.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 29 Jul 2009 15:18:12 +0000 (17:18 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 29 Jul 2009 15:18:12 +0000 (17:18 +0200)
src/config.cpp
src/config.h
src/main.cpp

index 4b3fa8c..bcb99a9 100644 (file)
@@ -142,6 +142,104 @@ ServicePtr Config::create_service(const string &protocol,const string &host, con
 
 
 /**
+ * Loads a service config file, invoked by load_config_from_files.
+ * @param full_filename Filename of the service config file to load.
+ * @return 0 if all is fine, 3 if an unknown option was detected, 4 if the service file could not be opened for reading.
+ */
+const int Config::load_service_config_file(const string& full_filename)
+{
+    cout << "Loading service config file: " << full_filename << endl;
+    ifstream service_config_file(full_filename.c_str(),ifstream::in);
+    if(service_config_file.is_open())
+    {
+        try
+        {
+            po::variables_map vm;
+            po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true);
+            po::store(parsed_service_options,vm);
+            po::notify(vm);
+
+            if(vm.count("service.protocol") && vm.count("service.host") && vm.count("service.login") && vm.count("service.password"))
+            {
+                // create the corresponding service
+                string protocol = vm["service.protocol"].as<string>();
+                string host = vm["service.host"].as<string>();
+                string login = vm["service.login"].as<string>();
+                string password = vm["service.password"].as<string>();
+
+                // TODO: convert protocol to lowercase
+                //protocol = tolower(protocol.c_str());
+
+                ServicePtr service = create_service(protocol,host,login,password);
+                if ( service != NULL )
+                {
+                    Services.push_back(service);
+                }
+            }
+        }
+        catch ( po::unknown_option e )
+        {
+            // unknown option in config file detected
+            service_config_file.close();
+            cout << "Unknown option in service config file detected!" << endl;
+            return 3;
+        }
+        service_config_file.close();
+    }
+    else
+    {
+        cout << "Can't open service config file for reading: " << service_config_file << endl;
+        return 4;
+    }
+    return 0;
+}
+
+/**
+ * Loads the main config file, invoked by load_config_from_files
+ * @param full_filename The full filename of the main config file to load
+ * @return 0 if all is fine. 3 if unknown option was detected, 4 if main config file could not be opened for reading
+ */
+const int Config::load_main_config_file(const string& full_filename)
+{
+    // load main config file
+    cout << "Loading main config file: " << full_filename << endl;
+    ifstream main_config_file(full_filename.c_str(),ifstream::in);
+    if(main_config_file.is_open())
+    {
+        try
+        {
+            po::variables_map vm;
+            po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true);
+            po::store(parsed_main_options,vm);
+            po::notify(vm);
+
+            if(vm.count("main.daemon_mode") && vm.count("main.logfile") && vm.count("main.loglevel") && vm.count("main.syslog"))
+            {
+                Daemon_mode = vm["main.daemon_mode"].as<bool>();
+                Logfile = vm["main.logfile"].as<string>();
+                Loglevel = vm["main.loglevel"].as<int>();
+                Syslog = vm["main.syslog"].as<bool>();
+            }
+        }
+        catch ( po::unknown_option e )
+        {
+            // unknown option in config file detected
+            main_config_file.close();
+            cout << "Unknown option in main config file detected!" << endl;
+            return 3;
+        }
+        main_config_file.close();
+    }
+    else
+    {
+        cout << "Can't open main config file for reading: " << main_config_file << endl;
+        return 4;
+    }
+    return 0;
+}
+
+// TODO: Seperate into smaller methods
+/**
  * Loads the main and the service config file and does the needed action.
  * @param config_path The path to the config directory.
  * @return 0 if all is fine.
@@ -159,90 +257,26 @@ const int Config::load_config_from_files(const string& config_path)
             {
                 string actual_file = dir_itr->path().filename();
                 boost::regex expr(".*\.conf?");
+
                 // If it is the main config file do the following
                 if ( actual_file == "bpdyndnsd.conf" )
                 {
+                    // Load the main config file
                     string full_filename = dir_itr->path().string();
-                    cout << "Loading main config file: " << full_filename << endl;
-                    ifstream main_config_file(full_filename.c_str(),ifstream::in);
-                    if(main_config_file.is_open())
-                    {
-                        try
-                        {
-                            po::variables_map vm;
-                            po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true);
-                            po::store(parsed_main_options,vm);
-                            po::notify(vm);
-
-                            if(vm.count("main.daemon_mode") && vm.count("main.logfile") && vm.count("main.loglevel") && vm.count("main.syslog"))
-                            {
-                                Daemon_mode = vm["main.daemon_mode"].as<bool>();
-                                Logfile = vm["main.logfile"].as<string>();
-                                Loglevel = vm["main.loglevel"].as<int>();
-                                Syslog = vm["main.syslog"].as<bool>();
-                            }
-                        }
-                        catch ( po::unknown_option e )
-                        {
-                            // unknown option in config file detected
-                            main_config_file.close();
-                            cout << "Unknown option in main config file detected!" << endl;
-                            return 3;
-                        }
-                        main_config_file.close();
-                    }
-                    else
+                    int ret_val = load_main_config_file(full_filename);
+                    if ( ret_val != 0 )
                     {
-                        cout << "Can't open main config file for reading: " << main_config_file << endl;
-                        return 4;
+                        return ret_val;
                     }
                 }
                 // If it is a service definition file *.conf, parse it and generate the corresponding service
                 else if ( boost::regex_search( actual_file,expr ) )
                 {
                     string full_filename = dir_itr->path().string();
-                    cout << "Loading service config file: " << full_filename << endl;
-                    ifstream service_config_file(full_filename.c_str(),ifstream::in);
-                    if(service_config_file.is_open())
-                    {
-                        try
-                        {
-                            po::variables_map vm;
-                            po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true);
-                            po::store(parsed_service_options,vm);
-                            po::notify(vm);
-
-                            if(vm.count("service.protocol") && vm.count("service.host") && vm.count("service.login") && vm.count("service.password"))
-                            {
-                                // create the corresponding service
-                                string protocol = vm["service.protocol"].as<string>();
-                                string host = vm["service.host"].as<string>();
-                                string login = vm["service.login"].as<string>();
-                                string password = vm["service.password"].as<string>();
-
-                                // TODO: convert protocol to lowercase
-                                protocol = tolower(protocol.c_str());
-
-                                ServicePtr service = create_service(protocol,host,login,password);
-                                if ( service != NULL )
-                                {
-                                    Services.push_back(service);
-                                }
-                            }
-                        }
-                        catch ( po::unknown_option e )
-                        {
-                            // unknown option in config file detected
-                            service_config_file.close();
-                            cout << "Unknown option in service config file detected!" << endl;
-                            return 3;
-                        }
-                        service_config_file.close();
-                    }
-                    else
+                    int ret_val = load_service_config_file(full_filename);
+                    if ( ret_val != 0 )
                     {
-                        cout << "Can't open service config file for reading: " << service_config_file << endl;
-                        return 4;
+                        return ret_val;
                     }
                 }
             }
@@ -250,7 +284,7 @@ const int Config::load_config_from_files(const string& config_path)
     }
     else
     {
-        cout << "Path doesn't exist or is not a directory" << endl;
+        cout << "Config path doesn't exist or is not a directory" << endl;
         return 4;
     }
     return 0;
index 08f9723..456fe4d 100644 (file)
@@ -47,6 +47,8 @@ private:
     bool Syslog;
 
     ServicePtr create_service(const string&,const string&,const string&,const string&);
+    const int load_main_config_file(const string&);
+    const int load_service_config_file(const string&);
 public:
     Config();
 
index 80218b9..b60e073 100644 (file)
@@ -60,14 +60,18 @@ int main(int argc, char *argv[])
         return 0;
     }
 
+    // Load the main config and the service files
     string config_path = "/home/bjoern/bpdyndnsd";  // TODO: standard config path should be /etc/bpdyndnsd if not specified other on command line
     ret_val = config->load_config_from_files(config_path);
     if(ret_val == 3)
     {
+        // unknown option
         cout << "See manpage for config file structure." << endl;
+        return 0;
     }
     else if(ret_val == 4)
     {
+        // error opening
         return 0;
     }