Split lines with many copyright years
[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, 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 <utility>
49 #include <vector>
50
51 #include "gromacs/compat/string_view.h"
52
53 namespace gmx
54 {
55
56 class Path
57 {
58 public:
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);
63
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).
71      *
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
82      * character). */
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
89      * result. */
90     static std::string concatenateBeforeExtension(const std::string& input, const std::string& stringToAdd);
91
92     static const char* stripSourcePrefix(const char* path);
93
94     static bool        exists(const char* path);
95     static bool        exists(const std::string& path);
96     static std::string getWorkingDirectory();
97
98     static void splitPathEnvironment(const std::string& pathEnv, std::vector<std::string>* result);
99     static std::vector<std::string> getExecutablePaths();
100
101     static std::string resolveSymlinks(const std::string& path);
102
103 private:
104     // Disallow instantiation.
105     Path();
106 };
107
108 class File
109 {
110 public:
111     struct NotFoundInfo
112     {
113         NotFoundInfo(const char* filename, const char* message, const char* call, bool wasError, int err) :
114             filename(filename),
115             message(message),
116             call(call),
117             wasError(wasError),
118             err(err)
119         {
120         }
121
122         const char* filename;
123         const char* message;
124         const char* call;
125         bool        wasError;
126         int         err;
127     };
128
129     static void              returnFalseOnError(const NotFoundInfo& info);
130     static void              throwOnError(const NotFoundInfo& info);
131     [[noreturn]] static void throwOnNotFound(const NotFoundInfo& info);
132
133     typedef void (*NotFoundHandler)(const NotFoundInfo& info);
134
135     /*! \brief
136      * Checks whether a file exists and is a regular file.
137      *
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.
142      *
143      * Does not throw, unless onNotFound throws.
144      */
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);
148
149 private:
150     // Disallow instantiation.
151     File();
152 };
153
154 class Directory
155 {
156 public:
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);
161
162 private:
163     // Disallow instantiation.
164     Directory();
165 };
166
167 } // namespace gmx
168
169 #endif