Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / gmxlib / 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  * 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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #ifdef HAVE_IO_H
46 #include <io.h>
47 #endif
48
49 #include "gmx_fatal.h"
50 #include "macros.h"
51 #include "smalloc.h"
52 #include "futil.h"
53 #include "filenm.h"
54 #include "string2.h"
55 #include "gmxfio.h"
56 #include "md5.h"
57
58 #ifdef GMX_THREAD_MPI
59 #include "thread_mpi.h"
60 #endif
61
62 #include "gmxfio_int.h"
63
64 /* This is the part that reads xdr files.  */
65
66
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);
72
73
74 const t_iotype xdr_iotype={do_xdrread, do_xdrwrite};
75
76
77 #ifdef USE_XDR
78
79 static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio, 
80                    const char *desc, const char *srcfile, int line)
81 {
82     unsigned char ucdum, *ucptr;
83     bool_t res = 0;
84     float fvec[DIM];
85     double dvec[DIM];
86     int j, m, *iptr, idum;
87     gmx_large_int_t sdum;
88     real *ptr;
89     unsigned short us;
90     double d = 0;
91     float f = 0;
92
93     gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
94     switch (eio)
95     {
96     case eioREAL:
97         if (fio->bDouble)
98         {
99             if (item && !fio->bRead)
100                 d = *((real *) item);
101             res = xdr_double(fio->xdr, &d);
102             if (item)
103                 *((real *) item) = d;
104         }
105         else
106         {
107             if (item && !fio->bRead)
108                 f = *((real *) item);
109             res = xdr_float(fio->xdr, &f);
110             if (item)
111                 *((real *) item) = f;
112         }
113         break;
114     case eioFLOAT:
115         if (item && !fio->bRead)
116             f = *((float *) item);
117         res = xdr_float(fio->xdr, &f);
118         if (item)
119             *((float *) item) = f;
120         break;
121     case eioDOUBLE:
122         if (item && !fio->bRead)
123             d = *((double *) item);
124         res = xdr_double(fio->xdr, &d);
125         if (item)
126             *((double *) item) = d;
127         break;
128     case eioINT:
129         if (item && !fio->bRead)
130             idum = *(int *) item;
131         res = xdr_int(fio->xdr, &idum);
132         if (item)
133             *(int *) item = idum;
134         break;
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.
138          */
139         if (item && !fio->bRead)
140             sdum = *(gmx_large_int_t *) item;
141         res = xdr_gmx_large_int(fio->xdr, &sdum, NULL);
142         if (item)
143             *(gmx_large_int_t *) item = sdum;
144         break;
145     case eioUCHAR:
146         if (item && !fio->bRead)
147             ucdum = *(unsigned char *) item;
148         res = xdr_u_char(fio->xdr, &ucdum);
149         if (item)
150             *(unsigned char *) item = ucdum;
151         break;
152     case eioNUCHAR:
153         ucptr = (unsigned char *) item;
154         res = 1;
155         for (j = 0; (j < nitem) && res; j++)
156         {
157             res = xdr_u_char(fio->xdr, &(ucptr[j]));
158         }
159         break;
160     case eioUSHORT:
161         if (item && !fio->bRead)
162             us = *(unsigned short *) item;
163         res = xdr_u_short(fio->xdr, (unsigned short *) &us);
164         if (item)
165             *(unsigned short *) item = us;
166         break;
167     case eioRVEC:
168         if (fio->bDouble)
169         {
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);
176             if (item)
177                 for (m = 0; (m < DIM); m++)
178                     ((real *) item)[m] = dvec[m];
179         }
180         else
181         {
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);
188             if (item)
189                 for (m = 0; (m < DIM); m++)
190                     ((real *) item)[m] = fvec[m];
191         }
192         break;
193     case eioNRVEC:
194         ptr = NULL;
195         res = 1;
196         for (j = 0; (j < nitem) && res; j++)
197         {
198             if (item)
199                 ptr = ((rvec *) item)[j];
200             res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line);
201         }
202         break;
203     case eioIVEC:
204         iptr = (int *) item;
205         res = 1;
206         for (m = 0; (m < DIM) && res; m++)
207         {
208             if (item && !fio->bRead)
209                 idum = iptr[m];
210             res = xdr_int(fio->xdr, &idum);
211             if (item)
212                 iptr[m] = idum;
213         }
214         break;
215     case eioSTRING:
216     {
217         char *cptr;
218         int slen;
219
220         if (item)
221         {
222             if (!fio->bRead)
223                 slen = strlen((char *) item) + 1;
224             else
225                 slen = 0;
226         }
227         else
228             slen = 0;
229
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)
234             snew(cptr,slen);
235         else
236             cptr=(char *)item;
237         if (cptr)
238             res = xdr_string(fio->xdr,&cptr,slen);
239         else
240             res = 1;
241         if (!item && fio->bRead)
242             sfree(cptr);
243         break;
244     }
245     default:
246         gmx_fio_fe(fio, eio, desc, srcfile, line);
247     }
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);
251
252     return (res != 0);
253 }
254
255
256 static gmx_bool do_xdrread(t_fileio *fio, void *item, int nitem, int eio, 
257                        const char *desc, const char *srcfile, int line)
258 {
259     return do_xdr(fio, item, nitem, eio, desc, srcfile, line);
260 }
261
262
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)
265 {
266     void *it=(void*)item; /* ugh.. */
267     return do_xdr(fio, it, nitem, eio, desc, srcfile, line);
268 }
269
270 #endif
271
272