4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-2001
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
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.
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.
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.
31 * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
34 * GRoups of Organic Molecules in ACtion for Science
40 static char *SRCID_gmxfio_h = "$Id$";
45 static char *SRCID_gmxfio_h = "$Id$";
50 /* Enumerated for different items in files */
51 enum { eitemHEADER, eitemIR, eitemBOX,
52 eitemTOP, eitemX, eitemV, eitemF, eitemNR };
54 /* Enumerated for data types in files */
55 enum { eioREAL, eioINT, eioNUCHAR, eioUSHORT,
56 eioRVEC, eioNRVEC, eioIVEC, eioSTRING, eioNR };
58 /* Functions for reading and writing data */
59 typedef bool do_func(void *item,int nitem,int eio,
60 char *desc,char *srcfile,int line);
62 /* Global variables defined in gmxfio.h */
63 extern do_func *do_read;
64 extern do_func *do_write;
65 extern char *itemstr[eitemNR];
66 extern char *comment_str[eitemNR];
68 /********************************************************
70 ********************************************************/
72 extern int fio_open(char *fn,char *mode);
73 /* Open a new file for reading or writing.
74 * The file type will be deduced from the file name.
75 * If fn is NULL, stdin / stdout will be used for Ascii I/O (TPA type)
76 * mode may be "r", "w", "a"
79 extern void fio_close(int fp);
80 /* Close the file corresponding to fp (if not stdio)
81 * The routine will exit when an invalid fio is handled.
84 extern void fio_select(int fp);
85 /* This routine sets the global variables do_read and do_write
86 * to point to the correct routines for fp.
89 /********************************************************
90 * Change properties of the open file
91 ********************************************************/
93 extern void fio_setprecision(int fio,bool bDouble);
94 /* Select the floating point precision for reading and writing files */
96 extern char *fio_getname(int fio);
97 /* Return the filename corresponding to the fio index */
99 extern int fio_getftp(int fio);
100 /* Return the filetype corresponding to the fio index */
102 extern void fio_setftp_fio(int fio,int ftp);
105 extern void fio_setdebug(int fio,bool bDebug);
106 /* Set the debug mode */
108 extern bool fio_getdebug(int fio);
109 /* Return whether debug mode is on in fio */
111 extern bool fio_getread(int fio);
112 /* Return whether read mode is on in fio */
114 /***************************************************
116 ***************************************************/
118 extern void fio_rewind(int fio);
119 /* Rewind the tpa file in fio */
121 extern void fio_flush(int fio);
124 extern long fio_ftell(int fio);
125 /* Return file position if possible */
127 extern void fio_seek(int fio,long fpos);
128 /* Set file position if possible, quit otherwise */
130 extern FILE *fio_getfp(int fio);
131 /* Return the file pointer itself */
133 extern XDR *fio_getxdr(int fio);
134 /* Return the file pointer itself */
136 extern void set_comment(char *comment);
137 /* Add this to the comment string for debugging */
139 extern void unset_comment(void);
140 /* Remove previously set comment */
143 /********************************************************
144 * Dirty C macros... Try this in FORTRAN
145 * (Oh, and you can do structured programming in C too)
146 *********************************************************/
147 #define do_real(item) (bRead ?\
148 do_read ((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__) : \
149 do_write((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__))
151 #define do_int(item) (bRead ?\
152 do_read ((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__) :\
153 do_write((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__))
155 #define do_nuchar(item,n) (bRead ?\
156 do_read ((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__) :\
157 do_write((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__))
159 #define do_ushort(item) (bRead ?\
160 do_read ((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__) :\
161 do_write((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__))
163 #define do_rvec(item) (bRead ?\
164 do_read ((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__) :\
165 do_write((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__))
167 #define do_ivec(item) (bRead ?\
168 do_read ((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__) :\
169 do_write((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__))
171 #define do_string(item) (bRead ?\
172 do_read ((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__) :\
173 do_write((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__))
175 #define ndo_real(item,n,bOK) {\
177 for(i=0; (i<n); i++) {\
179 sprintf(buf,"%s[%d]",#item,i);\
180 bOK = bOK && (bRead ?\
181 do_read ((void *)&((item)[i]),1,eioREAL,buf,__FILE__,__LINE__):\
182 do_write((void *)&(item[i]),1,eioREAL,buf,__FILE__,__LINE__));\
186 #define ndo_int(item,n,bOK) {\
188 for(i=0; (i<n); i++) {\
190 sprintf(buf,"%s[%d]",#item,i);\
191 bOK = bOK && (bRead ?\
192 do_read ((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__):\
193 do_write((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__));\
197 #define ndo_rvec(item,n) (bRead ?\
198 do_read ((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__) :\
199 do_write((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__))
201 #define ndo_ivec(item,n,bOK) {\
203 for(i=0; (i<n); i++) {\
205 sprintf(buf,"%s[%d]",#item,i);\
206 bOK = bOK && (bRead ?\
207 do_read ((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__):\
208 do_write((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__));\
212 #define ndo_string(item,n,bOK) {\
214 for(i=0; (i<n); i++) {\
216 sprintf(buf,"%s[%d]",#item,i);\
217 bOK = bOK && (bRead ?\
218 do_read ((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__):\
219 do_write((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__));\