From fed5c1dbf6a6c01719efc686a3d966f810ab0ade Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 24 Mar 2005 16:47:23 +0000 Subject: Made the configurationfile parser --- src/cprquerydialog.cc | 24 ++------- src/cprquerydialog.h | 3 +- src/mainwindow.cc | 12 +---- src/messagebox.cc | 8 --- src/miav.conf | 12 +++++ src/miav_config.cc | 144 +++++++++++++++++++++++++++++++++++++++++++------- src/miav_config.h | 3 ++ 7 files changed, 146 insertions(+), 60 deletions(-) create mode 100644 src/miav.conf (limited to 'src') diff --git a/src/cprquerydialog.cc b/src/cprquerydialog.cc index 23f508c..8ae2c60 100644 --- a/src/cprquerydialog.cc +++ b/src/cprquerydialog.cc @@ -34,9 +34,8 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, QLabel *lname, QWidget *parent, - const char *name, - bool modal) - : QDialog( parent, name, modal) + const char *name) + : QDialog(parent, name, TRUE) { MiavConfig cfg("cprquery.conf"); @@ -53,28 +52,11 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, cprSocket = new QSocket(this); connect(cprSocket, SIGNAL(readyRead()), SLOT(cprSocket_readyRead())); connect(cprSocket, SIGNAL(connected()), SLOT(cprSocket_connected())); - /* - - // connect(btn_cpr, SIGNAL(clicked()), SLOT(bcancel_clicked())); - - //show(); - //run(0); - */ lbl_cpr->setText("Indtast CPR"); // Generate input buttons - //digits = pos; - - QHBoxLayout *bl = new QHBoxLayout(this); - /* - QFrame *topf = new QFrame(this); - topf->setFrameStyle(QFrame::Box | QFrame::Raised); - topf->setLineWidth(3); - bl->addWidget(topf); - */ QGridLayout *gl = new QGridLayout(this, 4, 3, 10, 2); - bl->addLayout(gl); QButton *b1 = createButton(this, "1", 1); QButton *b2 = createButton(this, "2", 2); @@ -102,7 +84,7 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, gl->addWidget(bbs, 3,2); gl->addWidget(bca, 3,0); - /* Setup signals */ + // Setup signals connect(b1, SIGNAL(clicked()), SLOT(b_1_clicked())); connect(b2, SIGNAL(clicked()), SLOT(b_2_clicked())); connect(b3, SIGNAL(clicked()), SLOT(b_3_clicked())); diff --git a/src/cprquerydialog.h b/src/cprquerydialog.h index 303d69a..992e66a 100644 --- a/src/cprquerydialog.h +++ b/src/cprquerydialog.h @@ -75,8 +75,7 @@ public: CPRQueryDialog(QLabel *lcpr, QLabel *lname, QWidget * parent = 0, - const char * name = 0, - bool modal = FALSE); + const char * name = 0); ~CPRQueryDialog(); public slots: diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 77b8d94..2abfbc8 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -55,14 +55,8 @@ MainWindow::MainWindow( QWidget* parent, const char* name ) // statusBar()->setSizeGripEnabled(false); MiavConfig cfg("miav.conf"); - /** / - int resolution_w = 1280; // TODO: Read actual resolution - int resolution_h = 1024; // TODO: Read actual resolution - /**/ - /**/ - int resolution_w = 1024; // TODO: Read actual resolution - int resolution_h = 768; // TODO: Read actual resolution - /**/ + int resolution_w = cfg.readInt("pixel_width"); + int resolution_h = cfg.readInt("pixel_height"); unit = ((float)resolution_w / (float)(cfg.readFloat("screensize") * 3.1f)); move(0,0); @@ -103,8 +97,6 @@ MainWindow::MainWindow( QWidget* parent, const char* name ) aboutwindow = NULL; - // CPRQueryDialog dlg(lbl_cpr, lbl_name, this, "CPRQueryDialog"); - // dlg.exec(); // Open the CPR Dialog cpr_clicked(); } diff --git a/src/messagebox.cc b/src/messagebox.cc index a636bd7..fe255ae 100644 --- a/src/messagebox.cc +++ b/src/messagebox.cc @@ -64,19 +64,15 @@ MessageBox::MessageBox(QWidget* parent, { switch(type) { case TYPE_OK: - printf("using info icon!\n"); pix_icon->load( "info.png" ); break; case TYPE_OK_CANCEL: - printf("using warning icon!\n"); pix_icon->load( "warning.png" ); break; case TYPE_YES_NO: - printf("using question icon!\n"); pix_icon->load( "question.png" ); break; case TYPE_YES_NO_CANCEL: - printf("using question icon!\n"); pix_icon->load( "question.png" ); break; } @@ -84,25 +80,21 @@ MessageBox::MessageBox(QWidget* parent, } case ICON_INFO: // An info icon (matching the ok button) { - printf("using info icon!\n"); pix_icon->load( "info.png" ); break; } case ICON_WARN: // An warning icon (matching the ok/cancel button) { - printf("using warning icon!\n"); pix_icon->load( "warning.png" ); break; } case ICON_ERROR: // An critical error icon { - printf("using error icon!\n"); pix_icon->load( "error.png" ); break; } case ICON_QUESTION:// An question icon (matching the yes/no and yes/no/cancel buttons) { - printf("using question icon!\n"); pix_icon->load( "question.png" ); break; } diff --git a/src/miav.conf b/src/miav.conf new file mode 100644 index 0000000..214755f --- /dev/null +++ b/src/miav.conf @@ -0,0 +1,12 @@ +# +# The MIaV configuration file +# + +# Cpr Database configuration +cpr_host = "cpr.j.auh.dk" +cpr_port = 10301 + +# Size of the screen in inches +screensize = 19.0 +pixel_width = 1024 +pixel_height = 768 diff --git a/src/miav_config.cc b/src/miav_config.cc index 2af0ef1..9364131 100644 --- a/src/miav_config.cc +++ b/src/miav_config.cc @@ -30,27 +30,24 @@ MiavConfig::MiavConfig(char *file) { configs = NULL; - _cfg *cfg; - - // TODO: Read config from file. - - // Add a config - configs = cfg = (_cfg*) malloc(sizeof(_cfg)); - cfg->name = new string("cpr_host"); - cfg->stringval = new string("cpr.j.auh.dk"); - cfg->next = (_cfg*) malloc(sizeof(_cfg)); + // Read config file + FILE* fp = fopen(file, "r"); + + if(!fp) { + fprintf(stderr, "Error reading configuration file %s\n", file); + return; + } + fseek(fp, 0, SEEK_END); + int fsz = ftell(fp); + fseek(fp, 0, SEEK_SET); + + char *raw = (char*)calloc(fsz, 1); + fread(raw, 1, fsz, fp); - // Add another config - cfg = cfg->next; - cfg->name = new string("cpr_port"); - cfg->intval = 10301; - cfg->next = (_cfg*) malloc(sizeof(_cfg)); + fclose(fp); - // Add another config - cfg = cfg->next; - cfg->name = new string("screensize"); - cfg->floatval = 19.0f; - cfg->next = NULL; + parse(raw); + free(raw); } MiavConfig::~MiavConfig() @@ -66,6 +63,115 @@ MiavConfig::~MiavConfig() if(die) free(die); } +_cfg *MiavConfig::addConfig(_cfg *parent, char* conf) +{ + _cfg *cfg; + + printf("[%s]\n", conf); + + cfg = (_cfg*) malloc(sizeof(_cfg)); + if(!parent) configs = cfg; + + int namelen = strchr(conf, '=') - conf; + char* name = (char*)calloc(namelen + 1, 1); + strncpy(name, conf, namelen); + printf("name [%s]#%d - ", name, namelen); + + int vallen = conf + strlen(conf) - (strchr(conf, '=') + 1); + char* val = (char*)calloc(vallen + 1, 1); + strncpy(val, conf + strlen(conf) - vallen, vallen); + printf("val [%s]#%d\n", val, vallen); + + cfg->name = new string((const char*)name); + free(name); + + cfg->stringval = new string((const char*)val); + cfg->intval = atoi(val); + cfg->floatval = atof(val); + cfg->boolval = atoi(val) != 0; + free(val); + + cfg->next = NULL; + + if(parent) parent->next = cfg; + return cfg; +} + +int MiavConfig::parse(char* raw) +{ + // Strip the string + char *conf = strip(raw); + char *conf_end = conf + strlen(conf); + char *start = conf; + char *p; + + _cfg *cfg = NULL; + + // Iterate the lines in the string + for(p = conf; p < conf_end; p++) { + if(*p == '\n') { + *p = '\0'; + cfg = addConfig(cfg, start); + start = p+1; + } + } + free(conf); + printf("done!\n"); + return 0; +} + +char* MiavConfig::strip(char* conf) +{ + char *stripped = (char*)calloc(strlen(conf) + 2, 1); + char *r; + char *w = stripped; + + bool instring = false; + bool incomment = false; + + // Iterate over the characters in the input string. + for(r = conf; r < conf + strlen(conf); r++) { + if(strchr("#", *r)) incomment = true; + if(strchr("\n", *r)) incomment = false; + + if(!incomment) { + if(instring) { + // When in a string, accept anything, except ". + if(*r != '\"') { + *w = *r; + w++; + } + } else { + // Only copy valid characters + if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_,.=", *r)) { + // Change comma into dot + if(*r == ',') *r = '.'; + *w = *r; + w++; + } + // We don't want double newlines and initial newline! + if((*r == '\n') && (*(w-1) != '\n') && (w != stripped)) { + *w = *r; + w++; + } + } + } + + if(strchr("\"", *r)) instring = !instring; + } + + // If we are not ending on a newline, we better append one + if(*(w-1) != '\n') { + *w = '\n'; + w++; + } + + // Make sure we are nullterminated. + *w = '\0'; + + return stripped; +} + int MiavConfig::readInt(char *node) { return findNode(node)->intval; diff --git a/src/miav_config.h b/src/miav_config.h index 4b062e0..1ca6e04 100644 --- a/src/miav_config.h +++ b/src/miav_config.h @@ -50,6 +50,9 @@ public: float readFloat(char *node); private: + _cfg *addConfig(_cfg *parent, char* conf); + int parse(char* conf); + char *strip(char* conf); _cfg *findNode(char* node); _cfg *configs; }; -- cgit v1.2.3