free(name);
name = get_next_tok(p, _def_delim, 1, &p);
- printf("name='%s' line='%s'\n", name, line);
-
if (*name) {
if (*line)
snprintf(line, MAX_LINE, "%s %s", line, name);
printf("------ config ends here ------\n");
}
+ConfSection *ConfFile::_add_section(const char *section, ConfLine *cl)
+{
+ ConfSection *sec;
+#define BUF_SIZE 4096
+ char *buf;
+
+ buf = (char *)malloc(BUF_SIZE);
+
+ sec = new ConfSection(section);
+ sections[section] = sec;
+ sections_list.push_back(sec);
+
+ if (!cl) {
+ cl = new ConfLine();
+ snprintf(buf, BUF_SIZE, "[%s]", section);
+ cl->set_prefix(buf);
+ }
+ sec->conf_list.push_back(cl);
+
+ free(buf);
+
+ return sec;
+}
+
+
int ConfFile::parse()
{
char *buf;
- int len, i, l, map_index;
+ int len, i, l;
char line[MAX_LINE];
ConfLine *cl;
ConfSection *section;
section = new ConfSection("global");
sections["global"] = section;
sections_list.push_back(section);
-#define BUF_SIZE 4096
fd = open(filename, O_RDWR);
if (fd < 0)
return 0;
l = 0;
- map_index = 0;
buf = (char *)malloc(BUF_SIZE);
do {
parse_line(line, cl);
if (cl->get_var()) {
section->conf_map[cl->get_var()] = cl;
- printf("cl->var <---- '%s'\n", cl->get_var());
+ global_list.push_back(cl);
+ section->conf_list.push_back(cl);
} else if (cl->get_section()) {
- printf("cur_map <---- '%s'\n", cl->get_section());
- map_index = 0;
- section = new ConfSection(cl->get_section());
- sections[cl->get_section()] = section;
- sections_list.push_back(section);
+ section = _add_section(cl->get_section(), cl);
}
- global_list.push_back(cl);
- section->conf_list.push_back(cl);
l = 0;
break;
default:
}
} while (len);
+ free(buf);
+
return 1;
}
int rc;
if (fd < 0) {
- fd = open(filename, O_RDWR | O_CREAT);
+ fd = open(filename, O_RDWR | O_CREAT, 0644);
if (fd < 0) {
printf("error opening file %s errno=%d\n", filename, errno);
ConfSection *sec;
ConfMap::iterator cm_iter;
ConfLine *cl;
- char buf[128];
if (iter == sections.end() ) {
- sec = new ConfSection(section);
- sections[section] = sec;
- sections_list.push_back(sec);
-
- cl = (ConfLine *)malloc(sizeof(ConfLine));
- memset(cl, 0, sizeof(ConfLine));
- snprintf(buf, sizeof(buf), "[%s]", section);
- cl->set_prefix(buf);
- sec->conf_list.push_back(cl);
+ sec = _add_section(section, NULL);
} else {
sec = iter->second;
}
*dst_val = (char *)malloc(len + 1);
strncpy(*dst_val, str_val, len);
+ (*dst_val)[len] = '\0';
}
-static void _conf_decode(float *dst_val, char *str_val)
+static int _conf_decode(float *dst_val, char *str_val)
{
*dst_val = atof(str_val);
+
+ return 1;
}
-static void _conf_encode(char *dst_str, int len, int val)
+static int _conf_encode(char *dst_str, int len, int val)
{
snprintf(dst_str, len, "%d", val);
+
+ return 1;
}
-static void _conf_encode(char *dst_str, int len, float val)
+static int _conf_encode(char *dst_str, int len, float val)
{
snprintf(dst_str, len, "%g", val);
+
+ return 1;
}
-static void _conf_encode(char *dst_str, int len, bool val)
+static int _conf_encode(char *dst_str, int len, bool val)
{
snprintf(dst_str, len, "%s", (val ? "true" : "false"));
+
+ return 1;
}
-static void _conf_encode(char *dst_str, int max_len, char *val)
+static int _conf_encode(char *dst_str, int max_len, char *val)
{
int have_delim = 0;
int i;
- int len = strlen(val);
+ int len;
+
+ if (!val)
+ return 0;
+
+ len = strlen(val);
for (i=0; i<len; i++) {
if (is_delim(val[i], _def_delim)) {
else
snprintf(dst_str, max_len, "%s", val);
- return;
+ return 1;
}
template<typename T>
ConfLine *cl;
char line[MAX_LINE];
+ if (!_conf_encode(line, MAX_LINE, val))
+ return 0;
+
cl = _find_var(section, var);
if (!cl)
cl = _add_var(section, var);
- _conf_encode(line, MAX_LINE, val);
cl->set_val(line);
return 1;
CF_READ("global", "num_osd", num_osd);
CF_READ("global", "mkfs", mkfs);
CF_READ("global", "daemonize", daemonize);
- CF_READ_STR("global", "file_logs", file_logs);
+ CF_READ("global", "file_logs", file_logs);
CF_READ("global", "log", log);
CF_READ("global", "log_interval", log_interval);
- CF_READ_STR("global", "str_alloc", log_name);
+ CF_READ_STR("global", "log_name", log_name);
CF_READ("global", "log_messages", log_messages);
CF_READ("global", "log_pins", log_pins);
CF_READ_STR("global", "dout_dir", dout_dir);