1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
47 #include "gmx_fatal.h"
57 #include "thread_mpi.h"
60 #include "gmxfio_int.h"
62 /* This is the part that reads xdr files. */
65 /* file type functions */
66 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
67 const char *desc, const char *srcfile, int line);
68 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
69 const char *desc, const char *srcfile, int line);
72 const t_iotype xdr_iotype = {do_xdrread, do_xdrwrite};
77 static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
78 const char *desc, const char *srcfile, int line)
80 unsigned char ucdum, *ucptr;
84 int j, m, *iptr, idum;
91 gmx_fio_check_nitem(eio, nitem, srcfile, line);
97 if (item && !fio->bRead)
101 res = xdr_double(fio->xdr, &d);
104 *((real *) item) = d;
109 if (item && !fio->bRead)
111 f = *((real *) item);
113 res = xdr_float(fio->xdr, &f);
116 *((real *) item) = f;
121 if (item && !fio->bRead)
123 f = *((float *) item);
125 res = xdr_float(fio->xdr, &f);
128 *((float *) item) = f;
132 if (item && !fio->bRead)
134 d = *((double *) item);
136 res = xdr_double(fio->xdr, &d);
139 *((double *) item) = d;
143 if (item && !fio->bRead)
145 idum = *(int *) item;
147 res = xdr_int(fio->xdr, &idum);
150 *(int *) item = idum;
153 case eioGMX_LARGE_INT:
154 /* do_xdr will not generate a warning when a 64bit gmx_large_int_t
155 * value that is out of 32bit range is read into a 32bit gmx_large_int_t.
157 if (item && !fio->bRead)
159 sdum = *(gmx_large_int_t *) item;
161 res = xdr_gmx_large_int(fio->xdr, &sdum);
164 *(gmx_large_int_t *) item = sdum;
168 if (item && !fio->bRead)
170 ucdum = *(unsigned char *) item;
172 res = xdr_u_char(fio->xdr, &ucdum);
175 *(unsigned char *) item = ucdum;
179 ucptr = (unsigned char *) item;
181 for (j = 0; (j < nitem) && res; j++)
183 res = xdr_u_char(fio->xdr, &(ucptr[j]));
187 if (item && !fio->bRead)
189 us = *(unsigned short *) item;
191 res = xdr_u_short(fio->xdr, (unsigned short *) &us);
194 *(unsigned short *) item = us;
200 if (item && !fio->bRead)
202 for (m = 0; (m < DIM); m++)
204 dvec[m] = ((real *) item)[m];
207 res = xdr_vector(fio->xdr, (char *) dvec, DIM,
208 (unsigned int) sizeof(double),
209 (xdrproc_t) xdr_double);
212 for (m = 0; (m < DIM); m++)
214 ((real *) item)[m] = dvec[m];
220 if (item && !fio->bRead)
222 for (m = 0; (m < DIM); m++)
224 fvec[m] = ((real *) item)[m];
227 res = xdr_vector(fio->xdr, (char *) fvec, DIM,
228 (unsigned int) sizeof(float),
229 (xdrproc_t) xdr_float);
232 for (m = 0; (m < DIM); m++)
234 ((real *) item)[m] = fvec[m];
242 for (j = 0; (j < nitem) && res; j++)
246 ptr = ((rvec *) item)[j];
248 res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
254 for (m = 0; (m < DIM) && res; m++)
256 if (item && !fio->bRead)
260 res = xdr_int(fio->xdr, &idum);
276 slen = strlen((char *) item) + 1;
288 if (xdr_int(fio->xdr, &slen) <= 0)
290 gmx_fatal(FARGS, "wrong string length %d for string %s"
291 " (source %s, line %d)", slen, desc, srcfile, line);
293 if (!item && fio->bRead)
303 res = xdr_string(fio->xdr, &cptr, slen);
309 if (!item && fio->bRead)
316 gmx_fio_fe(fio, eio, desc, srcfile, line);
318 if ((res == 0) && (fio->bDebug))
320 fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
321 eioNames[eio], desc, fio->fn, srcfile, line);
328 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
329 const char *desc, const char *srcfile, int line)
331 return do_xdr(fio, item, nitem, eio, desc, srcfile, line);
335 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
336 const char *desc, const char *srcfile, int line)
338 void *it = (void*)item; /* ugh.. */
339 return do_xdr(fio, it, nitem, eio, desc, srcfile, line);