Reorder code within gmxfio
authorTeemu Murtola <teemu.murtola@gmail.com>
Tue, 30 Jun 2015 10:51:10 +0000 (13:51 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Thu, 2 Jul 2015 05:36:15 +0000 (08:36 +0300)
Move all XDR writing code, including helper functions only used by it,
into gmxfio_xdr.c and rename it into gmxfio-xdr.c.  Remove these from
gmxfio-impl.h and make them static.  No changes to the actual code,
except for some handling of the itmp variables in gmx_bool functions to
satisfy gcc maybe-uninitialized warnings.

Change-Id: If2190f9429585ac0e2ee86b24dd41969f4ae8d49

src/gromacs/fileio/gmxfio-impl.h
src/gromacs/fileio/gmxfio-xdr.c [moved from src/gromacs/fileio/gmxfio_rw.c with 53% similarity]
src/gromacs/fileio/gmxfio.c
src/gromacs/fileio/gmxfio_xdr.c [deleted file]

index e91745a52ded5bad4d2b9b328345f8254ff90204..f42facdc52b2c9a29e685acefe7bfd92dce9b444 100644 (file)
@@ -79,23 +79,9 @@ struct t_fileio
                                           a lock */
 };
 
-/** Names for different items that can be read/written with gmx_fio_do_*() */
-extern const char    *eioNames[eioNR];
-
-/** check the number of items against the allowed number of items */
-void gmx_fio_check_nitem(int eio, int nitem, const char *file,
-                         int line);
-/** check the output type against allowed values */
-void gmx_fio_fe(t_fileio *fio, int eio, const char *desc, const char *srcfile,
-                int line);
-
 /** lock the mutex associated with a fio  */
 void gmx_fio_lock(t_fileio *fio);
 /** unlock the mutex associated with a fio  */
 void gmx_fio_unlock(t_fileio *fio);
 
-/** xdr read/write routine */
-gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
-                const char *desc, const char *srcfile, int line);
-
 #endif
similarity index 53%
rename from src/gromacs/fileio/gmxfio_rw.c
rename to src/gromacs/fileio/gmxfio-xdr.c
index 3e180847cb584a9400713496555a3377a5ddb0b5..6b9b5eb56238e67d5fa27868ab6db6ffd974b730 100644 (file)
  */
 #include "gmxpre.h"
 
+#include <stdio.h>
+#include <string.h>
+
 #include "gromacs/fileio/gmxfio.h"
+#include "gromacs/fileio/xdrf.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
 
 #include "gmxfio-impl.h"
 
