2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * Copyright (c) 2013, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gmx_fatal.h"
58 #include "thread_mpi.h"
61 #include "gmxfio_int.h"
63 /* This is the part that reads xdr files. */
66 /* file type functions */
67 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
68 const char *desc, const char *srcfile, int line);
69 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
70 const char *desc, const char *srcfile, int line);
73 const t_iotype xdr_iotype = {do_xdrread, do_xdrwrite};
78 static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
79 const char *desc, const char *srcfile, int line)
81 unsigned char ucdum, *ucptr;
85 int j, m, *iptr, idum;
92 gmx_fio_check_nitem(eio, nitem, srcfile, line);
98 if (item && !fio->bRead)
100 d = *((real *) item);
102 res = xdr_double(fio->xdr, &d);
105 *((real *) item) = d;
110 if (item && !fio->bRead)
112 f = *((real *) item);
114 res = xdr_float(fio->xdr, &f);
117 *((real *) item) = f;
122 if (item && !fio->bRead)
124 f = *((float *) item);
126 res = xdr_float(fio->xdr, &f);
129 *((float *) item) = f;
133 if (item && !fio->bRead)
135 d = *((double *) item);
137 res = xdr_double(fio->xdr, &d);
140 *((double *) item) = d;
144 if (item && !fio->bRead)
146 idum = *(int *) item;
148 res = xdr_int(fio->xdr, &idum);
151 *(int *) item = idum;
154 case eioGMX_LARGE_INT:
155 /* do_xdr will not generate a warning when a 64bit gmx_large_int_t
156 * value that is out of 32bit range is read into a 32bit gmx_large_int_t.
158 if (item && !fio->bRead)
160 sdum = *(gmx_large_int_t *) item;
162 res = xdr_gmx_large_int(fio->xdr, &sdum);
165 *(gmx_large_int_t *) item = sdum;
169 if (item && !fio->bRead)
171 ucdum = *(unsigned char *) item;
173 res = xdr_u_char(fio->xdr, &ucdum);
176 *(unsigned char *) item = ucdum;
180 ucptr = (unsigned char *) item;
182 for (j = 0; (j < nitem) && res; j++)
184 res = xdr_u_char(fio->xdr, &(ucptr[j]));
188 if (item && !fio->bRead)
190 us = *(unsigned short *) item;
192 res = xdr_u_short(fio->xdr, (unsigned short *) &us);
195 *(unsigned short *) item = us;
201 if (item && !fio->bRead)
203 for (m = 0; (m < DIM); m++)
205 dvec[m] = ((real *) item)[m];
208 res = xdr_vector(fio->xdr, (char *) dvec, DIM,
209 (unsigned int) sizeof(double),
210 (xdrproc_t) xdr_double);
213 for (m = 0; (m < DIM); m++)
215 ((real *) item)[m] = dvec[m];
221 if (item && !fio->bRead)
223 for (m = 0; (m < DIM); m++)
225 fvec[m] = ((real *) item)[m];
228 res = xdr_vector(fio->xdr, (char *) fvec, DIM,
229 (unsigned int) sizeof(float),
230 (xdrproc_t) xdr_float);
233 for (m = 0; (m < DIM); m++)
235 ((real *) item)[m] = fvec[m];
243 for (j = 0; (j < nitem) && res; j++)
247 ptr = ((rvec *) item)[j];
249 res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
255 for (m = 0; (m < DIM) && res; m++)
257 if (item && !fio->bRead)
261 res = xdr_int(fio->xdr, &idum);
277 slen = strlen((char *) item) + 1;
289 if (xdr_int(fio->xdr, &slen) <= 0)
291 gmx_fatal(FARGS, "wrong string length %d for string %s"
292 " (source %s, line %d)", slen, desc, srcfile, line);
294 if (!item && fio->bRead)
304 res = xdr_string(fio->xdr, &cptr, slen);
310 if (!item && fio->bRead)
317 gmx_fio_fe(fio, eio, desc, srcfile, line);
319 if ((res == 0) && (fio->bDebug))
321 fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
322 eioNames[eio], desc, fio->fn, srcfile, line);
329 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
330 const char *desc, const char *srcfile, int line)
332 return do_xdr(fio, item, nitem, eio, desc, srcfile, line);
336 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
337 const char *desc, const char *srcfile, int line)
339 void *it = (void*)item; /* ugh.. */
340 return do_xdr(fio, it, nitem, eio, desc, srcfile, line);