Experimental usage.
[vlp.git] / src / kernel / kernel.cpp
index e17b6e50d54f7a18f9887f42380ca4d3065e608e..df3df49d4db4778a4f121ad9d4865fdab332692c 100644 (file)
 #include <fcntl.h>
 
 #include "genint1.h"
-#include "../head/comm.h"
+#include "comm.h"
 #include "socu.h"
 #include <netinet/in.h>
 
+#include <libconfig.h>
+#include "AppConfiguration.h"
+
 #define GPATH "loggr"
 #define IPATH "logi"
 #define NPATH "logn"
@@ -181,7 +184,7 @@ private:
   bool info_messages;
   
 
-  void LoadConfig();
+  void LoadConfig(char *);
   void RunGraphModule(char*);
   void RunNetModule();
   InterpEntry *findINTbySocket(int);
@@ -262,7 +265,7 @@ QKernel::QKernel()
  freeINTid = 1;
  ActiveConnections = 0;
  strcpy(LockPasswd,"");
- LoadConfig();
+ LoadConfig("vlp.cfg");
  RunNetModule();
 
  Net_Notify = new QSocketNotifier(net_sock,QSocketNotifier::Read,this);
@@ -289,36 +292,41 @@ void QKernel::n_impl()
 
 /* ###########     load configuration from file  ############# */
 
-void QKernel::LoadConfig()
+void QKernel::LoadConfig(char * fname)
 {
-QFile f("vlp.cfg");
- QString line;
- QString val;
- int br=0;
-
- if (!f.exists())
- {
-  WriteMessage("Cannot load configuration file!");sleep(2);exit(3);
+  /* 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);
+    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") {NodeNumber = val.toInt();};
-  if (line == "type") {if (val=="explicit") ConType=1; else
-                               ConType = 2; };
-  if (line == "host" ) {ConnectList.append(new ConnectEntry((char*)val.ascii()));};
-  if (line == "progdir") { strcpy(progdir,val.ascii());};
-  if (line == "homedir") { strcpy(HomeDir,val.ascii());};
-  br = f.readLine(line,256);
- }
- f.close();
+  /* File exists, so file has been locked. Release it. */
+  fclose(file);
+  
+  AppConfiguration config(fname);
+  
+  NodeNumber = config.getInt("node_number");
+  if (config.error() == TRUE) {
+    config.release();
+    exit(3);
+  }
+  
+  ConType = (strcmp("explicit", config.getString("type")) == 0) ? 1 : 2;
+  config.error();
+  
+  char host[255];
+  strcpy(host, config.getString("host"));//FIXME: buffer overflow
+  if (config.error() == FALSE) {
+    ConnectList.append(new ConnectEntry(config.getString("host")));
+  }
+  
+  strcpy(progdir, config.getString("progdir"));//FIXME: buffer overflow
+  config.error();
+  
+  strcpy(HomeDir, config.getString("homedir"));//FIXME: buffer overflow
+  config.error();
+  
+  config.release();
 }
 /* +++++++++++++++++++++++++++++++++++++++++++++++ */