Refactor logic for finding share/top/ files
[alexxy/gromacs.git] / src / gromacs / utility / file.cpp
index a194992bfd8e82198e3d5f09f9d70c0ac0ecef09..dbd809c38b2bf5a4f1c56737c9a8e88ab8c14871 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -109,6 +109,25 @@ File::Impl::~Impl()
     }
 }
 
+// static
+FILE *File::openRawHandle(const char *filename, const char *mode)
+{
+    FILE *fp = fopen(filename, mode);
+    if (fp == NULL)
+    {
+        GMX_THROW_WITH_ERRNO(
+                FileIOError(formatString("Could not open file '%s'", filename)),
+                "fopen", errno);
+    }
+    return fp;
+}
+
+// static
+FILE *File::openRawHandle(const std::string &filename, const char *mode)
+{
+    return openRawHandle(filename.c_str(), mode);
+}
+
 File::File(const char *filename, const char *mode)
     : impl_(new Impl(NULL, true))
 {
@@ -135,13 +154,7 @@ void File::open(const char *filename, const char *mode)
     GMX_RELEASE_ASSERT(impl_->fp_ == NULL,
                        "Attempted to open the same file object twice");
     // TODO: Port all necessary functionality from gmx_ffopen() here.
-    impl_->fp_ = fopen(filename, mode);
-    if (impl_->fp_ == NULL)
-    {
-        GMX_THROW_WITH_ERRNO(
-                FileIOError(formatString("Could not open file '%s'", filename)),
-                "fopen", errno);
-    }
+    impl_->fp_ = openRawHandle(filename, mode);
 }
 
 void File::open(const std::string &filename, const char *mode)