Fixes vlp-12.
[vlp.git] / src / net / lognet.cpp
index dda6733d9a023de3faf72fb019c5e2fdcec3f8db..fce3aee3f843a0576fb00a0f8fdf271d032c299c 100644 (file)
@@ -22,6 +22,9 @@
 #include <qstringlist.h>
 #include <unistd.h>
 
+#include <libconfig.h>
+#include "AppConfiguration.h"
+
 #define REMOTE_PATH "REMOTE"
 #define MAXLINKS 30
 #define LOGPORT 3600
@@ -183,44 +186,66 @@ NETMOD::NETMOD(char *kernel_name)
 
 void NETMOD::load_config(char *fname)
 {
- QFile f(fname);
- QString line;
- QString val;
- int br=0,on,k=0;
- NETlink *pomlink;
+  config_t cfg;
+  const char *str;
+  int on,k=0;
+  NETlink *pomlink;
 
- if (!f.exists())
- {
-  write_at_console("Cannot load configuration file!");sleep(2);exit(3);
- }
- f.open(IO_ReadOnly);
- br = f.readLine(line,256);
- while (br>0)
- {
-  QStringList l = QStringList::split("=",line,FALSE);
-  QStringList::Iterator it = l.begin();
-  line = *it;
-  ++it;
-  val = *it;
-  val = val.stripWhiteSpace();
-  if (line == "node_number") {MyNode = val.toInt();};
-  if (line == "host" ) {
-     k++;
-     pomlink = new NETlink;
-     strcpy(pomlink->addr,val.ascii());
-     pomlink->connected = FALSE;
-     pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); 
-     fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0));
-     on=1; 
-     setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on)); 
-     Links.append(pomlink); 
-     to_connect++;     
-  };
-  br = f.readLine(line,256);
- }
- f.close();
- if (k==0) all_connected=TRUE;
- if (MyNode==-1) {write_at_console("Node number must be specified");exit(1);};
+  config_init(&cfg);
+  
+  
+  /* Hack for checking if file exists without using external libs.*/
+  FILE * file = fopen(fname, "r");
+  if (!file) {
+    fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname);
+    write_at_console("Cannot load configuration file!");
+    exit(3);
+  }
+  /* File exists, so file has been locked. Release it. */
+  fclose(file);
+  
+  /* Read the file. If there is an error, report it and exit. */
+  if(!config_read_file(&cfg, fname)) 
+  {
+    AppConfiguration::error(&cfg);
+    config_destroy(&cfg);
+    exit(3);/* from original code. */
+  }
+  
+  if(config_lookup_int(&cfg, "node_number", &MyNode))
+  {
+    if (MyNode==-1) {
+      write_at_console("Node number must be specified");
+      config_destroy(&cfg);
+      exit(1);
+    };
+  }
+  else
+  {
+    AppConfiguration::error(&cfg);
+    config_destroy(&cfg);
+    exit(1);
+  }
+
+  if(config_lookup_string(&cfg, "host", &str)) {
+    k++;
+    pomlink = new NETlink;
+    strcpy(pomlink->addr, str);
+    pomlink->connected = FALSE;
+    pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); 
+    fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0));
+    on=1; 
+    setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on)); 
+    Links.append(pomlink); 
+    to_connect++;
+  }
+  else {
+    AppConfiguration::error(&cfg);
+  }
+
+  config_destroy(&cfg);
+
+  if (k==0) all_connected=TRUE;
 }