From c6da33bc2b90420a08de7dbbee3d1574fed45a9f Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Thu, 12 Mar 2009 10:48:24 -0700 Subject: [PATCH] cconf: restructure, use common infrastructure --- src/cconf.cc | 70 +++++++++++++++------------------------------------ src/config.cc | 26 +++++++++++++------ src/config.h | 1 + 3 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/cconf.cc b/src/cconf.cc index a4de351e201fb..18a7035f33df1 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -11,7 +11,7 @@ using namespace std; #include "config.h" #include "common/ConfUtils.h" - +#include "common/common_init.h" const char *id = NULL, *type = NULL; char *name, *alt_name; @@ -24,38 +24,34 @@ static void usage() int main(int argc, const char **argv) { - const char *fname = NULL; const char *key = NULL, *defval = NULL; const char *list_sections = 0; char *val; int param = 0; - deque args; + vector args, nargs; deque sections; - argv_to_deq(argc, argv, args); - env_to_deq(args); + argv_to_vec(argc, argv, args); + env_to_vec(args); if (args.size() < 2) usage(); for (unsigned i=0; i 3)) usage(); - if (!fname) - usage(); + ConfFile *cf = conf_get_conf_file(); - ConfFile cf(fname); - cf.set_post_process_func(conf_post_process_val); - parse_config_file(&cf, true, type, id); + assert(cf); if (list_sections) { - for (std::list::const_iterator p = cf.get_section_list().begin(); - p != cf.get_section_list().end(); + for (std::list::const_iterator p = cf->get_section_list().begin(); + p != cf->get_section_list().end(); p++) { if (strncmp(list_sections, (*p)->get_name().c_str(), strlen(list_sections)) == 0) cout << (*p)->get_name() << std::endl; @@ -104,31 +97,8 @@ int main(int argc, const char **argv) return 0; } - if (id) { - name = (char *)malloc(strlen(type) + strlen(id) + 2); - sprintf(name, "%s.%s", type, id); - alt_name = (char *)malloc(strlen(type) + strlen(id) + 1); - sprintf(alt_name, "%s%s", type, id); - } else { - name = (char *)type; - } - - g_conf.name = name; - g_conf.id = (char *)id; - g_conf.type = (char *)type; - - if (type) - sections.push_front(type); - - if (alt_name) - sections.push_front(alt_name); - if (name) - sections.push_front(name); - - sections.push_back("global"); - for (unsigned i=0; iread(sections[i], key, (char **)&val, NULL); if (val) { cout << val << std::endl; diff --git a/src/config.cc b/src/config.cc index 6d176e8adae85..8090458221fcb 100644 --- a/src/config.cc +++ b/src/config.cc @@ -40,6 +40,8 @@ atomic_t buffer_total_alloc; static bool show_config = false; +static ConfFile *cf = NULL; + /* struct foobar { foobar() { cerr << "config.cc init" << std::endl; } @@ -937,21 +939,26 @@ void parse_startup_config_options(std::vector& args, const char *mo args.swap(nargs); nargs.clear(); - g_conf.type = strdup(module_type); + if (module_type) { + g_conf.type = strdup(module_type); - if (g_conf.id) { - g_conf.name = (char *)malloc(strlen(module_type) + strlen(g_conf.id) + 2); + if (g_conf.id) { + g_conf.name = (char *)malloc(strlen(module_type) + strlen(g_conf.id) + 2); sprintf(g_conf.name, "%s.%s", g_conf.type, g_conf.id); - } else { + } else { g_conf.name = g_conf.type; + } } - ConfFile cf(g_conf.conf); + if (cf) + delete cf; - parse_config_file(&cf, true, g_conf.type, g_conf.id); + cf = new ConfFile(g_conf.conf); + + parse_config_file(cf, true, g_conf.type, g_conf.id); if (show_config) { - cf.dump(); + cf->dump(); exit(0); } } @@ -993,6 +1000,11 @@ void generic_client_usage() exit(1); } +ConfFile *conf_get_conf_file() +{ + return cf; +} + void parse_config_options(std::vector& args) { int opt_len = sizeof(config_optionsp)/sizeof(config_option); diff --git a/src/config.h b/src/config.h index 259f67e3a7fe9..e80b4c1bac651 100644 --- a/src/config.h +++ b/src/config.h @@ -373,6 +373,7 @@ void generic_server_usage(); void generic_client_usage(); class ConfFile; +ConfFile *conf_get_conf_file(); void parse_config_file(ConfFile *cf, bool auto_update, const char *module_type, const char *module_name); char *conf_post_process_val(const char *val); -- 2.39.5