#include "gromacs/utility/smalloc.h"
/* Use bitflag ... */
-#define IS_SET(fn) ((fn.flag &ffSET) != 0)
-#define IS_OPT(fn) ((fn.flag &ffOPT) != 0)
+static bool IS_SET(const t_filenm &fileOption)
+{
+ return (fileOption.flag & ffSET) != 0;
+}
+
+static bool IS_OPT(const t_filenm &fileOption)
+{
+ return (fileOption.flag & ffOPT) != 0;
+}
const t_filenm *getFilenm(const char *opt, int nfile, const t_filenm fnm[])
{
- int i;
+ GMX_RELEASE_ASSERT(nfile == 0 || fnm, "need a valid list of filenames");
- for (i = 0; (i < nfile); i++)
+ for (int i = 0; i < nfile; i++)
{
- if (strcmp(opt, fnm[i].opt) == 0)
+ if ((fnm[i].opt != nullptr && strcmp(opt, fnm[i].opt) == 0) ||
+ (fnm[i].opt == nullptr && strcmp(opt, ftp2defopt(fnm[i].ftp)) == 0))
{
return &fnm[i];
}
const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
{
- int i;
+ const t_filenm *fileOption = getFilenm(opt, nfile, fnm);
- for (i = 0; (i < nfile); i++)
+ if (fileOption)
{
- if (std::strcmp(opt, fnm[i].opt) == 0)
- {
- return fnm[i].fns[0];
- }
+ return fileOption->fns[0];
}
GMX_RELEASE_ASSERT(false, "opt2fn should be called with a valid option");
int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
{
- int i;
+ const t_filenm *fileOption = getFilenm(opt, nfile, fnm);
- for (i = 0; (i < nfile); i++)
+ if (fileOption)
{
- if (strcmp(opt, fnm[i].opt) == 0)
- {
- *fns = fnm[i].fns;
- return fnm[i].nfiles;
- }
+ *fns = fileOption->fns;
+ return fileOption->nfiles;
}
GMX_RELEASE_ASSERT(false, "opt2fns should be called with a valid option");
gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
{
- int i;
+ const t_filenm *fileOption = getFilenm(opt, nfile, fnm);
- for (i = 0; (i < nfile); i++)
+ if (fileOption)
{
- if (std::strcmp(opt, fnm[i].opt) == 0)
- {
- return (gmx_bool) IS_SET(fnm[i]);
- }
+ return (gmx_bool) IS_SET(*fileOption);
}
GMX_RELEASE_ASSERT(false, "opt2bSet should be called with a valid option");
const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
{
- int i;
+ const t_filenm *fileOption = getFilenm(opt, nfile, fnm);
- for (i = 0; (i < nfile); i++)
+ if (fileOption)
{
- if (std::strcmp(opt, fnm[i].opt) == 0)
+ if (IS_OPT(*fileOption) && !IS_SET(*fileOption))
{
- if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
- {
- return nullptr;
- }
- else
- {
- return fnm[i].fns[0];
- }
+ return nullptr;
+ }
+ else
+ {
+ return fileOption->fns[0];
}
}
*
* \inpublicapi
*/
-struct t_filenm {
+struct t_filenm
+{
int ftp; //!< File type (see enum in filetypes.h)
- const char *opt; //!< Command line option
- const char *fn; //!< File name (as set in source code)
+ const char *opt; //!< Command line option, can be nullptr in which case the commandline module, including all opt2??? functions below, will use the default option for the file type
+ const char *fn; //!< File name (as set in source code), can be nullptr in which case the commandline module will use the default file name for the file type
unsigned long flag; //!< Flag for all kinds of info (see defs)
int nfiles; //!< number of files
char **fns; //!< File names
void OptionsAdapter::filenmToOptions(Options *options, t_filenm *fnm)
{
- if (fnm->opt == nullptr)
- {
- // Existing code may use opt2fn() instead of ftp2fn() for
- // options that use the default option name, so we need to
- // keep the old behavior instead of fixing opt2fn().
- // TODO: Check that this is not the case, remove this, and make
- // opt2*() work even if fnm->opt is NULL for some options.
- fnm->opt = ftp2defopt(fnm->ftp);
- }
const bool bRead = ((fnm->flag & ffREAD) != 0);
const bool bWrite = ((fnm->flag & ffWRITE) != 0);
const bool bOptional = ((fnm->flag & ffOPT) != 0);
const bool bLibrary = ((fnm->flag & ffLIB) != 0);
const bool bMultiple = ((fnm->flag & ffMULT) != 0);
const bool bMissing = ((fnm->flag & ffALLOW_MISSING) != 0);
- const char *const name = &fnm->opt[1];
+ const char *const name = (fnm->opt ? &fnm->opt[1] : &ftp2defopt(fnm->ftp)[1]);
const char * defName = fnm->fn;
int defType = -1;
if (defName == nullptr)