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