Further gmxfio cleanup related to xdr handling
[alexxy/gromacs.git] / src / gromacs / fileio / gmxfio_rw.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 "gromacs/fileio/gmxfio.h"
40
41 #include "gmxfio-impl.h"
42
43 /*******************************************************************
44  *
45  * READ/WRITE FUNCTIONS
46  *
47  *******************************************************************/
48
49 gmx_bool gmx_fio_writee_string(t_fileio *fio, const char *item,
50                                const char *desc, const char *srcfile, int line)
51 {
52     gmx_bool ret;
53     void    *it = (void*)item; /* ugh.. */
54     gmx_fio_lock(fio);
55     ret = do_xdr(fio, it, 1, eioSTRING, desc, srcfile, line);
56     gmx_fio_unlock(fio);
57     return ret;
58 }
59
60 gmx_bool gmx_fio_doe_real(t_fileio *fio, real *item,
61                           const char *desc, const char *srcfile, int line)
62 {
63     gmx_bool ret;
64     gmx_fio_lock(fio);
65     ret = do_xdr(fio, item, 1, eioREAL, desc, srcfile, line);
66     gmx_fio_unlock(fio);
67     return ret;
68
69 }
70
71 gmx_bool gmx_fio_doe_float(t_fileio *fio, float *item,
72                            const char *desc, const char *srcfile, int line)
73 {
74     gmx_bool ret;
75     gmx_fio_lock(fio);
76     ret = do_xdr(fio, item, 1, eioFLOAT, desc, srcfile, line);
77     gmx_fio_unlock(fio);
78     return ret;
79 }
80
81 gmx_bool gmx_fio_doe_double(t_fileio *fio, double *item,
82                             const char *desc, const char *srcfile, int line)
83 {
84     gmx_bool ret;
85     gmx_fio_lock(fio);
86     ret = do_xdr(fio, item, 1, eioDOUBLE, desc, srcfile, line);
87     gmx_fio_unlock(fio);
88     return ret;
89 }
90
91
92 gmx_bool gmx_fio_doe_gmx_bool(t_fileio *fio, gmx_bool *item,
93                               const char *desc, const char *srcfile, int line)
94 {
95     gmx_bool ret;
96     int      itmp;
97
98     gmx_fio_lock(fio);
99     if (fio->bRead)
100     {
101         ret   = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
102         *item = itmp;
103     }
104     else
105     {
106         itmp = *item;
107         ret  = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line);
108     }
109     gmx_fio_unlock(fio);
110     return ret;
111 }
112
113 gmx_bool gmx_fio_doe_int(t_fileio *fio, int *item,
114                          const char *desc, const char *srcfile, int line)
115 {
116     gmx_bool ret;
117     gmx_fio_lock(fio);
118     ret = do_xdr(fio, item, 1, eioINT, desc, srcfile, line);
119     gmx_fio_unlock(fio);
120     return ret;
121 }
122
123 gmx_bool gmx_fio_doe_int64(t_fileio *fio, gmx_int64_t *item,
124                            const char *desc, const char *srcfile, int line)
125 {
126     gmx_bool ret;
127     gmx_fio_lock(fio);
128     ret = do_xdr(fio, item, 1, eioINT64, desc, srcfile, line);
129     gmx_fio_unlock(fio);
130     return ret;
131 }
132
133 gmx_bool gmx_fio_doe_uchar(t_fileio *fio, unsigned char *item,
134                            const char *desc, const char *srcfile, int line)
135 {
136     gmx_bool ret;
137     gmx_fio_lock(fio);
138     ret = do_xdr(fio, item, 1, eioUCHAR, desc, srcfile, line);
139     gmx_fio_unlock(fio);
140     return ret;
141 }
142
143 gmx_bool gmx_fio_doe_ushort(t_fileio *fio, unsigned short *item,
144                             const char *desc, const char *srcfile, int line)
145 {
146     gmx_bool ret;
147     gmx_fio_lock(fio);
148     ret = do_xdr(fio, item, 1, eioUSHORT, desc, srcfile, line);
149     gmx_fio_unlock(fio);
150     return ret;
151 }
152
153 gmx_bool gmx_fio_doe_rvec(t_fileio *fio, rvec *item,
154                           const char *desc, const char *srcfile, int line)
155 {
156     gmx_bool ret;
157     gmx_fio_lock(fio);
158     ret = do_xdr(fio, item, 1, eioRVEC, desc, srcfile, line);
159     gmx_fio_unlock(fio);
160     return ret;
161 }
162
163 gmx_bool gmx_fio_doe_ivec(t_fileio *fio, ivec *item,
164                           const char *desc, const char *srcfile, int line)
165 {
166     gmx_bool ret;
167     gmx_fio_lock(fio);
168     ret = do_xdr(fio, item, 1, eioIVEC, desc, srcfile, line);
169     gmx_fio_unlock(fio);
170     return ret;
171 }
172
173 gmx_bool gmx_fio_doe_string(t_fileio *fio, char *item,
174                             const char *desc, const char *srcfile, int line)
175 {
176     gmx_bool ret;
177     gmx_fio_lock(fio);
178     ret = do_xdr(fio, item, 1, eioSTRING, desc, srcfile, line);
179     gmx_fio_unlock(fio);
180     return ret;
181 }
182
183
184 /* Array reading & writing */
185
186 gmx_bool gmx_fio_ndoe_real(t_fileio *fio, real *item, int n,
187                            const char *desc, const char *srcfile, int line)
188 {
189     gmx_bool ret = TRUE;
190     int      i;
191     gmx_fio_lock(fio);
192     for (i = 0; i < n; i++)
193     {
194         ret = ret && do_xdr(fio, &(item[i]), 1, eioREAL, desc,
195                             srcfile, line);
196     }
197     gmx_fio_unlock(fio);
198     return ret;
199 }
200
201
202
203 gmx_bool gmx_fio_ndoe_float(t_fileio *fio, float *item, int n,
204                             const char *desc, const char *srcfile, int line)
205 {
206     gmx_bool ret = TRUE;
207     int      i;
208     gmx_fio_lock(fio);
209     for (i = 0; i < n; i++)
210     {
211         ret = ret && do_xdr(fio, &(item[i]), 1, eioFLOAT, desc,
212                             srcfile, line);
213     }
214     gmx_fio_unlock(fio);
215     return ret;
216 }
217
218
219
220 gmx_bool gmx_fio_ndoe_double(t_fileio *fio, double *item, int n,
221                              const char *desc, const char *srcfile, int line)
222 {
223     gmx_bool ret = TRUE;
224     int      i;
225     gmx_fio_lock(fio);
226     for (i = 0; i < n; i++)
227     {
228         ret = ret && do_xdr(fio, &(item[i]), 1, eioDOUBLE, desc,
229                             srcfile, line);
230     }
231     gmx_fio_unlock(fio);
232     return ret;
233 }
234
235
236
237 gmx_bool gmx_fio_ndoe_gmx_bool(t_fileio *fio, gmx_bool *item, int n,
238                                const char *desc, const char *srcfile, int line)
239 {
240     gmx_bool ret = TRUE;
241     int      i, itmp;
242
243     gmx_fio_lock(fio);
244     for (i = 0; i < n; i++)
245     {
246         if (fio->bRead)
247         {
248             ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc,
249                                 srcfile, line);
250             item[i] = itmp;
251         }
252         else
253         {
254             itmp = item[i];
255             ret  = ret && do_xdr(fio, &itmp, 1, eioINT, desc,
256                                  srcfile, line);
257         }
258     }
259     gmx_fio_unlock(fio);
260     return ret;
261 }
262
263 gmx_bool gmx_fio_ndoe_int(t_fileio *fio, int *item, int n,
264                           const char *desc, const char *srcfile, int line)
265 {
266     gmx_bool ret = TRUE;
267     int      i;
268     gmx_fio_lock(fio);
269     for (i = 0; i < n; i++)
270     {
271         ret = ret && do_xdr(fio, &(item[i]), 1, eioINT, desc,
272                             srcfile, line);
273     }
274     gmx_fio_unlock(fio);
275     return ret;
276 }
277
278
279
280 gmx_bool gmx_fio_ndoe_int64(t_fileio *fio, gmx_int64_t *item, int n,
281                             const char *desc, const char *srcfile, int line)
282 {
283     gmx_bool ret = TRUE;
284     int      i;
285     gmx_fio_lock(fio);
286     for (i = 0; i < n; i++)
287     {
288         ret = ret && do_xdr(fio, &(item[i]), 1, eioINT64, desc,
289                             srcfile, line);
290     }
291     gmx_fio_unlock(fio);
292     return ret;
293 }
294
295
296
297 gmx_bool gmx_fio_ndoe_uchar(t_fileio *fio, unsigned char *item, int n,
298                             const char *desc, const char *srcfile, int line)
299 {
300     gmx_bool ret = TRUE;
301     gmx_fio_lock(fio);
302     ret = ret && do_xdr(fio, item, n, eioNUCHAR, desc,
303                         srcfile, line);
304     gmx_fio_unlock(fio);
305     return ret;
306 }
307
308
309
310 gmx_bool gmx_fio_ndoe_ushort(t_fileio *fio, unsigned short *item, int n,
311                              const char *desc, const char *srcfile, int line)
312 {
313     gmx_bool ret = TRUE;
314     int      i;
315     gmx_fio_lock(fio);
316     for (i = 0; i < n; i++)
317     {
318         ret = ret && do_xdr(fio, &(item[i]), 1, eioUSHORT, desc,
319                             srcfile, line);
320     }
321     gmx_fio_unlock(fio);
322     return ret;
323 }
324
325
326
327 gmx_bool gmx_fio_ndoe_rvec(t_fileio *fio, rvec *item, int n,
328                            const char *desc, const char *srcfile, int line)
329 {
330     gmx_bool ret = TRUE;
331     gmx_fio_lock(fio);
332     ret = ret && do_xdr(fio, item, n, eioNRVEC, desc, srcfile, line);
333     gmx_fio_unlock(fio);
334     return ret;
335 }
336
337
338
339 gmx_bool gmx_fio_ndoe_ivec(t_fileio *fio, ivec *item, int n,
340                            const char *desc, const char *srcfile, int line)
341 {
342     gmx_bool ret = TRUE;
343     int      i;
344     gmx_fio_lock(fio);
345     for (i = 0; i < n; i++)
346     {
347         ret = ret && do_xdr(fio, &(item[i]), 1, eioIVEC, desc,
348                             srcfile, line);
349     }
350     gmx_fio_unlock(fio);
351     return ret;
352 }
353
354
355
356 gmx_bool gmx_fio_ndoe_string(t_fileio *fio, char *item[], int n,
357                              const char *desc, const char *srcfile, int line)
358 {
359     gmx_bool ret = TRUE;
360     int      i;
361     gmx_fio_lock(fio);
362     for (i = 0; i < n; i++)
363     {
364         ret = ret && do_xdr(fio, &(item[i]), 1, eioSTRING, desc,
365                             srcfile, line);
366     }
367     gmx_fio_unlock(fio);
368     return ret;
369 }