Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / fileio / gmxfio_bin.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, 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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <errno.h>
44 #ifdef HAVE_IO_H
45 #include <io.h>
46 #endif
47
48 #include "gmx_fatal.h"
49 #include "macros.h"
50 #include "smalloc.h"
51 #include "futil.h"
52 #include "filenm.h"
53 #include "string2.h"
54 #include "gmxfio.h"
55 #include "md5.h"
56
57 #ifdef GMX_THREAD_MPI
58 #include "thread_mpi.h"
59 #endif
60
61 #include "gmxfio_int.h"
62
63 /* This is the part that reads dummy and ascii files.  */
64
65
66 static gmx_bool do_binread(t_fileio *fio, void *item, int nitem, int eio,
67                            const char *desc, const char *srcfile, int line);
68 static gmx_bool do_binwrite(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 bin_iotype = {do_binread, do_binwrite};
73
74
75 static gmx_bool do_binwrite(t_fileio *fio, const void *item, int nitem, int eio,
76                             const char *desc, const char *srcfile, int line)
77 {
78     size_t size = 0, wsize;
79     int    ssize;
80
81     gmx_fio_check_nitem(eio, nitem, srcfile, line);
82     switch (eio)
83     {
84         case eioREAL:
85             size = sizeof(real);
86             break;
87         case eioFLOAT:
88             size = sizeof(float);
89             break;
90         case eioDOUBLE:
91             size = sizeof(double);
92             break;
93         case eioINT:
94             size = sizeof(int);
95             break;
96         case eioGMX_LARGE_INT:
97             size = sizeof(gmx_large_int_t);
98             break;
99         case eioUCHAR:
100             size = sizeof(unsigned char);
101             break;
102         case eioNUCHAR:
103             size = sizeof(unsigned char);
104             break;
105         case eioUSHORT:
106             size = sizeof(unsigned short);
107             break;
108         case eioRVEC:
109             size = sizeof(rvec);
110             break;
111         case eioNRVEC:
112             size = sizeof(rvec);
113             break;
114         case eioIVEC:
115             size = sizeof(ivec);
116             break;
117         case eioSTRING:
118             size = ssize = strlen((char *) item) + 1;
119             do_binwrite(fio, &ssize, 1, eioINT, desc, srcfile, line);
120             break;
121         default:
122             gmx_fio_fe(fio, eio, desc, srcfile, line);
123     }
124
125     wsize = fwrite(item, size, nitem, fio->fp);
126
127     if ((wsize != nitem) && fio->bDebug)
128     {
129         fprintf(stderr,
130                 "Error writing %s %s to file %s (source %s, line %d)\n",
131                 eioNames[eio], desc, fio->fn, srcfile, line);
132         fprintf(stderr, "written size %u bytes, source size %u bytes\n",
133                 (unsigned int) wsize, (unsigned int) size);
134     }
135     return (wsize == nitem);
136 }
137
138 static gmx_bool do_binread(t_fileio *fio, void *item, int nitem, int eio,
139                            const char *desc, const char *srcfile, int line)
140 {
141     size_t size = 0, rsize;
142     int    ssize;
143
144     gmx_fio_check_nitem(eio, nitem, srcfile, line);
145     switch (eio)
146     {
147         case eioREAL:
148             if (fio->bDouble)
149             {
150                 size = sizeof(double);
151             }
152             else
153             {
154                 size = sizeof(float);
155             }
156             break;
157         case eioFLOAT:
158             size = sizeof(float);
159             break;
160         case eioDOUBLE:
161             size = sizeof(double);
162             break;
163         case eioINT:
164             size = sizeof(int);
165             break;
166         case eioGMX_LARGE_INT:
167             size = sizeof(gmx_large_int_t);
168             break;
169         case eioUCHAR:
170             size = sizeof(unsigned char);
171             break;
172         case eioNUCHAR:
173             size = sizeof(unsigned char);
174             break;
175         case eioUSHORT:
176             size = sizeof(unsigned short);
177             break;
178         case eioRVEC:
179         case eioNRVEC:
180             if (fio->bDouble)
181             {
182                 size = sizeof(double) * DIM;
183             }
184             else
185             {
186                 size = sizeof(float) * DIM;
187             }
188             break;
189         case eioIVEC:
190             size = sizeof(ivec);
191             break;
192         case eioSTRING:
193             do_binread(fio, &ssize, 1, eioINT, desc, srcfile, line);
194             size = ssize;
195             break;
196         default:
197             gmx_fio_fe(fio, eio, desc, srcfile, line);
198     }
199     if (item)
200     {
201         rsize = fread(item, size, nitem, fio->fp);
202     }
203     else
204     {
205         /* Skip over it if we have a NULL pointer here */
206         gmx_fseek(fio->fp, (gmx_off_t)(size*nitem), SEEK_CUR);
207         rsize = nitem;
208     }
209     if ((rsize != nitem) && (fio->bDebug))
210     {
211         fprintf(stderr,
212                 "Error reading %s %s from file %s (source %s, line %d)\n",
213                 eioNames[eio], desc, fio->fn, srcfile, line);
214     }
215
216     return (rsize == nitem);
217 }