Update instructions in containers.rst
[gromacs.git] / src / gromacs / utility / path.h
blob354ae7ea80bbc55054eb7aea63608502d0f6d801
1 /*
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
37 * \brief
38 * Declares functions for OS-independent path handling.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \inlibraryapi
42 * \ingroup module_utility
44 #ifndef GMX_UTILITY_PATH_H
45 #define GMX_UTILITY_PATH_H
47 #include <string>
48 #include <string_view>
49 #include <utility>
50 #include <vector>
52 namespace gmx
55 class Path
57 public:
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
81 * character). */
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
88 * result. */
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);
102 private:
103 // Disallow instantiation.
104 Path();
107 class File
109 public:
110 struct NotFoundInfo
112 NotFoundInfo(const char* filename, const char* message, const char* call, bool wasError, int err) :
113 filename(filename),
114 message(message),
115 call(call),
116 wasError(wasError),
117 err(err)
121 const char* filename;
122 const char* message;
123 const char* call;
124 bool wasError;
125 int err;
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);
134 /*! \brief
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);
148 private:
149 // Disallow instantiation.
150 File();
153 class Directory
155 public:
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);
161 private:
162 // Disallow instantiation.
163 Directory();
166 } // namespace gmx
168 #endif