}
};
-
-struct conf_line {
- char *prefix;
- char *var;
- char *mid;
- char *val;
- char *suffix;
- char *section;
-};
-
static const char *_def_delim=" \t\n\r";
static const char *_eq_delim="= \t\n\r";
static const char *_eol_delim="\n\r";
return out;
}
-static int _parse_section(char *str, struct conf_line *parsed)
+static int _parse_section(char *str, ConfLine *parsed)
{
char *open, *close;
char *name = NULL;
} while (*name);
if (*line)
- parsed->section = strdup(line);
+ parsed->set_section(line);
out:
return ret;
}
-int parse_line(char *line, struct conf_line *parsed)
+int parse_line(char *line, ConfLine *parsed)
{
char *dup=strdup(line);
char *p = NULL;
char *eq;
int ret = 0;
- memset(parsed, 0, sizeof(struct conf_line));
+ memset(parsed, 0, sizeof(ConfLine));
- parsed->prefix = get_next_delim(dup, _def_delim, 1, &p);
+ parsed->set_prefix(get_next_delim(dup, _def_delim, 1, &p));
if (!*p)
goto out;
switch (*p) {
case '#':
- parsed->suffix = strdup(p);
+ parsed->set_suffix(p);
goto out;
case '[':
- parsed->suffix = strdup(p);
+ parsed->set_suffix(p);
return _parse_section(p, parsed);
}
- parsed->var = get_next_tok(p, _def_delim, 1, &p);
+ parsed->set_var(get_next_tok(p, _def_delim, 1, &p));
if (!*p)
goto out;
- parsed->mid = get_next_delim(p, _eq_delim, 1, &p);
+ parsed->set_mid(get_next_delim(p, _eq_delim, 1, &p));
if (!*p)
goto out;
- eq = get_next_tok(parsed->mid, _def_delim, 0, NULL);
+ eq = get_next_tok(parsed->get_mid(), _def_delim, 0, NULL);
if (*eq != '=') {
goto out;
}
- parsed->val = get_next_tok(p, _def_delim, 1, &p);
+ parsed->set_val(get_next_tok(p, _def_delim, 1, &p));
if (!*p)
goto out;
ret = 1;
- parsed->suffix = strdup(p);
+ parsed->set_suffix(p);
out:
free(dup);
return ret;
return len;
}
+ConfSection::~ConfSection()
+{
+ ConfList::iterator conf_iter, conf_end;
+ ConfLine *cl;
+
+ conf_end = conf_list.end();
+
+ for (conf_iter = conf_list.begin(); conf_iter != conf_end; ++conf_iter) {
+ cl = *conf_iter;
+
+ delete cl;
+ }
+}
+
+void ConfLine::_set(char **dst, const char *val)
+{
+ if (*dst)
+ free(*dst);
+
+ if (!val)
+ *dst = NULL;
+ else
+ *dst = strdup(val);
+}
+
+void ConfLine::set_prefix(const char *val)
+{
+ _set(&prefix, val);
+}
+
+void ConfLine::set_var(const char *val)
+{
+ _set(&var, val);
+}
+
+void ConfLine::set_mid(const char *val)
+{
+ _set(&mid, val);
+}
+
+void ConfLine::set_val(const char *val)
+{
+ _set(&this->val, val);
+}
+
+void ConfLine::set_suffix(const char *val)
+{
+ _set(&suffix, val);
+}
+
+void ConfLine::set_section(const char *val)
+{
+ _set(§ion, val);
+}
+
+ConfLine::~ConfLine()
+{
+ if (prefix)
+ free(prefix);
+ if(var)
+ free(var);
+ if (mid)
+ free(mid);
+ if (val)
+ free(val);
+ if (suffix)
+ free(suffix);
+ if (section)
+ free(section);
+}
+
+ConfFile::~ConfFile()
+{
+ SectionList::iterator sec_iter, sec_end;
+ ConfSection *sec;
+
+ free(filename);
+
+ sec_end = sections_list.end();
+
+ for (sec_iter = sections_list.begin(); sec_iter != sec_end; ++sec_iter) {
+ sec = *sec_iter;
+
+ delete sec;
+ }
+}
+
+int ConfLine::output(char *line, int max_len)
+{
+ int len = 0;
+
+ if (!max_len)
+ return 0;
+
+ line[0] = '\0';
+ if (prefix)
+ len += _str_cat(&line[len], max_len-len, prefix);
+ if (var)
+ len += _str_cat(&line[len], max_len-len, var);
+ if (mid)
+ len += _str_cat(&line[len], max_len-len, mid);
+ if (val)
+ len += _str_cat(&line[len], max_len-len, val);
+ if (suffix)
+ len += _str_cat(&line[len], max_len-len, suffix);
+
+ return len;
+}
+
+
void ConfFile::dump()
{
SectionList::iterator sec_iter, sec_end;
- struct conf_line *cl;
+ ConfLine *cl;
char line[MAX_LINE];
int len = 0;
char *p;
if (cl) {
line[0] = '\0';
- if (cl->prefix)
- len += _str_cat(&line[len], MAX_LINE-len, cl->prefix);
- if (cl->var)
- len += _str_cat(&line[len], MAX_LINE-len, cl->var);
- if (cl->mid)
- len += _str_cat(&line[len], MAX_LINE-len, cl->mid);
- if (cl->val)
- len += _str_cat(&line[len], MAX_LINE-len, cl->val);
- if (cl->suffix)
- len += _str_cat(&line[len], MAX_LINE-len, cl->suffix);
+ cl->output(line, MAX_LINE);
printf("%s\n", line);
}
}
char *buf;
int len, i, l, map_index;
char line[MAX_LINE];
- struct conf_line *cl;
+ ConfLine *cl;
ConfSection *section;
section = new ConfSection("global");
continue;
case '\n' :
line[l] = '\0';
- cl = (struct conf_line *)malloc(sizeof(struct conf_line));
+ cl = new ConfLine();
parse_line(line, cl);
- if (cl->var) {
- section->conf_map[cl->var] = cl;
- printf("cl->var <---- '%s'\n", cl->var);
- } else if (cl->section) {
- printf("cur_map <---- '%s'\n", cl->section);
+ if (cl->get_var()) {
+ section->conf_map[cl->get_var()] = cl;
+ printf("cl->var <---- '%s'\n", cl->get_var());
+ } else if (cl->get_section()) {
+ printf("cur_map <---- '%s'\n", cl->get_section());
map_index = 0;
- section = new ConfSection(cl->section);
- sections[cl->section] = section;
+ section = new ConfSection(cl->get_section());
+ sections[cl->get_section()] = section;
sections_list.push_back(section);
}
global_list.push_back(cl);
return 1;
}
-struct conf_line *ConfFile::_find_var(const char *section, const char* var)
+ConfLine *ConfFile::_find_var(const char *section, const char* var)
{
SectionMap::iterator iter = sections.find(section);
ConfSection *sec;
ConfMap::iterator cm_iter;
- struct conf_line *cl;
+ ConfLine *cl;
if (iter == sections.end() )
goto notfound;
return 0;
}
-struct conf_line *ConfFile::_add_var(const char *section, const char* var)
+ConfLine *ConfFile::_add_var(const char *section, const char* var)
{
SectionMap::iterator iter = sections.find(section);
ConfSection *sec;
ConfMap::iterator cm_iter;
- struct conf_line *cl;
+ ConfLine *cl;
char buf[128];
if (iter == sections.end() ) {
sections[section] = sec;
sections_list.push_back(sec);
- cl = (struct conf_line *)malloc(sizeof(struct conf_line));
- memset(cl, 0, sizeof(struct conf_line));
+ cl = (ConfLine *)malloc(sizeof(ConfLine));
+ memset(cl, 0, sizeof(ConfLine));
snprintf(buf, sizeof(buf), "[%s]", section);
- cl->prefix = strdup(buf);
+ cl->set_prefix(buf);
+ cl->set_mid(" = ");
sec->conf_list.push_back(cl);
} else {
sec = iter->second;
}
- cl = (struct conf_line *)malloc(sizeof(struct conf_line));
- memset(cl, 0, sizeof(struct conf_line));
+ cl = new ConfLine();
- cl->prefix = strdup("\t");
- cl->var = strdup(var);
- cl->mid = strdup(" = ");
+ cl->set_prefix("\t");
+ cl->set_var(var);
+ cl->set_mid(" = ");
sec->conf_map[var] = cl;
sec->conf_list.push_back(cl);
template<typename T>
int ConfFile::_read(const char *section, const char *var, T *val, T def_val)
{
- struct conf_line *cl;
+ ConfLine *cl;
cl = _find_var(section, var);
- if (!cl || !cl->val)
+ if (!cl || !cl->get_val())
goto notfound;
- _conf_decode(val, cl->val);
+ _conf_decode(val, cl->get_val());
return 1;
notfound:
template<typename T>
int ConfFile::_write(const char *section, const char *var, T val)
{
- struct conf_line *cl;
+ ConfLine *cl;
char line[MAX_LINE];
cl = _find_var(section, var);
if (!cl)
cl = _add_var(section, var);
- if (cl->val)
- free(cl->val);
-
_conf_encode(line, MAX_LINE, val);
- cl->val = strdup(line);
+ cl->set_val(line);
return 1;
}
void parse_test(char *line)
{
- struct conf_line cl;
+ ConfLine cl;
int rc;
rc = parse_line(line, &cl);
printf("ret=%d\n", rc);
- printf("pre: '%s'\n", cl.prefix);
- printf("var: '%s'\n", cl.var);
- printf("mid: '%s'\n", cl.mid);
- printf("val: '%s'\n", cl.val);
- printf("suf: '%s'\n", cl.suffix);
- printf("section: '%s'\n", cl.section);
+ printf("pre: '%s'\n", cl.get_prefix());
+ printf("var: '%s'\n", cl.get_var());
+ printf("mid: '%s'\n", cl.get_mid());
+ printf("val: '%s'\n", cl.get_val());
+ printf("suf: '%s'\n", cl.get_suffix());
+ printf("section: '%s'\n", cl.get_section());
}
#include <string>
#include <list>
-typedef std::map<std::string, struct conf_line *> ConfMap;
-typedef std::list<struct conf_line *> ConfList;
+class ConfLine {
+ char *prefix;
+ char *var;
+ char *mid;
+ char *val;
+ char *suffix;
+ char *section;
+
+ void _set(char **dst, const char *val);
+public:
+ ConfLine() : prefix(NULL), var(NULL), mid(NULL), val(NULL),
+ suffix(NULL), section(NULL) {}
+ ~ConfLine();
+
+ void set_prefix(const char *val);
+ void set_var(const char *val);
+ void set_mid(const char *val);
+ void set_val(const char *val);
+ void set_suffix(const char *val);
+ void set_section(const char *val);
+
+ char *get_prefix() { return prefix; }
+ char *get_var() { return var; }
+ char *get_mid() { return mid; }
+ char *get_val() { return val; }
+ char *get_suffix() { return suffix; }
+ char *get_section() { return section; }
+
+ int output(char *line, int max_len);
+};
+
+typedef std::map<std::string, ConfLine *> ConfMap;
+typedef std::list<ConfLine *> ConfList;
class ConfFile;
ConfList conf_list;
ConfMap conf_map;
public:
+ ~ConfSection();
ConfSection(std::string sec_name) : name(sec_name) { }
};
SectionList sections_list;
ConfList global_list;
- struct conf_line *_find_var(const char *section, const char* var);
- struct conf_line *_add_var(const char *section, const char* var);
+ ConfLine *_find_var(const char *section, const char* var);
+ ConfLine *_add_var(const char *section, const char* var);
template<typename T>
int _read(const char *section, const char *var, T *val, T def_val);
int _write(const char *section, const char *var, T val);
public:
ConfFile(char *fname) : filename(strdup(fname)) {}
- ~ConfFile() { free(filename); }
+ ~ConfFile();
int parse();
int read(const char *section, const char *var, int *val, int def_val);