Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / gmxlib / gmxfio_xdr.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  *                        VERSION 3.2.0
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.
15
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.
20  *
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.
27  *
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.
30  *
31  * For more info, check our website at http://www.gromacs.org
32  *
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <ctype.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #ifdef HAVE_IO_H
44 #include <io.h>
45 #endif
46
47 #include "gmx_fatal.h"
48 #include "macros.h"
49 #include "smalloc.h"
50 #include "futil.h"
51 #include "filenm.h"
52 #include "string2.h"
53 #include "gmxfio.h"
54 #include "md5.h"
55
56 #ifdef GMX_THREAD_MPI
57 #include "thread_mpi.h"
58 #endif
59
60 #include "gmxfio_int.h"
61
62 /* This is the part that reads xdr files.  */
63
64
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);
70
71
72 const t_iotype xdr_iotype = {do_xdrread, do_xdrwrite};
73
74
75 #ifdef USE_XDR
76
77 static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio,
78                        const char *desc, const char *srcfile, int line)
79 {
80     unsigned char   ucdum, *ucptr;
81     bool_t          res = 0;
82     float           fvec[DIM];
83     double          dvec[DIM];
84     int             j, m, *iptr, idum;
85     gmx_large_int_t sdum;
86     real           *ptr;
87     unsigned short  us;
88     double          d = 0;
89     float           f = 0;
90
91     gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
92     switch (eio)
93     {
94         case eioREAL:
95             if (fio->bDouble)
96             {
97                 if (item && !fio->bRead)
98                 {
99                     d = *((real *) item);
100                 }
101                 res = xdr_double(fio->xdr, &d);
102                 if (item)
103                 {
104                     *((real *) item) = d;
105                 }
106             }
107             else
108             {
109                 if (item && !fio->bRead)
110                 {
111                     f = *((real *) item);
112                 }
113                 res = xdr_float(fio->xdr, &f);
114                 if (item)
115                 {
116                     *((real *) item) = f;
117                 }
118             }
119             break;
120         case eioFLOAT:
121             if (item && !fio->bRead)
122             {
123                 f = *((float *) item);
124             }
125             res = xdr_float(fio->xdr, &f);
126             if (item)
127             {
128                 *((float *) item) = f;
129             }
130             break;
131         case eioDOUBLE:
132             if (item && !fio->bRead)
133             {
134                 d = *((double *) item);
135             }
136             res = xdr_double(fio->xdr, &d);
137             if (item)
138             {
139                 *((double *) item) = d;
140             }
141             break;
142         case eioINT:
143             if (item && !fio->bRead)
144             {
145                 idum = *(int *) item;
146             }
147             res = xdr_int(fio->xdr, &idum);
148             if (item)
149             {
150                 *(int *) item = idum;
151             }
152             break;
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.
156              */
157             if (item && !fio->bRead)
158             {
159                 sdum = *(gmx_large_int_t *) item;
160             }
161             res = xdr_gmx_large_int(fio->xdr, &sdum, NULL);
162             if (item)
163             {
164                 *(gmx_large_int_t *) item = sdum;
165             }
166             break;
167         case eioUCHAR:
168             if (item && !fio->bRead)
169             {
170                 ucdum = *(unsigned char *) item;
171             }
172             res = xdr_u_char(fio->xdr, &ucdum);
173             if (item)
174             {
175                 *(unsigned char *) item = ucdum;
176             }
177             break;
178         case eioNUCHAR:
179             ucptr = (unsigned char *) item;
180             res   = 1;
181             for (j = 0; (j < nitem) && res; j++)
182             {
183                 res = xdr_u_char(fio->xdr, &(ucptr[j]));
184             }
185             break;
186         case eioUSHORT:
187             if (item && !fio->bRead)
188             {
189                 us = *(unsigned short *) item;
190             }
191             res = xdr_u_short(fio->xdr, (unsigned short *) &us);
192             if (item)
193             {
194                 *(unsigned short *) item = us;
195             }
196             break;
197         case eioRVEC:
198             if (fio->bDouble)
199             {
200                 if (item && !fio->bRead)
201                 {
202                     for (m = 0; (m < DIM); m++)
203                     {
204                         dvec[m] = ((real *) item)[m];
205                     }
206                 }
207                 res = xdr_vector(fio->xdr, (char *) dvec, DIM,
208                                  (unsigned int) sizeof(double),
209                                  (xdrproc_t) xdr_double);
210                 if (item)
211                 {
212                     for (m = 0; (m < DIM); m++)
213                     {
214                         ((real *) item)[m] = dvec[m];
215                     }
216                 }
217             }
218             else
219             {
220                 if (item && !fio->bRead)
221                 {
222                     for (m = 0; (m < DIM); m++)
223                     {
224                         fvec[m] = ((real *) item)[m];
225                     }
226                 }
227                 res = xdr_vector(fio->xdr, (char *) fvec, DIM,
228                                  (unsigned int) sizeof(float),
229                                  (xdrproc_t) xdr_float);
230                 if (item)
231                 {
232                     for (m = 0; (m < DIM); m++)
233                     {
234                         ((real *) item)[m] = fvec[m];
235                     }
236                 }
237             }
238             break;
239         case eioNRVEC:
240             ptr = NULL;
241             res = 1;
242             for (j = 0; (j < nitem) && res; j++)
243             {
244                 if (item)
245                 {
246                     ptr = ((rvec *) item)[j];
247                 }
248                 res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
249             }
250             break;
251         case eioIVEC:
252             iptr = (int *) item;
253             res  = 1;
254             for (m = 0; (m < DIM) && res; m++)
255             {
256                 if (item && !fio->bRead)
257                 {
258                     idum = iptr[m];
259                 }
260                 res = xdr_int(fio->xdr, &idum);
261                 if (item)
262                 {
263                     iptr[m] = idum;
264                 }
265             }
266             break;
267         case eioSTRING:
268         {
269             char *cptr;
270             int   slen;
271
272             if (item)
273             {
274                 if (!fio->bRead)
275                 {
276                     slen = strlen((char *) item) + 1;
277                 }
278                 else
279                 {
280                     slen = 0;
281                 }
282             }
283             else
284             {
285                 slen = 0;
286             }
287
288             if (xdr_int(fio->xdr, &slen) <= 0)
289             {
290                 gmx_fatal(FARGS, "wrong string length %d for string %s"
291                           " (source %s, line %d)", slen, desc, srcfile, line);
292             }
293             if (!item && fio->bRead)
294             {
295                 snew(cptr, slen);
296             }
297             else
298             {
299                 cptr = (char *)item;
300             }
301             if (cptr)
302             {
303                 res = xdr_string(fio->xdr, &cptr, slen);
304             }
305             else
306             {
307                 res = 1;
308             }
309             if (!item && fio->bRead)
310             {
311                 sfree(cptr);
312             }
313             break;
314         }
315         default:
316             gmx_fio_fe(fio, eio, desc, srcfile, line);
317     }
318     if ((res == 0) && (fio->bDebug))
319     {
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);
322     }
323
324     return (res != 0);
325 }
326
327
328 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio,
329                            const char *desc, const char *srcfile, int line)
330 {
331     return do_xdr(fio, item, nitem, eio, desc, srcfile, line);
332 }
333
334
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)
337 {
338     void *it = (void*)item; /* ugh.. */
339     return do_xdr(fio, it, nitem, eio, desc, srcfile, line);
340 }
341
342 #endif