Parcourir la source

check if file is reachable from working directory first

before checking if cwd is different and manipulating path
replaced if with else if
Stefano Acquaviti il y a 6 ans
Parent
commit
3902601000
2 fichiers modifiés avec 26 ajouts et 2 suppressions
  1. 24 2
      code_boost/src/cxx/statistics_db.cpp
  2. 2 0
      code_boost/src/cxx/statistics_db.h

+ 24 - 2
code_boost/src/cxx/statistics_db.cpp

@@ -525,15 +525,21 @@ std::string statistics_db::getNmapPath()
     // Working directory
     std::string dir(getcwd(buff, FILENAME_MAX));
 
+    // Check if working directory is id2t.sh directory(try to reach file from working directory)
+    if(pathExists(dir + resourcesDir + filename))
+    {
+        return dir + resourcesDir + filename;
+    }
+
     // If working directory is test directory(happens if tests are called from pycharm for example)
-    if(dir.rfind(testDir) == (dir.size()-testDir.size()))
+    else if(dir.rfind(testDir) == (dir.size()-testDir.size()))
     {
         // Remove test directory from path
         dir = dir.substr(0, (dir.size()-testDir.size()));
     }
 
     // If working directory is code directory(happens if tests are called with testscript)
-    if(dir.rfind(codeDir) == (dir.size()-codeDir.size()))
+    else if(dir.rfind(codeDir) == (dir.size()-codeDir.size()))
     {
         // Remove code directory from path
         dir = dir.substr(0, (dir.size()-codeDir.size()));
@@ -542,4 +548,20 @@ std::string statistics_db::getNmapPath()
     dir = dir + resourcesDir + filename;
 
     return dir;
+}
+
+bool statistics_db::pathExists(std::string path)
+{
+    std::ifstream file;
+    file.open(path, std::ios::in);
+    if(file.is_open())
+    {
+        file.close();
+        return true;
+    }
+
+    else
+    {
+        return false;
+    }
 }

+ 2 - 0
code_boost/src/cxx/statistics_db.h

@@ -58,6 +58,8 @@ public:
 
     std::string getNmapPath();
 
+    bool pathExists(std::string path);
+
 private:
     // Pointer to the SQLite database
     std::unique_ptr<SQLite::Database> db;