diff options
author | deva <deva> | 2005-04-05 11:22:05 +0000 |
---|---|---|
committer | deva <deva> | 2005-04-05 11:22:05 +0000 |
commit | 55a7afdedc7cee2df9012f600dd51aff92ad1af3 (patch) | |
tree | db6568a6aab7cfdec0d0afcb9bd7566939a4a318 /src/miav_config.cc | |
parent | 6f03a23e7876516d3ba011d23acf454138432654 (diff) |
Added validity checking on the conf parser.
Added error object to conf parser.
Cleaned up the configure.in script
Diffstat (limited to 'src/miav_config.cc')
-rw-r--r-- | src/miav_config.cc | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/src/miav_config.cc b/src/miav_config.cc index 1c2d0c6..8a9307d 100644 --- a/src/miav_config.cc +++ b/src/miav_config.cc @@ -26,15 +26,20 @@ #include <config.h> #include "miav_config.h" -MiavConfig::MiavConfig(char *file) +MiavConfig::MiavConfig(char *file, Error* err) { + error = err; configs = NULL; + + filename = string(file); // Read config file FILE* fp = fopen(file, "r"); if(!fp) { - fprintf(stderr, "Error reading configuration file %s\n", file); + char errbuf[256]; + sprintf(errbuf, "Error reading configuration file %s\n", file); + if(error) error->pushError(errbuf); return; } fseek(fp, 0, SEEK_END); @@ -63,16 +68,39 @@ MiavConfig::~MiavConfig() if(die) free(die); } +/** + * Prints a reasonable error message when a parse error occurres. + */ +_cfg *MiavConfig::parseError(char* msg, char* line) +{ + char errbuf[512]; + sprintf(errbuf, "Error parsing file %s at line:\n\t%s\n\t%s\n", filename.c_str(), line, msg); + if(error) error->pushError(errbuf); + return NULL; +} + /** * Adds one configuration entry, from a single zero terminated line. */ _cfg *MiavConfig::addConfig(_cfg *parent, char* conf) { + // Check for wellformed input: + // Check for = + if(strstr(conf, "=") == 0) return parseError("Missing '='", conf); + /* + if(strstr(conf, "\"")) { + if(strstr(conf, "=") > strstr(conf, "\"")) + return parseError("Missing '=', first occurrence inside string", conf); + } + */ - // - // FIXME: Check for wellformedness - // + // Check for nonempty left side + if(strstr(conf, "=") == conf) return parseError("Empty left side", conf); + // Check for nonempty right side + if(strstr(conf, "=") == conf + strlen(conf) - 1) return parseError("Empty right side.", conf); + + // Parse this wellformed input. _cfg *cfg; cfg = (_cfg*) malloc(sizeof(_cfg)); @@ -86,6 +114,8 @@ _cfg *MiavConfig::addConfig(_cfg *parent, char* conf) char* val = (char*)calloc(vallen + 1, 1); strncpy(val, conf + strlen(conf) - vallen, vallen); + // TODO: Check valid rightside (true, false, number or "..") + cfg->name = new string((const char*)name); free(name); @@ -118,13 +148,12 @@ int MiavConfig::parse(char* raw) for(p = conf; p < conf_end; p++) { if(*p == '\n') { *p = '\0'; - cfg = addConfig(cfg, start); + if(!(cfg = addConfig(cfg, start))) return 1; start = p+1; } } // Allocated in strip free(conf); - printf("done!\n"); return 0; } @@ -186,22 +215,30 @@ char* MiavConfig::strip(char* conf) int MiavConfig::readInt(char *node) { - return findNode(node)->intval; + _cfg* n = findNode(node); + if(n) return n->intval; + else return 0; } bool MiavConfig::readBool(char *node) { - return findNode(node)->boolval; + _cfg* n = findNode(node); + if(n) return n->boolval; + else return false; } string *MiavConfig::readString(char *node) { - return findNode(node)->stringval; + _cfg* n = findNode(node); + if(n) return n->stringval; + else return &emptyString; } float MiavConfig::readFloat(char *node) { - return findNode(node)->floatval; + _cfg* n = findNode(node); + if(n) return n->floatval; + else return 0.0f; } _cfg *MiavConfig::findNode(char* node) @@ -212,6 +249,8 @@ _cfg *MiavConfig::findNode(char* node) if(!strcmp(node, cfg->name->c_str())) return cfg; cfg = cfg->next; } - fprintf(stderr, "ERROR: Request for nonexisting node \"%s\"!\n", node); - exit(1); + char errbuf[256]; + sprintf(errbuf, "Request for nonexisting node \"%s\"!\n", node); + if(error) error->pushError(errbuf); + return NULL; } |