Remove gmx::File (except for File::exists())
[alexxy/gromacs.git] / src / gromacs / utility / datafilefinder.cpp
index dfad443b4a3762e07125bc4678053a3802853656..e773fff84b2a55db6f2c69199d288f9e43088ab0 100644 (file)
 #include <string>
 #include <vector>
 
+#include "buildinfo.h"
 #include "gromacs/utility/directoryenumerator.h"
 #include "gromacs/utility/exceptions.h"
-#include "gromacs/utility/file.h"
+#include "gromacs/utility/filestream.h"
 #include "gromacs/utility/path.h"
 #include "gromacs/utility/programcontext.h"
 #include "gromacs/utility/stringutil.h"
@@ -65,6 +66,8 @@ namespace gmx
 class DataFileFinder::Impl
 {
     public:
+        static std::string getDefaultPath();
+
         Impl() : envName_(NULL), bEnvIsSet_(false) {}
 
         const char               *envName_;
@@ -72,6 +75,19 @@ class DataFileFinder::Impl
         std::vector<std::string>  searchPath_;
 };
 
+std::string DataFileFinder::Impl::getDefaultPath()
+{
+    const InstallationPrefixInfo installPrefix
+        = getProgramContext().installationPrefix();
+    if (!isNullOrEmpty(installPrefix.path))
+    {
+        const char *const dataPath
+            = installPrefix.bSourceLayout ? "share" : DATA_INSTALL_DIR;
+        return Path::join(installPrefix.path, dataPath, "top");
+    }
+    return std::string();
+}
+
 /********************************************************************
  * DataFileFinder
  */
@@ -117,7 +133,7 @@ FILE *DataFileFinder::openFile(const DataFileOptions &options) const
         fprintf(debug, "Opening library file %s\n", fn);
     }
 #endif
-    return File::openRawHandle(filename, "r");
+    return TextInputFile::openRawHandle(filename);
 }
 
 std::string DataFileFinder::findFile(const DataFileOptions &options) const
@@ -140,8 +156,8 @@ std::string DataFileFinder::findFile(const DataFileOptions &options) const
             }
         }
     }
-    const char *const defaultPath = getProgramContext().defaultLibraryDataPath();
-    if (defaultPath != NULL && defaultPath[0] != '\0')
+    const std::string &defaultPath = Impl::getDefaultPath();
+    if (!defaultPath.empty())
     {
         std::string testPath = Path::join(defaultPath, options.filename_);
         if (Path::exists(testPath))
@@ -179,7 +195,7 @@ std::string DataFileFinder::findFile(const DataFileOptions &options) const
                 message.append(*i);
             }
         }
-        if (defaultPath != NULL && defaultPath[0] != '\0')
+        if (!defaultPath.empty())
         {
             message.append("\n  ");
             message.append(defaultPath);
@@ -228,12 +244,12 @@ DataFileFinder::enumerateFiles(const DataFileOptions &options) const
             }
         }
     }
-    const char *const defaultPath = getProgramContext().defaultLibraryDataPath();
-    if (defaultPath != NULL && defaultPath[0] != '\0')
+    const std::string &defaultPath = Impl::getDefaultPath();
+    if (!defaultPath.empty())
     {
         std::vector<std::string> files
             = DirectoryEnumerator::enumerateFilesWithExtension(
-                        defaultPath, options.filename_, false);
+                        defaultPath.c_str(), options.filename_, false);
         for (i = files.begin(); i != files.end(); ++i)
         {
             result.push_back(DataFileInfo(defaultPath, *i, true));