Move fileio sources to C++
[alexxy/gromacs.git] / src / gromacs / fileio / enxio.cpp
similarity index 97%
rename from src/gromacs/fileio/enxio.c
rename to src/gromacs/fileio/enxio.cpp
index 892ef18119eead0d04ae0aa1f3f8f2fb5b73cc65..ece8f4eea44334e7271851864b1127f2fb214628 100644 (file)
 
 #include "enxio.h"
 
-#include <stdlib.h>
-#include <string.h>
+#include <cstdlib>
+#include <cstring>
+
+#include <algorithm>
 
 #include "gromacs/fileio/gmxfio.h"
 #include "gromacs/fileio/gmxfio-xdr.h"
@@ -50,6 +52,7 @@
 #include "gromacs/topology/topology.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
 
 /* The source code in this file should be thread-safe.
@@ -386,7 +389,6 @@ void do_enxnms(ener_file_t ef, int *nre, gmx_enxnm_t **nms)
     XDR     *xdr;
     gmx_bool bRead = gmx_fio_getread(ef->fio);
     int      file_version;
-    int      i;
 
     xdr = gmx_fio_getxdr(ef->fio);
 
@@ -438,9 +440,8 @@ static gmx_bool do_eheader(ener_file_t ef, int *file_version, t_enxframe *fr,
 {
     int          magic = -7777777;
     real         first_real_to_check;
-    int          b, i, zero = 0, dum = 0;
+    int          b, zero = 0, dum = 0;
     gmx_bool     bRead      = gmx_fio_getread(ef->fio);
-    int          tempfix_nr = 0;
     int          ndisre     = 0;
     int          startb     = 0;
 #ifndef GMX_DOUBLE
@@ -536,7 +537,7 @@ static gmx_bool do_eheader(ener_file_t ef, int *file_version, t_enxframe *fr,
         }
         else
         {
-            fr->nsteps = max(1, fr->nsum);
+            fr->nsteps = std::max(1, fr->nsum);
         }
         if (*file_version >= 5)
         {
@@ -596,7 +597,10 @@ static gmx_bool do_eheader(ener_file_t ef, int *file_version, t_enxframe *fr,
         ((fr->nre > 0 && fr->nre != nre_test) ||
          fr->nre < 0 || ndisre < 0 || fr->nblock < 0))
     {
-        *bWrongPrecision = TRUE;
+        if (bWrongPrecision)
+        {
+            *bWrongPrecision = TRUE;
+        }
         return *bOK;
     }
 
@@ -746,7 +750,17 @@ void close_enx(ener_file_t ef)
     }
 }
 
-static gmx_bool empty_file(const char *fn)
+/*!\brief Return TRUE if a file exists but is empty, otherwise FALSE.
+ *
+ * If the file exists but has length larger than zero, if it does not exist, or
+ * if there is a file system error, FALSE will be returned instead.
+ *
+ * \param fn  File name to test
+ *
+ * \return TRUE if file could be open but is empty, otherwise FALSE.
+ */
+static gmx_bool
+empty_file(const char *fn)
 {
     FILE    *fp;
     char     dum;
@@ -758,13 +772,15 @@ static gmx_bool empty_file(const char *fn)
     bEmpty = feof(fp);
     gmx_fio_fclose(fp);
 
-    return bEmpty;
+    // bEmpty==TRUE but ret!=0 would likely be some strange I/O error, but at
+    // least it's not a normal empty file, so we return FALSE in that case.
+    return (bEmpty && ret == 0);
 }
 
 
 ener_file_t open_enx(const char *fn, const char *mode)
 {
-    int               nre, i;
+    int               nre;
     gmx_enxnm_t      *nms          = NULL;
     int               file_version = -1;
     t_enxframe       *fr;
@@ -970,7 +986,7 @@ gmx_bool do_enx(ener_file_t ef, t_enxframe *fr)
     {
         fprintf(stderr, "\nWARNING: there may be something wrong with energy file %s\n",
                 gmx_fio_getname(ef->fio));
-        fprintf(stderr, "Found: step=%"GMX_PRId64 ", nre=%d, nblock=%d, time=%g.\n"
+        fprintf(stderr, "Found: step=%" GMX_PRId64 ", nre=%d, nblock=%d, time=%g.\n"
                 "Trying to skip frame expect a crash though\n",
                 fr->step, fr->nre, fr->nblock, fr->t);
     }
@@ -1045,7 +1061,6 @@ gmx_bool do_enx(ener_file_t ef, t_enxframe *fr)
             }
 
             /* read/write data */
-            bOK1 = TRUE;
             switch (sub->type)
             {
                 case xdr_datatype_float:
@@ -1107,7 +1122,7 @@ static real find_energy(const char *name, int nre, gmx_enxnm_t *enm,
 
     for (i = 0; i < nre; i++)
     {
-        if (strcmp(enm[i].name, name) == 0)
+        if (std::strcmp(enm[i].name, name) == 0)
         {
             return fr->ener[i].e;
         }
@@ -1128,10 +1143,6 @@ void get_enx_state(const char *fn, real t, gmx_groups_t *groups, t_inputrec *ir,
         "Box-Vel-YX", "Box-Vel-ZX", "Box-Vel-ZY"
     };
 
-    static const char *pcouplmu_nm[] = {
-        "Pcoupl-Mu-XX", "Pcoupl-Mu-YY", "Pcoupl-Mu-ZZ",
-        "Pcoupl-Mu-YX", "Pcoupl-Mu-ZX", "Pcoupl-Mu-ZY"
-    };
     static const char *baro_nm[] = {
         "Barostat"
     };