From 55a7afdedc7cee2df9012f600dd51aff92ad1af3 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 5 Apr 2005 11:22:05 +0000 Subject: Added validity checking on the conf parser. Added error object to conf parser. Cleaned up the configure.in script --- src/miav_config.cc | 65 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 13 deletions(-) (limited to 'src/miav_config.cc') 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 #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; } -- cgit v1.2.3