Removed back AppConfiguration class. Postponed its implementation.
[vlp.git] / src / kernel / kernel.cpp
index ad259d30d4bccc7dc5abee72d449bd491d55f101..3126480c8cf1f07f052f0feb639e101e15ace3db 100644 (file)
@@ -71,6 +71,8 @@
 #include "socu.h"
 #include <netinet/in.h>
 
+#include <libconfig.h>
+
 #define GPATH "loggr"
 #define IPATH "logi"
 #define NPATH "logn"
@@ -181,7 +183,7 @@ private:
   bool info_messages;
   
 
-  void LoadConfig();
+  void LoadConfig(char *);
   void RunGraphModule(char*);
   void RunNetModule();
   InterpEntry *findINTbySocket(int);
@@ -262,7 +264,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 +291,87 @@ 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;
+  config_t cfg;
+  const char *str;
+
+  /* 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);
+  }
+  /* File exists, so file has been locked. Release it. */
+  
+  config_init(&cfg);
+  
+  /* Read the file. If there is an error, report it and exit. */
+  if(!config_read(&cfg, file)) 
+  {
+    fprintf(stderr, "%s! In file %s, line %d\n",
+        config_error_text(&cfg),
+        config_error_file(&cfg),
+        config_error_line(&cfg));
+    config_destroy(&cfg);
+    fclose(file);
+    exit(3);/* from original code. */
+  }
 
- if (!f.exists())
- {
-  WriteMessage("Cannot load configuration file!");sleep(2);exit(3);
+  if(!config_lookup_int(&cfg, "node_number", &NodeNumber))
+  {
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+        "Warning",
+        fname,
+        "node_number");
+    config_destroy(&cfg);
+    fclose(file);
+    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();
+  
+  if(config_lookup_string(&cfg, "type", &str)){
+    ConType = (strcmp(str, "explicit") == 0) ? 1 : 2;
+  }
+  else {
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+        "Warning",
+        fname,
+        "type");
+  }
+  
+  
+  if(config_lookup_string(&cfg, "host", &str)) {
+    ConnectList.append(new ConnectEntry((char*)str));
+  }
+  else {
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+        "Warning",
+        fname,
+        "host");
+  }
+  
+  if(config_lookup_string(&cfg, "progdir", &str)){
+    strncpy(progdir, str, 256);
+  }
+  else {
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+        "Warning",
+        fname,
+        "progdir");
+  }
+  
+  if(config_lookup_string(&cfg, "homedir", &str)){
+    strncpy(HomeDir, str, 255);
+  }
+  else {
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+        "Warning",
+        fname,
+        "homedir");
+  }
+  
+  config_destroy(&cfg);
+  fclose(file);
 }
 /* +++++++++++++++++++++++++++++++++++++++++++++++ */