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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
49 #include "gmx_fatal.h"
59 #include "thread_mpi.h"
62 #include "gmxfio_int.h"
64 /* This is the part that reads xdr files. */
67 /* file type functions */
68 static gmx_bool
do_xdrread(t_fileio
*fio
, void *item
, int nitem
, int eio
,
69 const char *desc
, const char *srcfile
, int line
);
70 static gmx_bool
do_xdrwrite(t_fileio
*fio
, const void *item
, int nitem
, int eio
,
71 const char *desc
, const char *srcfile
, int line
);
74 const t_iotype xdr_iotype
={do_xdrread
, do_xdrwrite
};
79 static gmx_bool
do_xdr(t_fileio
*fio
, void *item
, int nitem
, int eio
,
80 const char *desc
, const char *srcfile
, int line
)
82 unsigned char ucdum
, *ucptr
;
86 int j
, m
, *iptr
, idum
;
93 gmx_fio_check_nitem(fio
, eio
, nitem
, srcfile
, line
);
99 if (item
&& !fio
->bRead
)
100 d
= *((real
*) item
);
101 res
= xdr_double(fio
->xdr
, &d
);
103 *((real
*) item
) = d
;
107 if (item
&& !fio
->bRead
)
108 f
= *((real
*) item
);
109 res
= xdr_float(fio
->xdr
, &f
);
111 *((real
*) item
) = f
;
115 if (item
&& !fio
->bRead
)
116 f
= *((float *) item
);
117 res
= xdr_float(fio
->xdr
, &f
);
119 *((float *) item
) = f
;
122 if (item
&& !fio
->bRead
)
123 d
= *((double *) item
);
124 res
= xdr_double(fio
->xdr
, &d
);
126 *((double *) item
) = d
;
129 if (item
&& !fio
->bRead
)
130 idum
= *(int *) item
;
131 res
= xdr_int(fio
->xdr
, &idum
);
133 *(int *) item
= idum
;
135 case eioGMX_LARGE_INT
:
136 /* do_xdr will not generate a warning when a 64bit gmx_large_int_t
137 * value that is out of 32bit range is read into a 32bit gmx_large_int_t.
139 if (item
&& !fio
->bRead
)
140 sdum
= *(gmx_large_int_t
*) item
;
141 res
= xdr_gmx_large_int(fio
->xdr
, &sdum
, NULL
);
143 *(gmx_large_int_t
*) item
= sdum
;
146 if (item
&& !fio
->bRead
)
147 ucdum
= *(unsigned char *) item
;
148 res
= xdr_u_char(fio
->xdr
, &ucdum
);
150 *(unsigned char *) item
= ucdum
;
153 ucptr
= (unsigned char *) item
;
155 for (j
= 0; (j
< nitem
) && res
; j
++)
157 res
= xdr_u_char(fio
->xdr
, &(ucptr
[j
]));
161 if (item
&& !fio
->bRead
)
162 us
= *(unsigned short *) item
;
163 res
= xdr_u_short(fio
->xdr
, (unsigned short *) &us
);
165 *(unsigned short *) item
= us
;
170 if (item
&& !fio
->bRead
)
171 for (m
= 0; (m
< DIM
); m
++)
172 dvec
[m
] = ((real
*) item
)[m
];
173 res
= xdr_vector(fio
->xdr
, (char *) dvec
, DIM
,
174 (unsigned int) sizeof(double),
175 (xdrproc_t
) xdr_double
);
177 for (m
= 0; (m
< DIM
); m
++)
178 ((real
*) item
)[m
] = dvec
[m
];
182 if (item
&& !fio
->bRead
)
183 for (m
= 0; (m
< DIM
); m
++)
184 fvec
[m
] = ((real
*) item
)[m
];
185 res
= xdr_vector(fio
->xdr
, (char *) fvec
, DIM
,
186 (unsigned int) sizeof(float),
187 (xdrproc_t
) xdr_float
);
189 for (m
= 0; (m
< DIM
); m
++)
190 ((real
*) item
)[m
] = fvec
[m
];
196 for (j
= 0; (j
< nitem
) && res
; j
++)
199 ptr
= ((rvec
*) item
)[j
];
200 res
= do_xdr(fio
, ptr
, 1, eioRVEC
, desc
, srcfile
, line
);
206 for (m
= 0; (m
< DIM
) && res
; m
++)
208 if (item
&& !fio
->bRead
)
210 res
= xdr_int(fio
->xdr
, &idum
);
223 slen
= strlen((char *) item
) + 1;
230 if (xdr_int(fio
->xdr
, &slen
) <= 0)
231 gmx_fatal(FARGS
, "wrong string length %d for string %s"
232 " (source %s, line %d)",slen
,desc
,srcfile
,line
);
233 if (!item
&& fio
->bRead
)
238 res
= xdr_string(fio
->xdr
,&cptr
,slen
);
241 if (!item
&& fio
->bRead
)
246 gmx_fio_fe(fio
, eio
, desc
, srcfile
, line
);
248 if ((res
== 0) && (fio
->bDebug
))
249 fprintf(stderr
,"Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
250 eioNames
[eio
],desc
,fio
->fn
,srcfile
,line
);
256 static gmx_bool
do_xdrread(t_fileio
*fio
, void *item
, int nitem
, int eio
,
257 const char *desc
, const char *srcfile
, int line
)
259 return do_xdr(fio
, item
, nitem
, eio
, desc
, srcfile
, line
);
263 static gmx_bool
do_xdrwrite(t_fileio
*fio
, const void *item
, int nitem
, int eio
,
264 const char *desc
, const char *srcfile
, int line
)
266 void *it
=(void*)item
; /* ugh.. */
267 return do_xdr(fio
, it
, nitem
, eio
, desc
, srcfile
, line
);