From 31d49cd787312431117965de1b91de922a842d22 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Thu, 11 Jul 2013 10:36:46 +0200 Subject: [PATCH] Fixes: vlp-17. Using libconfig in lgconfig. Changed host config variable to array type. Changes in kernel and lognet so hosts array type will be read correctly. --- configure.ac | 2 +- src/kernel/kernel.cpp | 103 +++++++++++++++++++++++++------------- src/lgconfig/lgconfig.cpp | 70 +++++++++++++++++++------- src/net/lognet.cpp | 45 +++++++++++------ 4 files changed, 149 insertions(+), 71 deletions(-) diff --git a/configure.ac b/configure.ac index 49dc6ee..e7397f6 100644 --- a/configure.ac +++ b/configure.ac @@ -16,7 +16,7 @@ PKG_CHECK_MODULES([bin_lognet], [qt-mt < 4.0 libconfig]) PKG_CHECK_MODULES([bin_logker], [qt-mt < 4.0 libconfig]) #PKG_CHECK_MODULES([bin_logint], [qt-mt < 4.0]) PKG_CHECK_MODULES([bin_logedit], [qt-mt < 4.0]) -PKG_CHECK_MODULES([bin_lgconfig], [qt-mt < 4.0]) +PKG_CHECK_MODULES([bin_lgconfig], [qt-mt < 4.0 libconfig]) PKG_CHECK_MODULES([bin_logcomp], [qt-mt < 4.0]) PKG_CHECK_MODULES([bin_loghelp], [qt-mt < 4.0]) diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 3126480..e747d1e 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -294,10 +294,11 @@ void QKernel::n_impl() void QKernel::LoadConfig(char * fname) { config_t cfg; + config_setting_t *setting; const char *str; /* Hack for checking if file exists without using external libs.*/ - FILE * file = fopen(fname, "r"); + FILE * file = fopen(fname, "rt"); if (!file) { fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname); exit(3); @@ -307,7 +308,7 @@ void QKernel::LoadConfig(char * fname) config_init(&cfg); /* Read the file. If there is an error, report it and exit. */ - if(!config_read(&cfg, file)) + if(!config_read(&cfg, file)) { fprintf(stderr, "%s! In file %s, line %d\n", config_error_text(&cfg), @@ -318,8 +319,11 @@ void QKernel::LoadConfig(char * fname) exit(3);/* from original code. */ } - if(!config_lookup_int(&cfg, "node_number", &NodeNumber)) - { + setting = config_lookup(&cfg, "node_number"); + if(setting) { + NodeNumber = config_setting_get_int(setting); + } + else { fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, @@ -329,8 +333,9 @@ void QKernel::LoadConfig(char * fname) exit(3); } - if(config_lookup_string(&cfg, "type", &str)){ - ConType = (strcmp(str, "explicit") == 0) ? 1 : 2; + setting = config_lookup(&cfg, "type"); + if(setting) { + ConType = (strcmp(config_setting_get_string(setting), "explicit") == 0) ? 1 : 2; } else { fprintf(stderr, "%s! In file %s, '%s' was not found.\n", @@ -339,9 +344,23 @@ void QKernel::LoadConfig(char * fname) "type"); } - - if(config_lookup_string(&cfg, "host", &str)) { - ConnectList.append(new ConnectEntry((char*)str)); + setting = config_lookup(&cfg, "host"); + if(setting) { + switch(config_setting_type(setting)) { + case CONFIG_TYPE_STRING:/* TODO: Deprecated. Made for back compatibility. */ + ConnectList.append(new ConnectEntry((char*)config_setting_get_string(setting))); + break; + case CONFIG_TYPE_ARRAY: + for (int i = 0; i < config_setting_length(setting); i++) { + ConnectList.append(new ConnectEntry((char*)config_setting_get_string_elem(setting, i))); + } + break; + default: + fprintf(stderr, "%s! In file %s, bad entry type for %s. Will not be read.\n", + "Error", + fname, + "host"); + } } else { fprintf(stderr, "%s! In file %s, '%s' was not found.\n", @@ -350,8 +369,9 @@ void QKernel::LoadConfig(char * fname) "host"); } - if(config_lookup_string(&cfg, "progdir", &str)){ - strncpy(progdir, str, 256); + setting = config_lookup(&cfg, "progdir"); + if(setting){ + strncpy(progdir, config_setting_get_string(setting), 256); } else { fprintf(stderr, "%s! In file %s, '%s' was not found.\n", @@ -360,8 +380,9 @@ void QKernel::LoadConfig(char * fname) "progdir"); } - if(config_lookup_string(&cfg, "homedir", &str)){ - strncpy(HomeDir, str, 255); + setting = config_lookup(&cfg, "homedir"); + if(setting) { + strncpy(HomeDir, config_setting_get_string(setting), 255); } else { fprintf(stderr, "%s! In file %s, '%s' was not found.\n", @@ -892,9 +913,7 @@ void QKernel::SetMessages() void QKernel::SetOptions() { QDialog dlg(this,"Options",TRUE); - QFile *vlp_file; ConnectEntry *e; - char line[256]; unsigned int i; @@ -982,31 +1001,43 @@ void QKernel::SetOptions() dlg.resize( 400, 310 ); if (dlg.exec()) { - unlink("vlp.cfg"); - vlp_file = new QFile("vlp.cfg"); - vlp_file->open(IO_WriteOnly); - sprintf(line,"progdir=%s\n",progs->text().ascii()); - vlp_file->writeBlock(line,strlen(line)); - strcpy(progdir,progs->text()); - sprintf(line,"node_number=%d\n",atoi(nn->text())); - vlp_file->writeBlock(line,strlen(line)); + + config_t cfg; + config_setting_t *root, *setting; + config_init(&cfg); + + root = config_root_setting(&cfg); + + setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING); + config_setting_set_string(setting, progs->text().ascii()); + strcpy(progdir,progs->text()); + + setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT); + config_setting_set_int(setting, atoi(nn->text())); + + setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING); + config_setting_set_string(setting, HomeDir); + + setting = config_setting_add(root, "type", CONFIG_TYPE_STRING); if ( exp->isChecked()) { - sprintf(line,"type=explicit\n"); - vlp_file->writeBlock(line,strlen(line)); - for(i=0;icount();i++) - { - sprintf(line,"host=%s\n",connections->text(i).ascii()); - vlp_file->writeBlock(line,strlen(line)); - } + config_setting_set_string(setting, "explicit"); + + config_setting_t *hosts = NULL; + hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY); + for(i=0;icount();i++) { + setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING); + config_setting_set_string(setting, connections->text(i).ascii()); + } + } + else { + config_setting_set_string(setting, "register"); + } + if(!config_write_file(&cfg, "vlp.cfg")) { + fprintf(stderr, "Error while writing to file: %s.\n", "vlp.cfg"); } - else - { - sprintf(line,"type=register\n"); - vlp_file->writeBlock(line,strlen(line)); - } - vlp_file->close(); + config_destroy(&cfg); }; } diff --git a/src/lgconfig/lgconfig.cpp b/src/lgconfig/lgconfig.cpp index 4d599dc..c46d5a3 100644 --- a/src/lgconfig/lgconfig.cpp +++ b/src/lgconfig/lgconfig.cpp @@ -23,6 +23,7 @@ #include #include +#include class VLPEntry { @@ -239,9 +240,8 @@ void QInstall::SetOptions() QLabel* tmpQLabel; QPushButton *addbtn,*delbtn,*okbtn,*cancelbtn; VLPEntry *pom; - QFile *vlp_file; int i,j; - char pomstr[255],line[255]; + char pomstr[255]; //dlg.setStyle(WindowsStyle); nodelist = new QListBox( &dlg, "ListBox_1" ); @@ -278,25 +278,57 @@ void QInstall::SetOptions() { j = Nodes.at(); sprintf(pomstr,"%s.cfg",pom->addr); - vlp_file = new QFile(pomstr); - vlp_file->open(IO_WriteOnly); - sprintf(line,"progdir=%s\n",pom->progdir); - vlp_file->writeBlock(line,strlen(line)); - sprintf(line,"homedir=%s\n",pom->homedir); - vlp_file->writeBlock(line,strlen(line)); - sprintf(line,"node_number=%d\n",pom->ID); - vlp_file->writeBlock(line,strlen(line)); - if ( pom->type == 0) - { - sprintf(line,"type=explicit\n"); - vlp_file->writeBlock(line,strlen(line)); + + config_t cfg; + config_setting_t *root, *setting; + config_init(&cfg); + + root = config_root_setting(&cfg); + + setting = config_setting_get_member(root, "progdir"); + if(!setting) { + setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING); + } + config_setting_set_string(setting, pom->progdir); + + setting = config_setting_get_member(root, "homedir"); + if(!setting) { + setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING); + } + config_setting_set_string(setting, pom->homedir); + + setting = config_setting_get_member(root, "node_number"); + if(!setting) { + setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT); + } + config_setting_set_int(setting, pom->ID); + + if (pom->type == 0) + { + setting = config_setting_get_member(root, "type"); + if(!setting) { + setting = config_setting_add(root, "type", CONFIG_TYPE_STRING); } + config_setting_set_string(setting, "explicit"); + } for(i=0;iaddr); - vlp_file->writeBlock(line,strlen(line)); - }; - vlp_file->close(); + { + if (pom!=Nodes.at(i)) + { + setting = config_setting_get_member(root, "host"); + if(!setting) { + setting = config_setting_add(root, "host", CONFIG_TYPE_STRING); + } + config_setting_set_string(setting, Nodes.at(i)->addr); + }; + } + + if(!config_write_file(&cfg, pomstr)) + { + fprintf(stderr, "Error while writing to file: %s.\n", pomstr); + } + config_destroy(&cfg); + pom = Nodes.at(j); pom = Nodes.next(); } diff --git a/src/net/lognet.cpp b/src/net/lognet.cpp index dc2a750..088ef9f 100644 --- a/src/net/lognet.cpp +++ b/src/net/lognet.cpp @@ -186,7 +186,7 @@ NETMOD::NETMOD(char *kernel_name) void NETMOD::load_config(char *fname) { config_t cfg; - const char *str; + config_setting_t *setting; int on,k=0; NETlink *pomlink; @@ -194,7 +194,7 @@ void NETMOD::load_config(char *fname) /* Hack for checking if file exists without using external libs.*/ - FILE * file = fopen(fname, "r"); + FILE * file = fopen(fname, "rt"); if (!file) { fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname); write_at_console("Cannot load configuration file!"); @@ -214,30 +214,45 @@ void NETMOD::load_config(char *fname) exit(3);/* from original code. */ } - if(config_lookup_int(&cfg, "node_number", &MyNode)) + setting = config_lookup(&cfg, "node_number"); + if(setting) { - if (MyNode==-1) { - write_at_console("Node number must be specified"); - config_destroy(&cfg); - fclose(file); - exit(1); - }; + MyNode = config_setting_get_int(setting); } - else + /* else */ + if (!setting || MyNode==-1) { fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", - fname, - "node_number"); + "Error", + fname, + "node_number"); + write_at_console("Node number must be specified"); config_destroy(&cfg); fclose(file); exit(1); } - if(config_lookup_string(&cfg, "host", &str)) { + setting = config_lookup(&cfg, "host"); + if(setting) { k++; pomlink = new NETlink; - strcpy(pomlink->addr, str); + + switch(config_setting_type(setting)) { + case CONFIG_TYPE_STRING:/* TODO: Deprecated. Made for back compatibility. */ + strncpy(pomlink->addr, config_setting_get_string(setting), 255); + break; + case CONFIG_TYPE_ARRAY: + strncpy(pomlink->addr, config_setting_get_string_elem(setting, 0), 255); + break; + default: + fprintf(stderr, "%s! In file %s, bad entry type for %s. Will not be read.\n" + "Fatal error", + fname, + "host"); + config_destroy(&cfg); + fclose(file); + exit(1); + } pomlink->connected = FALSE; pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0)); -- 2.30.2