Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / commandline / filenm.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "filenm.h"
40
41 #include <cctype>
42 #include <cstdio>
43 #include <cstring>
44
45 #include "gromacs/compat/string_view.h"
46 #include "gromacs/fileio/filetypes.h"
47 #include "gromacs/utility/basedefinitions.h"
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/path.h"
51 #include "gromacs/utility/smalloc.h"
52 #include "gromacs/utility/stringutil.h"
53
54 /* Use bitflag ... */
55 static bool IS_SET(const t_filenm& fileOption)
56 {
57     return (fileOption.flag & ffSET) != 0;
58 }
59
60 static bool IS_OPT(const t_filenm& fileOption)
61 {
62     return (fileOption.flag & ffOPT) != 0;
63 }
64
65 static const t_filenm* getFileOption(const char* opt, int nfile, const t_filenm fnm[])
66 {
67     GMX_RELEASE_ASSERT(nfile == 0 || fnm, "need a valid list of filenames");
68
69     for (int i = 0; i < nfile; i++)
70     {
71         if ((fnm[i].opt != nullptr && strcmp(opt, fnm[i].opt) == 0)
72             || (fnm[i].opt == nullptr && strcmp(opt, ftp2defopt(fnm[i].ftp)) == 0))
73         {
74             return &fnm[i];
75         }
76     }
77
78     return nullptr;
79 }
80
81 const char* opt2fn(const char* opt, int nfile, const t_filenm fnm[])
82 {
83     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
84
85     if (fileOption)
86     {
87         return fileOption->filenames[0].c_str();
88     }
89
90     GMX_RELEASE_ASSERT(false, "opt2fn should be called with a valid option");
91
92     return nullptr;
93 }
94
95 gmx::ArrayRef<const std::string> opt2fns(const char* opt, int nfile, const t_filenm fnm[])
96 {
97     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
98
99     if (fileOption)
100     {
101         return fileOption->filenames;
102     }
103
104     GMX_RELEASE_ASSERT(false, "opt2fns should be called with a valid option");
105
106     return {};
107 }
108
109 gmx::ArrayRef<const std::string> opt2fnsIfOptionSet(const char* opt, int nfile, const t_filenm fnm[])
110 {
111     if (opt2bSet(opt, nfile, fnm))
112     {
113         return opt2fns(opt, nfile, fnm);
114     }
115     else
116     {
117         return {};
118     }
119 }
120
121 const char* ftp2fn(int ftp, int nfile, const t_filenm fnm[])
122 {
123     int i;
124
125     for (i = 0; (i < nfile); i++)
126     {
127         if (ftp == fnm[i].ftp)
128         {
129             return fnm[i].filenames[0].c_str();
130         }
131     }
132
133     GMX_RELEASE_ASSERT(false, "ftp2fn should be called with a valid option");
134
135     return nullptr;
136 }
137
138 gmx::ArrayRef<const std::string> ftp2fns(int ftp, int nfile, const t_filenm fnm[])
139 {
140     for (int i = 0; (i < nfile); i++)
141     {
142         if (ftp == fnm[i].ftp)
143         {
144             return fnm[i].filenames;
145         }
146     }
147
148     GMX_RELEASE_ASSERT(false, "ftp2fns should be called with a valid option");
149
150     return {};
151 }
152
153 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
154 {
155     int i;
156
157     for (i = 0; (i < nfile); i++)
158     {
159         if (ftp == fnm[i].ftp)
160         {
161             return static_cast<gmx_bool>(IS_SET(fnm[i]));
162         }
163     }
164
165     GMX_RELEASE_ASSERT(false, "ftp2bSet should be called with a valid option");
166
167     return FALSE;
168 }
169
170 gmx_bool opt2bSet(const char* opt, int nfile, const t_filenm fnm[])
171 {
172     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
173
174     if (fileOption)
175     {
176         return static_cast<gmx_bool>(IS_SET(*fileOption));
177     }
178
179     GMX_RELEASE_ASSERT(false, "opt2bSet should be called with a valid option");
180
181     return FALSE;
182 }
183
184 const char* opt2fn_null(const char* opt, int nfile, const t_filenm fnm[])
185 {
186     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
187
188     if (fileOption)
189     {
190         if (IS_OPT(*fileOption) && !IS_SET(*fileOption))
191         {
192             return nullptr;
193         }
194         else
195         {
196             return fileOption->filenames[0].c_str();
197         }
198     }
199
200     GMX_RELEASE_ASSERT(false, "opt2fn_null should be called with a valid option");
201
202     return nullptr;
203 }
204
205 const char* ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
206 {
207     int i;
208
209     for (i = 0; (i < nfile); i++)
210     {
211         if (ftp == fnm[i].ftp)
212         {
213             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
214             {
215                 return nullptr;
216             }
217             else
218             {
219                 return fnm[i].filenames[0].c_str();
220             }
221         }
222     }
223
224     GMX_RELEASE_ASSERT(false, "ftp2fn_null should be called with a valid option");
225
226     return nullptr;
227 }
228
229 gmx_bool is_optional(const t_filenm* fnm)
230 {
231     return ((fnm->flag & ffOPT) == ffOPT);
232 }
233
234 gmx_bool is_output(const t_filenm* fnm)
235 {
236     return ((fnm->flag & ffWRITE) == ffWRITE);
237 }
238
239 gmx_bool is_set(const t_filenm* fnm)
240 {
241     return ((fnm->flag & ffSET) == ffSET);
242 }
243
244 namespace
245 {
246
247 /*! \brief Return the first position within \c filename of the ".partNNNN"
248  * interior sequence produced by mdrun -noappend, or npos if not found. */
249 size_t findSuffixFromNoAppendPosition(const gmx::compat::string_view filename)
250 {
251     size_t partPosition = filename.find(".part");
252     if ((partPosition != decltype(filename)::npos) && (filename.length() - partPosition >= 10)
253         && (std::isdigit(filename[partPosition + 5])) && (std::isdigit(filename[partPosition + 6]))
254         && (std::isdigit(filename[partPosition + 7])) && (std::isdigit(filename[partPosition + 8]))
255         && filename[partPosition + 9] == '.')
256     {
257         return partPosition;
258     }
259     return decltype(filename)::npos;
260 }
261
262 } // namespace
263
264 bool hasSuffixFromNoAppend(const gmx::compat::string_view filename)
265 {
266     return (findSuffixFromNoAppendPosition(filename) != decltype(filename)::npos);
267 }
268
269 int add_suffix_to_output_names(t_filenm* fnm, int nfile, const char* suffix)
270 {
271     for (int i = 0; i < nfile; i++)
272     {
273         if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
274         {
275             /* We never use multiple _outputs_, but we might as well check
276                for it, just in case... */
277             for (std::string& filename : fnm[i].filenames)
278             {
279                 // mdrun should not generate files like
280                 // md.part0002.part0003.log. mdrun should permit users
281                 // to name files like md.equil.part0002.log. So,
282                 // before we use Path::concatenateBeforeExtension to
283                 // add the requested suffix, we need to check for
284                 // files matching mdrun's pattern for adding part
285                 // numbers. Then we can remove that if needed.
286                 for (size_t partPosition;
287                      (partPosition = findSuffixFromNoAppendPosition(filename)) != std::string::npos;)
288                 {
289                     // Remove the ".partNNNN" that we have found,
290                     // and then run the loop again to make sure
291                     // there isn't another one to remove, somehow.
292                     std::string temporary = filename.substr(0, partPosition);
293                     temporary += filename.substr(partPosition + 9);
294                     filename.swap(temporary);
295                 }
296                 filename = gmx::Path::concatenateBeforeExtension(filename, suffix);
297             }
298         }
299     }
300     return 0;
301 }