From fc87cdbbe1b4e294c63c62147b1f3f7e83730d80 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Fri, 24 Jul 2009 17:12:44 +0200 Subject: [PATCH] The config class. --- src/config.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 80 insertions(+), 22 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 8f021f9..7727464 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -11,11 +11,12 @@ // #include "config.h" + +// Define allowed command line and config file options Config::Config() { - // define valid command line parameters + // Define valid command line parameters opt_desc_cmd = new po::options_description("Command line options"); - opt_desc_cmd->add_options() ("help,?","Show help.") ("version,v","Show version.") @@ -25,27 +26,40 @@ Config::Config() ("password,p",po::value(),"Set the password.") ; - // define valid config file parameters - opt_desc_conf = new po::options_description("Config file options"); + // Define valid config file main section parameters + opt_desc_conf_main = new po::options_description("Config file main section options"); + opt_desc_conf_main->add_options() + ("main.daemon_mode",po::value()->default_value(false),"Run as system daemon.") + ("main.logfile",po::value()->default_value("/var/log/bpdyndns.log"),"Where to log.") + ("main.loglevel",po::value()->default_value(0),"Loglevel.") + ("main.syslog",po::value()->default_value(false),"Use syslog facility.") + ; - opt_desc_conf->add_options() - ("help,?","Show help.") - ("version,v","Show version.") + // Define valid config file main section parameters + opt_desc_conf_service = new po::options_description("Config file service section options"); + opt_desc_conf_service->add_options() + ("service.protocol",po::value(),"The service protocol.") + ("service.host",po::value(),"The hostname to update.") + ("service.login",po::value(),"Login name.") + ("service.password",po::value(),"Corresponding password.") ; } + Config::~Config() { delete opt_desc_cmd; - delete opt_desc_conf; + delete opt_desc_conf_main; + delete opt_desc_conf_service; } + int Config::parse_cmd_line(int argc, char *argv[]) { try { po::variables_map vm; - po::store(po::parse_command_line(argc, argv, *opt_desc_cmd), vm); + po::store(po::parse_command_line(argc, argv, *this->opt_desc_cmd), vm); po::notify(vm); if(vm.count("help")) @@ -53,17 +67,18 @@ int Config::parse_cmd_line(int argc, char *argv[]) else if(vm.count("version")) return 2; + // Are all needed options set to create a Service Object if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password")) { - // get the cmd parameter values for protocol host login and password + // Get the cmd parameter values for protocol host login and password string protocol = vm["protocol"].as(); string host = vm["host"].as(); string login = vm["login"].as(); string password = vm["password"].as(); - //TODO: convert all options to lowercase + //TODO: convert protocol option to lowercase - // create Service object + // Create corresponding Service object TODO: swap to extra method if(protocol == "dhs") { Service * service = new DHS(host,login,password); @@ -81,7 +96,7 @@ int Config::parse_cmd_line(int argc, char *argv[]) return 1; } } - catch(boost::program_options::unknown_option e) + catch(po::unknown_option e) { cout << "Unknown option set." << endl; return 1; @@ -89,23 +104,66 @@ int Config::parse_cmd_line(int argc, char *argv[]) return 0; } -void Config::print_usage() + +int Config::load_config_from_files(string config_path) { - cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl; - cout << *opt_desc_cmd << endl; + // first load the main config file bpdyndnsd.conf + string main_conf_file = config_path.append("/bpdyndnsd.conf"); + ifstream config_file (main_conf_file.c_str(),ifstream::in); + if(config_file.is_open()) + { + try + { + // parse the config file + po::variables_map vm; + po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true); + po::store(parsed,vm); + po::notify(vm); + + if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog")) + { + // Get the options from main section + this->daemon_mode = vm["main.daemon_mode"].as(); + this->logfile = vm["main.logfile"].as(); + this->loglevel = vm["main.loglevel"].as(); + this->syslog = vm["main.syslog"].as(); + } + } + catch(po::unknown_option e) + { + // unknown option in config file detected + config_file.close(); + cout << "Unknown option in config file detected!" << endl; + return 3; + } + config_file.close(); + } + else + { + cout << "Can't open main config file for reading: " << main_conf_file << endl; + return 4; + } + + // then load all service definition files in config path + + // TODO: code to load service definition files in config path } -void Config::print_version(string version) + +list Config::get_services() { - cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl; + return this->services; } -void Config::load_config_file(string filename) -{ +void Config::print_usage() +{ + cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl; + cout << *opt_desc_cmd << endl; } -list Config::get_services() + +void Config::print_version(string version) { - return this->services; + cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl; } -- 1.7.1