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