2 * This file is part of the GROMACS molecular simulation package.
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, 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.
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.
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.
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.
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.
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.
44 #include "gromacs/fileio/filetypes.h"
45 #include "gromacs/utility/basedefinitions.h"
46 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/gmxassert.h"
48 #include "gromacs/utility/smalloc.h"
49 #include "gromacs/utility/stringutil.h"
52 static bool IS_SET(const t_filenm &fileOption)
54 return (fileOption.flag & ffSET) != 0;
57 static bool IS_OPT(const t_filenm &fileOption)
59 return (fileOption.flag & ffOPT) != 0;
62 static const t_filenm *getFileOption(const char *opt, int nfile, const t_filenm fnm[])
64 GMX_RELEASE_ASSERT(nfile == 0 || fnm, "need a valid list of filenames");
66 for (int i = 0; i < nfile; i++)
68 if ((fnm[i].opt != nullptr && strcmp(opt, fnm[i].opt) == 0) ||
69 (fnm[i].opt == nullptr && strcmp(opt, ftp2defopt(fnm[i].ftp)) == 0))
78 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
80 const t_filenm *fileOption = getFileOption(opt, nfile, fnm);
84 return fileOption->filenames[0].c_str();
87 GMX_RELEASE_ASSERT(false, "opt2fn should be called with a valid option");
92 gmx::ArrayRef<const std::string>
93 opt2fns(const char *opt, int nfile, const t_filenm fnm[])
95 const t_filenm *fileOption = getFileOption(opt, nfile, fnm);
99 return fileOption->filenames;
102 GMX_RELEASE_ASSERT(false, "opt2fns should be called with a valid option");
104 return gmx::EmptyArrayRef();
107 gmx::ArrayRef<const std::string>
108 opt2fnsIfOptionSet(const char *opt, int nfile, const t_filenm fnm[])
110 if (opt2bSet(opt, nfile, fnm))
112 return opt2fns(opt, nfile, fnm);
116 return gmx::EmptyArrayRef();
120 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
124 for (i = 0; (i < nfile); i++)
126 if (ftp == fnm[i].ftp)
128 return fnm[i].filenames[0].c_str();
132 GMX_RELEASE_ASSERT(false, "ftp2fn should be called with a valid option");
137 gmx::ArrayRef<const std::string>
138 ftp2fns(int ftp, int nfile, const t_filenm fnm[])
140 for (int i = 0; (i < nfile); i++)
142 if (ftp == fnm[i].ftp)
144 return fnm[i].filenames;
148 GMX_RELEASE_ASSERT(false, "ftp2fns should be called with a valid option");
150 return gmx::EmptyArrayRef();
153 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
157 for (i = 0; (i < nfile); i++)
159 if (ftp == fnm[i].ftp)
161 return static_cast<gmx_bool>(IS_SET(fnm[i]));
165 GMX_RELEASE_ASSERT(false, "ftp2bSet should be called with a valid option");
170 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
172 const t_filenm *fileOption = getFileOption(opt, nfile, fnm);
176 return static_cast<gmx_bool>(IS_SET(*fileOption));
179 GMX_RELEASE_ASSERT(false, "opt2bSet should be called with a valid option");
184 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
186 const t_filenm *fileOption = getFileOption(opt, nfile, fnm);
190 if (IS_OPT(*fileOption) && !IS_SET(*fileOption))
196 return fileOption->filenames[0].c_str();
200 GMX_RELEASE_ASSERT(false, "opt2fn_null should be called with a valid option");
205 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
209 for (i = 0; (i < nfile); i++)
211 if (ftp == fnm[i].ftp)
213 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
219 return fnm[i].filenames[0].c_str();
224 GMX_RELEASE_ASSERT(false, "ftp2fn_null should be called with a valid option");
229 gmx_bool is_optional(const t_filenm *fnm)
231 return ((fnm->flag & ffOPT) == ffOPT);
234 gmx_bool is_output(const t_filenm *fnm)
236 return ((fnm->flag & ffWRITE) == ffWRITE);
239 gmx_bool is_set(const t_filenm *fnm)
241 return ((fnm->flag & ffSET) == ffSET);
244 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
249 for (int i = 0; i < nfile; i++)
251 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
253 /* We never use multiple _outputs_, but we might as well check
254 for it, just in case... */
255 for (std::string &filename : fnm[i].filenames)
257 std::strncpy(buf, filename.c_str(), STRLEN - 1);
258 extpos = strrchr(buf, '.');
260 filename = gmx::formatString("%s%s.%s", buf, suffix, extpos + 1);