+static const char *eioNames[eioNR] =
+{
+    "REAL", "INT", "GMX_STE_T", "UCHAR", "NUCHAR", "USHORT", "RVEC", "NRVEC",
+    "IVEC", "STRING"
+};
+
+/* check the number of items given against the type */
+static void gmx_fio_check_nitem(int eio, int nitem, const char *file, int line)
+{
+    if ((nitem != 1) && !((eio == eioNRVEC) || (eio == eioNUCHAR)))
+    {
+        gmx_fatal(FARGS,
+                  "nitem (%d) may differ from 1 only for %s or %s, not   for %s"
+                  "(%s, %d)", nitem, eioNames[eioNUCHAR], eioNames[eioNRVEC],
+                  eioNames[eio], file, line);
+    }
+}
+
+
+/* output a data type error. */
+static void gmx_fio_fe(t_fileio *fio, int eio, const char *desc,
+                       const char *srcfile, int line)
+{
+
+    gmx_fatal(FARGS, "Trying to %s %s type %d (%s), src %s, line %d",
+              fio->bRead ? "read" : "write", desc, eio,
+              ((eio >= 0) && (eio < eioNR)) ? eioNames[eio] : "unknown",
+              srcfile, line);
+}
+
+/* This is the part that reads xdr files.  */
+
+static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
+                       const char *desc, const char *srcfile, int line)
+{
+    unsigned char   ucdum, *ucptr;
+    bool_t          res = 0;
+    float           fvec[DIM];
+    double          dvec[DIM];
+    int             j, m, *iptr, idum;
+    gmx_int64_t     sdum;
+    real           *ptr;
+    unsigned short  us;
+    double          d = 0;
+    float           f = 0;
+
+    gmx_fio_check_nitem(eio, nitem, srcfile, line);
+    switch (eio)
+    {
+        case eioREAL:
+            if (fio->bDouble)
+            {
+                if (item && !fio->bRead)
+                {
+                    d = *((real *) item);
+                }
+                res = xdr_double(fio->xdr, &d);
+                if (item)
+                {
+                    *((real *) item) = d;
+                }
+            }
+            else
+            {
+                if (item && !fio->bRead)
+                {
+                    f = *((real *) item);
+                }
+                res = xdr_float(fio->xdr, &f);
+                if (item)
+                {
+                    *((real *) item) = f;
+                }
+            }
+            break;
+        case eioFLOAT:
+            if (item && !fio->bRead)
+            {
+                f = *((float *) item);
+            }
+            res = xdr_float(fio->xdr, &f);
+            if (item)
+            {
+                *((float *) item) = f;
+            }
+            break;
+        case eioDOUBLE:
+            if (item && !fio->bRead)
+            {
+                d = *((double *) item);
+            }
+            res = xdr_double(fio->xdr, &d);
+            if (item)
+            {
+                *((double *) item) = d;
+            }
+            break;
+        case eioINT:
+            if (item && !fio->bRead)
+            {
+                idum = *(int *) item;
+            }
+            res = xdr_int(fio->xdr, &idum);
+            if (item)
+            {
+                *(int *) item = idum;
+            }
+            break;
+        case eioINT64:
+            if (item && !fio->bRead)
+            {
+                sdum = *(gmx_int64_t *) item;
+            }
+            res = xdr_int64(fio->xdr, &sdum);
+            if (item)
+            {
+                *(gmx_int64_t *) item = sdum;
+            }
+            break;
+        case eioUCHAR:
+            if (item && !fio->bRead)
+            {
+                ucdum = *(unsigned char *) item;
+            }
+            res = xdr_u_char(fio->xdr, &ucdum);
+            if (item)
+            {
+                *(unsigned char *) item = ucdum;
+            }
+            break;
+        case eioNUCHAR:
+            ucptr = (unsigned char *) item;
+            res   = 1;
+            for (j = 0; (j < nitem) && res; j++)
+            {
+                res = xdr_u_char(fio->xdr, &(ucptr[j]));
+            }
+            break;
+        case eioUSHORT:
+            if (item && !fio->bRead)
+            {
+                us = *(unsigned short *) item;
+            }
+            res = xdr_u_short(fio->xdr, (unsigned short *) &us);
+            if (item)
+            {
+                *(unsigned short *) item = us;
+            }
+            break;
+        case eioRVEC:
+            if (fio->bDouble)
+            {
+                if (item && !fio->bRead)
+                {
+                    for (m = 0; (m < DIM); m++)
+                    {
+                        dvec[m] = ((real *) item)[m];
+                    }
+                }
+                res = xdr_vector(fio->xdr, (char *) dvec, DIM,
+                                 (unsigned int) sizeof(double),
+                                 (xdrproc_t) xdr_double);
+                if (item)
+                {
+                    for (m = 0; (m < DIM); m++)
+                    {
+                        ((real *) item)[m] = dvec[m];
+                    }
+                }
+            }
+            else
+            {
+                if (item && !fio->bRead)
+                {
+                    for (m = 0; (m < DIM); m++)
+                    {
+                        fvec[m] = ((real *) item)[m];
+                    }
+                }
+                res = xdr_vector(fio->xdr, (char *) fvec, DIM,
+                                 (unsigned int) sizeof(float),
+                                 (xdrproc_t) xdr_float);
+                if (item)
+                {
+                    for (m = 0; (m < DIM); m++)
+                    {
+                        ((real *) item)[m] = fvec[m];
+                    }
+                }
+            }
+            break;
+        case eioNRVEC:
+            ptr = NULL;
+            res = 1;
+            for (j = 0; (j < nitem) && res; j++)
+            {
+                if (item)
+                {
+                    ptr = ((rvec *) item)[j];
+                }
+                res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
+            }
+            break;
+        case eioIVEC:
+            iptr = (int *) item;
+            res  = 1;
+            for (m = 0; (m < DIM) && res; m++)
+            {
+                if (item && !fio->bRead)
+                {
+                    idum = iptr[m];
+                }
+                res = xdr_int(fio->xdr, &idum);
+                if (item)
+                {
+                    iptr[m] = idum;
+                }
+            }
+            break;
+        case eioSTRING:
+        {
+            char *cptr;
+            int   slen;
+
+            if (item)
+            {
+                if (!fio->bRead)
+                {
+                    slen = strlen((char *) item) + 1;
+                }
+                else
+                {
+                    slen = 0;
+                }
+            }
+            else
+            {
+                slen = 0;
+            }
+
+            if (xdr_int(fio->xdr, &slen) <= 0)
+            {
+                gmx_fatal(FARGS, "wrong string length %d for string %s"
+                          " (source %s, line %d)", slen, desc, srcfile, line);
+            }
+            if (!item && fio->bRead)
+            {
+                snew(cptr, slen);
+            }
+            else
+            {
+                cptr = (char *)item;
+            }
+            if (cptr)
+            {
+                res = xdr_string(fio->xdr, &cptr, slen);
+            }
+            else
+            {
+                res = 1;
+            }
+            if (!item && fio->bRead)
+            {
+                sfree(cptr);
+            }
+            break;
+        }
+        default:
+            gmx_fio_fe(fio, eio, desc, srcfile, line);
+    }
+    if ((res == 0) && (fio->bDebug))
+    {
+        fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
+                eioNames[eio], desc, fio->fn, srcfile, line);
+    }
+
+    return (res != 0);
+}
+
 /*******************************************************************
  *
  * READ/WRITE FUNCTIONS
@@ -93,18 +378,18 @@ gmx_bool gmx_fio_doe_gmx_bool(t_fileio *fio, gmx_bool *item,
                               const char *desc, const char *srcfile, int line)
 {
     gmx_bool ret;
-    int      itmp;
 
     gmx_fio_lock(fio);
     if (fio->bRead)
     {
-        ret   = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
-        *item = itmp;
+        int itmp = 0;
+        ret      = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+        *item    = itmp;
     }
     else
     {
-        itmp = *item;
-        ret  = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+        int itmp = *item;
+        ret      = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
     }
     gmx_fio_unlock(fio);
     return ret;
@@ -238,22 +523,21 @@ gmx_bool gmx_fio_ndoe_gmx_bool(t_fileio *fio, gmx_bool *item, int n,
                                const char *desc, const char *srcfile, int line)
 {
     gmx_bool ret = TRUE;
-    int      i, itmp;
+    int      i;
 
     gmx_fio_lock(fio);
     for (i = 0; i < n; i++)
     {
         if (fio->bRead)
         {
-            ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc,
-                                srcfile, line);
-            item[i] = itmp;
+            int itmp = 0;
+            ret      = ret && do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+            item[i]  = itmp;
         }
         else
         {
-            itmp = item[i];
-            ret  = ret && do_xdr(fio, &itmp, 1, eioINT, desc,
-                                 srcfile, line);
+            int itmp = item[i];
+            ret      = ret && do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
         }
     }
     gmx_fio_unlock(fio);
index cd487b1b5cc771008ea74e3700ca3b1592bc0a5e..6b000142439d2e8c9e10bc48e3a575d89d0915b2 100644 (file)
@@ -81,12 +81,6 @@ static t_fileio *open_files = NULL;
    during the simulation. */
 static tMPI_Thread_mutex_t open_file_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
 
