2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012,2013,2014,2015 by the GROMACS development team.
5 * Copyright (c) 2016,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \libinternal \file
38 * Declares functions for OS-independent path handling.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_utility
44 #ifndef GMX_UTILITY_PATH_H
45 #define GMX_UTILITY_PATH_H
51 #include "gromacs/compat/string_view.h"
59 static bool containsDirectory(const std::string& path);
60 static bool isAbsolute(const char* path);
61 static bool isAbsolute(const std::string& path);
62 static bool isEquivalent(const std::string& path1, const std::string& path2);
64 static std::string join(const std::string& path1, const std::string& path2);
65 static std::string join(const std::string& path1, const std::string& path2, const std::string& path3);
66 //! Return a path using directory separators that suit the execution OS.
67 static std::string normalize(const std::string& path);
68 /*! \brief Returns a copy of the parent path (ie. directory
69 * components) of \c input ie. up to but excluding the last
70 * directory separator (if one exists).
72 * \returns A copy of the parent path-components, or empty if
73 * no directory separator exists. */
74 static std::string getParentPath(const std::string& input);
75 /*! \brief Returns a copy of the filename in \c input
76 * ie. after the last directory separator (if one exists). */
77 static std::string getFilename(const std::string& input);
78 //! Returns whether an extension is present in \c input.
79 static bool hasExtension(const std::string& input);
80 /*! \brief Returns whether the extension present in \c input
81 * matches \c extension (which does not include the separator
83 static bool extensionMatches(compat::string_view input, compat::string_view extension);
84 /*! \brief Returns a copy of the input without any trailing
85 * extension found in the filename component. */
86 static std::string stripExtension(const std::string& input);
87 /*! \brief Concatenate \c stringToAdd to a copy of \c input,
88 * before any file extension (if one exists), and return the
90 static std::string concatenateBeforeExtension(const std::string& input, const std::string& stringToAdd);
92 static const char* stripSourcePrefix(const char* path);
94 static bool exists(const char* path);
95 static bool exists(const std::string& path);
96 static std::string getWorkingDirectory();
98 static void splitPathEnvironment(const std::string& pathEnv, std::vector<std::string>* result);
99 static std::vector<std::string> getExecutablePaths();
101 static std::string resolveSymlinks(const std::string& path);
104 // Disallow instantiation.
113 NotFoundInfo(const char* filename, const char* message, const char* call, bool wasError, int err) :
122 const char* filename;
129 static void returnFalseOnError(const NotFoundInfo& info);
130 static void throwOnError(const NotFoundInfo& info);
131 [[noreturn]] static void throwOnNotFound(const NotFoundInfo& info);
133 typedef void (*NotFoundHandler)(const NotFoundInfo& info);
136 * Checks whether a file exists and is a regular file.
138 * \param[in] filename Path to the file to check.
139 * \param[in] onNotFound Function to call when the file does not
140 * exists or there is an error accessing it.
141 * \returns `true` if \p filename exists and is accessible.
143 * Does not throw, unless onNotFound throws.
145 static bool exists(const char* filename, NotFoundHandler onNotFound);
146 //! \copydoc exists(const char *, NotFoundHandler)
147 static bool exists(const std::string& filename, NotFoundHandler onNotFound);
150 // Disallow instantiation.
157 static int create(const char* path);
158 static int create(const std::string& path);
159 static bool exists(const char* path);
160 static bool exists(const std::string& path);
163 // Disallow instantiation.