Accept relative paths to dmlite configuration file.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Wed, 16 Oct 2013 14:17:32 +0000 (16:17 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Wed, 16 Oct 2013 17:49:07 +0000 (19:49 +0200)
run.cpp
scan.cpp

diff --git a/run.cpp b/run.cpp
index 7df59e0..11a53a0 100644 (file)
--- a/run.cpp
+++ b/run.cpp
@@ -1,3 +1,4 @@
+#include <unistd.h>
 #include <cstring>
 #include <cstdio>
 #include <iostream>
@@ -28,6 +29,11 @@ int main(int argc, char *argv[]) {
        }
 
        if (argc >= 2) conf = argv[1];
+       if (conf[0] != '/') {
+               char *cwd = get_current_dir_name();
+               conf = std::string(cwd) + "/" + conf;
+               free(cwd);
+       }
        std::cout << "Config file: " << conf << std::endl;
 
        try {
index 3551ce8..2ef9076 100644 (file)
--- a/scan.cpp
+++ b/scan.cpp
@@ -1,4 +1,5 @@
 #include <getopt.h>
+#include <unistd.h>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
@@ -88,7 +89,8 @@ int main(int argc, char *argv[]) {
        dmlite::Catalog* catalog = NULL;
        //dmlite::PoolManager* poolManager = NULL;
        dmlite::SecurityCredentials creds;
-       char *dmlite_config = NULL, *user = NULL, *address = NULL, *path, *target;
+       char *user = NULL, *address = NULL, *path, *target;
+       std::string dmlite_config;
        int opt;
        bool safetyCheck = true;
 
@@ -100,8 +102,7 @@ int main(int argc, char *argv[]) {
                                address = strdup(optarg);
                                break;
                        case 'c':
-                               free(dmlite_config);
-                               dmlite_config = strdup(optarg);
+                               dmlite_config = optarg;
                                break;
                        case 'h':
                                usage(argv[0]);
@@ -123,9 +124,15 @@ int main(int argc, char *argv[]) {
                        throw dmlite::DmException();
                }
 
+               if (dmlite_config[0] != '/') {
+                       char *cwd = get_current_dir_name();
+                       dmlite_config = std::string(cwd) + "/" + dmlite_config;
+                       free(cwd);
+               }
+
                // load configuration
                try {
-                       manager.loadConfiguration(dmlite_config ? : DMLITE_CONFIG_DEFAULT);
+                       manager.loadConfiguration(dmlite_config.empty() ? DMLITE_CONFIG_DEFAULT : dmlite_config);
                } catch (dmlite::DmException& e) {
                        std::cout << "Could not load the configuration file." << std::endl
                                << "Reason: " << e.what() << std::endl;
@@ -178,20 +185,17 @@ int main(int argc, char *argv[]) {
                delete stack;
        } catch (dmlite::DmException& e) {
                free(address);
-               free(dmlite_config);
                free(user);
                delete stack;
                return 1;
        } catch (...) {
                free(address);
-               free(dmlite_config);
                free(user);
                delete stack;
                throw;
        }
 
        free(address);
-       free(dmlite_config);
        free(user);
        return 0;
 }