Manually sort some includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / file.cpp
index d14005e6b48a7059b97132fc843c9488cd5fc721..31055f28b8637ee408f8dbb038a15b512f5c46fc 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * Copyright (c) 2012,2013,2014, 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.
  *
  * GROMACS is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -39,6 +39,8 @@
  * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_utility
  */
+#include "gmxpre.h"
+
 #include "file.h"
 
 #include <cerrno>
 #include <string>
 #include <vector>
 
-#include "gromacs/legacyheaders/futil.h"
+#include <sys/stat.h>
+
+#include "config.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
@@ -69,7 +77,7 @@ class File::Impl
         /*! \brief
          * Initialize a file object with the given handle.
          *
-         * \param[in]  fp     File handle to use (may be NULL).
+         * \param[in]  fp     %File handle to use (may be NULL).
          * \param[in]  bClose Whether this object should close its file handle.
          */
         Impl(FILE *fp, bool bClose);
@@ -126,7 +134,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 ffopen() here.
+    // TODO: Port all necessary functionality from gmx_ffopen() here.
     impl_->fp_ = fopen(filename, mode);
     if (impl_->fp_ == NULL)
     {
@@ -156,6 +164,17 @@ void File::close()
     }
 }
 
+bool File::isInteractive() const
+{
+    GMX_RELEASE_ASSERT(impl_->fp_ != NULL,
+                       "Attempted to access a file object that is not open");
+#ifdef HAVE_UNISTD_H
+    return isatty(fileno(impl_->fp_));
+#else
+    return true;
+#endif
+}
+
 FILE *File::handle()
 {
     GMX_RELEASE_ASSERT(impl_->fp_ != NULL,
@@ -189,6 +208,20 @@ void File::readBytes(void *buffer, size_t bytes)
 }
 
 bool File::readLine(std::string *line)
+{
+    if (!readLineWithTrailingSpace(line))
+    {
+        return false;
+    }
+    size_t endPos = line->find_last_not_of(" \t\r\n");
+    if (endPos != std::string::npos)
+    {
+        line->resize(endPos + 1);
+    }
+    return true;
+}
+
+bool File::readLineWithTrailingSpace(std::string *line)
 {
     line->clear();
     const size_t bufsize = 256;
@@ -242,7 +275,30 @@ void File::writeLine()
 // static
 bool File::exists(const char *filename)
 {
-    return gmx_fexist(filename);
+    if (filename == NULL)
+    {
+        return false;
+    }
+    FILE *test = fopen(filename, "r");
+    if (test == NULL)
+    {
+        return false;
+    }
+    else
+    {
+        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