Simplify code structure for C++ analysis tools
[alexxy/gromacs.git] / src / gromacs / gmxlib / gmxfio_bin.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  *
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  *
31  * For more info, check our website at http://www.gromacs.org
32  *
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <ctype.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #ifdef HAVE_IO_H
44 #include <io.h>
45 #endif
46
47 #include "gmx_fatal.h"
48 #include "macros.h"
49 #include "smalloc.h"
50 #include "futil.h"
51 #include "filenm.h"
52 #include "string2.h"
53 #include "gmxfio.h"
54 #include "md5.h"
55
56 #ifdef GMX_THREAD_MPI
57 #include "thread_mpi.h"
58 #endif
59
60 #include "gmxfio_int.h"
61
62 /* This is the part that reads dummy and ascii files.  */
63
64
65 static gmx_bool do_binread(t_fileio *fio, void *item, int nitem, int eio,
66                            const char *desc, const char *srcfile, int line);
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
71 const t_iotype bin_iotype = {do_binread, do_binwrite};
72
73
74 static gmx_bool do_binwrite(t_fileio *fio, const void *item, int nitem, int eio,
75                             const char *desc, const char *srcfile, int line)
76 {
77     size_t size = 0, wsize;
78     int    ssize;
79
80     gmx_fio_check_nitem(eio, nitem, srcfile, line);
81     switch (eio)
82     {
83         case eioREAL:
84             size = sizeof(real);
85             break;
86         case eioFLOAT:
87             size = sizeof(float);
88             break;
89         case eioDOUBLE:
90             size = sizeof(double);
91             break;
92         case eioINT:
93             size = sizeof(int);
94             break;
95         case eioGMX_LARGE_INT:
96             size = sizeof(gmx_large_int_t);
97             break;
98         case eioUCHAR:
99             size = sizeof(unsigned char);
100             break;
101         case eioNUCHAR:
102             size = sizeof(unsigned char);
103             break;
104         case eioUSHORT:
105             size = sizeof(unsigned short);
106             break;
107         case eioRVEC:
108             size = sizeof(rvec);
109             break;
110         case eioNRVEC:
111             size = sizeof(rvec);
112             break;
113         case eioIVEC:
114             size = sizeof(ivec);
115             break;
116         case eioSTRING:
117             size = ssize = strlen((char *) item) + 1;
118             do_binwrite(fio, &ssize, 1, eioINT, desc, srcfile, line);
119             break;
120         default:
121             gmx_fio_fe(fio, eio, desc, srcfile, line);
122     }
123
124     wsize = fwrite(item, size, nitem, fio->fp);
125
126     if ((wsize != nitem) && fio->bDebug)
127     {
128         fprintf(stderr,
129                 "Error writing %s %s to file %s (source %s, line %d)\n",
130                 eioNames[eio], desc, fio->fn, srcfile, line);
131         fprintf(stderr, "written size %u bytes, source size %u bytes\n",
132                 (unsigned int) wsize, (unsigned int) size);
133     }
134     return (wsize == nitem);
135 }
136
137 static gmx_bool do_binread(t_fileio *fio, void *item, int nitem, int eio,
138                            const char *desc, const char *srcfile, int line)
139 {
140     size_t size = 0, rsize;
141     int    ssize;
142
143     gmx_fio_check_nitem(eio, nitem, srcfile, line);
144     switch (eio)
145     {
146         case eioREAL:
147             if (fio->bDouble)
148             {
149                 size = sizeof(double);
150             }
151             else
152             {
153                 size = sizeof(float);
154             }
155             break;
156         case eioFLOAT:
157             size = sizeof(float);
158             break;
159         case eioDOUBLE:
160             size = sizeof(double);
161             break;
162         case eioINT:
163             size = sizeof(int);
164             break;
165         case eioGMX_LARGE_INT:
166             size = sizeof(gmx_large_int_t);
167             break;
168         case eioUCHAR:
169             size = sizeof(unsigned char);
170             break;
171         case eioNUCHAR:
172             size = sizeof(unsigned char);
173             break;
174         case eioUSHORT:
175             size = sizeof(unsigned short);
176             break;
177         case eioRVEC:
178         case eioNRVEC:
179             if (fio->bDouble)
180             {
181                 size = sizeof(double) * DIM;
182             }
183             else
184             {
185                 size = sizeof(float) * DIM;
186             }
187             break;
188         case eioIVEC:
189             size = sizeof(ivec);
190             break;
191         case eioSTRING:
192             do_binread(fio, &ssize, 1, eioINT, desc, srcfile, line);
193             size = ssize;
194             break;
195         default:
196             gmx_fio_fe(fio, eio, desc, srcfile, line);
197     }
198     if (item)
199     {
200         rsize = fread(item, size, nitem, fio->fp);
201     }
202     else
203     {
204         /* Skip over it if we have a NULL pointer here */
205         gmx_fseek(fio->fp, (gmx_off_t)(size*nitem), SEEK_CUR);
206         rsize = nitem;
207     }
208     if ((rsize != nitem) && (fio->bDebug))
209     {
210         fprintf(stderr,
211                 "Error reading %s %s from file %s (source %s, line %d)\n",
212                 eioNames[eio], desc, fio->fn, srcfile, line);
213     }
214
215     return (rsize == nitem);
216 }