Leave nullptr opt in t_filenm
authorBerk Hess <hess@kth.se>
Fri, 23 Feb 2018 13:01:15 +0000 (14:01 +0100)
committerBerk Hess <hess@kth.se>
Mon, 5 Mar 2018 13:32:13 +0000 (14:32 +0100)
File options that use the default extension of the file type by
setting opt=nullptr in t_filenm are no longer updated by
parse_common_args. Instead the (default) option name is looked up
based on the file type in the utilty functions.
This is preparation for converting t_filenm to C++.

Change-Id: I907c1e88f91ab2e524a72c03c27fd67ad641bc02

src/gromacs/commandline/filenm.cpp
src/gromacs/commandline/filenm.h
src/gromacs/commandline/pargs.cpp

index 904be4ce834b9eebf0d89ffa5e10e8bac8639d4f..3cefb0c3e73fec6b80c8a22e75c5ce3e0f277a42 100644 (file)
 #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];
         }
@@ -68,14 +76,11 @@ const t_filenm *getFilenm(const char *opt, int nfile, const t_filenm fnm[])
 
 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");
@@ -85,15 +90,12 @@ const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
 
 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");
@@ -155,14 +157,11 @@ gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
 
 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");
@@ -172,20 +171,17 @@ gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
 
 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];
         }
     }
 
index 6d2a3545bd7b66418b90f6947ce6a328a399fad0..d39b54909a305dba665054352e598588a549f0d9 100644 (file)
  *
  * \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
index 311cfbbc30286ae3fb323239f7e20ac68affea1c..c14efdabcfaa4b8d0ab8b6bf47e00782cedfda27 100644 (file)
@@ -316,22 +316,13 @@ class OptionsAdapter
 
 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)