696368f08a7f2e5f09587362f82da8a254e4b439
[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 <string_view>
47
48 #include "gromacs/fileio/filetypes.h"
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/gmxassert.h"
52 #include "gromacs/utility/path.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "gromacs/utility/stringutil.h"
55
56 /* Use bitflag ... */
57 static bool IS_SET(const t_filenm& fileOption)
58 {
59     return (fileOption.flag & ffSET) != 0;
60 }
61
62 static bool IS_OPT(const t_filenm& fileOption)
63 {
64     return (fileOption.flag & ffOPT) != 0;
65 }
66
67 static const t_filenm* getFileOption(const char* opt, int nfile, const t_filenm fnm[])
68 {
69     GMX_RELEASE_ASSERT(nfile == 0 || fnm, "need a valid list of filenames");
70
71     for (int i = 0; i < nfile; i++)
72     {
73         if ((fnm[i].opt != nullptr && strcmp(opt, fnm[i].opt) == 0)
74             || (fnm[i].opt == nullptr && strcmp(opt, ftp2defopt(fnm[i].ftp)) == 0))
75         {
76             return &fnm[i];
77         }
78     }
79
80     return nullptr;
81 }
82
83 const char* opt2fn(const char* opt, int nfile, const t_filenm fnm[])
84 {
85     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
86
87     if (fileOption)
88     {
89         return fileOption->filenames[0].c_str();
90     }
91
92     GMX_RELEASE_ASSERT(false, "opt2fn should be called with a valid option");
93
94     return nullptr;
95 }
96
97 gmx::ArrayRef<const std::string> opt2fns(const char* opt, int nfile, const t_filenm fnm[])
98 {
99     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
100
101     if (fileOption)
102     {
103         return fileOption->filenames;
104     }
105
106     GMX_RELEASE_ASSERT(false, "opt2fns should be called with a valid option");
107
108     return {};
109 }
110
111 gmx::ArrayRef<const std::string> opt2fnsIfOptionSet(const char* opt, int nfile, const t_filenm fnm[])
112 {
113     if (opt2bSet(opt, nfile, fnm))
114     {
115         return opt2fns(opt, nfile, fnm);
116     }
117     else
118     {
119         return {};
120     }
121 }
122
123 const char* ftp2fn(int ftp, int nfile, const t_filenm fnm[])
124 {
125     int i;
126
127     for (i = 0; (i < nfile); i++)
128     {
129         if (ftp == fnm[i].ftp)
130         {
131             return fnm[i].filenames[0].c_str();
132         }
133     }
134
135     GMX_RELEASE_ASSERT(false, "ftp2fn should be called with a valid option");
136
137     return nullptr;
138 }
139
140 gmx::ArrayRef<const std::string> ftp2fns(int ftp, int nfile, const t_filenm fnm[])
141 {
142     for (int i = 0; (i < nfile); i++)
143     {
144         if (ftp == fnm[i].ftp)
145         {
146             return fnm[i].filenames;
147         }
148     }
149
150     GMX_RELEASE_ASSERT(false, "ftp2fns should be called with a valid option");
151
152     return {};
153 }
154
155 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
156 {
157     int i;
158
159     for (i = 0; (i < nfile); i++)
160     {
161         if (ftp == fnm[i].ftp)
162         {
163             return static_cast<gmx_bool>(IS_SET(fnm[i]));
164         }
165     }
166
167     GMX_RELEASE_ASSERT(false, "ftp2bSet should be called with a valid option");
168
169     return FALSE;
170 }
171
172 gmx_bool opt2bSet(const char* opt, int nfile, const t_filenm fnm[])
173 {
174     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
175
176     if (fileOption)
177     {
178         return static_cast<gmx_bool>(IS_SET(*fileOption));
179     }
180
181     GMX_RELEASE_ASSERT(false, "opt2bSet should be called with a valid option");
182
183     return FALSE;
184 }
185
186 const char* opt2fn_null(const char* opt, int nfile, const t_filenm fnm[])
187 {
188     const t_filenm* fileOption = getFileOption(opt, nfile, fnm);
189
190     if (fileOption)
191     {
192         if (IS_OPT(*fileOption) && !IS_SET(*fileOption))
193         {
194             return nullptr;
195         }
196         else
197         {
198             return fileOption->filenames[0].c_str();
199         }
200     }
201
202     GMX_RELEASE_ASSERT(false, "opt2fn_null should be called with a valid option");
203
204     return nullptr;
205 }
206
207 const char* ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
208 {
209     int i;
210
211     for (i = 0; (i < nfile); i++)
212     {
213         if (ftp == fnm[i].ftp)
214         {
215             if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
216             {
217                 return nullptr;
218             }
219             else
220             {
221                 return fnm[i].filenames[0].c_str();
222             }
223         }
224     }
225
226     GMX_RELEASE_ASSERT(false, "ftp2fn_null should be called with a valid option");
227
228     return nullptr;
229 }
230
231 gmx_bool is_optional(const t_filenm* fnm)
232 {
233     return ((fnm->flag & ffOPT) == ffOPT);
234 }
235
236 gmx_bool is_output(const t_filenm* fnm)
237 {
238     return ((fnm->flag & ffWRITE) == ffWRITE);
239 }
240
241 gmx_bool is_set(const t_filenm* fnm)
242 {
243     return ((fnm->flag & ffSET) == ffSET);
244 }
245
246 namespace
247 {
248
249 /*! \brief Return the first position within \c filename of the ".partNNNN"
250  * interior sequence produced by mdrun -noappend, or npos if not found. */
251 size_t findSuffixFromNoAppendPosition(const std::string_view filename)
252 {
253     size_t partPosition = filename.find(".part");
254     if ((partPosition != decltype(filename)::npos) && (filename.length() - partPosition >= 10)
255         && (std::isdigit(filename[partPosition + 5])) && (std::isdigit(filename[partPosition + 6]))
256         && (std::isdigit(filename[partPosition + 7])) && (std::isdigit(filename[partPosition + 8]))
257         && filename[partPosition + 9] == '.')
258     {
259         return partPosition;
260     }
261     return decltype(filename)::npos;
262 }
263
264 } // namespace
265
266 bool hasSuffixFromNoAppend(const std::string_view filename)
267 {
268     return (findSuffixFromNoAppendPosition(filename) != decltype(filename)::npos);
269 }
270
271 int add_suffix_to_output_names(t_filenm* fnm, int nfile, const char* suffix)
272 {
273     for (int i = 0; i < nfile; i++)
274     {
275         if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
276         {
277             /* We never use multiple _outputs_, but we might as well check
278                for it, just in case... */
279             for (std::string& filename : fnm[i].filenames)
280             {
281                 // mdrun should not generate files like
282                 // md.part0002.part0003.log. mdrun should permit users
283                 // to name files like md.equil.part0002.log. So,
284                 // before we use Path::concatenateBeforeExtension to
285                 // add the requested suffix, we need to check for
286                 // files matching mdrun's pattern for adding part
287                 // numbers. Then we can remove that if needed.
288                 for (size_t partPosition;
289                      (partPosition = findSuffixFromNoAppendPosition(filename)) != std::string::npos;)
290                 {
291                     // Remove the ".partNNNN" that we have found,
292                     // and then run the loop again to make sure
293                     // there isn't another one to remove, somehow.
294                     std::string temporary = filename.substr(0, partPosition);
295                     temporary += filename.substr(partPosition + 9);
296                     filename.swap(temporary);
297                 }
298                 filename = gmx::Path::concatenateBeforeExtension(filename, suffix);
299             }
300         }
301     }
302     return 0;
303 }