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
48 #include <string_view>
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
, const std::string
& path2
);
63 static std::string
join(const std::string
& path1
, const std::string
& path2
);
64 static std::string
join(const std::string
& path1
, const std::string
& path2
, const std::string
& path3
);
65 //! Return a path using directory separators that suit the execution OS.
66 static std::string
normalize(const std::string
& path
);
67 /*! \brief Returns a copy of the parent path (ie. directory
68 * components) of \c input ie. up to but excluding the last
69 * directory separator (if one exists).
71 * \returns A copy of the parent path-components, or empty if
72 * no directory separator exists. */
73 static std::string
getParentPath(const std::string
& input
);
74 /*! \brief Returns a copy of the filename in \c input
75 * ie. after the last directory separator (if one exists). */
76 static std::string
getFilename(const std::string
& input
);
77 //! Returns whether an extension is present in \c input.
78 static bool hasExtension(const std::string
& input
);
79 /*! \brief Returns whether the extension present in \c input
80 * matches \c extension (which does not include the separator
82 static bool extensionMatches(std::string_view input
, std::string_view extension
);
83 /*! \brief Returns a copy of the input without any trailing
84 * extension found in the filename component. */
85 static std::string
stripExtension(const std::string
& input
);
86 /*! \brief Concatenate \c stringToAdd to a copy of \c input,
87 * before any file extension (if one exists), and return the
89 static std::string
concatenateBeforeExtension(const std::string
& input
, const std::string
& stringToAdd
);
91 static const char* stripSourcePrefix(const char* path
);
93 static bool exists(const char* path
);
94 static bool exists(const std::string
& path
);
95 static std::string
getWorkingDirectory();
97 static void splitPathEnvironment(const std::string
& pathEnv
, std::vector
<std::string
>* result
);
98 static std::vector
<std::string
> getExecutablePaths();
100 static std::string
resolveSymlinks(const std::string
& path
);
103 // Disallow instantiation.
112 NotFoundInfo(const char* filename
, const char* message
, const char* call
, bool wasError
, int err
) :
121 const char* filename
;
128 static void returnFalseOnError(const NotFoundInfo
& info
);
129 static void throwOnError(const NotFoundInfo
& info
);
130 [[noreturn
]] static void throwOnNotFound(const NotFoundInfo
& info
);
132 typedef void (*NotFoundHandler
)(const NotFoundInfo
& info
);
135 * Checks whether a file exists and is a regular file.
137 * \param[in] filename Path to the file to check.
138 * \param[in] onNotFound Function to call when the file does not
139 * exists or there is an error accessing it.
140 * \returns `true` if \p filename exists and is accessible.
142 * Does not throw, unless onNotFound throws.
144 static bool exists(const char* filename
, NotFoundHandler onNotFound
);
145 //! \copydoc exists(const char *, NotFoundHandler)
146 static bool exists(const std::string
& filename
, NotFoundHandler onNotFound
);
149 // Disallow instantiation.
156 static int create(const char* path
);
157 static int create(const std::string
& path
);
158 static bool exists(const char* path
);
159 static bool exists(const std::string
& path
);
162 // Disallow instantiation.