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