vlp-10 Using coding style in loghelp.
[vlp.git] / src / net / lognet.cpp
index fce3aee3f843a0576fb00a0f8fdf271d032c299c..088ef9fc24f012f8f8e73b8a05a65fa309ccccd5 100644 (file)
@@ -23,7 +23,6 @@
 #include <unistd.h>
 
 #include <libconfig.h>
-#include "AppConfiguration.h"
 
 #define REMOTE_PATH "REMOTE"
 #define MAXLINKS 30
@@ -187,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;
 
@@ -195,42 +194,65 @@ 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!");
+    fclose(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)) 
+  if(!config_read(&cfg, file)) 
   {
-    AppConfiguration::error(&cfg);
+    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(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);
-      exit(1);
-    };
+    MyNode = config_setting_get_int(setting);
   }
-  else
+  /* else */
+  if (!setting || MyNode==-1)
   {
-    AppConfiguration::error(&cfg);
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+      "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));
@@ -240,10 +262,14 @@ void NETMOD::load_config(char *fname)
     to_connect++;
   }
   else {
-    AppConfiguration::error(&cfg);
+    fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+        "Warning",
+        fname,
+        "host");
   }
 
   config_destroy(&cfg);
+  fclose(file);
 
   if (k==0) all_connected=TRUE;
 }