-const char                *eioNames[eioNR] =
-{
-    "REAL", "INT", "GMX_STE_T", "UCHAR", "NUCHAR", "USHORT", "RVEC", "NRVEC",
-    "IVEC", "STRING"
-};
-
 /******************************************************************
  *
  * Internal functions:
@@ -109,31 +103,6 @@ static int gmx_fio_int_flush(t_fileio* fio)
     return rc;
 }
 
-
-/* check the number of items given against the type */
-void gmx_fio_check_nitem(int eio, int nitem, const char *file, int line)
-{
-    if ((nitem != 1) && !((eio == eioNRVEC) || (eio == eioNUCHAR)))
-    {
-        gmx_fatal(FARGS,
-                  "nitem (%d) may differ from 1 only for %s or %s, not   for %s"
-                  "(%s, %d)", nitem, eioNames[eioNUCHAR], eioNames[eioNRVEC],
-                  eioNames[eio], file, line);
-    }
-}
-
-
-/* output a data type error. */
-void gmx_fio_fe(t_fileio *fio, int eio, const char *desc,
-                const char *srcfile, int line)
-{
-
-    gmx_fatal(FARGS, "Trying to %s %s type %d (%s), src %s, line %d",
-              fio->bRead ? "read" : "write", desc, eio,
-              ((eio >= 0) && (eio < eioNR)) ? eioNames[eio] : "unknown",
-              srcfile, line);
-}
-
 /* lock the mutex associated with this fio. This needs to be done for every
    type of access to the fio's elements. */
 void gmx_fio_lock(t_fileio *fio)
