diff --git a/src/Version.h b/src/Version.h index 8104cb0dc1..5f787e234e 100644 --- a/src/Version.h +++ b/src/Version.h @@ -12,7 +12,7 @@ * Whenever a number that precedes is incremented, all subsequent * numbers should be reset to 0. */ -#define CPPTRAJ_INTERNAL_VERSION "V7.12.5" +#define CPPTRAJ_INTERNAL_VERSION "V7.12.6" /// PYTRAJ relies on this #define CPPTRAJ_VERSION_STRING CPPTRAJ_INTERNAL_VERSION #endif diff --git a/src/xdrfile/ms_stdint.h b/src/xdrfile/ms_stdint.h new file mode 100644 index 0000000000..cb2acd9384 --- /dev/null +++ b/src/xdrfile/ms_stdint.h @@ -0,0 +1,259 @@ +// ISO C9x compliant stdint.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_STDINT_H_ // [ +#define _MSC_STDINT_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#if _MSC_VER >= 1600 // [ +#include +#else // ] _MSC_VER >= 1600 [ + +#include + +// For Visual Studio 6 in C++ mode and for many Visual Studio versions when +// compiling for ARM we should wrap include with 'extern "C++" {}' +// or compiler give many errors like this: +// error C2733: second C linkage of overloaded function 'wmemchr' not allowed +#ifdef __cplusplus +extern "C" { +#endif +# include +#ifdef __cplusplus +} +#endif + +// Define _W64 macros to mark types changing their size, like intptr_t. +#ifndef _W64 +# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 +# define _W64 __w64 +# else +# define _W64 +# endif +#endif + + +// 7.18.1 Integer types + +// 7.18.1.1 Exact-width integer types + +// Visual Studio 6 and Embedded Visual C++ 4 doesn't +// realize that, e.g. char has the same size as __int8 +// so we give up on __intX for them. +#if (_MSC_VER < 1300) + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; +#else + typedef signed __int8 int8_t; + typedef signed __int16 int16_t; + typedef signed __int32 int32_t; + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; +#endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; + + +// 7.18.1.2 Minimum-width integer types +typedef int8_t int_least8_t; +typedef int16_t int_least16_t; +typedef int32_t int_least32_t; +typedef int64_t int_least64_t; +typedef uint8_t uint_least8_t; +typedef uint16_t uint_least16_t; +typedef uint32_t uint_least32_t; +typedef uint64_t uint_least64_t; + +// 7.18.1.3 Fastest minimum-width integer types +typedef int8_t int_fast8_t; +typedef int16_t int_fast16_t; +typedef int32_t int_fast32_t; +typedef int64_t int_fast64_t; +typedef uint8_t uint_fast8_t; +typedef uint16_t uint_fast16_t; +typedef uint32_t uint_fast32_t; +typedef uint64_t uint_fast64_t; + +// 7.18.1.4 Integer types capable of holding object pointers +#ifdef _WIN64 // [ + typedef signed __int64 intptr_t; + typedef unsigned __int64 uintptr_t; +#else // _WIN64 ][ + typedef _W64 signed int intptr_t; + typedef _W64 unsigned int uintptr_t; +#endif // _WIN64 ] + +// 7.18.1.5 Greatest-width integer types +typedef int64_t intmax_t; +typedef uint64_t uintmax_t; + + +// 7.18.2 Limits of specified-width integer types + +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 + +// 7.18.2.1 Limits of exact-width integer types +#define INT8_MIN ((int8_t)_I8_MIN) +#define INT8_MAX _I8_MAX +#define INT16_MIN ((int16_t)_I16_MIN) +#define INT16_MAX _I16_MAX +#define INT32_MIN ((int32_t)_I32_MIN) +#define INT32_MAX _I32_MAX +#define INT64_MIN ((int64_t)_I64_MIN) +#define INT64_MAX _I64_MAX +#define UINT8_MAX _UI8_MAX +#define UINT16_MAX _UI16_MAX +#define UINT32_MAX _UI32_MAX +#define UINT64_MAX _UI64_MAX + +// 7.18.2.2 Limits of minimum-width integer types +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +// 7.18.2.3 Limits of fastest minimum-width integer types +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST16_MAX INT16_MAX +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT16_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +// 7.18.2.4 Limits of integer types capable of holding object pointers +#ifdef _WIN64 // [ +# define INTPTR_MIN INT64_MIN +# define INTPTR_MAX INT64_MAX +# define UINTPTR_MAX UINT64_MAX +#else // _WIN64 ][ +# define INTPTR_MIN INT32_MIN +# define INTPTR_MAX INT32_MAX +# define UINTPTR_MAX UINT32_MAX +#endif // _WIN64 ] + +// 7.18.2.5 Limits of greatest-width integer types +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +// 7.18.3 Limits of other integer types + +#ifdef _WIN64 // [ +# define PTRDIFF_MIN _I64_MIN +# define PTRDIFF_MAX _I64_MAX +#else // _WIN64 ][ +# define PTRDIFF_MIN _I32_MIN +# define PTRDIFF_MAX _I32_MAX +#endif // _WIN64 ] + +#define SIG_ATOMIC_MIN INT_MIN +#define SIG_ATOMIC_MAX INT_MAX + +#ifndef SIZE_MAX // [ +# ifdef _WIN64 // [ +# define SIZE_MAX _UI64_MAX +# else // _WIN64 ][ +# define SIZE_MAX _UI32_MAX +# endif // _WIN64 ] +#endif // SIZE_MAX ] + +// WCHAR_MIN and WCHAR_MAX are also defined in +#ifndef WCHAR_MIN // [ +# define WCHAR_MIN 0 +#endif // WCHAR_MIN ] +#ifndef WCHAR_MAX // [ +# define WCHAR_MAX _UI16_MAX +#endif // WCHAR_MAX ] + +#define WINT_MIN 0 +#define WINT_MAX _UI16_MAX + +#endif // __STDC_LIMIT_MACROS ] + + +// 7.18.4 Limits of other integer types + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#endif // _MSC_VER >= 1600 ] + +#endif // _MSC_STDINT_H_ ] diff --git a/src/xdrfile/xdr_seek.c b/src/xdrfile/xdr_seek.c index f9abb66c08..51768d8299 100644 --- a/src/xdrfile/xdr_seek.c +++ b/src/xdrfile/xdr_seek.c @@ -48,3 +48,8 @@ int xdr_seek(XDRFILE *xd, int64_t pos, int whence) return exdrOK; } + +int xdr_flush(XDRFILE* xdr) +{ + return fflush(xdr->fp); +} diff --git a/src/xdrfile/xdr_seek.h b/src/xdrfile/xdr_seek.h index 9dd6cacfa6..114d8f3942 100644 --- a/src/xdrfile/xdr_seek.h +++ b/src/xdrfile/xdr_seek.h @@ -7,7 +7,7 @@ extern "C" #endif // for int64_t on older M$ Visual Studio -#if _MSC_VER && _MSC_VER < 1600 && !__INTEL_COMPILER +#if _MSC_VER && _MSVC_VER < 1600 && !__INTEL_COMPILER #include "ms_stdint.h" #else #include @@ -17,6 +17,7 @@ extern "C" int64_t xdr_tell(XDRFILE *xd); int xdr_seek(XDRFILE *xd, int64_t pos, int whence); +int xdr_flush(XDRFILE* xd); #ifdef __cplusplus } diff --git a/src/xdrfile/xdrfile.c b/src/xdrfile/xdrfile.c index 1d322a89ec..2cda49a531 100644 --- a/src/xdrfile/xdrfile.c +++ b/src/xdrfile/xdrfile.c @@ -25,6 +25,14 @@ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * NOTE: The self-contained XDR layer at the bottom of this file (guarded by + * #ifndef HAVE_RPC_XDR_H) is derived from the Sun RPC XDR code that Oracle + * America, Inc. relicensed under the 3-clause BSD license. That portion of + * the file is licensed under the BSD-3-Clause license reproduced there; the + * rest of the file (the XTC/TRR higher-level routines and the compressed + * coordinate code) is licensed under the BSD 2-clause license above. + * See the README in this directory for provenance and references. */ /* Get HAVE_RPC_XDR_H, F77_FUNC from config.h if available */ @@ -61,9 +69,9 @@ #endif char *exdr_message[exdrNR] = { - "OK", + "OK", "Header", - "String", + "String", "Double", "Integer", "Float", @@ -73,7 +81,7 @@ char *exdr_message[exdrNR] = { "Magic number", "Not enough memory", "End of file", - "File not found" + "File not found" }; /* @@ -82,6 +90,14 @@ char *exdr_message[exdrNR] = { * * We don't want the low-level XDR implementation as part of the Gromacs * documentation, so skip it for doxygen too... + * + * The XDR interface types below (enum xdr_op, struct XDR / struct xdr_ops + * and the accessor macros) are adapted from the BSD-3-Clause licensed + * shipped in FreeBSD libc (include/rpc/xdr.h); see the + * license notice further down in this file and the README. The + * function-pointer types here use int32_t/uint32_t so that the 4-byte + * XDR unit is unambiguous even where sizeof(int) or sizeof(long) is not + * exactly 4 bytes. */ #if (!defined HAVE_RPC_XDR_H && !defined DOXYGEN) @@ -118,7 +134,7 @@ typedef struct XDR XDR; struct XDR { - enum xdr_op x_op; + enum xdr_op x_op; struct xdr_ops { int (*x_getlong) (XDR *__xdrs, int32_t *__lp); @@ -126,11 +142,11 @@ struct XDR int (*x_getbytes) (XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes) (XDR *__xdrs, char *__addr, unsigned int __len); /* two next routines are not 64-bit IO safe - don't use! */ - unsigned int (*x_getpostn) (XDR *__xdrs); + unsigned int (*x_getpostn) (XDR *__xdrs); int (*x_setpostn) (XDR *__xdrs, unsigned int __pos); - void (*x_destroy) (XDR *__xdrs); - } - *x_ops; + void (*x_destroy) (XDR *__xdrs); + } + *x_ops; char *x_private; }; @@ -164,26 +180,26 @@ static void xdrstdio_create (XDR *xdrs, FILE *fp, enum xdr_op xop); /** Contents of the abstract XDRFILE data structure. * * @internal - * + * * This structure is used to provide an XDR file interface that is * virtual identical to the standard UNIX fopen/fread/fwrite/fclose. */ -struct XDRFILE -{ +struct XDRFILE +{ FILE * fp; /**< pointer to standard C library file handle */ XDR * xdr; /**< pointer to corresponding XDR handle */ char mode; /**< r=read, w=write, a=append */ int * buf1; /**< Buffer for internal use */ - int buf1size; /**< Current allocated length of buf1 */ + int buf1size; /**< Current allocated length of buf1 */ int * buf2; /**< Buffer for internal use */ - int buf2size; /**< Current allocated length of buf2 */ + int buf2size; /**< Current allocated length of buf2 */ }; /************************************************************* - * Implementation of higher-level routines to read/write * + * Implementation of higher-level routines to read/write * * portable data based on the XDR standard. These should be * * called from C - see further down for Fortran77 wrappers. * *************************************************************/ @@ -194,13 +210,13 @@ xdrfile_open(const char *path, const char *mode) char newmode[5]; enum xdr_op xdrmode; XDRFILE *xfp; - + /* make sure XDR files are opened in binary mode... */ - if(*mode=='w' || *mode=='W') + if(*mode=='w' || *mode=='W') { sprintf(newmode,"wb+"); xdrmode=XDR_ENCODE; - } else if(*mode == 'a' || *mode == 'A') + } else if(*mode == 'a' || *mode == 'A') { sprintf(newmode,"ab+"); xdrmode = XDR_ENCODE; @@ -210,7 +226,7 @@ xdrfile_open(const char *path, const char *mode) xdrmode = XDR_DECODE; } else /* cannot determine mode */ return NULL; - + if((xfp=(XDRFILE *)malloc(sizeof(XDRFILE)))==NULL) return NULL; if((xfp->fp=fopen(path,newmode))==NULL) @@ -218,7 +234,7 @@ xdrfile_open(const char *path, const char *mode) free(xfp); return NULL; } - if((xfp->xdr=(XDR *)malloc(sizeof(XDR)))==NULL) + if((xfp->xdr=(XDR *)malloc(sizeof(XDR)))==NULL) { fclose(xfp->fp); free(xfp); @@ -231,11 +247,11 @@ xdrfile_open(const char *path, const char *mode) return xfp; } -int +int xdrfile_close(XDRFILE *xfp) { int ret=exdrCLOSE; - if(xfp) + if(xfp) { /* flush and destroy XDR stream */ if(xfp->xdr) @@ -254,149 +270,149 @@ xdrfile_close(XDRFILE *xfp) -int -xdrfile_read_int(int *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_int(int *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; - + return i; } -int -xdrfile_write_int(int *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_int(int *ptr, int ndata, XDRFILE* xfp) { int i=0; - - /* read write is encoded in the XDR struct */ + + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_uint(unsigned int *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_uint(unsigned int *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; - + return i; } -int -xdrfile_write_uint(unsigned int *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_uint(unsigned int *ptr, int ndata, XDRFILE* xfp) { int i=0; - - /* read write is encoded in the XDR struct */ + + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_char(char *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_char(char *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; - + return i; } -int -xdrfile_write_char(char *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_char(char *ptr, int ndata, XDRFILE* xfp) { int i=0; - - /* read write is encoded in the XDR struct */ + + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_uchar(unsigned char *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_uchar(unsigned char *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; - + return i; } -int -xdrfile_write_uchar(unsigned char *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_uchar(unsigned char *ptr, int ndata, XDRFILE* xfp) { int i=0; - - /* read write is encoded in the XDR struct */ + + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_short(short *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_short(short *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; - + return i; } -int -xdrfile_write_short(short *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_short(short *ptr, int ndata, XDRFILE* xfp) { int i=0; - - /* read write is encoded in the XDR struct */ + + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_ushort(unsigned short *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_ushort(unsigned short *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; - + return i; } -int -xdrfile_write_ushort(unsigned short *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_ushort(unsigned short *ptr, int ndata, XDRFILE* xfp) { int i=0; - - /* read write is encoded in the XDR struct */ + + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_float(float *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_float(float *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ @@ -405,18 +421,18 @@ xdrfile_read_float(float *ptr, int ndata, XDRFILE* xfp) return i; } -int -xdrfile_write_float(float *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_float(float *ptr, int ndata, XDRFILE* xfp) { int i=0; - /* read write is encoded in the XDR struct */ + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; } -int -xdrfile_read_double(double *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_read_double(double *ptr, int ndata, XDRFILE* xfp) { int i=0; /* read write is encoded in the XDR struct */ @@ -425,11 +441,11 @@ xdrfile_read_double(double *ptr, int ndata, XDRFILE* xfp) return i; } -int -xdrfile_write_double(double *ptr, int ndata, XDRFILE* xfp) +int +xdrfile_write_double(double *ptr, int ndata, XDRFILE* xfp) { int i=0; - /* read write is encoded in the XDR struct */ + /* read write is encoded in the XDR struct */ while(ixdr),ptr+i)) i++; return i; @@ -455,8 +471,8 @@ int xdrfile_write_string(char *ptr, XDRFILE* xfp) { int len=strlen(ptr)+1; - - if(xdr_string((XDR *)(xfp->xdr),&ptr,len)) + + if(xdr_string((XDR *)(xfp->xdr),&ptr,len)) return len; else return 0; @@ -468,7 +484,7 @@ xdrfile_read_opaque(char *ptr, int cnt, XDRFILE* xfp) { if(xdr_opaque((XDR *)(xfp->xdr),ptr,cnt)) return cnt; - else + else return 0; } @@ -483,16 +499,16 @@ xdrfile_write_opaque(char *ptr, int cnt, XDRFILE* xfp) } -/* Internal support routines for reading/writing compressed coordinates +/* Internal support routines for reading/writing compressed coordinates * sizeofint - calculate smallest number of bits necessary * to represent a certain integer. */ -static int +static int sizeofint(int size) { unsigned int num = 1; int num_of_bits = 0; - - while (size >= num && num_of_bits < 32) + + while (size >= num && num_of_bits < 32) { num_of_bits++; num <<= 1; @@ -507,13 +523,13 @@ sizeofint(int size) { * given a number of small unsigned integers and the maximum value * return the number of bits needed to read or write them with the * routines encodeints/decodeints. You need this parameter when - * calling those routines. - * (However, in some cases we can just use the variable 'smallidx' + * calling those routines. + * (However, in some cases we can just use the variable 'smallidx' * which is the exact number of bits, and them we dont need to call * this routine). */ -static int -sizeofints(int num_of_ints, unsigned int sizes[]) +static int +sizeofints(int num_of_ints, unsigned int sizes[]) { int i, num; unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp; @@ -521,7 +537,7 @@ sizeofints(int num_of_ints, unsigned int sizes[]) bytes[0] = 1; num_of_bits = 0; for (i=0; i < num_of_ints; i++) - { + { tmp = 0; for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) { @@ -529,7 +545,7 @@ sizeofints(int num_of_ints, unsigned int sizes[]) bytes[bytecnt] = tmp & 0xff; tmp >>= 8; } - while (tmp != 0) + while (tmp != 0) { bytes[bytecnt++] = tmp & 0xff; tmp >>= 8; @@ -538,7 +554,7 @@ sizeofints(int num_of_ints, unsigned int sizes[]) } num = 1; num_of_bytes--; - while (bytes[num_of_bytes] >= num) + while (bytes[num_of_bytes] >= num) { num_of_bits++; num *= 2; @@ -546,7 +562,7 @@ sizeofints(int num_of_ints, unsigned int sizes[]) return num_of_bits + num_of_bytes * 8; } - + /* * encodebits - encode num into buf using the specified number of bits @@ -556,14 +572,14 @@ sizeofints(int num_of_ints, unsigned int sizes[]) * better make sure that this number of bits is enough to hold the value. * Num must also be positive. */ -static void -encodebits(int buf[], int num_of_bits, int num) +static void +encodebits(int buf[], int num_of_bits, int num) { - + unsigned int cnt, lastbyte; int lastbits; unsigned char * cbuf; - + cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf); cnt = (unsigned int) buf[0]; lastbits = buf[1]; @@ -578,7 +594,7 @@ encodebits(int buf[], int num_of_bits, int num) { lastbyte = (lastbyte << num_of_bits) | num; lastbits += num_of_bits; - if (lastbits >= 8) + if (lastbits >= 8) { lastbits -= 8; cbuf[cnt++] = lastbyte >> lastbits; @@ -587,7 +603,7 @@ encodebits(int buf[], int num_of_bits, int num) buf[0] = cnt; buf[1] = lastbits; buf[2] = lastbyte; - if (lastbits>0) + if (lastbits>0) { cbuf[cnt] = lastbyte << (8 - lastbits); } @@ -607,10 +623,10 @@ encodebits(int buf[], int num_of_bits, int num) * THese things are checked in the calling routines, so make sure not * to remove those checks... */ - -static void + +static void encodeints(int buf[], int num_of_ints, int num_of_bits, - unsigned int sizes[], unsigned int nums[]) + unsigned int sizes[], unsigned int nums[]) { int i; @@ -618,13 +634,13 @@ encodeints(int buf[], int num_of_ints, int num_of_bits, tmp = nums[0]; num_of_bytes = 0; - do + do { bytes[num_of_bytes++] = tmp & 0xff; tmp >>= 8; } while (tmp != 0); - for (i = 1; i < num_of_ints; i++) + for (i = 1; i < num_of_ints; i++) { if (nums[i] >= sizes[i]) { @@ -632,9 +648,9 @@ encodeints(int buf[], int num_of_ints, int num_of_bits, "match size %u\n", nums[i], sizes[i]); abort(); } - /* use one step multiply */ + /* use one step multiply */ tmp = nums[i]; - for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) + for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++) { tmp = bytes[bytecnt] * sizes[i] + tmp; bytes[bytecnt] = tmp & 0xff; @@ -647,14 +663,14 @@ encodeints(int buf[], int num_of_ints, int num_of_bits, } num_of_bytes = bytecnt; } - if (num_of_bits >= num_of_bytes * 8) + if (num_of_bits >= num_of_bytes * 8) { - for (i = 0; i < num_of_bytes; i++) + for (i = 0; i < num_of_bytes; i++) { encodebits(buf, 8, bytes[i]); } encodebits(buf, num_of_bits - num_of_bytes * 8, 0); - } + } else { for (i = 0; i < num_of_bytes-1; i++) @@ -668,17 +684,17 @@ encodeints(int buf[], int num_of_ints, int num_of_bits, /* * decodebits - decode number from buf using specified number of bits - * + * * extract the number of bits from the array buf and construct an integer * from it. Return that value. * */ -static int -decodebits(int buf[], int num_of_bits) +static int +decodebits(int buf[], int num_of_bits) { - int cnt, num; + int cnt, num; unsigned int lastbits, lastbyte; unsigned char * cbuf; int mask = (1 << num_of_bits) -1; @@ -687,7 +703,7 @@ decodebits(int buf[], int num_of_bits) cnt = buf[0]; lastbits = (unsigned int) buf[1]; lastbyte = (unsigned int) buf[2]; - + num = 0; while (num_of_bits >= 8) { @@ -695,9 +711,9 @@ decodebits(int buf[], int num_of_bits) num |= (lastbyte >> lastbits) << (num_of_bits - 8); num_of_bits -=8; } - if (num_of_bits > 0) + if (num_of_bits > 0) { - if (lastbits < num_of_bits) + if (lastbits < num_of_bits) { lastbits += 8; lastbyte = (lastbyte << 8) | cbuf[cnt++]; @@ -709,7 +725,7 @@ decodebits(int buf[], int num_of_bits) buf[0] = cnt; buf[1] = lastbits; buf[2] = lastbyte; - return num; + return num; } /* @@ -722,14 +738,14 @@ decodebits(int buf[], int num_of_bits) * */ -static void +static void decodeints(int buf[], int num_of_ints, int num_of_bits, unsigned int sizes[], int nums[]) { int bytes[32]; int i, j, num_of_bytes, p, num; - + bytes[1] = bytes[2] = bytes[3] = 0; num_of_bytes = 0; while (num_of_bits > 8) @@ -741,10 +757,10 @@ decodeints(int buf[], int num_of_ints, int num_of_bits, { bytes[num_of_bytes++] = decodebits(buf, num_of_bits); } - for (i = num_of_ints-1; i > 0; i--) + for (i = num_of_ints-1; i > 0; i--) { num = 0; - for (j = num_of_bytes-1; j >=0; j--) + for (j = num_of_bytes-1; j >=0; j--) { num = (num << 8) | bytes[j]; p = num / sizes[i]; @@ -755,17 +771,17 @@ decodeints(int buf[], int num_of_ints, int num_of_bits, } nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); } - -static const int magicints[] = + +static const int magicints[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 101, 128, 161, 203, 256, 322, 406, 512, 645, 812, 1024, 1290, - 1625, 2048, 2580, 3250, 4096, 5060, 6501, 8192, 10321, 13003, - 16384, 20642, 26007, 32768, 41285, 52015, 65536,82570, 104031, - 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561, - 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, - 4194304, 5284491, 6658042, 8388607, 10568983, 13316085, 16777216 + 1625, 2048, 2580, 3250, 4096, 5060, 6501, 8192, 10321, 13003, + 16384, 20642, 26007, 32768, 41285, 52015, 65536,82570, 104031, + 131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561, + 832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021, + 4194304, 5284491, 6658042, 8388607, 10568983, 13316085, 16777216 }; #define FIRSTIDX 9 @@ -792,7 +808,7 @@ xdrfile_decompress_coord_float(float *ptr, int tmp, *thiscoord, prevcoord[3]; unsigned int bitsize; const float* ptrstart = ptr; - + bitsizeint[0] = 0; bitsizeint[1] = 0; bitsizeint[2] = 0; @@ -808,7 +824,7 @@ xdrfile_decompress_coord_float(float *ptr, fprintf(stderr, "(xdrfile error) Size could not be read\n"); return -1; /* return if we could not read size */ } - if (*size < lsize) + if (*size < lsize) { fprintf(stderr, "(xdrfile error) Requested to decompress %d coords, file contains %d\n", *size, lsize); @@ -816,12 +832,12 @@ xdrfile_decompress_coord_float(float *ptr, } *size = lsize; size3 = *size * 3; - if(size3>xfp->buf1size) + if(size3>xfp->buf1size) { - if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL) + if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL) { fprintf(stderr, "(xdrfile error) Cannot allocate memory for decompressing coordinates.\n"); - return -1; + return -1; } xfp->buf1size=size3; xfp->buf2size=size3*1.2; @@ -832,26 +848,26 @@ xdrfile_decompress_coord_float(float *ptr, } } /* Dont bother with compression for three atoms or less */ - if(*size<=9) + if(*size<=9) { return xdrfile_read_float(ptr,size3,xfp)/3; /* return number of coords, not floats */ } /* Compression-time if we got here. Read precision first */ xdrfile_read_float(precision,1,xfp); - + /* avoid repeated pointer dereferencing. */ - buf1=xfp->buf1; + buf1=xfp->buf1; buf2=xfp->buf2; /* buf2[0-2] are special and do not contain actual data */ buf2[0] = buf2[1] = buf2[2] = 0; xdrfile_read_int(minint,3,xfp); xdrfile_read_int(maxint,3,xfp); - + sizeint[0] = maxint[0] - minint[0]+1; sizeint[1] = maxint[1] - minint[1]+1; sizeint[2] = maxint[2] - minint[2]+1; - + /* check if one of the sizes is to big to be multiplied */ if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) { @@ -860,11 +876,11 @@ xdrfile_decompress_coord_float(float *ptr, bitsizeint[2] = sizeofint(sizeint[2]); bitsize = 0; /* flag the use of large sizes */ } - else + else { bitsize = sizeofints(3, sizeint); } - + if (xdrfile_read_int(&smallidx,1,xfp) == 0) { fprintf(stderr,"(xdrfile error) Undocumented error 1"); return 0; /* not sure what has happened here or why we return... */ @@ -880,7 +896,7 @@ xdrfile_decompress_coord_float(float *ptr, //larger = magicints[maxidx]; /* buf2[0] holds the length in bytes */ - + if (xdrfile_read_int(buf2,1,xfp) == 0) { fprintf(stderr, "(xdrfile error) Undocumented error 2"); return 0; @@ -890,17 +906,17 @@ xdrfile_decompress_coord_float(float *ptr, return 0; } buf2[0] = buf2[1] = buf2[2] = 0; - + lfp = ptr; inv_precision = 1.0 / * precision; run = 0; i = 0; lip = buf1; - while ( i < lsize ) + while ( i < lsize ) { thiscoord = (int *)(lip) + i * 3; - - if (bitsize == 0) + + if (bitsize == 0) { thiscoord[0] = decodebits(buf2, bitsizeint[0]); thiscoord[1] = decodebits(buf2, bitsizeint[1]); @@ -910,19 +926,19 @@ xdrfile_decompress_coord_float(float *ptr, { decodeints(buf2, 3, bitsize, sizeint, thiscoord); } - + i++; thiscoord[0] += minint[0]; thiscoord[1] += minint[1]; thiscoord[2] += minint[2]; - + prevcoord[0] = thiscoord[0]; prevcoord[1] = thiscoord[1]; prevcoord[2] = thiscoord[2]; - + flag = decodebits(buf2, 1); is_smaller = 0; - if (flag == 1) + if (flag == 1) { run = decodebits(buf2, 5); is_smaller = run % 3; @@ -937,7 +953,7 @@ xdrfile_decompress_coord_float(float *ptr, if (run > 0) { thiscoord += 3; - for (k = 0; k < run; k+=3) + for (k = 0; k < run; k+=3) { decodeints(buf2, 3, smallidx, sizesmall, thiscoord); i++; @@ -966,27 +982,27 @@ xdrfile_decompress_coord_float(float *ptr, *lfp++ = thiscoord[1] * inv_precision; *lfp++ = thiscoord[2] * inv_precision; } - } + } else { *lfp++ = thiscoord[0] * inv_precision; *lfp++ = thiscoord[1] * inv_precision; - *lfp++ = thiscoord[2] * inv_precision; + *lfp++ = thiscoord[2] * inv_precision; } smallidx += is_smaller; - if (is_smaller < 0) + if (is_smaller < 0) { smallnum = smaller; - - if (smallidx > FIRSTIDX) + + if (smallidx > FIRSTIDX) { smaller = magicints[smallidx - 1] /2; - } - else + } + else { smaller = 0; } - } + } else if (is_smaller > 0) { smaller = smallnum; @@ -1019,11 +1035,11 @@ xdrfile_compress_coord_float(float *ptr, unsigned int tmpcoord[30]; //int errval=1; unsigned int bitsize; - + if(xfp==NULL) return -1; size3=3*size; - + bitsizeint[0] = 0; bitsizeint[1] = 0; bitsizeint[2] = 0; @@ -1033,7 +1049,7 @@ xdrfile_compress_coord_float(float *ptr, if(size3>xfp->buf1size) { - if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL) + if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL) { fprintf(stderr, "(xdrfile error) Cannot allocate memory for compressing coordinates.\n"); return -1; @@ -1049,7 +1065,7 @@ xdrfile_compress_coord_float(float *ptr, if(xdrfile_write_int(&size,1,xfp)==0) return -1; /* return if we could not write size */ /* Dont bother with compression for three atoms or less */ - if(size<=9) + if(size<=9) { return xdrfile_write_float(ptr,size3,xfp)/3; /* return number of coords, not floats */ @@ -1059,7 +1075,7 @@ xdrfile_compress_coord_float(float *ptr, precision = 1000; xdrfile_write_float(&precision,1,xfp); /* avoid repeated pointer dereferencing. */ - buf1=xfp->buf1; + buf1=xfp->buf1; buf2=xfp->buf2; /* buf2[0-2] are special and do not contain actual data */ buf2[0] = buf2[1] = buf2[2] = 0; @@ -1077,7 +1093,7 @@ xdrfile_compress_coord_float(float *ptr, lf = *lfp * precision + 0.5; else lf = *lfp * precision - 0.5; - if (fabs(lf) > INT_MAX-2) + if (fabs(lf) > INT_MAX-2) { /* scaling would cause overflow */ fprintf(stderr, "(xdrfile error) Internal overflow compressing coordinates.\n"); @@ -1107,9 +1123,9 @@ xdrfile_compress_coord_float(float *ptr, lf = *lfp * precision + 0.5; else lf = *lfp * precision - 0.5; - if (fabs(lf) > INT_MAX-2) + if (fabs(lf) > INT_MAX-2) { - //errval=0; + //errval=0; } lint3 = lf; if (lint3 < minint[2]) minint[2] = lint3; @@ -1122,10 +1138,10 @@ xdrfile_compress_coord_float(float *ptr, oldlint1 = lint1; oldlint2 = lint2; oldlint3 = lint3; - } + } xdrfile_write_int(minint,3,xfp); xdrfile_write_int(maxint,3,xfp); - + if ((float)maxint[0] - (float)minint[0] >= INT_MAX-2 || (float)maxint[1] - (float)minint[1] >= INT_MAX-2 || (float)maxint[2] - (float)minint[2] >= INT_MAX-2) { @@ -1138,7 +1154,7 @@ xdrfile_compress_coord_float(float *ptr, sizeint[0] = maxint[0] - minint[0]+1; sizeint[1] = maxint[1] - minint[1]+1; sizeint[2] = maxint[2] - minint[2]+1; - + /* check if one of the sizes is to big to be multiplied */ if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) { @@ -1169,7 +1185,7 @@ xdrfile_compress_coord_float(float *ptr, sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx]; larger = magicints[maxidx] / 2; i = 0; - while (i < size) + while (i < size) { is_small = 0; thiscoord = (int *)(luip) + i * 3; @@ -1178,8 +1194,8 @@ xdrfile_compress_coord_float(float *ptr, abs(thiscoord[1] - prevcoord[1]) < larger && abs(thiscoord[2] - prevcoord[2]) < larger) { is_smaller = 1; - } - else if (smallidx > minidx) + } + else if (smallidx > minidx) { is_smaller = -1; } @@ -1187,11 +1203,11 @@ xdrfile_compress_coord_float(float *ptr, { is_smaller = 0; } - if (i + 1 < size) + if (i + 1 < size) { if (abs(thiscoord[0] - thiscoord[3]) < smallnum && abs(thiscoord[1] - thiscoord[4]) < smallnum && - abs(thiscoord[2] - thiscoord[5]) < smallnum) + abs(thiscoord[2] - thiscoord[5]) < smallnum) { /* interchange first with second atom for better * compression of water molecules @@ -1203,17 +1219,17 @@ xdrfile_compress_coord_float(float *ptr, tmp = thiscoord[2]; thiscoord[2] = thiscoord[5]; thiscoord[5] = tmp; is_small = 1; - } + } } tmpcoord[0] = thiscoord[0] - minint[0]; tmpcoord[1] = thiscoord[1] - minint[1]; tmpcoord[2] = thiscoord[2] - minint[2]; - if (bitsize == 0) + if (bitsize == 0) { encodebits(buf2, bitsizeint[0], tmpcoord[0]); encodebits(buf2, bitsizeint[1], tmpcoord[1]); encodebits(buf2, bitsizeint[2], tmpcoord[2]); - } + } else { encodeints(buf2, 3, bitsize, sizeint, tmpcoord); @@ -1230,7 +1246,7 @@ xdrfile_compress_coord_float(float *ptr, while (is_small && run < 8*3) { tmpsum=0; - for(j=0;j<3;j++) + for(j=0;j<3;j++) { tmp=thiscoord[j] - prevcoord[j]; tmpsum+=tmp*tmp; @@ -1239,15 +1255,15 @@ xdrfile_compress_coord_float(float *ptr, { is_smaller = 0; } - + tmpcoord[run++] = thiscoord[0] - prevcoord[0] + smallnum; tmpcoord[run++] = thiscoord[1] - prevcoord[1] + smallnum; tmpcoord[run++] = thiscoord[2] - prevcoord[2] + smallnum; - + prevcoord[0] = thiscoord[0]; prevcoord[1] = thiscoord[1]; prevcoord[2] = thiscoord[2]; - + i++; thiscoord = thiscoord + 3; is_small = 0; @@ -1259,35 +1275,35 @@ xdrfile_compress_coord_float(float *ptr, is_small = 1; } } - if (run != prevrun || is_smaller != 0) + if (run != prevrun || is_smaller != 0) { prevrun = run; encodebits(buf2, 1, 1); /* flag the change in run-length */ encodebits(buf2, 5, run+is_smaller+1); - } - else + } + else { encodebits(buf2, 1, 0); /* flag the fact that runlength did not change */ } - for (k=0; k < run; k+=3) + for (k=0; k < run; k+=3) { - encodeints(buf2, 3, smallidx, sizesmall, &tmpcoord[k]); + encodeints(buf2, 3, smallidx, sizesmall, &tmpcoord[k]); } - if (is_smaller != 0) + if (is_smaller != 0) { smallidx += is_smaller; - if (is_smaller < 0) + if (is_smaller < 0) { smallnum = smaller; smaller = magicints[smallidx-1] / 2; - } - else + } + else { smaller = smallnum; smallnum = magicints[smallidx] / 2; } sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx]; - } + } } if (buf2[1] != 0) buf2[0]++; xdrfile_write_int(buf2,1,xfp); /* buf2[0] holds the length in bytes */ @@ -1300,7 +1316,7 @@ xdrfile_compress_coord_float(float *ptr, int -xdrfile_decompress_coord_double(double *ptr, +xdrfile_decompress_coord_double(double *ptr, int *size, double *precision, XDRFILE* xfp) @@ -1315,7 +1331,7 @@ xdrfile_decompress_coord_double(double *ptr, float float_prec, tmpdata[30]; int tmp, *thiscoord, prevcoord[3]; unsigned int bitsize; - + bitsizeint[0] = 0; bitsizeint[1] = 0; bitsizeint[2] = 0; @@ -1327,7 +1343,7 @@ xdrfile_decompress_coord_double(double *ptr, tmp=xdrfile_read_int(&lsize,1,xfp); if(tmp==0) return -1; /* return if we could not read size */ - if (*size < lsize) + if (*size < lsize) { fprintf(stderr, "(xdrfile error) Requested to decompress %d coords, file contains %d\n", *size, lsize); @@ -1335,12 +1351,12 @@ xdrfile_decompress_coord_double(double *ptr, } *size = lsize; size3 = *size * 3; - if(size3>xfp->buf1size) + if(size3>xfp->buf1size) { - if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL) + if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL) { fprintf(stderr, "(xdrfile error) Cannot allocate memory for decompression coordinates.\n"); - return -1; + return -1; } xfp->buf1size=size3; xfp->buf2size=size3*1.2; @@ -1363,17 +1379,17 @@ xdrfile_decompress_coord_double(double *ptr, xdrfile_read_float(&float_prec,1,xfp); *precision=float_prec; /* avoid repeated pointer dereferencing. */ - buf1=xfp->buf1; + buf1=xfp->buf1; buf2=xfp->buf2; /* buf2[0-2] are special and do not contain actual data */ buf2[0] = buf2[1] = buf2[2] = 0; xdrfile_read_int(minint,3,xfp); xdrfile_read_int(maxint,3,xfp); - + sizeint[0] = maxint[0] - minint[0]+1; sizeint[1] = maxint[1] - minint[1]+1; sizeint[2] = maxint[2] - minint[2]+1; - + /* check if one of the sizes is to big to be multiplied */ if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) { @@ -1382,12 +1398,12 @@ xdrfile_decompress_coord_double(double *ptr, bitsizeint[2] = sizeofint(sizeint[2]); bitsize = 0; /* flag the use of large sizes */ } - else + else { bitsize = sizeofints(3, sizeint); } - - if (xdrfile_read_int(&smallidx,1,xfp) == 0) + + if (xdrfile_read_int(&smallidx,1,xfp) == 0) return 0; tmp=smallidx+8; //maxidx = (LASTIDX 0) + if (run > 0) { thiscoord += 3; - for (k = 0; k < run; k+=3) + for (k = 0; k < run; k+=3) { decodeints(buf2, 3, smallidx, sizesmall, thiscoord); i++; @@ -1468,7 +1484,7 @@ xdrfile_decompress_coord_double(double *ptr, *lfp++ = prevcoord[1] * inv_precision; *lfp++ = prevcoord[2] * inv_precision; } - else + else { prevcoord[0] = thiscoord[0]; prevcoord[1] = thiscoord[1]; @@ -1481,7 +1497,7 @@ xdrfile_decompress_coord_double(double *ptr, } else { *lfp++ = thiscoord[0] * inv_precision; *lfp++ = thiscoord[1] * inv_precision; - *lfp++ = thiscoord[2] * inv_precision; + *lfp++ = thiscoord[2] * inv_precision; } smallidx += is_smaller; if (is_smaller < 0) { @@ -1518,7 +1534,7 @@ xdrfile_compress_coord_double(double *ptr, unsigned int tmpcoord[30]; //int errval=1; unsigned int bitsize; - + bitsizeint[0] = 0; bitsizeint[1] = 0; bitsizeint[2] = 0; @@ -1556,7 +1572,7 @@ xdrfile_compress_coord_double(double *ptr, float_prec=precision; xdrfile_write_float(&float_prec,1,xfp); /* avoid repeated pointer dereferencing. */ - buf1=xfp->buf1; + buf1=xfp->buf1; buf2=xfp->buf2; /* buf2[0-2] are special and do not contain actual data */ buf2[0] = buf2[1] = buf2[2] = 0; @@ -1602,7 +1618,7 @@ xdrfile_compress_coord_double(double *ptr, else lf = (float)*lfp * float_prec - 0.5; if (fabs(lf) > INT_MAX-2) { - //errval=0; + //errval=0; } lint3 = lf; if (lint3 < minint[2]) minint[2] = lint3; @@ -1615,10 +1631,10 @@ xdrfile_compress_coord_double(double *ptr, oldlint1 = lint1; oldlint2 = lint2; oldlint3 = lint3; - } + } xdrfile_write_int(minint,3,xfp); xdrfile_write_int(maxint,3,xfp); - + if ((float)maxint[0] - (float)minint[0] >= INT_MAX-2 || (float)maxint[1] - (float)minint[1] >= INT_MAX-2 || (float)maxint[2] - (float)minint[2] >= INT_MAX-2) { @@ -1631,7 +1647,7 @@ xdrfile_compress_coord_double(double *ptr, sizeint[0] = maxint[0] - minint[0]+1; sizeint[1] = maxint[1] - minint[1]+1; sizeint[2] = maxint[2] - minint[2]+1; - + /* check if one of the sizes is to big to be multiplied */ if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff) { bitsizeint[0] = sizeofint(sizeint[0]); @@ -1685,7 +1701,7 @@ xdrfile_compress_coord_double(double *ptr, tmp = thiscoord[2]; thiscoord[2] = thiscoord[5]; thiscoord[5] = tmp; is_small = 1; - } + } } tmpcoord[0] = thiscoord[0] - minint[0]; tmpcoord[1] = thiscoord[1] - minint[1]; @@ -1715,15 +1731,15 @@ xdrfile_compress_coord_double(double *ptr, if (is_smaller == -1 && tmpsum >= smaller * smaller) { is_smaller = 0; } - + tmpcoord[run++] = thiscoord[0] - prevcoord[0] + smallnum; tmpcoord[run++] = thiscoord[1] - prevcoord[1] + smallnum; tmpcoord[run++] = thiscoord[2] - prevcoord[2] + smallnum; - + prevcoord[0] = thiscoord[0]; prevcoord[1] = thiscoord[1]; prevcoord[2] = thiscoord[2]; - + i++; thiscoord = thiscoord + 3; is_small = 0; @@ -1742,7 +1758,7 @@ xdrfile_compress_coord_double(double *ptr, encodebits(buf2, 1, 0); /* flag the fact that runlength did not change */ } for (k=0; k < run; k+=3) { - encodeints(buf2, 3, smallidx, sizesmall, &tmpcoord[k]); + encodeints(buf2, 3, smallidx, sizesmall, &tmpcoord[k]); } if (is_smaller != 0) { smallidx += is_smaller; @@ -1754,7 +1770,7 @@ xdrfile_compress_coord_double(double *ptr, smallnum = magicints[smallidx] / 2; } sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx]; - } + } } if (buf2[1] != 0) buf2[0]++; xdrfile_write_int(buf2,1,xfp); /* buf2[0] holds the length in bytes */ @@ -1762,21 +1778,21 @@ xdrfile_compress_coord_double(double *ptr, if(tmp==(unsigned int)buf2[0]) return size; else - return -1; + return -1; } /* Dont try do document Fortran interface, since - * Doxygen barfs at the F77_FUNC macro + * Doxygen barfs at the F77_FUNC macro */ -#ifndef DOXYGEN +#ifndef DOXYGEN /************************************************************* * Fortran77 interface for reading/writing portable data * * The routine are not threadsafe when called from Fortran * * (as they are when called from C) unless you compile with * * this file with posix thread support. * - * Note that these are not multithread-safe. * + * Note that these are not multithread-safe. * *************************************************************/ #define MAX_FORTRAN_XDR 1024 static XDRFILE *f77xdr[MAX_FORTRAN_XDR]; /* array of file handles */ @@ -1794,7 +1810,7 @@ F77_FUNC(xdropen,XDROPEN)(int *fid, char *filename, char *mode, char cfilename[512]; char cmode[5]; int i; - + /* zero array at first invocation */ if(f77init) { for(i=0;i (e.g. on Windows). * + * * + * These routines are derived from the Sun RPC XDR code * + * that Oracle America, Inc. relicensed under the 3-clause * + * BSD license. The canonical BSD-licensed copies we track * + * are the ones shipped in FreeBSD libc: * + * * + * - lib/libc/xdr/xdr.c * + * - lib/libc/xdr/xdr_stdio.c * + * - lib/libc/xdr/xdr_float.c * + * - include/rpc/xdr.h * + * * + * (https://cgit.freebsd.org/src/tree/lib/libc/xdr) * + * * + * The same BSD-licensed code is also available in libtirpc: * + * https://gitlab.com/libtirpc/libtirpc (- src/xdr.c, * + * src/xdr_stdio.c) * + * * + * The BSD-3-Clause license for this portion is reproduced * + * below; see also the README in this directory. The code is * + * adapted to be self-contained (no dependency on * + * or ), keeps all symbols static, and uses * + * int32_t/uint32_t for the 4-byte XDR unit so it is correct * + * even where sizeof(int) or sizeof(long) differs from 4 * + * (e.g. LP64 platforms). The floating-point word-order * + * detection in xdr_double() is retained from the GROMACS- * + * derived (BSD 2-clause) code that predates this change. * *************************************************************/ #ifndef HAVE_RPC_XDR_H /* - * What follows is a modified version of the Sun XDR code. For reference - * we include their copyright and license: - * - * Sun RPC is a product of Sun Microsystems, Inc. and is provided for - * unrestricted use provided that this legend is included on all tape - * media and as a part of the software program in whole or part. Users - * may copy or modify Sun RPC without charge, but are not authorized - * to license or distribute it to anyone else except as part of a product or - * program developed by the user. + * BSD-3-Clause license for the XDR layer below, as used by + * the FreeBSD libc copies of the Oracle-relicensed Sun RPC code: * - * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE - * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR - * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. + * SPDX-License-Identifier: BSD-3-Clause * - * Sun RPC is provided with no support and without any obligation on the - * part of Sun Microsystems, Inc. to assist in its use, correction, - * modification or enhancement. + * Copyright (c) 2010, Oracle America, Inc. * - * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE - * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC - * OR ANY PART THEREOF. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * In no event will Sun Microsystems, Inc. be liable for any lost revenue - * or profits or other special, indirect and consequential damages, even if - * Sun has been advised of the possibility of such damages. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * * Neither the name of the "Oracle America, Inc." nor the names of + * its contributors may be used to endorse or promote products + * derived from this software without specific prior written + * permission. * - * Sun Microsystems, Inc. - * 2550 Garcia Avenue - * Mountain View, California 94043 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* INT_MAX is defined in limits.h according to ANSI C */ #if (INT_MAX > 2147483647) # error Error: Cannot use builtin XDR support when size of int -# error is larger than 4 bytes. Use your system XDR libraries +# error is larger than 4 bytes. Use your system XDR libraries # error instead, or modify the source code in xdrfile.c #endif /* Check for 4 byte int type */ @@ -2135,7 +2181,7 @@ typedef int (*xdrproc_t) (XDR *, void *,...); #define xdr_putbytes(xdrs, addr, len) \ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) -#define BYTES_PER_XDR_UNIT 4 +#define BYTES_PER_XDR_UNIT 4 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0}; static int32_t @@ -2144,10 +2190,10 @@ xdr_swapbytes(int32_t x) int32_t y,i; char *px=(char *)&x; char *py=(char *)&y; - + for(i=0;i<4;i++) py[i]=px[3-i]; - + return y; } @@ -2180,7 +2226,7 @@ xdr_ntohl(int x) static int xdr_int (XDR *xdrs, int *ip) { - int32_t i32 = 0; + int32_t i32; switch (xdrs->x_op) { @@ -2194,6 +2240,8 @@ xdr_int (XDR *xdrs, int *ip) return 0; } *ip = (int) i32; + return 1; + case XDR_FREE: return 1; } @@ -2217,6 +2265,8 @@ xdr_u_int (XDR *xdrs, unsigned int *up) return 0; } *up = (uint32_t) ui32; + return 1; + case XDR_FREE: return 1; } @@ -2276,14 +2326,14 @@ xdr_u_short (XDR *xdrs, unsigned short *sp) static int xdr_char (XDR *xdrs, char *cp) { - int i; + unsigned int i; - i = (*cp); - if (!xdr_int (xdrs, &i)) + i = *((unsigned char *)cp); + if (!xdr_u_int (xdrs, &i)) { return 0; } - *cp = i; + *((unsigned char *)cp) = i; return 1; } @@ -2310,7 +2360,7 @@ static int xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt) { unsigned int rndup; - static char crud[BYTES_PER_XDR_UNIT]; + static int crud[BYTES_PER_XDR_UNIT]; /* * if no data we are done @@ -2325,9 +2375,8 @@ xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt) if (rndup > 0) rndup = BYTES_PER_XDR_UNIT - rndup; - switch (xdrs->x_op) + if (xdrs->x_op == XDR_DECODE) { - case XDR_DECODE: if (!xdr_getbytes (xdrs, cp, cnt)) { return 0; @@ -2335,8 +2384,10 @@ xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt) if (rndup == 0) return 1; return xdr_getbytes (xdrs, (char *)crud, rndup); + } - case XDR_ENCODE: + if (xdrs->x_op == XDR_ENCODE) + { if (!xdr_putbytes (xdrs, cp, cnt)) { return 0; @@ -2344,11 +2395,11 @@ xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt) if (rndup == 0) return 1; return xdr_putbytes (xdrs, xdr_zero, rndup); - - case XDR_FREE: - return 1; } -#undef BYTES_PER_XDR_UNIT + + if (xdrs->x_op == XDR_FREE) + return 1; + return 0; } @@ -2360,8 +2411,9 @@ static int xdr_string (XDR *xdrs, char **cpp, unsigned int maxsize) { char *sp = *cpp; /* sp is the actual string pointer */ - unsigned int size = 0; + unsigned int size; unsigned int nodesize; + int ret, allocated = 0; /* * first deal with the length since xdr strings are counted-strings @@ -2373,10 +2425,11 @@ xdr_string (XDR *xdrs, char **cpp, unsigned int maxsize) { return 1; /* already free */ } + /* xxx: ignore maxsize during free (it may be + * invalid when called via xdr_free()) */ + maxsize = ~0U; /* fall through... */ case XDR_ENCODE: - if (sp == NULL) - return 0; size = strlen (sp); break; case XDR_DECODE: @@ -2403,7 +2456,10 @@ xdr_string (XDR *xdrs, char **cpp, unsigned int maxsize) return 1; } if (sp == NULL) - *cpp = sp = (char *) malloc (nodesize); + { + *cpp = sp = (char *) malloc (nodesize); + allocated = 1; + } if (sp == NULL) { (void) fputs ("xdr_string: out of memory\n", stderr); @@ -2413,7 +2469,16 @@ xdr_string (XDR *xdrs, char **cpp, unsigned int maxsize) /* fall into ... */ case XDR_ENCODE: - return xdr_opaque (xdrs, sp, size); + ret = xdr_opaque (xdrs, sp, size); + if (xdrs->x_op == XDR_DECODE && ret == 0) + { + if (allocated) + { + free (sp); + *cpp = NULL; + } + } + return ret; case XDR_FREE: free (sp); @@ -2430,28 +2495,19 @@ xdr_string (XDR *xdrs, char **cpp, unsigned int maxsize) static int xdr_float(XDR *xdrs, float *fp) { + /* + * All supported platforms use IEEE 754 single precision and a + * 32-bit int32_t; the float bits map 1:1 onto one XDR unit. + * (This is the IEEEFP path of xdr_float() in FreeBSD libc's + * lib/libc/xdr/xdr_float.c.) + */ switch (xdrs->x_op) { case XDR_ENCODE: - if (sizeof(float) == sizeof(int32_t)) - return (xdr_putlong(xdrs, (int32_t *)fp)); - else if (sizeof(float) == sizeof(int)) { - int32_t tmp = *(int *)fp; - return (xdr_putlong(xdrs, &tmp)); - } - break; + return (xdr_putlong(xdrs, (int32_t *)fp)); case XDR_DECODE: - if (sizeof(float) == sizeof(int32_t)) - return (xdr_getlong(xdrs, (int32_t *)fp)); - else if (sizeof(float) == sizeof(int)) { - int32_t tmp; - if (xdr_getlong(xdrs, &tmp)) { - *(int *)fp = tmp; - return (1); - } - } - break; + return (xdr_getlong(xdrs, (int32_t *)fp)); case XDR_FREE: return (1); @@ -2463,33 +2519,26 @@ xdr_float(XDR *xdrs, float *fp) static int xdr_double(XDR *xdrs, double *dp) { - /* Gromacs detects floating-point stuff at compile time, which is faster */ -#ifdef GROMACS -# ifndef FLOAT_FORMAT_IEEE754 -# error non-IEEE floating point system, or you defined GROMACS yourself... -# endif - int LSW; -# ifdef IEEE754_BIG_ENDIAN_WORD_ORDER - int LSW=1; -# else - int LSW=0; -# endif /* Big endian word order */ -#else - /* Outside Gromacs we rely on dynamic detection of FP order. */ + /* The 32-bit word transfer below follows FreeBSD libc's + * xdr_double() (lib/libc/xdr/xdr_float.c, IEEEFP path). Because + * we cannot rely on or BYTE_ORDER on every + * supported platform, we keep the dynamic detection of the + * floating-point word order that was carried in the previous, + * Gromacs-derived version of this file (BSD 2-clause). */ int LSW; /* Least significant fp word */ double x=0.987654321; /* Just a number */ unsigned char ix = *((char *)&x); - - /* Possible representations in IEEE double precision: + + /* Possible representations in IEEE double precision: * (S=small endian, B=big endian) - * + * * Byte order, Word order, Hex - * S S b8 56 0e 3c dd 9a ef 3f + * S S b8 56 0e 3c dd 9a ef 3f * B S 3c 0e 56 b8 3f ef 9a dd * S B dd 9a ef 3f b8 56 0e 3c * B B 3f ef 9a dd 3c 0e 56 b8 - */ + */ if(ix==0xdd || ix==0x3f) LSW=1; /* Big endian word order */ else if(ix==0xb8 || ix==0x3c) @@ -2500,10 +2549,9 @@ xdr_double(XDR *xdrs, double *dp) "Use system XDR libraries or fix xdr_double().\n"); abort(); } -#endif /* end of dynamic detection of fp word order */ switch (xdrs->x_op) { - + case XDR_ENCODE: if (2*sizeof(int32_t) == sizeof(double)) { int32_t *lp = (int32_t *)dp; @@ -2518,7 +2566,7 @@ xdr_double(XDR *xdrs, double *dp) xdr_putlong(xdrs, tmp+1)); } break; - + case XDR_DECODE: if (2*sizeof(int32_t) == sizeof(double)) { int32_t *lp = (int32_t *)dp; @@ -2535,7 +2583,7 @@ xdr_double(XDR *xdrs, double *dp) } } break; - + case XDR_FREE: return (1); } @@ -2624,7 +2672,7 @@ static int xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len) { if ((len != 0) && (fwrite (addr, (int) len, 1, - (FILE *) xdrs->x_private) != 1)) + (FILE *) xdrs->x_private) != 1)) return 0; return 1; } diff --git a/src/xdrfile/xdrfile_trr.c b/src/xdrfile/xdrfile_trr.c index f59cb4d088..7e3551d2e5 100644 --- a/src/xdrfile/xdrfile_trr.c +++ b/src/xdrfile/xdrfile_trr.c @@ -67,7 +67,7 @@ typedef struct /* This struct describes the order and the */ static int nFloatSize(t_trnheader *sh,int *nflsz) { int nflsize=0; - + if (sh->box_size) nflsize = sh->box_size/(DIM*DIM); else if (sh->x_size) @@ -76,14 +76,14 @@ static int nFloatSize(t_trnheader *sh,int *nflsz) nflsize = sh->v_size/(sh->natoms*DIM); else if (sh->f_size) nflsize = sh->f_size/(sh->natoms*DIM); - else + else return exdrHEADER; - + if (((nflsize != sizeof(float)) && (nflsize != sizeof(double)))) return exdrHEADER; - + *nflsz = nflsize; - + return exdrOK; } @@ -93,7 +93,7 @@ extern int do_trnheader(XDRFILE *xd,mybool bRead,t_trnheader *sh) int nflsz,slen,result; char *version = "GMX_trn_file"; char buf[BUFSIZE]; - + if (xdrfile_read_int(&magic,1,xd) != 1) { /* modification by RTM to return the right EOF code this is what's happening in the XTC code */ @@ -104,7 +104,7 @@ extern int do_trnheader(XDRFILE *xd,mybool bRead,t_trnheader *sh) } if (magic != GROMACS_MAGIC) return exdrMAGIC; - if (bRead) + if (bRead) { if (xdrfile_read_int(&slen,1,xd) != 1) return exdrINT; @@ -113,7 +113,7 @@ extern int do_trnheader(XDRFILE *xd,mybool bRead,t_trnheader *sh) if (xdrfile_read_string(buf,BUFSIZE,xd) <= 0) return exdrSTRING; } - else + else { slen = strlen(version)+1; if (xdrfile_read_int(&slen,1,xd) != 1) @@ -143,16 +143,16 @@ extern int do_trnheader(XDRFILE *xd,mybool bRead,t_trnheader *sh) return exdrINT; if (xdrfile_read_int(&sh->natoms,1,xd) != 1) return exdrINT; - + if ((result = nFloatSize(sh,&nflsz)) != exdrOK) return result; sh->bDouble = (nflsz == sizeof(double)); - + if (xdrfile_read_int(&sh->step,1,xd) != 1) return exdrINT; if (xdrfile_read_int(&sh->nre,1,xd) != 1) return exdrINT; - if (sh->bDouble) + if (sh->bDouble) { if (xdrfile_read_double(&sh->td,1,xd) != 1) return exdrDOUBLE; @@ -161,7 +161,7 @@ extern int do_trnheader(XDRFILE *xd,mybool bRead,t_trnheader *sh) return exdrDOUBLE; sh->lambdaf = sh->lambdad; } - else + else { if (xdrfile_read_float(&sh->tf,1,xd) != 1) return exdrFLOAT; @@ -170,7 +170,7 @@ extern int do_trnheader(XDRFILE *xd,mybool bRead,t_trnheader *sh) return exdrFLOAT; sh->lambdad = sh->lambdaf; } - + return exdrOK; } @@ -182,12 +182,12 @@ static int do_htrn(XDRFILE *xd,mybool bRead,t_trnheader *sh, float pvf[DIM*DIM]; float *fx=NULL; int i,j; - - if (sh->bDouble) + + if (sh->bDouble) { - if (sh->box_size != 0) + if (sh->box_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (ivir_size != 0) + + if (sh->vir_size != 0) { - if (xdrfile_read_double(pvd,DIM*DIM,xd) != DIM*DIM) + if (xdrfile_read_double(pvd,DIM*DIM,xd) != DIM*DIM) return exdrDOUBLE; } - - if (sh->pres_size!= 0) + + if (sh->pres_size!= 0) { - if (xdrfile_read_double(pvd,DIM*DIM,xd) != DIM*DIM) + if (xdrfile_read_double(pvd,DIM*DIM,xd) != DIM*DIM) return exdrDOUBLE; } - + if ((sh->x_size != 0) || (sh->v_size != 0) || (sh->f_size != 0)) { dx = (double *)calloc(sh->natoms*DIM,sizeof(dx[0])); if (NULL == dx) return exdrNOMEM; } - if (sh->x_size != 0) + if (sh->x_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (inatoms); i++) for(j=0; (jnatoms*DIM,xd) == sh->natoms*DIM) { - if (bRead) + if (bRead) { for(i=0; (inatoms); i++) for(j=0; (jv_size != 0) + if (sh->v_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (inatoms); i++) for(j=0; (jf_size != 0) + if (sh->f_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (inatoms); i++) for(j=0; (jbox_size != 0) + if (sh->box_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (ivir_size != 0) + + if (sh->vir_size != 0) { - if (xdrfile_read_float(pvf,DIM*DIM,xd) != DIM*DIM) + if (xdrfile_read_float(pvf,DIM*DIM,xd) != DIM*DIM) return exdrFLOAT; } - - if (sh->pres_size!= 0) + + if (sh->pres_size!= 0) { - if (xdrfile_read_float(pvf,DIM*DIM,xd) != DIM*DIM) + if (xdrfile_read_float(pvf,DIM*DIM,xd) != DIM*DIM) return exdrFLOAT; } - + if ((sh->x_size != 0) || (sh->v_size != 0) || (sh->f_size != 0)) { fx = (float *)calloc(sh->natoms*DIM,sizeof(fx[0])); if (NULL == fx) return exdrNOMEM; } - if (sh->x_size != 0) + if (sh->x_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (inatoms); i++) for(j=0; (jnatoms*DIM,xd) == sh->natoms*DIM) { - if (bRead) + if (bRead) { for(i=0; (inatoms); i++) for(j=0; (jv_size != 0) + if (sh->v_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (inatoms); i++) for(j=0; (jf_size != 0) + if (sh->f_size != 0) { - if (!bRead) + if (!bRead) { for(i=0; (inatoms); i++) for(j=0; (jbox_size = (NULL != box) ? sizeof(matrix):0; sh->x_size = ((NULL != x) ? (*natoms*sizeof(x[0])):0); @@ -461,7 +461,7 @@ static int do_trn(XDRFILE *xd,mybool bRead,int *step,float *t,float *lambda, return result; free(sh); - + return exdrOK; } @@ -470,21 +470,23 @@ static int do_trn(XDRFILE *xd,mybool bRead,int *step,float *t,float *lambda, * The following routines are the exported ones * ************************************************************/ - + int read_trr_natoms(char *fn,int *natoms) { XDRFILE *xd; t_trnheader sh; int result; - + xd = xdrfile_open(fn,"r"); if (NULL == xd) return exdrFILENOTFOUND; - if ((result = do_trnheader(xd,1,&sh)) != exdrOK) + if ((result = do_trnheader(xd,1,&sh)) != exdrOK) { + xdrfile_close(xd); return result; + } xdrfile_close(xd); *natoms = sh.natoms; - + return exdrOK; } @@ -506,14 +508,15 @@ int read_trr_nframes(char *fn, unsigned long *nframes) { x = malloc(natoms * sizeof(*x)); do { - result = read_trr(xd, natoms, &step, &time, &lambda, box, x, NULL, NULL); - if (exdrENDOFFILE != result) { - (*nframes)++; - } + result = read_trr(xd, natoms, &step, &time, &lambda, + box, x, NULL, NULL); + if (exdrENDOFFILE != result) { + (*nframes)++; + } } while (result == exdrOK); - xdrfile_close(xd); - free(x); + xdrfile_close(xd); + free(x); return exdrOK; } @@ -528,4 +531,3 @@ int read_trr(XDRFILE *xd,int natoms,int *step,float *t,float *lambda, { return do_trn(xd,1,step,t,lambda,box,&natoms,x,v,f); } - diff --git a/src/xdrfile/xdrfile_trr.h b/src/xdrfile/xdrfile_trr.h index f48c709594..9d97785e47 100644 --- a/src/xdrfile/xdrfile_trr.h +++ b/src/xdrfile/xdrfile_trr.h @@ -35,15 +35,15 @@ extern "C" { #endif #include "xdrfile.h" - + /* All functions return exdrOK if successful. * (error codes defined in xdrfile.h). - */ - + */ + /* This function returns the number of atoms in the xtc file in *natoms */ extern int read_trr_natoms(char *fn,int *natoms); extern int read_trr_nframes(char* fn, unsigned long *nframes); - + /* Read one frame of an open xtc file. If either of x,v,f,box are NULL the arrays will be read from the file but not used. */ extern int read_trr(XDRFILE *xd,int natoms,int *step,float *t,float *lambda, @@ -53,7 +53,7 @@ extern "C" { extern int write_trr(XDRFILE *xd,int natoms,int step,float t,float lambda, matrix box,rvec *x,rvec *v,rvec *f); - + #ifdef __cplusplus } #endif diff --git a/src/xdrfile/xdrfile_xtc.h b/src/xdrfile/xdrfile_xtc.h index 550c89d9b0..6b5170f676 100644 --- a/src/xdrfile/xdrfile_xtc.h +++ b/src/xdrfile/xdrfile_xtc.h @@ -36,7 +36,7 @@ extern "C" { #include "xdrfile.h" - /* All functions return exdrOK if succesfull. + /* All functions return exdrOK if successful. * (error codes defined in xdrfile.h). */