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