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