/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2011,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) 2011,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
* \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_utility
*/
+#include "gmxpre.h"
+
#include "path.h"
-#include "gmx_header_config.h"
+#include <cctype>
+#include <cerrno>
+#include <cstdlib>
+#include <cstring>
+
+#include <algorithm>
-#include <errno.h>
#include <sys/stat.h>
+#include "config.h"
+
#ifdef GMX_NATIVE_WINDOWS
#include <direct.h>
+#else
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
#endif
+#include "gromacs/utility/futil.h"
+#include "gromacs/utility/stringutil.h"
+
namespace
{
const char cDirSeparator = '/';
//! Directory separators to use when parsing paths.
const char cDirSeparators[] = "/\\";
+/*! \var cPathSeparator
+ * \brief
+ * Separator to use to split the PATH environment variable.
+ *
+ * When reading the PATH environment variable, Unix separates entries
+ * with colon, while windows uses semicolon.
+ */
+#ifdef GMX_NATIVE_WINDOWS
+const char cPathSeparator = ';';
+#else
+const char cPathSeparator = ':';
+#endif
+
+//! Check whether a given character is a directory separator.
+bool isDirSeparator(char chr)
+{
+ return std::strchr(cDirSeparators, chr);
+}
} // namespace
namespace gmx
{
+/********************************************************************
+ * Path
+ */
+
+bool Path::containsDirectory(const std::string &path)
+{
+ return path.find_first_of(cDirSeparators) != std::string::npos;
+}
+
+/* Check if the program name begins with "/" on unix/cygwin, or
+ * with "\" or "X:\" on windows. If not, the program name
+ * is relative to the current directory.
+ */
+bool Path::isAbsolute(const char *path)
+{
+ if (isDirSeparator(path[0]))
+ {
+ return true;
+ }
+#ifdef GMX_NATIVE_WINDOWS
+ return path[0] != '\0' && path[1] == ':' && isDirSeparator(path[2]);
+#else
+ return false;
+#endif
+}
+
+bool Path::isAbsolute(const std::string &path)
+{
+ return isAbsolute(path.c_str());
+}
+
+bool Path::startsWith(const std::string &path, const std::string &prefix)
+{
+ return gmx::startsWith(normalize(path), normalize(prefix));
+}
+
std::string Path::join(const std::string &path1,
const std::string &path2)
{
return path1 + cDirSeparator + path2 + cDirSeparator + path3;
}
-std::pair<std::string, std::string>
-Path::splitToPathAndFilename(const std::string &path)
+std::string Path::getParentPath(const std::string &path)
{
size_t pos = path.find_last_of(cDirSeparators);
if (pos == std::string::npos)
{
- return std::make_pair(std::string(), path);
+ return std::string();
+ }
+ return path.substr(0, pos);
+}
+
+std::string Path::getFilename(const std::string &path)
+{
+ size_t pos = path.find_last_of(cDirSeparators);
+ if (pos == std::string::npos)
+ {
+ return path;
+ }
+ return path.substr(pos+1);
+}
+
+std::string Path::stripExtension(const std::string &path)
+{
+ size_t dirSeparatorPos = path.find_last_of(cDirSeparators);
+ size_t extPos = path.find_last_of('.');
+ if (extPos == std::string::npos
+ || (dirSeparatorPos != std::string::npos && extPos < dirSeparatorPos))
+ {
+ return path;
+ }
+ return path.substr(0, extPos);
+}
+
+std::string Path::normalize(const std::string &path)
+{
+ std::string result(path);
+ // TODO: Remove . and .. entries.
+ if (DIR_SEPARATOR != '/')
+ {
+ std::replace(result.begin(), result.end(), '/', DIR_SEPARATOR);
+ }
+#ifdef GMX_NATIVE_WINDOWS
+ if (std::isalpha(result[0]) && result[1] == ':')
+ {
+ result[0] = std::toupper(result[0]);
}
- return std::make_pair(path.substr(0, pos), path.substr(pos+1));
+#endif
+ return result;
}
+bool Path::exists(const char *path)
+{
+ return gmx_fexist(path);
+}
+
+bool Path::exists(const std::string &path)
+{
+ return exists(path.c_str());
+}
+
+std::string Path::getWorkingDirectory()
+{
+ // TODO: Use exceptions instead of gmx_fatal().
+ char cwd[GMX_PATH_MAX];
+ gmx_getcwd(cwd, sizeof(cwd));
+ return cwd;
+}
+
+void Path::splitPathEnvironment(const std::string &pathEnv,
+ std::vector<std::string> *result)
+{
+ size_t prevPos = 0;
+ size_t separator;
+ do
+ {
+ separator = pathEnv.find(cPathSeparator, prevPos);
+ result->push_back(pathEnv.substr(prevPos, separator - prevPos));
+ prevPos = separator + 1;
+ }
+ while (separator != std::string::npos);
+}
+
+std::vector<std::string> Path::getExecutablePaths()
+{
+ std::vector<std::string> result;
+#ifdef GMX_NATIVE_WINDOWS
+ // Add the local dir since it is not in the path on Windows.
+ result.push_back("");
+#endif
+ const char *path = std::getenv("PATH");
+ if (path != NULL)
+ {
+ splitPathEnvironment(path, &result);
+ }
+ return result;
+}
+
+std::string Path::resolveSymlinks(const std::string &path)
+{
+ std::string result(path);
+#ifndef GMX_NATIVE_WINDOWS
+ char buf[GMX_PATH_MAX];
+ int length;
+ while ((length = readlink(result.c_str(), buf, sizeof(buf)-1)) > 0)
+ {
+ buf[length] = '\0';
+ if (isAbsolute(buf))
+ {
+ result = buf;
+ }
+ else
+ {
+ result = join(getParentPath(result), buf);
+ }
+ }
+#endif
+ return result;
+}
+
+
+/********************************************************************
+ * Directory
+ */
int Directory::create(const char *path)
{