Merge branch 'master' of git://repo.or.cz/gromacs
[gromacs/adressmacs.git] / include / gmxfio.h
blobb692f27fd6c3cac0f41f707a08ca4489a1cd1e12
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Gromacs Runs On Most of All Computer Systems
36 #ifndef _gmxfio_h
37 #define _gmxfio_h
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
43 #include <stdio.h>
44 #include "sysstuff.h"
45 #include "typedefs.h"
46 #include "xdrf.h"
48 /* Highest number of open input/output files. This is usually limited to 1024 by the OS, anyway. */
49 #define GMX_MAXFILES 1024
51 /* Enumerated for different items in files */
52 enum { eitemHEADER, eitemIR, eitemBOX,
53 eitemTOP, eitemX, eitemV, eitemF, eitemNR };
55 /* Enumerated for data types in files */
56 enum { eioREAL, eioDOUBLE, eioINT, eioGMX_STEP_T,
57 eioUCHAR, eioNUCHAR, eioUSHORT,
58 eioRVEC, eioNRVEC, eioIVEC, eioSTRING, eioNR };
60 /* Functions for reading and writing data */
61 typedef bool do_func(void *item,int nitem,int eio,
62 const char *desc,const char *srcfile,int line);
64 /* Global variables defined in gmxfio.h */
65 extern do_func *do_read;
66 extern do_func *do_write;
67 extern const char *itemstr[eitemNR];
68 extern const char *comment_str[eitemNR];
70 /********************************************************
71 * Open and Close
72 ********************************************************/
74 int
75 gmx_fio_open(const char *fn,const char *mode);
76 /* Open a new file for reading or writing.
77 * The file type will be deduced from the file name.
78 * If fn is NULL, stdin / stdout will be used for Ascii I/O (TPA type)
79 * mode may be "r", "w", or "a". You should append a "b" to the mode
80 * if you are writing a binary file, but the routine will also
81 * doublecheck it and try to do it if you forgot. This has no effect on
82 * unix, but is important on windows.
85 int
86 gmx_fio_close(int fp);
87 /* Close the file corresponding to fp (if not stdio)
88 * The routine will exit when an invalid fio is handled.
89 * Returns 0 on success.
92 void
93 gmx_fio_select(int fp);
94 /* This routine sets the global variables do_read and do_write
95 * to point to the correct routines for fp.
98 /********************************************************
99 * Change properties of the open file
100 ********************************************************/
102 extern void gmx_fio_setprecision(int fio,bool bDouble);
103 /* Select the floating point precision for reading and writing files */
105 extern char *gmx_fio_getname(int fio);
106 /* Return the filename corresponding to the fio index */
108 extern int gmx_fio_getftp(int fio);
109 /* Return the filetype corresponding to the fio index */
111 extern void gmx_fio_setftp_fio(int fio,int ftp);
112 /* And set it */
114 extern void gmx_fio_setdebug(int fio,bool bDebug);
115 /* Set the debug mode */
117 extern bool gmx_fio_getdebug(int fio);
118 /* Return whether debug mode is on in fio */
120 extern bool gmx_fio_getread(int fio);
121 /* Return whether read mode is on in fio */
123 /***************************************************
124 * FILE Operations
125 ***************************************************/
127 extern void gmx_fio_rewind(int fio);
128 /* Rewind the tpa file in fio */
131 gmx_fio_flush(int fio);
132 /* Flush the fio, returns 0 on success */
134 extern off_t gmx_fio_ftell(int fio);
135 /* Return file position if possible */
137 extern void gmx_fio_seek(int fio,off_t fpos);
138 /* Set file position if possible, quit otherwise */
140 extern FILE *gmx_fio_getfp(int fio);
141 /* Return the file pointer itself */
143 extern XDR *gmx_fio_getxdr(int fio);
144 /* Return the file pointer itself */
146 /* Open a file, return a stream, record the entry in internal FIO object */
147 FILE *
148 gmx_fio_fopen(const char *fn,const char *mode);
150 /* Close a file previously opened with gmx_fio_fopen.
151 * Do not mix these calls with standard fopen/fclose ones!
152 * Returns 0 on success.
155 gmx_fio_fclose(FILE *fp);
157 /* Element with information about position in a currently open file.
158 * off_t should be defined by autoconf if your system does not have it.
159 * If you do not have it on some other platform you do not have largefile support
160 * at all, and you can define it to int (or better, find out how to enable large files).
162 typedef struct
164 char filename[STRLEN];
165 off_t offset;
167 gmx_file_position_t;
172 * Return the name and file pointer positions for all currently open
173 * output files. This is used for saving in the checkpoint files, so we
174 * can truncate output files upon restart-with-appending.
176 * For the first argument you should use a pointer, which will be set to
177 * point to a list of open files.
180 gmx_fio_get_output_file_positions (gmx_file_position_t ** outputfiles,
181 int * nfiles );
184 extern int
185 xtc_seek_frame(int frame, int fio, int natoms);
187 extern int
188 xtc_seek_time(real time, int fio, int natoms);
191 extern void set_comment(const char *comment);
192 /* Add this to the comment string for debugging */
194 extern void unset_comment(void);
195 /* Remove previously set comment */
198 /********************************************************
199 * Dirty C macros... Try this in FORTRAN
200 * (Oh, and you can do structured programming in C too)
201 *********************************************************/
202 #define do_real(item) (bRead ?\
203 do_read ((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__) : \
204 do_write((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__))
206 #define do_double(item) (bRead ? \
207 do_read ((void *)&(item),1,eioDOUBLE,(#item),__FILE__,__LINE__) : \
208 do_write((void *)&(item),1,eioDOUBLE,(#item),__FILE__,__LINE__))
210 #define do_int(item) (bRead ?\
211 do_read ((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__) :\
212 do_write((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__))
214 #define do_gmx_step_t(item) (bRead ? \
215 do_read ((void *)&(item),1,eioGMX_STEP_T,(#item),__FILE__,__LINE__) :\
216 do_write((void *)&(item),1,eioGMX_STEP_T,(#item),__FILE__,__LINE__))
218 #define do_uchar(item) (bRead ?\
219 do_read ((void *)&(item),1,eioUCHAR,(#item),__FILE__,__LINE__) :\
220 do_write((void *)&(item),1,eioUCHAR,(#item),__FILE__,__LINE__))
222 #define do_nuchar(item,n) (bRead ?\
223 do_read ((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__) :\
224 do_write((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__))
226 #define do_ushort(item) (bRead ?\
227 do_read ((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__) :\
228 do_write((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__))
230 #define do_rvec(item) (bRead ?\
231 do_read ((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__) :\
232 do_write((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__))
234 #define do_ivec(item) (bRead ?\
235 do_read ((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__) :\
236 do_write((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__))
238 #define do_string(item) (bRead ?\
239 do_read ((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__) :\
240 do_write((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__))
242 #define ndo_real(item,n,bOK) {\
243 bOK=TRUE;\
244 for(i=0; (i<n); i++) {\
245 char buf[128];\
246 sprintf(buf,"%s[%d]",#item,i);\
247 bOK = bOK && (bRead ?\
248 do_read ((void *)&((item)[i]),1,eioREAL,buf,__FILE__,__LINE__):\
249 do_write((void *)&(item[i]),1,eioREAL,buf,__FILE__,__LINE__));\
253 #define ndo_double(item,n,bOK) {\
254 bOK=TRUE;\
255 for(i=0; (i<n); i++) {\
256 char buf[128];\
257 sprintf(buf,"%s[%d]",#item,i);\
258 bOK = bOK && (bRead ?\
259 do_read ((void *)&((item)[i]),1,eioDOUBLE,buf,__FILE__,__LINE__):\
260 do_write((void *)&(item[i]),1,eioDOUBLE,buf,__FILE__,__LINE__));\
264 #define ndo_int(item,n,bOK) {\
265 bOK=TRUE;\
266 for(i=0; (i<n); i++) {\
267 char buf[128];\
268 sprintf(buf,"%s[%d]",#item,i);\
269 bOK = bOK && (bRead ?\
270 do_read ((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__):\
271 do_write((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__));\
275 #define ndo_nuchar(item,n,bOK) {\
276 bOK=TRUE;\
277 for(i=0; (i<n); i++) {\
278 char buf[128];\
279 sprintf(buf,"%s[%d]",#item,i);\
280 bOK = bOK && (bRead ?\
281 do_read ((void *)&(item[i]),1,eioNUCHAR,buf,__FILE__,__LINE__):\
282 do_write((void *)&(item[i]),1,eioNUCHAR,buf,__FILE__,__LINE__));\
286 #define ndo_rvec(item,n) (bRead ?\
287 do_read ((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__) :\
288 do_write((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__))
290 #define ndo_ivec(item,n,bOK) {\
291 bOK=TRUE;\
292 for(i=0; (i<n); i++) {\
293 char buf[128];\
294 sprintf(buf,"%s[%d]",#item,i);\
295 bOK = bOK && (bRead ?\
296 do_read ((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__):\
297 do_write((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__));\
301 #define ndo_string(item,n,bOK) {\
302 bOK=TRUE;\
303 for(i=0; (i<n); i++) {\
304 char buf[128];\
305 sprintf(buf,"%s[%d]",#item,i);\
306 bOK = bOK && (bRead ?\
307 do_read ((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__):\
308 do_write((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__));\
312 #endif