*
* Copyright (c) 1991-2000, University of Groningen, The Netherlands.
* Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * 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.
*/
#include "gmxpre.h"
-#include <limits.h>
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <climits>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+#include <algorithm>
#include "gromacs/fileio/xdr_datatype.h"
#include "gromacs/fileio/xdrf.h"
*/
#define MAXABS INT_MAX-2
-#ifndef MIN
-#define MIN(x, y) ((x) < (y) ? (x) : (y))
-#endif
-#ifndef MAX
-#define MAX(x, y) ((x) > (y) ? (x) : (y))
-#endif
#ifndef SQR
#define SQR(x) ((x)*(x))
#endif
#define FIRSTIDX 9
/* note that magicints[FIRSTIDX-1] == 0 */
-#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
+#define LASTIDX static_cast<int>((sizeof(magicints) / sizeof(*magicints)))
/*____________________________________________________________________________
int lastbits;
unsigned char * cbuf;
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
- cnt = (unsigned int) buf[0];
+ cbuf = (reinterpret_cast<unsigned char *>(buf)) + 3 * sizeof(*buf);
+ cnt = static_cast<unsigned int>(buf[0]);
lastbits = buf[1];
- lastbyte = (unsigned int) buf[2];
+ lastbyte = static_cast<unsigned int>(buf[2]);
while (num_of_bits >= 8)
{
lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
unsigned char * cbuf;
int mask = (1 << num_of_bits) -1;
- cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
+ cbuf = reinterpret_cast<unsigned char *>(buf) + 3 * sizeof(*buf);
cnt = buf[0];
- lastbits = (unsigned int) buf[1];
- lastbyte = (unsigned int) buf[2];
+ lastbits = static_cast<unsigned int>(buf[1]);
+ lastbyte = static_cast<unsigned int>(buf[2]);
num = 0;
while (num_of_bits >= 8)
int tmp, *thiscoord, prevcoord[3];
unsigned int tmpcoord[30];
- int bufsize, xdrid, lsize;
+ int bufsize, lsize;
unsigned int bitsize;
float inv_precision;
int errval = 1;
bitsizeint[0] = bitsizeint[1] = bitsizeint[2] = 0;
prevcoord[0] = prevcoord[1] = prevcoord[2] = 0;
+ // The static analyzer warns about garbage values for thiscoord[] further
+ // down. It might be thrown off by all the reinterpret_casts, but we might
+ // as well make sure the small preallocated buffer is zero-initialized.
+ for (i = 0; i < static_cast<int>(prealloc_size); i++)
+ {
+ prealloc_ip[i] = 0;
+ }
+
if (!bRead)
{
/* xdrs is open for writing */
*/
if (*size <= 9)
{
- return (xdr_vector(xdrs, (char *) fp, (unsigned int)size3,
- (unsigned int)sizeof(*fp), (xdrproc_t)xdr_float));
+ return (xdr_vector(xdrs, reinterpret_cast<char *>(fp), static_cast<unsigned int>(size3),
+ static_cast<unsigned int>(sizeof(*fp)), (xdrproc_t)xdr_float));
}
if (xdr_float(xdrs, precision) == 0)
else
{
we_should_free = 1;
- bufsize = size3 * 1.2;
- ip = (int *)malloc((size_t)(size3 * sizeof(*ip)));
- buf = (int *)malloc((size_t)(bufsize * sizeof(*buf)));
+ bufsize = static_cast<int>(size3 * 1.2);
+ ip = reinterpret_cast<int *>(malloc(size3 * sizeof(*ip)));
+ buf = reinterpret_cast<int *>(malloc(bufsize * sizeof(*buf)));
if (ip == NULL || buf == NULL)
{
fprintf(stderr, "malloc failed\n");
/* scaling would cause overflow */
errval = 0;
}
- lint1 = lf;
+ lint1 = static_cast<int>(lf);
if (lint1 < minint[0])
{
minint[0] = lint1;
/* scaling would cause overflow */
errval = 0;
}
- lint2 = lf;
+ lint2 = static_cast<int>(lf);
if (lint2 < minint[1])
{
minint[1] = lint2;
{
lf = *lfp * *precision - 0.5;
}
- if (fabs(lf) > MAXABS)
+ if (std::abs(lf) > MAXABS)
{
/* scaling would cause overflow */
errval = 0;
}
- lint3 = lf;
+ lint3 = static_cast<int>(lf);
if (lint3 < minint[2])
{
minint[2] = lint3;
}
*lip++ = lint3;
lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
+ diff = std::abs(oldlint1-lint1)+std::abs(oldlint2-lint2)+std::abs(oldlint3-lint3);
if (diff < mindiff && lfp > fp + 3)
{
mindiff = diff;
{
bitsize = sizeofints(3, sizeint);
}
- lip = ip;
- luip = (unsigned int *) ip;
+ luip = reinterpret_cast<unsigned int *>(ip);
smallidx = FIRSTIDX;
while (smallidx < LASTIDX && magicints[smallidx] < mindiff)
{
return 0;
}
- maxidx = MIN(LASTIDX, smallidx + 8);
+ maxidx = std::min(LASTIDX, smallidx + 8);
minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ smaller = magicints[std::max(FIRSTIDX, smallidx-1)] / 2;
smallnum = magicints[smallidx] / 2;
sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
larger = magicints[maxidx] / 2;
while (i < *size)
{
is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
+ thiscoord = reinterpret_cast<int *>(luip) + i * 3;
if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger)
+ std::abs(thiscoord[0] - prevcoord[0]) < larger &&
+ std::abs(thiscoord[1] - prevcoord[1]) < larger &&
+ std::abs(thiscoord[2] - prevcoord[2]) < larger)
{
is_smaller = 1;
}
}
if (i + 1 < *size)
{
- if (abs(thiscoord[0] - thiscoord[3]) < smallnum &&
- abs(thiscoord[1] - thiscoord[4]) < smallnum &&
- abs(thiscoord[2] - thiscoord[5]) < smallnum)
+ if (std::abs(thiscoord[0] - thiscoord[3]) < smallnum &&
+ std::abs(thiscoord[1] - thiscoord[4]) < smallnum &&
+ std::abs(thiscoord[2] - thiscoord[5]) < smallnum)
{
/* interchange first with second atom for better
* compression of water molecules
}
- rc = errval * (xdr_opaque(xdrs, (char *)&(buf[3]), (unsigned int)buf[0]));
+ rc = errval * (xdr_opaque(xdrs, reinterpret_cast<char *>(&(buf[3])), static_cast<unsigned int>(buf[0])));
if (we_should_free)
{
free(ip);
if (*size <= 9)
{
*precision = -1;
- return (xdr_vector(xdrs, (char *) fp, (unsigned int)size3,
- (unsigned int)sizeof(*fp), (xdrproc_t)xdr_float));
+ return (xdr_vector(xdrs, reinterpret_cast<char *>(fp), static_cast<unsigned int>(size3),
+ static_cast<unsigned int>(sizeof(*fp)), (xdrproc_t)xdr_float));
}
if (xdr_float(xdrs, precision) == 0)
{
else
{
we_should_free = 1;
- bufsize = size3 * 1.2;
- ip = (int *)malloc((size_t)(size3 * sizeof(*ip)));
- buf = (int *)malloc((size_t)(bufsize * sizeof(*buf)));
+ bufsize = static_cast<int>(size3 * 1.2);
+ ip = reinterpret_cast<int *>(malloc(size3 * sizeof(*ip)));
+ buf = reinterpret_cast<int *>(malloc(bufsize * sizeof(*buf)));
if (ip == NULL || buf == NULL)
{
fprintf(stderr, "malloc failed\n");
return 0;
}
- maxidx = MIN(LASTIDX, smallidx + 8);
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ smaller = magicints[std::max(FIRSTIDX, smallidx-1)] / 2;
smallnum = magicints[smallidx] / 2;
sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx];
/* buf[0] holds the length in bytes */
}
- if (xdr_opaque(xdrs, (char *)&(buf[3]), (unsigned int)buf[0]) == 0)
+ if (xdr_opaque(xdrs, reinterpret_cast<char *>(&(buf[3])), static_cast<unsigned int>(buf[0])) == 0)
{
if (we_should_free)
{
lip = ip;
while (i < lsize)
{
- thiscoord = (int *)(lip) + i * 3;
+ thiscoord = reinterpret_cast<int *>(lip) + i * 3;
if (bitsize == 0)
{
static gmx_off_t xtc_get_next_frame_start(FILE *fp, XDR *xdrs, int natoms)
{
- int inp;
gmx_off_t res;
int ret;
int step;
gmx_bool bOK = FALSE;
gmx_off_t low = 0;
gmx_off_t high, offset, pos;
- int res;
int dt_sign = 0;
if (bSeekForwardOnly)
{
float time;
gmx_off_t off;
- int res;
*bOK = 1;
off = gmx_ftell(fp);
if (off < 0)
return -1;
}
- if ( (res = gmx_fseek(fp, -3*XDR_INT_SIZE, SEEK_END)) != 0)
+ if (gmx_fseek(fp, -3*XDR_INT_SIZE, SEEK_END) != 0)
{
*bOK = 0;
return -1;
return -1;
}
- if ( (res = gmx_fseek(fp, off, SEEK_SET)) != 0)
+ if (gmx_fseek(fp, off, SEEK_SET) != 0)
{
*bOK = 0;
return -1;
{
int frame;
gmx_off_t off;
- int res;
*bOK = 1;
if ((off = gmx_ftell(fp)) < 0)