i++;
}
+ i--;
+
while (str[i] && is_delim(str[i], _def_delim)) {
i--;
}
+
+ i++;
}
if (alloc) {
return out;
}
-
+#if 0
static char *str_trim(char *str)
{
char *head = str;
done:
return head;
}
+#endif
/*
* normalizes a var name, removes extra spaces; e.g., 'foo bar' -> 'foo bar'
*/
-static char *normalize_name(char *name)
+static char *normalize_name(const char *name)
{
char *newname, *p;
int i, len;
int last_delim = 0, had_delim = 0, had_non_delim = 0;
+ if (!name)
+ return NULL;
+
len = strlen(name);
newname = (char *)malloc(len + 1);
last_delim = now_delim;
}
+ *p = '\0';
return newname;
}
char *p = NULL;
char *eq;
int ret = 0;
- char *var_name;
memset(parsed, 0, sizeof(ConfLine));
return _parse_section(p, parsed);
}
- parsed->set_var(get_next_tok(p, _def_delim, 1, &p));
+ parsed->set_var(get_next_name(p, 1, &p));
if (!*p)
goto out;
void ConfLine::set_var(const char *val)
{
_set(&var, val);
+
+ if (norm_var) {
+ free(norm_var);
+ norm_var = NULL;
+ }
}
void ConfLine::set_mid(const char *val)
_set(§ion, val);
}
+char *ConfLine::get_norm_var()
+{
+ if (!norm_var)
+ norm_var = normalize_name(var);
+
+ return norm_var;
+}
+
+
ConfLine::~ConfLine()
{
if (prefix)
delete sec;
}
-
- if (fd >= 0)
- close(fd);
}
int ConfLine::output(char *line, int max_len)
}
-int ConfFile::parse()
+int ConfFile::_parse(char *filename, ConfSection **psection)
{
char *buf;
int len, i, l;
char line[MAX_LINE];
ConfLine *cl;
- ConfSection *section;
+ ConfSection *section = *psection;
+ int fd;
- section = new ConfSection("global");
- sections["global"] = section;
- sections_list.push_back(section);
fd = open(filename, O_RDWR);
if (fd < 0)
return 0;
cl = new ConfLine();
parse_line(line, cl);
if (cl->get_var()) {
- section->conf_map[cl->get_var()] = cl;
- global_list.push_back(cl);
- section->conf_list.push_back(cl);
+ if (strcmp(cl->get_var(), "include") == 0) {
+ if (!_parse(cl->get_val(), §ion)) {
+ printf("error parsing %s\n", cl->get_val());
+ }
+ } else {
+ section->conf_map[cl->get_norm_var()] = cl;
+ global_list.push_back(cl);
+ section->conf_list.push_back(cl);
+ }
} else if (cl->get_section()) {
section = _add_section(cl->get_section(), cl);
}
free(buf);
+ *psection = section;
+
return 1;
}
+int ConfFile::parse()
+{
+ ConfSection *section;
+
+ section = new ConfSection("global");
+ sections["global"] = section;
+ sections_list.push_back(section);
+
+ return _parse(filename, §ion);
+}
+
int ConfFile::flush()
{
int rc;
+ int fd;
- if (fd < 0) {
- fd = open(filename, O_RDWR | O_CREAT, 0644);
+ fd = open(filename, O_RDWR | O_CREAT, 0644);
- if (fd < 0) {
- printf("error opening file %s errno=%d\n", filename, errno);
- return 0;
- }
- } else {
- rc = lseek(fd, 0, SEEK_SET);
- if (rc < 0) {
- printf("error seeking file %s errno = %d\n", filename, errno);
- return 0;
- }
+ if (fd < 0) {
+ printf("error opening file %s errno=%d\n", filename, errno);
+ return 0;
}
_dump(fd);
- rc = fsync(fd);
+ rc = close(fd);
if (rc < 0)
return 0;
ConfSection *sec;
ConfMap::iterator cm_iter;
ConfLine *cl;
+ char *norm_var;
if (iter == sections.end() )
goto notfound;
sec = iter->second;
- cm_iter = sec->conf_map.find(var);
+ norm_var = normalize_name(var);
+
+ cm_iter = sec->conf_map.find(norm_var);
+ free(norm_var);
if (cm_iter == sec->conf_map.end())
goto notfound;
char *s;
- s = " hello world = kaka ";
+ s = " hello world= kaka ";
printf("s'%s'\n", s);
s = get_next_name(s, 1, NULL);
cf.read("foo", "lala7", &bval, false);
cf.read("foo", "str", &str_val, "hello world2");
+ cf.read("foo", " two words are good ", &str_val, "hello world5");
printf("read str=%s\n", str_val);
printf("read bool=%d\n", bval);
char *val;
char *suffix;
char *section;
+ char *norm_var;
void _set(char **dst, const char *val);
public:
ConfLine() : prefix(NULL), var(NULL), mid(NULL), val(NULL),
- suffix(NULL), section(NULL) {}
+ suffix(NULL), section(NULL), norm_var(NULL) {}
~ConfLine();
void set_prefix(const char *val);
char *get_val() { return val; }
char *get_suffix() { return suffix; }
char *get_section() { return section; }
+ char *get_norm_var();
int output(char *line, int max_len);
};
typedef std::list<ConfSection *> SectionList;
class ConfFile {
- int fd;
char *filename;
bool auto_update;
ConfSection *_add_section(const char *section, ConfLine *cl);
void _dump(int fd);
+ int _parse(char *filename, ConfSection **psection);
public:
- ConfFile(const char *fname) : fd(-1), filename(strdup(fname)), auto_update(false) {}
+ ConfFile(const char *fname) : filename(strdup(fname)), auto_update(false) {}
~ConfFile();
int parse();