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);
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),
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,
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",
"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",
"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",
"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",
void QKernel::SetOptions()
{
QDialog dlg(this,"Options",TRUE);
- QFile *vlp_file;
ConnectEntry *e;
- char line[256];
unsigned int i;
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;i<connections->count();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;i<connections->count();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);
};
}
#include <stdlib.h>
#include <unistd.h>
+#include <libconfig.h>
class VLPEntry
{
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" );
{
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;i<Nodes.count();i++)
- if (pom!=Nodes.at(i)) {
- sprintf(line,"host=%s\n",Nodes.at(i)->addr);
- 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();
}
void NETMOD::load_config(char *fname)
{
config_t cfg;
- const char *str;
+ config_setting_t *setting;
int on,k=0;
NETlink *pomlink;
/* 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!");
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));