* Copyright (c) 1991-2000, University of Groningen, The Netherlands.
* Copyright (c) 2001-2004, The GROMACS development team.
* Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,2020,2021, 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.
#include "gromacs/fileio/gmxfio.h"
#include "gromacs/fileio/xdrf.h"
+#include "gromacs/utility/enumerationhelpers.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/smalloc.h"
#include "gmxfio_impl.h"
/* Enumerated for data types in files */
-enum
-{
- eioREAL,
- eioFLOAT,
- eioDOUBLE,
- eioINT,
- eioINT32,
- eioINT64,
- eioUCHAR,
- eioCHAR,
- eioNCHAR,
- eioNUCHAR,
- eioUSHORT,
- eioRVEC,
- eioNRVEC,
- eioIVEC,
- eioSTRING,
- eioOPAQUE,
- eioNR
+enum class IOType : int
+{
+ REAL,
+ FLOAT,
+ DOUBLE,
+ INT,
+ INT32,
+ INT64,
+ UCHAR,
+ CHAR,
+ NCHAR,
+ NUCHAR,
+ USHORT,
+ RVEC,
+ NRVEC,
+ IVEC,
+ STRING,
+ OPAQUE,
+ Count
};
-static const char* eioNames[eioNR] = { "REAL", "FLOAT", "DOUBLE", "INT", "INT32", "INT64",
- "UCHAR", "CHAR", "NCHAR", "NUCHAR", "USHORT", "RVEC",
- "NRVEC", "IVEC", "STRING", "OPAQUE" };
+static const char* enumValueToString(IOType enumValue)
+{
+ constexpr gmx::EnumerationArray<IOType, const char*> ioTypeNames = {
+ "REAL", "FLOAT", "DOUBLE", "INT", "INT32", "INT64", "UCHAR", "CHAR",
+ "NCHAR", "NUCHAR", "USHORT", "RVEC", "NRVEC", "IVEC", "STRING", "OPAQUE"
+ };
+ return ioTypeNames[enumValue];
+}
void gmx_fio_setprecision(t_fileio* fio, gmx_bool bDouble)
{
}
/* check the number of items given against the type */
-static void gmx_fio_check_nitem(int eio, std::size_t nitem, const char* file, int line)
+static void gmx_fio_check_nitem(IOType eio, std::size_t nitem, const char* file, int line)
{
if ((nitem != 1)
- && !((eio == eioNRVEC) || (eio == eioNUCHAR) || (eio == eioNCHAR) || (eio == eioOPAQUE)))
+ && !((eio == IOType::NRVEC) || (eio == IOType::NUCHAR) || (eio == IOType::NCHAR)
+ || (eio == IOType::OPAQUE)))
{
gmx_fatal(FARGS,
"nitem may differ from 1 only for %s, %s, %s or %s, not for %s"
"(%s, %d)",
- eioNames[eioNUCHAR],
- eioNames[eioNRVEC],
- eioNames[eioNCHAR],
- eioNames[eioOPAQUE],
- eioNames[eio],
+ enumValueToString(IOType::NUCHAR),
+ enumValueToString(IOType::NRVEC),
+ enumValueToString(IOType::NCHAR),
+ enumValueToString(IOType::OPAQUE),
+ enumValueToString(eio),
file,
line);
}
}
/* output a data type error. */
-[[noreturn]] static void gmx_fio_fe(t_fileio* fio, int eio, const char* desc, const char* srcfile, int line)
+[[noreturn]] static void gmx_fio_fe(t_fileio* fio, IOType 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",
+ static_cast<int>(eio),
+ ((eio >= IOType::REAL) && (eio < IOType::Count)) ? enumValueToString(eio) : "unknown",
srcfile,
line);
}
/* This is the part that reads xdr files. */
-
static gmx_bool
-do_xdr(t_fileio* fio, void* item, std::size_t nitem, int eio, const char* desc, const char* srcfile, int line)
+do_xdr(t_fileio* fio, void* item, std::size_t nitem, IOType eio, const char* desc, const char* srcfile, int line)
{
unsigned char ucdum, *ucptr;
char cdum, *cptr;
gmx_fio_check_nitem(eio, nitem, srcfile, line);
switch (eio)
{
- case eioREAL:
+ case IOType::REAL:
if (fio->bDouble)
{
if (item && !fio->bRead)
}
}
break;
- case eioFLOAT:
+ case IOType::FLOAT:
if (item && !fio->bRead)
{
f = *(static_cast<float*>(item));
*(static_cast<float*>(item)) = f;
}
break;
- case eioDOUBLE:
+ case IOType::DOUBLE:
if (item && !fio->bRead)
{
d = *(static_cast<double*>(item));
*(static_cast<double*>(item)) = d;
}
break;
- case eioINT:
+ case IOType::INT:
if (item && !fio->bRead)
{
idum = *static_cast<int*>(item);
*static_cast<int*>(item) = idum;
}
break;
- case eioINT32:
+ case IOType::INT32:
if (item && !fio->bRead)
{
s32dum = *static_cast<int32_t*>(item);
*static_cast<int32_t*>(item) = s32dum;
}
break;
- case eioINT64:
+ case IOType::INT64:
if (item && !fio->bRead)
{
s64dum = *static_cast<int64_t*>(item);
*static_cast<int64_t*>(item) = s64dum;
}
break;
- case eioUCHAR:
+ case IOType::UCHAR:
if (item && !fio->bRead)
{
ucdum = *static_cast<unsigned char*>(item);
*static_cast<unsigned char*>(item) = ucdum;
}
break;
- case eioCHAR:
+ case IOType::CHAR:
if (item && !fio->bRead)
{
cdum = *static_cast<char*>(item);
*static_cast<char*>(item) = cdum;
}
break;
- case eioNCHAR:
+ case IOType::NCHAR:
cptr = static_cast<char*>(item);
GMX_RELEASE_ASSERT(nitem < static_cast<std::size_t>(std::numeric_limits<int>::max()),
"The XDR interface cannot handle array lengths > 2^31");
static_cast<unsigned int>(sizeof(char)),
reinterpret_cast<xdrproc_t>(xdr_char));
break;
- case eioNUCHAR:
+ case IOType::NUCHAR:
ucptr = static_cast<unsigned char*>(item);
GMX_RELEASE_ASSERT(nitem < static_cast<std::size_t>(std::numeric_limits<int>::max()),
"The XDR interface cannot handle array lengths > 2^31");
static_cast<unsigned int>(sizeof(unsigned char)),
reinterpret_cast<xdrproc_t>(xdr_u_char));
break;
- case eioUSHORT:
+ case IOType::USHORT:
if (item && !fio->bRead)
{
us = *static_cast<unsigned short*>(item);
*static_cast<unsigned short*>(item) = us;
}
break;
- case eioRVEC:
+ case IOType::RVEC:
if (fio->bDouble)
{
if (item && !fio->bRead)
}
}
break;
- case eioNRVEC:
+ case IOType::NRVEC:
ptr = nullptr;
res = 1;
for (std::size_t j = 0; j < nitem && res; j++)
{
ptr = (static_cast<rvec*>(item))[j];
}
- res = static_cast<bool_t>(do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line));
+ res = static_cast<bool_t>(do_xdr(fio, ptr, 1, IOType::RVEC, desc, srcfile, line));
}
break;
- case eioIVEC:
+ case IOType::IVEC:
iptr = static_cast<int*>(item);
res = 1;
for (m = 0; (m < DIM) && res; m++)
}
}
break;
- case eioSTRING:
+ case IOType::STRING:
{
char* cptr;
int slen;
}
break;
}
- case eioOPAQUE:
+ case IOType::OPAQUE:
{
if (item == nullptr && nitem > 0)
{
gmx_bool ret;
void* it = const_cast<char*>(item); /* ugh.. */
gmx_fio_lock(fio);
- ret = do_xdr(fio, it, 1, eioSTRING, desc, srcfile, line);
+ ret = do_xdr(fio, it, 1, IOType::STRING, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioREAL, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::REAL, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioFLOAT, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::FLOAT, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioDOUBLE, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::DOUBLE, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
if (fio->bRead)
{
int itmp = 0;
- ret = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+ ret = do_xdr(fio, &itmp, 1, IOType::INT, desc, srcfile, line);
*item = (itmp != 0);
}
else
{
int itmp = static_cast<int>(*item);
- ret = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+ ret = do_xdr(fio, &itmp, 1, IOType::INT, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioINT, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::INT, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioINT32, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::INT32, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioINT64, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::INT64, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioUCHAR, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::UCHAR, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioCHAR, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::CHAR, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioUSHORT, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::USHORT, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioRVEC, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::RVEC, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioIVEC, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::IVEC, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, item, 1, eioSTRING, desc, srcfile, line);
+ ret = do_xdr(fio, item, 1, IOType::STRING, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret;
gmx_fio_lock(fio);
- ret = do_xdr(fio, data, size, eioOPAQUE, desc, srcfile, line);
+ ret = do_xdr(fio, data, size, IOType::OPAQUE, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioREAL, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::REAL, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioFLOAT, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::FLOAT, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioDOUBLE, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::DOUBLE, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
if (fio->bRead)
{
int itmp = 0;
- ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &itmp, 1, IOType::INT, desc, srcfile, line);
item[i] = (itmp != 0);
}
else
{
int itmp = static_cast<int>(item[i]);
- ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &itmp, 1, IOType::INT, desc, srcfile, line);
}
}
gmx_fio_unlock(fio);
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioINT, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::INT, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioINT64, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::INT64, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
{
gmx_bool ret = TRUE;
gmx_fio_lock(fio);
- ret = ret && do_xdr(fio, item, n, eioNUCHAR, desc, srcfile, line);
+ ret = ret && do_xdr(fio, item, n, IOType::NUCHAR, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
{
gmx_bool ret = TRUE;
gmx_fio_lock(fio);
- ret = ret && do_xdr(fio, item, n, eioNCHAR, desc, srcfile, line);
+ ret = ret && do_xdr(fio, item, n, IOType::NCHAR, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioUSHORT, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::USHORT, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
{
gmx_bool ret = TRUE;
gmx_fio_lock(fio);
- ret = ret && do_xdr(fio, item, n, eioNRVEC, desc, srcfile, line);
+ ret = ret && do_xdr(fio, item, n, IOType::NRVEC, desc, srcfile, line);
gmx_fio_unlock(fio);
return ret;
}
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioIVEC, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::IVEC, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;
gmx_fio_lock(fio);
for (i = 0; i < n; i++)
{
- ret = ret && do_xdr(fio, &(item[i]), 1, eioSTRING, desc, srcfile, line);
+ ret = ret && do_xdr(fio, &(item[i]), 1, IOType::STRING, desc, srcfile, line);
}
gmx_fio_unlock(fio);
return ret;