Remove gmx::File (except for File::exists())
[alexxy/gromacs.git] / src / gromacs / utility / path.cpp
index d6aa17140d65db27cd77cd0f31ef291dd0a6903a..60c0e0cd7adbb164f5c3a1ad20f47f7762e71b16 100644 (file)
 
 #include <cctype>
 #include <cerrno>
+#include <cstdio>
 #include <cstdlib>
 #include <cstring>
 
 #include <algorithm>
+#include <string>
 
 #include <sys/stat.h>
 
@@ -386,6 +388,44 @@ std::string Path::resolveSymlinks(const std::string &path)
     return result;
 }
 
+/********************************************************************
+ * File
+ */
+
+// static
+bool File::exists(const char *filename)
+{
+    if (filename == NULL)
+    {
+        return false;
+    }
+    FILE *test = std::fopen(filename, "r");
+    if (test == NULL)
+    {
+        return false;
+    }
+    else
+    {
+        std::fclose(test);
+        // Windows doesn't allow fopen of directory, so we don't need to check
+        // this separately.
+#ifndef GMX_NATIVE_WINDOWS
+        struct stat st_buf;
+        int         status = stat(filename, &st_buf);
+        if (status != 0 || !S_ISREG(st_buf.st_mode))
+        {
+            return false;
+        }
+#endif
+        return true;
+    }
+}
+
+// static
+bool File::exists(const std::string &filename)
+{
+    return exists(filename.c_str());
+}
 
 /********************************************************************
  * Directory