renamed GMX_THREADS to GMX_THREAD_MPI
[alexxy/gromacs.git] / src / 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(fio, 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(fio, eio, nitem, srcfile, line);
144     switch (eio)
145     {
146     case eioREAL:
147         if (fio->bDouble)
148             size = sizeof(double);
149         else
150             size = sizeof(float);
151         break;
152     case eioFLOAT:
153         size = sizeof(float);
154         break;
155     case eioDOUBLE:
156         size = sizeof(double);
157         break;
158     case eioINT:
159         size = sizeof(int);
160         break;
161     case eioGMX_LARGE_INT:
162         size = sizeof(gmx_large_int_t);
163         break;
164     case eioUCHAR:
165         size = sizeof(unsigned char);
166         break;
167     case eioNUCHAR:
168         size = sizeof(unsigned char);
169         break;
170     case eioUSHORT:
171         size = sizeof(unsigned short);
172         break;
173     case eioRVEC:
174     case eioNRVEC:
175         if (fio->bDouble)
176             size = sizeof(double) * DIM;
177         else
178             size = sizeof(float) * DIM;
179         break;
180     case eioIVEC:
181         size = sizeof(ivec);
182         break;
183     case eioSTRING:
184         do_binread(fio, &ssize, 1, eioINT, desc, srcfile, line);
185         size = ssize;
186         break;
187     default:
188         gmx_fio_fe(fio, eio, desc, srcfile, line);
189     }
190     if (item)
191         rsize = fread(item, size, nitem, fio->fp);
192     else
193     {
194         /* Skip over it if we have a NULL pointer here */
195         gmx_fseek(fio->fp, (gmx_off_t)(size*nitem), SEEK_CUR);
196         rsize = nitem;
197     }
198     if ((rsize != nitem) && (fio->bDebug))
199         fprintf(stderr,
200                 "Error reading %s %s from file %s (source %s, line %d)\n",
201                 eioNames[eio], desc, fio->fn, srcfile, line);
202
203     return (rsize == nitem);
204 }
205
206