From 68a144514f672cf981ed70abc23df818178ba16f Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Wed, 21 Jan 2009 17:09:04 -0800 Subject: [PATCH] confutils: going forward --- src/common/ConfUtils.cc | 202 ++++++++++++++++++++++++++++------------ src/common/ConfUtils.h | 26 +++++- 2 files changed, 164 insertions(+), 64 deletions(-) diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index 47e1f7406f467..8dae3d13fa94c 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -34,6 +34,7 @@ struct conf_line { static const char *_def_delim=" \t\n\r"; static const char *_eq_delim="= \t\n\r"; +static const char *_eol_delim="\n\r"; static const char *_pr_delim="[] \t\n\r"; @@ -53,13 +54,24 @@ char *get_next_tok(char *str, const char *delim, int alloc, char **p) char *tok; int i=0; char *out; + int is_str = 0; while (*str && is_delim(*str, delim)) { str++; } - while (str[i] && !is_delim(str[i], delim)) { - i++; + if (*str == '"') { + while (str[i] && !is_delim(str[i], _eol_delim)) { + i++; + if (str[i] == '"') { + i++; + break; + } + } + } else { + while (str[i] && !is_delim(str[i], delim)) { + i++; + } } if (alloc) { @@ -214,52 +226,83 @@ static int _str_cat(char *str1, int max, char *str2) void ConfFile::dump() { -#if 0 - std::list::iterator iter, end; - struct conf_line *cl; + SectionList::iterator sec_iter, sec_end; + struct conf_line *cl; char line[MAX_LINE]; int len = 0; char *p; - end=list.end(); - - for (iter=list.begin(); iter != end; ++iter) { - cl = *iter; - p = line; - len = 0; - - 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); - printf("line=%s\n", line); + sec_end=sections_list.end(); + + printf("------ config starts here ------\n"); + + for (sec_iter=sections_list.begin(); sec_iter != sec_end; ++sec_iter) { + ConfList::iterator iter, end; + ConfSection *sec; + sec = *sec_iter; + + end = sec->conf_list.end(); + + for (iter = sec->conf_list.begin(); iter != end; ++iter) { + cl = *iter; + p = line; + len = 0; + + 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); + printf("%s\n", line); + } } } -#endif - ConfMap *cur_map; - ConfMap::iterator map_iter, map_end; - SectionMap::iterator sec_iter, sec_end; + printf("------ config ends here ------\n"); +#if 0 + ConfUnsortedMap *cur_map; + ConfUnsortedMap::iterator map_iter, map_end; + SectionUnsortedMap::iterator sec_iter, sec_end; + struct conf_line *cl; + char line[MAX_LINE]; + int len = 0; - sec_end = sections.end(); + sec_end = unsorted_sections.end(); - for (sec_iter = sections.begin(); sec_iter != sec_end; ++sec_iter) { + for (sec_iter = unsorted_sections.begin(); sec_iter != sec_end; ++sec_iter) { + printf("section %s\n", (char *)sec_iter->first.c_str()); cur_map = sec_iter->second; map_end = cur_map->end(); for (map_iter = cur_map->begin(); map_iter != map_end; ++map_iter) { - + cl = map_iter->second; +/* p = line; */ + len = 0; + + 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); + printf("line=%s\n", line); + } } } - +#endif } int ConfFile::parse() @@ -268,12 +311,11 @@ int ConfFile::parse() int len, i, l, map_index; char line[MAX_LINE]; struct conf_line *cl; - ConfMap *cur_map; - ConfUnsortedMap *cur_map; + ConfSection *section; - cur_map = new ConfMap; - cur_unsorted_map = new ConfMap; - sections["global"] = cur_map; + section = new ConfSection("global"); + sections["global"] = section; + sections_list.push_back(section); #define BUF_SIZE 4096 fd = open(filename, O_RDWR); if (fd < 0) @@ -294,18 +336,18 @@ int ConfFile::parse() line[l] = '\0'; cl = (struct conf_line *)malloc(sizeof(struct conf_line)); parse_line(line, cl); - list.push_back(cl); if (cl->var) { - (*cur_map)[cl->var] = cl; - (*cur_unsorted_map)[map_index] = cl; + section->conf_map[cl->var] = cl; printf("cl->var <---- '%s'\n", cl->var); } else if (cl->section) { printf("cur_map <---- '%s'\n", cl->section); map_index = 0; - cur_map = new ConfMap; - cur_unsorted_map = new ConfUnsortedMap; - sections[cl->section] = cur_map; + section = new ConfSection(cl->section); + sections[cl->section] = section; + sections_list.push_back(section); } + global_list.push_back(cl); + section->conf_list.push_back(cl); l = 0; break; default: @@ -319,18 +361,18 @@ int ConfFile::parse() struct conf_line *ConfFile::_find_var(char *section, char* var) { - std::map::iterator iter = sections.find(section); - ConfMap *map; + SectionMap::iterator iter = sections.find(section); + ConfSection *sec; ConfMap::iterator cm_iter; struct conf_line *cl; if (iter == sections.end() ) goto notfound; - map = iter->second; - cm_iter = map->find(var); + sec = iter->second; + cm_iter = sec->conf_map.find(var); - if (cm_iter == map->end()) + if (cm_iter == sec->conf_map.end()) goto notfound; cl = cm_iter->second; @@ -340,6 +382,33 @@ notfound: return 0; } +struct conf_line *ConfFile::_add_var(char *section, char* var) +{ + SectionMap::iterator iter = sections.find(section); + ConfSection *sec; + ConfMap::iterator cm_iter; + struct conf_line *cl; + + if (iter == sections.end() ) { + sec = new ConfSection(section); + sections[section] = sec; + } else { + sec = iter->second; + } + + cl = (struct conf_line *)malloc(sizeof(struct conf_line)); + memset(cl, 0, sizeof(struct conf_line)); + + cl->prefix = strdup("\t"); + cl->var = strdup(var); + cl->mid = strdup(" = "); + + sec->conf_map[var] = cl; + sec->conf_list.push_back(cl); + + return cl; +} + int ConfFile::read_int(char *section, char *var, int *val, int def_val) { struct conf_line *cl; @@ -362,18 +431,16 @@ int ConfFile::write_int(char *section, char *var, int val) char line[MAX_LINE]; cl = _find_var(section, var); - if (!cl || !cl->val) - goto notfound; + if (!cl) + cl = _add_var(section, var); - free(cl->val); + if (cl->val) + free(cl->val); sprintf(line, "%d", val); cl->val = strdup(line); - return 1; -notfound: - return 0; } int ConfFile::read_bool(char *section, char *var, bool *val, bool def_val) @@ -417,17 +484,28 @@ notfound: int ConfFile::read_str(char *section, char *var, char *val, int size, char *def_val) { struct conf_line *cl; + char *use_val; + int ret = 0; cl = _find_var(section, var); - if (!cl || !cl->val) - goto notfound; + if (!cl || !cl->val) { + use_val = def_val; + } else { + ret = 1; + use_val = cl->val; + } + + if (*use_val == '"') { + use_val++; + for (size = strlen(use_val)-1; size > 0; size--) { + if (use_val[size] == '"') + break; + } + } - strncpy(val, cl->val, size); + strncpy(val, use_val, size); return 1; -notfound: - strncpy(val, def_val, size); - return 0; } int ConfFile::read_str_alloc(char *section, char *var, char **val, char *def_val) @@ -469,6 +547,8 @@ int main(int argc, char *argv[]) cf.parse(); cf.dump(); cf.read_int("core", "repositoryformatversion", &val, 12); + cf.write_int("core", "lola", 15); + cf.dump(); printf("read val=%d\n", val); diff --git a/src/common/ConfUtils.h b/src/common/ConfUtils.h index fa891d8a8eac9..ac6d584fa551c 100644 --- a/src/common/ConfUtils.h +++ b/src/common/ConfUtils.h @@ -1,21 +1,41 @@ #ifndef __CONFUTILS_H #define __CONFUTILS_H + +#include #include #include #include typedef std::map ConfMap; -typedef std::map ConfUnsortedMap; -typedef std::map SectionMap; +typedef std::list ConfList; + +class ConfFile; + +class ConfSection +{ + friend class ConfFile; + + std::string name; + ConfList conf_list; + ConfMap conf_map; +public: + ConfSection(std::string sec_name) : name(sec_name) { } +}; + +typedef std::map SectionMap; +typedef std::list SectionList; class ConfFile { int fd; char *filename; + SectionMap sections; - std::list list; + SectionList sections_list; + ConfList global_list; struct conf_line *_find_var(char *section, char* var); + struct conf_line *_add_var(char *section, char* var); public: ConfFile(char *fname) : filename(strdup(fname)) {} ~ConfFile() { free(filename); } -- 2.39.5