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