98f099727835afa85699ce895bb98be3cdb115bb
[alexxy/gromacs.git] / include / gmxfio.h
1 /*
2  * $Id$
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 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Good ROcking Metal Altar for Chronical Sinners
28  */
29
30 #ifndef _gmxfio_h
31 #define _gmxfio_h
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 static char *SRCID_gmxfio_h = "$Id$";
38
39 #include "typedefs.h"
40 #include "xdrf.h"
41
42 /* Enumerated for different items in files */
43 enum { eitemHEADER, eitemIR, eitemBOX, 
44        eitemTOP, eitemX, eitemV, eitemF, eitemNR };
45        
46 /* Enumerated for data types in files */
47 enum { eioREAL, eioINT,   eioNUCHAR, eioUSHORT, 
48        eioRVEC, eioNRVEC, eioIVEC,  eioSTRING, eioNR };
49
50 /* Functions for reading and writing data */
51 typedef bool do_func(void *item,int nitem,int eio,
52                      char *desc,char *srcfile,int line);
53                      
54 /* Global variables defined in gmxfio.h */
55 extern do_func *do_read;
56 extern do_func *do_write;
57 extern char *itemstr[eitemNR];
58 extern char *comment_str[eitemNR];
59
60 /********************************************************
61  * Open and Close 
62  ********************************************************/
63
64 extern int fio_open(char *fn,char *mode);
65 /* Open a new file for reading or writing.
66  * The file type will be deduced from the file name.
67  * If fn is NULL, stdin / stdout will be used for Ascii I/O (TPA type)
68  * mode may be "r", "w", "a"
69  */
70  
71 extern void fio_close(int fp);
72 /* Close the file corresponding to fp (if not stdio)
73  * The routine will exit when an invalid fio is handled.
74  */
75
76 extern void fio_select(int fp);
77 /* This routine sets the global variables do_read and do_write
78  * to point to the correct routines for fp.
79  */
80
81 /********************************************************
82  * Change properties of the open file
83  ********************************************************/
84
85 extern void fio_setprecision(int fio,bool bDouble);
86 /* Select the floating point precision for reading and writing files */
87
88 extern char *fio_getname(int fio);
89 /* Return the filename corresponding to the fio index */
90
91 extern int fio_getftp(int fio);
92 /* Return the filetype corresponding to the fio index */
93
94 extern void fio_setftp_fio(int fio,int ftp);
95 /* And set it */
96
97 extern void fio_setdebug(int fio,bool bDebug);
98 /* Set the debug mode */
99
100 extern bool fio_getdebug(int fio);
101 /* Return  whether debug mode is on in fio  */
102
103 extern bool fio_getread(int fio);
104 /* Return  whether read mode is on in fio  */
105
106 /***************************************************
107  * FILE Operations
108  ***************************************************/
109
110 extern void fio_rewind(int fio);
111 /* Rewind the tpa file in fio */
112
113 extern void fio_flush(int fio);
114 /* Flush the fio */
115
116 extern long fio_ftell(int fio);
117 /* Return file position if possible */
118
119 extern void fio_seek(int fio,long fpos);
120 /* Set file position if possible, quit otherwise */
121
122 extern FILE *fio_getfp(int fio);
123 /* Return the file pointer itself */
124
125 extern XDR *fio_getxdr(int fio);
126 /* Return the file pointer itself */
127
128 extern void set_comment(char *comment);
129 /* Add this to the comment string for debugging */
130
131 extern void unset_comment(void);
132 /* Remove previously set comment */
133
134
135 /********************************************************
136  * Dirty C macros... Try this in FORTRAN 
137  * (Oh, and you can do structured programming in C too) 
138  *********************************************************/
139 #define do_real(item)         (bRead ?\
140   do_read ((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__) : \
141   do_write((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__))
142   
143 #define do_int(item)          (bRead ?\
144   do_read ((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__) :\
145   do_write((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__))
146   
147 #define do_nuchar(item,n)     (bRead ?\
148   do_read ((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__) :\
149   do_write((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__))
150   
151 #define do_ushort(item)          (bRead ?\
152   do_read ((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__) :\
153   do_write((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__))
154   
155 #define do_rvec(item)         (bRead ?\
156   do_read ((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__) :\
157   do_write((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__))
158   
159 #define do_ivec(item)         (bRead ?\
160   do_read ((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__) :\
161   do_write((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__))
162   
163 #define do_string(item)       (bRead ?\
164   do_read ((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__) :\
165   do_write((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__))
166   
167 #define ndo_real(item,n,bOK) {\
168   bOK=TRUE;\
169   for(i=0; (i<n); i++) {\
170     char buf[128];\
171     sprintf(buf,"%s[%d]",#item,i);\
172     bOK = bOK && (bRead ?\
173       do_read ((void *)&((item)[i]),1,eioREAL,buf,__FILE__,__LINE__):\
174       do_write((void *)&(item[i]),1,eioREAL,buf,__FILE__,__LINE__));\
175   }\
176 }
177      
178 #define ndo_int(item,n,bOK)  {\
179   bOK=TRUE;\
180   for(i=0; (i<n); i++) {\
181     char buf[128];\
182     sprintf(buf,"%s[%d]",#item,i);\
183     bOK = bOK && (bRead ?\
184       do_read ((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__):\
185       do_write((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__));\
186   }\
187 }
188   
189 #define ndo_rvec(item,n)      (bRead ?\
190   do_read ((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__) :\
191   do_write((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__))
192   
193 #define ndo_ivec(item,n,bOK) {\
194   bOK=TRUE;\
195   for(i=0; (i<n); i++) {\
196     char buf[128];\
197     sprintf(buf,"%s[%d]",#item,i);\
198     bOK = bOK && (bRead ?\
199       do_read ((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__):\
200       do_write((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__));\
201   }\
202 }
203   
204 #define ndo_string(item,n,bOK) {\
205   bOK=TRUE;\
206   for(i=0; (i<n); i++) {\
207     char buf[128];\
208     sprintf(buf,"%s[%d]",#item,i);\
209     bOK = bOK && (bRead ?\
210       do_read ((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__):\
211       do_write((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__));\
212   }\
213 }
214
215 #endif