Tidy: modernize-use-nullptr
[gromacs.git] / src / gromacs / commandline / filenm.cpp
blobcbda55311ff5e91b4d9435ee27bb6666288015ff
1 /*
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, 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.
37 #include "gmxpre.h"
39 #include "filenm.h"
41 #include <cstdio>
42 #include <cstring>
44 #include "gromacs/fileio/filetypes.h"
45 #include "gromacs/utility/basedefinitions.h"
46 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/smalloc.h"
49 /* Use bitflag ... */
50 #define IS_SET(fn) ((fn.flag &ffSET) != 0)
51 #define IS_OPT(fn) ((fn.flag &ffOPT) != 0)
53 const t_filenm *getFilenm(const char *opt, int nfile, const t_filenm fnm[])
55 int i;
57 for (i = 0; (i < nfile); i++)
59 if (strcmp(opt, fnm[i].opt) == 0)
61 return &fnm[i];
65 return nullptr;
68 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
70 int i;
72 for (i = 0; (i < nfile); i++)
74 if (std::strcmp(opt, fnm[i].opt) == 0)
76 return fnm[i].fns[0];
80 fprintf(stderr, "No option %s\n", opt);
82 return nullptr;
85 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
87 int i;
89 for (i = 0; (i < nfile); i++)
91 if (strcmp(opt, fnm[i].opt) == 0)
93 *fns = fnm[i].fns;
94 return fnm[i].nfiles;
98 fprintf(stderr, "No option %s\n", opt);
99 return 0;
102 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
104 int i;
106 for (i = 0; (i < nfile); i++)
108 if (ftp == fnm[i].ftp)
110 return fnm[i].fns[0];
114 fprintf(stderr, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp));
115 return nullptr;
118 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
120 int i;
122 for (i = 0; (i < nfile); i++)
124 if (ftp == fnm[i].ftp)
126 *fns = fnm[i].fns;
127 return fnm[i].nfiles;
131 fprintf(stderr, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp));
132 return 0;
135 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
137 int i;
139 for (i = 0; (i < nfile); i++)
141 if (ftp == fnm[i].ftp)
143 return (gmx_bool) IS_SET(fnm[i]);
147 fprintf(stderr, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp));
149 return FALSE;
152 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
154 int i;
156 for (i = 0; (i < nfile); i++)
158 if (std::strcmp(opt, fnm[i].opt) == 0)
160 return (gmx_bool) IS_SET(fnm[i]);
164 fprintf(stderr, "No option %s\n", opt);
166 return FALSE;
169 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
171 int i;
173 for (i = 0; (i < nfile); i++)
175 if (std::strcmp(opt, fnm[i].opt) == 0)
177 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
179 return nullptr;
181 else
183 return fnm[i].fns[0];
187 fprintf(stderr, "No option %s\n", opt);
188 return nullptr;
191 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
193 int i;
195 for (i = 0; (i < nfile); i++)
197 if (ftp == fnm[i].ftp)
199 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
201 return nullptr;
203 else
205 return fnm[i].fns[0];
209 fprintf(stderr, "ftp2fn: No filetype %s\n", ftp2ext_with_dot(ftp));
210 return nullptr;
213 gmx_bool is_optional(const t_filenm *fnm)
215 return ((fnm->flag & ffOPT) == ffOPT);
218 gmx_bool is_output(const t_filenm *fnm)
220 return ((fnm->flag & ffWRITE) == ffWRITE);
223 gmx_bool is_set(const t_filenm *fnm)
225 return ((fnm->flag & ffSET) == ffSET);
228 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
230 int i, j;
231 char buf[STRLEN], newname[STRLEN];
232 char *extpos;
234 for (i = 0; i < nfile; i++)
236 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
238 /* We never use multiple _outputs_, but we might as well check
239 for it, just in case... */
240 for (j = 0; j < fnm[i].nfiles; j++)
242 std::strncpy(buf, fnm[i].fns[j], STRLEN - 1);
243 extpos = strrchr(buf, '.');
244 *extpos = '\0';
245 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
246 sfree(fnm[i].fns[j]);
247 fnm[i].fns[j] = gmx_strdup(newname);
251 return 0;
254 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
256 int i, j;
257 t_filenm *ret;
259 snew(ret, nf);
260 for (i = 0; i < nf; i++)
262 ret[i] = tfn[i]; /* just directly copy all non-string fields */
263 if (tfn[i].opt)
265 ret[i].opt = gmx_strdup(tfn[i].opt);
267 else
269 ret[i].opt = nullptr;
272 if (tfn[i].fn)
274 ret[i].fn = gmx_strdup(tfn[i].fn);
276 else
278 ret[i].fn = nullptr;
281 if (tfn[i].nfiles > 0)
283 snew(ret[i].fns, tfn[i].nfiles);
284 for (j = 0; j < tfn[i].nfiles; j++)
286 ret[i].fns[j] = gmx_strdup(tfn[i].fns[j]);
290 return ret;
293 void done_filenms(int nf, t_filenm fnm[])
295 int i, j;
297 for (i = 0; i < nf; ++i)
299 for (j = 0; j < fnm[i].nfiles; ++j)
301 sfree(fnm[i].fns[j]);
303 sfree(fnm[i].fns);
304 fnm[i].fns = nullptr;