SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / utility / path.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013,2014,2015 by the GROMACS development team.
5  * Copyright (c) 2016,2018,2019,2020,2021, 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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \libinternal \file
37  * \brief
38  * Declares functions for OS-independent path handling.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \inlibraryapi
42  * \ingroup module_utility
43  */
44 #ifndef GMX_UTILITY_PATH_H
45 #define GMX_UTILITY_PATH_H
46
47 #include <string>
48 #include <string_view>
49 #include <utility>
50 #include <vector>
51
52 namespace gmx
53 {
54
55 class Path
56 {
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);
62
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).
70      *
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);
90
91     static const char* stripSourcePrefix(const char* path);
92
93     static bool        exists(const char* path);
94     static bool        exists(const std::string& path);
95     static std::string getWorkingDirectory();
96
97     static void splitPathEnvironment(const std::string& pathEnv, std::vector<std::string>* result);
98     static std::vector<std::string> getExecutablePaths();
99
100     static std::string resolveSymlinks(const std::string& path);
101
102 private:
103     // Disallow instantiation.
104     Path();
105 };
106
107 class File
108 {
109 public:
110     struct NotFoundInfo
111     {
112         NotFoundInfo(const char* filename, const char* message, const char* call, bool wasError, int err) :
113             filename(filename), message(message), call(call), wasError(wasError), err(err)
114         {
115         }
116
117         const char* filename;
118         const char* message;
119         const char* call;
120         bool        wasError;
121         int         err;
122     };
123
124     static void              returnFalseOnError(const NotFoundInfo& info);
125     static void              throwOnError(const NotFoundInfo& info);
126     [[noreturn]] static void throwOnNotFound(const NotFoundInfo& info);
127
128     typedef void (*NotFoundHandler)(const NotFoundInfo& info);
129
130     /*! \brief
131      * Checks whether a file exists and is a regular file.
132      *
133      * \param[in] filename    Path to the file to check.
134      * \param[in] onNotFound  Function to call when the file does not
135      *     exists or there is an error accessing it.
136      * \returns   `true` if \p filename exists and is accessible.
137      *
138      * Does not throw, unless onNotFound throws.
139      */
140     static bool exists(const char* filename, NotFoundHandler onNotFound);
141     //! \copydoc exists(const char *, NotFoundHandler)
142     static bool exists(const std::string& filename, NotFoundHandler onNotFound);
143
144 private:
145     // Disallow instantiation.
146     File();
147 };
148
149 class Directory
150 {
151 public:
152     static int  create(const char* path);
153     static int  create(const std::string& path);
154     static bool exists(const char* path);
155     static bool exists(const std::string& path);
156
157 private:
158     // Disallow instantiation.
159     Directory();
160 };
161
162 } // namespace gmx
163
164 #endif