Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / fileio / gmxfio_xdr.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <errno.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #ifdef HAVE_IO_H
46 #include <io.h>
47 #endif
48
49 #include "gromacs/fileio/filenm.h"
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/fileio/gmxfio_int.h"
52 #include "gromacs/fileio/md5.h"
53 #include "gromacs/legacyheaders/macros.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/futil.h"
56 #include "gromacs/utility/smalloc.h"
57
58 /* This is the part that reads xdr files.  */
59
60
61 /* file type functions */
62 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
63                            const char *desc, const char *srcfile, int line);
64 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
65                             const char *desc, const char *srcfile, int line);
66
67
68 const t_iotype xdr_iotype = {do_xdrread, do_xdrwrite};
69
70
71 #ifdef USE_XDR
72
73 static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
74                        const char *desc, const char *srcfile, int line)
75 {
76     unsigned char   ucdum, *ucptr;
77     bool_t          res = 0;
78     float           fvec[DIM];
79     double          dvec[DIM];
80     int             j, m, *iptr, idum;
81     gmx_int64_t     sdum;
82     real           *ptr;
83     unsigned short  us;
84     double          d = 0;
85     float           f = 0;
86
87     gmx_fio_check_nitem(eio, nitem, srcfile, line);
88     switch (eio)
89     {
90         case eioREAL:
91             if (fio->bDouble)
92             {
93                 if (item && !fio->bRead)
94                 {
95                     d = *((real *) item);
96                 }
97                 res = xdr_double(fio->xdr, &d);
98                 if (item)
99                 {
100                     *((real *) item) = d;
101                 }
102             }
103             else
104             {
105                 if (item && !fio->bRead)
106                 {
107                     f = *((real *) item);
108                 }
109                 res = xdr_float(fio->xdr, &f);
110                 if (item)
111                 {
112                     *((real *) item) = f;
113                 }
114             }
115             break;
116         case eioFLOAT:
117             if (item && !fio->bRead)
118             {
119                 f = *((float *) item);
120             }
121             res = xdr_float(fio->xdr, &f);
122             if (item)
123             {
124                 *((float *) item) = f;
125             }
126             break;
127         case eioDOUBLE:
128             if (item && !fio->bRead)
129             {
130                 d = *((double *) item);
131             }
132             res = xdr_double(fio->xdr, &d);
133             if (item)
134             {
135                 *((double *) item) = d;
136             }
137             break;
138         case eioINT:
139             if (item && !fio->bRead)
140             {
141                 idum = *(int *) item;
142             }
143             res = xdr_int(fio->xdr, &idum);
144             if (item)
145             {
146                 *(int *) item = idum;
147             }
148             break;
149         case eioINT64:
150             if (item && !fio->bRead)
151             {
152                 sdum = *(gmx_int64_t *) item;
153             }
154             res = xdr_int64(fio->xdr, &sdum);
155             if (item)
156             {
157                 *(gmx_int64_t *) item = sdum;
158             }
159             break;
160         case eioUCHAR:
161             if (item && !fio->bRead)
162             {
163                 ucdum = *(unsigned char *) item;
164             }
165             res = xdr_u_char(fio->xdr, &ucdum);
166             if (item)
167             {
168                 *(unsigned char *) item = ucdum;
169             }
170             break;
171         case eioNUCHAR:
172             ucptr = (unsigned char *) item;
173             res   = 1;
174             for (j = 0; (j < nitem) && res; j++)
175             {
176                 res = xdr_u_char(fio->xdr, &(ucptr[j]));
177             }
178             break;
179         case eioUSHORT:
180             if (item && !fio->bRead)
181             {
182                 us = *(unsigned short *) item;
183             }
184             res = xdr_u_short(fio->xdr, (unsigned short *) &us);
185             if (item)
186             {
187                 *(unsigned short *) item = us;
188             }
189             break;
190         case eioRVEC:
191             if (fio->bDouble)
192             {
193                 if (item && !fio->bRead)
194                 {
195                     for (m = 0; (m < DIM); m++)
196                     {
197                         dvec[m] = ((real *) item)[m];
198                     }
199                 }
200                 res = xdr_vector(fio->xdr, (char *) dvec, DIM,
201                                  (unsigned int) sizeof(double),
202                                  (xdrproc_t) xdr_double);
203                 if (item)
204                 {
205                     for (m = 0; (m < DIM); m++)
206                     {
207                         ((real *) item)[m] = dvec[m];
208                     }
209                 }
210             }
211             else
212             {
213                 if (item && !fio->bRead)
214                 {
215                     for (m = 0; (m < DIM); m++)
216                     {
217                         fvec[m] = ((real *) item)[m];
218                     }
219                 }
220                 res = xdr_vector(fio->xdr, (char *) fvec, DIM,
221                                  (unsigned int) sizeof(float),
222                                  (xdrproc_t) xdr_float);
223                 if (item)
224                 {
225                     for (m = 0; (m < DIM); m++)
226                     {
227                         ((real *) item)[m] = fvec[m];
228                     }
229                 }
230             }
231             break;
232         case eioNRVEC:
233             ptr = NULL;
234             res = 1;
235             for (j = 0; (j < nitem) && res; j++)
236             {
237                 if (item)
238                 {
239                     ptr = ((rvec *) item)[j];
240                 }
241                 res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
242             }
243             break;
244         case eioIVEC:
245             iptr = (int *) item;
246             res  = 1;
247             for (m = 0; (m < DIM) && res; m++)
248             {
249                 if (item && !fio->bRead)
250                 {
251                     idum = iptr[m];
252                 }
253                 res = xdr_int(fio->xdr, &idum);
254                 if (item)
255                 {
256                     iptr[m] = idum;
257                 }
258             }
259             break;
260         case eioSTRING:
261         {
262             char *cptr;
263             int   slen;
264
265             if (item)
266             {
267                 if (!fio->bRead)
268                 {
269                     slen = strlen((char *) item) + 1;
270                 }
271                 else
272                 {
273                     slen = 0;
274                 }
275             }
276             else
277             {
278                 slen = 0;
279             }
280
281             if (xdr_int(fio->xdr, &slen) <= 0)
282             {
283                 gmx_fatal(FARGS, "wrong string length %d for string %s"
284                           " (source %s, line %d)", slen, desc, srcfile, line);
285             }
286             if (!item && fio->bRead)
287             {
288                 snew(cptr, slen);
289             }
290             else
291             {
292                 cptr = (char *)item;
293             }
294             if (cptr)
295             {
296                 res = xdr_string(fio->xdr, &cptr, slen);
297             }
298             else
299             {
300                 res = 1;
301             }
302             if (!item && fio->bRead)
303             {
304                 sfree(cptr);
305             }
306             break;
307         }
308         default:
309             gmx_fio_fe(fio, eio, desc, srcfile, line);
310     }
311     if ((res == 0) && (fio->bDebug))
312     {
313         fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n",
314                 eioNames[eio], desc, fio->fn, srcfile, line);
315     }
316
317     return (res != 0);
318 }
319
320
321 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
322                            const char *desc, const char *srcfile, int line)
323 {
324     return do_xdr(fio, item, nitem, eio, desc, srcfile, line);
325 }
326
327
328 static gmx_bool do_xdrwrite(t_fileio *fio, const void *item, int nitem, int eio,
329                             const char *desc, const char *srcfile, int line)
330 {
331     void *it = (void*)item; /* ugh.. */
332     return do_xdr(fio, it, nitem, eio, desc, srcfile, line);
333 }
334
335 #endif