#include "config.h"
#include "common/ConfUtils.h"
-
+#include "common/common_init.h"
const char *id = NULL, *type = NULL;
char *name, *alt_name;
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<const char*> args;
+ vector<const char*> args, nargs;
deque<const char *> 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<args.size(); i++) {
- if (strcmp(args[i], "-c") == 0) {
- if (i < args.size() - 1)
- fname = args[++i];
- else
- usage();
- } else if (strcmp(args[i], "-t") == 0) {
- if (param == 0)
- param++;
+ if (strcmp(args[i], "-t") == 0) {
if (i < args.size() - 1)
type = args[++i];
else
usage();
- } else if (strcmp(args[i], "-i") == 0) {
- if (i < args.size() - 1)
- id = args[++i];
- else
- usage();
- } else if (strcmp(args[i], "-l") == 0 ||
+ } else {
+ nargs.push_back(args[i]);
+ }
+ }
+ args.swap(nargs);
+
+ common_init(args, type);
+
+ for (unsigned i=0; i<args.size(); i++) {
+ if (strcmp(args[i], "-l") == 0 ||
strcmp(args[i], "--list_sections") == 0) {
if (i < args.size() - 1)
list_sections = args[++i];
if (!list_sections && (param < 1 || param > 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<ConfSection*>::const_iterator p = cf.get_section_list().begin();
- p != cf.get_section_list().end();
+ for (std::list<ConfSection*>::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;
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; i<sections.size(); i++) {
- cf.read(sections[i], key, (char **)&val, NULL);
+ cf->read(sections[i], key, (char **)&val, NULL);
if (val) {
cout << val << std::endl;
static bool show_config = false;
+static ConfFile *cf = NULL;
+
/*
struct foobar {
foobar() { cerr << "config.cc init" << std::endl; }
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);
}
}
exit(1);
}
+ConfFile *conf_get_conf_file()
+{
+ return cf;
+}
+
void parse_config_options(std::vector<const char*>& args)
{
int opt_len = sizeof(config_optionsp)/sizeof(config_option);