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