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(fio, eio, nitem, srcfile, line);
97 if (item && !fio->bRead)
99 res = xdr_double(fio->xdr, &d);
101 *((real *) item) = d;
105 if (item && !fio->bRead)
106 f = *((real *) item);
107 res = xdr_float(fio->xdr, &f);
109 *((real *) item) = f;
113 if (item && !fio->bRead)
114 f = *((float *) item);
115 res = xdr_float(fio->xdr, &f);
117 *((float *) item) = f;
120 if (item && !fio->bRead)
121 d = *((double *) item);
122 res = xdr_double(fio->xdr, &d);
124 *((double *) item) = d;
127 if (item && !fio->bRead)
128 idum = *(int *) item;
129 res = xdr_int(fio->xdr, &idum);
131 *(int *) item = idum;
133 case eioGMX_LARGE_INT:
134 /* do_xdr will not generate a warning when a 64bit gmx_large_int_t
135 * value that is out of 32bit range is read into a 32bit gmx_large_int_t.
137 if (item && !fio->bRead)
138 sdum = *(gmx_large_int_t *) item;
139 res = xdr_gmx_large_int(fio->xdr, &sdum, NULL);
141 *(gmx_large_int_t *) item = sdum;
144 if (item && !fio->bRead)
145 ucdum = *(unsigned char *) item;
146 res = xdr_u_char(fio->xdr, &ucdum);
148 *(unsigned char *) item = ucdum;
151 ucptr = (unsigned char *) item;
153 for (j = 0; (j < nitem) && res; j++)
155 res = xdr_u_char(fio->xdr, &(ucptr[j]));
159 if (item && !fio->bRead)
160 us = *(unsigned short *) item;
161 res = xdr_u_short(fio->xdr, (unsigned short *) &us);
163 *(unsigned short *) item = us;
168 if (item && !fio->bRead)
169 for (m = 0; (m < DIM); m++)
170 dvec[m] = ((real *) item)[m];
171 res = xdr_vector(fio->xdr, (char *) dvec, DIM,
172 (unsigned int) sizeof(double),
173 (xdrproc_t) xdr_double);
175 for (m = 0; (m < DIM); m++)
176 ((real *) item)[m] = dvec[m];
180 if (item && !fio->bRead)
181 for (m = 0; (m < DIM); m++)
182 fvec[m] = ((real *) item)[m];
183 res = xdr_vector(fio->xdr, (char *) fvec, DIM,
184 (unsigned int) sizeof(float),
185 (xdrproc_t) xdr_float);
187 for (m = 0; (m < DIM); m++)
188 ((real *) item)[m] = fvec[m];
194 for (j = 0; (j < nitem) && res; j++)
197 ptr = ((rvec *) item)[j];
198 res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
204 for (m = 0; (m < DIM) && res; m++)
206 if (item && !fio->bRead)
208 res = xdr_int(fio->xdr, &idum);
221 slen = strlen((char *) item) + 1;
228 if (xdr_int(fio->xdr, &slen) <= 0)
229 gmx_fatal(FARGS, "wrong string length %d for string %s"
230 " (source %s, line %d)",slen,desc,srcfile,line);
231 if (!item && fio->bRead)
236 res = xdr_string(fio->xdr,&cptr,slen);
239 if (!item && fio->bRead)
244 gmx_fio_fe(fio, eio, desc, srcfile, line);
246 if ((res == 0) && (fio->bDebug))
247 fprintf(stderr,"Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
248 eioNames[eio],desc,fio->fn,srcfile,line);
254 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
255 const char *desc, const char *srcfile, int line)
257 return do_xdr(fio, item, nitem, eio, desc, srcfile, line);
261 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
262 const char *desc, const char *srcfile, int line)
264 void *it=(void*)item; /* ugh.. */
265 return do_xdr(fio, it, nitem, eio, desc, srcfile, line);