diff --git a/src/gromacs/fileio/gmxfio_xdr.c b/src/gromacs/fileio/gmxfio_xdr.c
deleted file mode 100644 (file)
index 3823670..0000000
+++ /dev/null
@@ -1,296 +0,0 @@
-/*
- * This file is part of the GROMACS molecular simulation package.
- *
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
- * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
- * and including many others, as listed in the AUTHORS file in the
- * top-level source directory and at http://www.gromacs.org.
- *
- * GROMACS is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1
- * of the License, or (at your option) any later version.
- *
- * GROMACS is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with GROMACS; if not, see
- * http://www.gnu.org/licenses, or write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
- *
- * If you want to redistribute modifications to GROMACS, please
- * consider that scientific software is very special. Version
- * control is crucial - bugs must be traceable. We will be happy to
- * consider code for inclusion in the official distribution, but
- * derived work must not be called official GROMACS. Details are found
- * in the README & COPYING files - if they are missing, get the
- * official version at http://www.gromacs.org.
- *
- * To help us fund GROMACS development, we humbly ask that you cite
- * the research papers on the package. Check out http://www.gromacs.org.
- */
-#include "gmxpre.h"
-
-#include <stdio.h>
-#include <string.h>
-
-#include "gromacs/fileio/gmxfio.h"
-#include "gromacs/fileio/xdrf.h"
-#include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/smalloc.h"
-
-#include "gmxfio-impl.h"
-
-/* This is the part that reads xdr files.  */
-
-gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
-                const char *desc, const char *srcfile, int line)
-{
-    unsigned char   ucdum, *ucptr;
-    bool_t          res = 0;
-    float           fvec[DIM];
-    double          dvec[DIM];
-    int             j, m, *iptr, idum;
-    gmx_int64_t     sdum;
-    real           *ptr;
-    unsigned short  us;
-    double          d = 0;
-    float           f = 0;
-
-    gmx_fio_check_nitem(eio, nitem, srcfile, line);
-    switch (eio)
-    {
-        case eioREAL:
-            if (fio->bDouble)
-            {
-                if (item && !fio->bRead)
-                {
-                    d = *((real *) item);
-                }
-                res = xdr_double(fio->xdr, &d);
-                if (item)
-                {
-                    *((real *) item) = d;
-                }
-            }
-            else
-            {
-                if (item && !fio->bRead)
-                {
-                    f = *((real *) item);
-                }
-                res = xdr_float(fio->xdr, &f);
-                if (item)
-                {
-                    *((real *) item) = f;
-                }
-            }
-            break;
-        case eioFLOAT:
-            if (item && !fio->bRead)
-            {
-                f = *((float *) item);
-            }
-            res = xdr_float(fio->xdr, &f);
-            if (item)
-            {
-                *((float *) item) = f;
-            }
-            break;
-        case eioDOUBLE:
-            if (item && !fio->bRead)
-            {
-                d = *((double *) item);
-            }
-            res = xdr_double(fio->xdr, &d);
-            if (item)
-            {
-                *((double *) item) = d;
-            }
-            break;
-        case eioINT:
-            if (item && !fio->bRead)
-            {
-                idum = *(int *) item;
-            }
-            res = xdr_int(fio->xdr, &idum);
-            if (item)
-            {
-                *(int *) item = idum;
-            }
-            break;
-        case eioINT64:
-            if (item && !fio->bRead)
-            {
-                sdum = *(gmx_int64_t *) item;
-            }
-            res = xdr_int64(fio->xdr, &sdum);
-            if (item)
-            {
-                *(gmx_int64_t *) item = sdum;
-            }
-            break;
-        case eioUCHAR:
-            if (item && !fio->bRead)
-            {
-                ucdum = *(unsigned char *) item;
-            }
-            res = xdr_u_char(fio->xdr, &ucdum);
-            if (item)
-            {
-                *(unsigned char *) item = ucdum;
-            }
-            break;
-        case eioNUCHAR:
-            ucptr = (unsigned char *) item;
-            res   = 1;
-            for (j = 0; (j < nitem) && res; j++)
-            {
-                res = xdr_u_char(fio->xdr, &(ucptr[j]));
-            }
-            break;
-        case eioUSHORT:
-            if (item && !fio->bRead)
-            {
-                us = *(unsigned short *) item;
-            }
-            res = xdr_u_short(fio->xdr, (unsigned short *) &us);
-            if (item)
-            {
-                *(unsigned short *) item = us;
-            }
-            break;
-        case eioRVEC:
-            if (fio->bDouble)
-            {
-                if (item && !fio->bRead)
-                {
-                    for (m = 0; (m < DIM); m++)
-                    {
-                        dvec[m] = ((real *) item)[m];
-                    }
-                }
-                res = xdr_vector(fio->xdr, (char *) dvec, DIM,
-                                 (unsigned int) sizeof(double),
-                                 (xdrproc_t) xdr_double);
-                if (item)
-                {
-                    for (m = 0; (m < DIM); m++)
-                    {
-                        ((real *) item)[m] = dvec[m];
-                    }
-                }
-            }
-            else
-            {
-                if (item && !fio->bRead)
-                {
-                    for (m = 0; (m < DIM); m++)
-                    {
-                        fvec[m] = ((real *) item)[m];
-                    }
-                }
-                res = xdr_vector(fio->xdr, (char *) fvec, DIM,
-                                 (unsigned int) sizeof(float),
-                                 (xdrproc_t) xdr_float);
-                if (item)
-                {
-                    for (m = 0; (m < DIM); m++)
-                    {
-                        ((real *) item)[m] = fvec[m];
-                    }
-                }
-            }
-            break;
-        case eioNRVEC:
-            ptr = NULL;
-            res = 1;
-            for (j = 0; (j < nitem) && res; j++)
-            {
-                if (item)
-                {
-                    ptr = ((rvec *) item)[j];
-                }
-                res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
-            }
-            break;
-        case eioIVEC:
-            iptr = (int *) item;
-            res  = 1;
-            for (m = 0; (m < DIM) && res; m++)
-            {
-                if (item && !fio->bRead)
-                {
-                    idum = iptr[m];
-                }
-                res = xdr_int(fio->xdr, &idum);
-                if (item)
-                {
-                    iptr[m] = idum;
-                }
-            }
-            break;
-        case eioSTRING:
-        {
-            char *cptr;
-            int   slen;
-
-            if (item)
-            {
-                if (!fio->bRead)
-                {
-                    slen = strlen((char *) item) + 1;
-                }
-                else
-                {
-                    slen = 0;
-                }
-            }
-            else
-            {
-                slen = 0;
-            }
-
-            if (xdr_int(fio->xdr, &slen) <= 0)
-            {
-                gmx_fatal(FARGS, "wrong string length %d for string %s"
-                          " (source %s, line %d)", slen, desc, srcfile, line);
-            }
-            if (!item && fio->bRead)
-            {
-                snew(cptr, slen);
-            }
-            else
-            {
-                cptr = (char *)item;
-            }
-            if (cptr)
-            {
-                res = xdr_string(fio->xdr, &cptr, slen);
-            }
-            else
-            {
-                res = 1;
-            }
-            if (!item && fio->bRead)
-            {
-                sfree(cptr);
-            }
-            break;
-        }
-        default:
-            gmx_fio_fe(fio, eio, desc, srcfile, line);
-    }
-    if ((res == 0) && (fio->bDebug))
-    {
-        fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
-                eioNames[eio], desc, fio->fn, srcfile, line);
-    }
-
-    return (res != 0);
-}