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/decoder.cc | 3 +++ src/decoder.h | 4 ++++ src/encoder.cc | 4 ++++ src/encoder.h | 3 +++ src/mainwindow.cc | 8 ++++++- src/mainwindow.h | 2 ++ src/messagebox.cc | 4 +++- src/messagebox.h | 5 +++++ src/miav.conf | 2 ++ src/miav_config.cc | 65 +++++++++++++++++++++++++++++++++++++++++++----------- src/miav_config.h | 8 ++++++- 11 files changed, 92 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/decoder.cc b/src/decoder.cc index bf9b75c..c7a4e50 100644 --- a/src/decoder.cc +++ b/src/decoder.cc @@ -21,6 +21,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include +#ifdef USE_GUI #include "decoder.h" @@ -179,3 +180,5 @@ void Decoder::decode() void Decoder::run() { decode(); } + +#endif /*USE_GUI*/ diff --git a/src/decoder.h b/src/decoder.h index 846ac26..fcf61cf 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -20,6 +20,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include +#ifdef USE_GUI + #ifndef __RTVIDEOREC_DECODER_H #define __RTVIDEOREC_DECODER_H @@ -68,3 +70,5 @@ public: }; #endif + +#endif/*USE_GUI*/ diff --git a/src/encoder.cc b/src/encoder.cc index fc19921..3a4ade2 100644 --- a/src/encoder.cc +++ b/src/encoder.cc @@ -22,6 +22,8 @@ */ #include +#ifdef USE_GUI + #include "encoder.h" Encoder::Encoder(Error* err, @@ -154,3 +156,5 @@ void Encoder::stop() { n = NULL; } } + +#endif /*USE_GUI*/ diff --git a/src/encoder.h b/src/encoder.h index 8a458c8..554839c 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -20,6 +20,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include +#ifdef USE_GUI + #ifndef __RTVIDEOREC_ENCODER_H #define __RTVIDEOREC_ENCODER_H @@ -96,3 +98,4 @@ private: #endif +#endif /*USE_GUI*/ diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 5da340b..8bfc53e 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -50,9 +50,15 @@ MainWindow::MainWindow( QWidget* parent, const char* name ) : QWidget( parent, name, WStyle_Customize | WStyle_NoBorder ) { - MiavConfig cfg("miav.conf"); + error = new Error(); + MiavConfig cfg("miav.conf", error); int resolution_w = cfg.readInt("pixel_width"); int resolution_h = cfg.readInt("pixel_height"); + while(error->hasError()) { + show(); + MessageBox(this, TXT_ERROR_TITLE, error->popErrorString().c_str(), + TYPE_OK, ICON_ERROR).exec(); + } unit = ((float)resolution_w / (float)(cfg.readFloat("screensize") * 3.1f)); move(0,0); diff --git a/src/mainwindow.h b/src/mainwindow.h index 18f616d..25a938b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -82,6 +82,8 @@ public slots: void freeze_clicked(); private: + Error *error; + void checkErrors(); void createGui(); diff --git a/src/messagebox.cc b/src/messagebox.cc index fe5420f..63bbeee 100644 --- a/src/messagebox.cc +++ b/src/messagebox.cc @@ -22,7 +22,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - +#include +#ifdef USE_GUI #include "messagebox.h" //////////////////////////////////////////////////////////////////////////////////////// @@ -184,3 +185,4 @@ void MessageBox::bno_clicked() { done(MSG_NO); } +#endif/*USE_GUI*/ diff --git a/src/messagebox.h b/src/messagebox.h index 5cb2bd8..313d395 100644 --- a/src/messagebox.h +++ b/src/messagebox.h @@ -23,6 +23,9 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include +#ifdef USE_GUI + #ifndef __MIAV_MESSAGEBOX_H__ #define __MIAV_MESSAGEBOX_H__ @@ -98,3 +101,5 @@ private: }; #endif/*__MIAV_MESSAGEBOX_H__*/ + +#endif /*USE_GUI*/ diff --git a/src/miav.conf b/src/miav.conf index 3fab263..ba2ceda 100644 --- a/src/miav.conf +++ b/src/miav.conf @@ -3,6 +3,8 @@ # # Cpr Database configuration +fisk = + = true cpr_host = "cpr.j.auh.dk" cpr_port = 10301 cpr_timeout = 10000 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; } diff --git a/src/miav_config.h b/src/miav_config.h index 1ca6e04..45eab0c 100644 --- a/src/miav_config.h +++ b/src/miav_config.h @@ -30,6 +30,8 @@ #include using namespace std; +#include "error.h" + typedef struct __cfg { string *name; bool boolval; @@ -41,7 +43,7 @@ typedef struct __cfg { class MiavConfig { public: - MiavConfig(char *file); + MiavConfig(char *file, Error* err = NULL); ~MiavConfig(); int readInt(char *node); @@ -50,9 +52,13 @@ public: float readFloat(char *node); private: + string emptyString; + Error* error; + string filename; _cfg *addConfig(_cfg *parent, char* conf); int parse(char* conf); char *strip(char* conf); + _cfg *parseError(char* msg, char* line); _cfg *findNode(char* node); _cfg *configs; }; -- cgit v1.2.3