Fixed segfaults with empty selections.
[gromacs/qmmm-gamess-us.git] / include / gmxfio.h
blob17f4a0418e775f224b4114eb90a811dee1e4ab73
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 /* Note that some functions list beow are NOT THREADSAFE
49 * when multiple threads use the same file pointer.
52 /* Highest number of open input/output files. This is usually limited to 1024 by the OS, anyway. */
53 #define GMX_MAXFILES 1024
55 /* Enumerated for different items in files */
56 enum { eitemHEADER, eitemIR, eitemBOX,
57 eitemTOP, eitemX, eitemV, eitemF, eitemNR };
59 /* Enumerated for data types in files */
60 enum { eioREAL, eioDOUBLE, eioINT, eioGMX_LARGE_INT,
61 eioUCHAR, eioNUCHAR, eioUSHORT,
62 eioRVEC, eioNRVEC, eioIVEC, eioSTRING, eioNR };
64 /* Functions for reading and writing data */
65 typedef bool do_func(void *item,int nitem,int eio,
66 const char *desc,const char *srcfile,int line);
68 /* Global variables defined in gmxfio.h */
69 extern do_func *do_read;
70 extern do_func *do_write;
71 extern const char *itemstr[eitemNR];
72 extern const char *comment_str[eitemNR];
74 /********************************************************
75 * Open and Close
76 ********************************************************/
78 int gmx_fio_open(const char *fn,const char *mode);
79 /* Open a new file for reading or writing.
80 * The file type will be deduced from the file name.
81 * If fn is NULL, stdin / stdout will be used for Ascii I/O (TPA type)
82 * mode may be "r", "w", or "a". You should append a "b" to the mode
83 * if you are writing a binary file, but the routine will also
84 * doublecheck it and try to do it if you forgot. This has no effect on
85 * unix, but is important on windows.
88 int gmx_fio_close(int fp);
89 /* Close the file corresponding to fp (if not stdio)
90 * The routine will exit when an invalid fio is handled.
91 * Returns 0 on success.
94 void gmx_fio_select(int fp);
95 /* This routine sets the global variables do_read and do_write
96 * to point to the correct routines for fp.
99 /********************************************************
100 * Change properties of the open file
101 ********************************************************/
103 extern void gmx_fio_setprecision(int fio,bool bDouble);
104 /* Select the floating point precision for reading and writing files */
106 extern char *gmx_fio_getname(int fio);
107 /* Return the filename corresponding to the fio index */
109 extern int gmx_fio_getftp(int fio);
110 /* Return the filetype corresponding to the fio index */
112 extern void gmx_fio_setftp_fio(int fio,int ftp);
113 /* And set it */
115 extern void gmx_fio_setdebug(int fio,bool bDebug);
116 /* Set the debug mode */
118 extern bool gmx_fio_getdebug(int fio);
119 /* Return whether debug mode is on in fio */
121 extern bool gmx_fio_getread(int fio);
122 /* Return whether read mode is on in fio */
124 /***************************************************
125 * FILE Operations
126 ***************************************************/
128 extern void gmx_fio_rewind(int fio);
129 /* Rewind the tpa file in fio */
131 int 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 int 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 * gmx_fio_fopen(const char *fn,const char *mode);
149 /* Close a file previously opened with gmx_fio_fopen.
150 * Do not mix these calls with standard fopen/fclose ones!
151 * Returns 0 on success.
153 int gmx_fio_fclose(FILE *fp);
155 /* Element with information about position in a currently open file.
156 * off_t should be defined by autoconf if your system does not have it.
157 * If you do not have it on some other platform you do not have largefile support
158 * at all, and you can define it to int (or better, find out how to enable large files).
160 typedef struct
162 char filename[STRLEN];
163 off_t offset;
164 unsigned char chksum[16];
165 int chksum_size;
167 gmx_file_position_t;
171 * Check if the file position is out of the range of off_t.
172 * The result is stored along with the other file data of fio.
175 gmx_fio_check_file_position(int fio);
178 * Return the name and file pointer positions for all currently open
179 * output files. This is used for saving in the checkpoint files, so we
180 * can truncate output files upon restart-with-appending.
182 * For the first argument you should use a pointer, which will be set to
183 * point to a list of open files.
186 gmx_fio_get_output_file_positions (gmx_file_position_t ** outputfiles,
187 int *nfiles );
189 int gmx_fio_get_file_md5(int fio, off_t offset, unsigned char digest[]);
192 extern int xtc_seek_frame(int frame, int fio, int natoms);
194 extern int xtc_seek_time(real time, int fio, int natoms);
197 extern void set_comment(const char *comment);
198 /* Add this to the comment string for debugging */
200 extern void unset_comment(void);
201 /* Remove previously set comment */
204 /********************************************************
205 * Dirty C macros... Try this in FORTRAN
206 * (Oh, and you can do structured programming in C too)
207 *********************************************************/
208 #define do_real(item) (bRead ?\
209 do_read ((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__) : \
210 do_write((void *)&(item),1,eioREAL,(#item),__FILE__,__LINE__))
212 #define do_double(item) (bRead ? \
213 do_read ((void *)&(item),1,eioDOUBLE,(#item),__FILE__,__LINE__) : \
214 do_write((void *)&(item),1,eioDOUBLE,(#item),__FILE__,__LINE__))
216 #define do_int(item) (bRead ?\
217 do_read ((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__) :\
218 do_write((void *)&(item),1,eioINT,(#item),__FILE__,__LINE__))
220 #define do_gmx_large_int(item) (bRead ? \
221 do_read ((void *)&(item),1,eioGMX_LARGE_INT,(#item),__FILE__,__LINE__) :\
222 do_write((void *)&(item),1,eioGMX_LARGE_INT,(#item),__FILE__,__LINE__))
224 #define do_uchar(item) (bRead ?\
225 do_read ((void *)&(item),1,eioUCHAR,(#item),__FILE__,__LINE__) :\
226 do_write((void *)&(item),1,eioUCHAR,(#item),__FILE__,__LINE__))
228 #define do_nuchar(item,n) (bRead ?\
229 do_read ((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__) :\
230 do_write((void *)(item),n,eioNUCHAR,(#item),__FILE__,__LINE__))
232 #define do_ushort(item) (bRead ?\
233 do_read ((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__) :\
234 do_write((void *)&(item),1,eioUSHORT,(#item),__FILE__,__LINE__))
236 #define do_rvec(item) (bRead ?\
237 do_read ((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__) :\
238 do_write((void *)(item),1,eioRVEC,(#item),__FILE__,__LINE__))
240 #define do_ivec(item) (bRead ?\
241 do_read ((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__) :\
242 do_write((void *)(item),1,eioIVEC,(#item),__FILE__,__LINE__))
244 #define do_string(item) (bRead ?\
245 do_read ((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__) :\
246 do_write((void *)(item),1,eioSTRING,(#item),__FILE__,__LINE__))
248 #define ndo_real(item,n,bOK) {\
249 bOK=TRUE;\
250 for(i=0; (i<n); i++) {\
251 char buf[128];\
252 sprintf(buf,"%s[%d]",#item,i);\
253 bOK = bOK && (bRead ?\
254 do_read ((void *)&((item)[i]),1,eioREAL,buf,__FILE__,__LINE__):\
255 do_write((void *)&(item[i]),1,eioREAL,buf,__FILE__,__LINE__));\
259 #define ndo_double(item,n,bOK) {\
260 bOK=TRUE;\
261 for(i=0; (i<n); i++) {\
262 char buf[128];\
263 sprintf(buf,"%s[%d]",#item,i);\
264 bOK = bOK && (bRead ?\
265 do_read ((void *)&((item)[i]),1,eioDOUBLE,buf,__FILE__,__LINE__):\
266 do_write((void *)&(item[i]),1,eioDOUBLE,buf,__FILE__,__LINE__));\
270 #define ndo_int(item,n,bOK) {\
271 bOK=TRUE;\
272 for(i=0; (i<n); i++) {\
273 char buf[128];\
274 sprintf(buf,"%s[%d]",#item,i);\
275 bOK = bOK && (bRead ?\
276 do_read ((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__):\
277 do_write((void *)&(item[i]),1,eioINT,buf,__FILE__,__LINE__));\
281 #define ndo_nuchar(item,n,bOK) {\
282 bOK=TRUE;\
283 for(i=0; (i<n); i++) {\
284 char buf[128];\
285 sprintf(buf,"%s[%d]",#item,i);\
286 bOK = bOK && (bRead ?\
287 do_read ((void *)&(item[i]),1,eioNUCHAR,buf,__FILE__,__LINE__):\
288 do_write((void *)&(item[i]),1,eioNUCHAR,buf,__FILE__,__LINE__));\
292 #define ndo_rvec(item,n) (bRead ?\
293 do_read ((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__) :\
294 do_write((void *)(item),n,eioNRVEC,(#item),__FILE__,__LINE__))
296 #define ndo_ivec(item,n,bOK) {\
297 bOK=TRUE;\
298 for(i=0; (i<n); i++) {\
299 char buf[128];\
300 sprintf(buf,"%s[%d]",#item,i);\
301 bOK = bOK && (bRead ?\
302 do_read ((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__):\
303 do_write((void *)(item)[i],1,eioIVEC,buf,__FILE__,__LINE__));\
307 #define ndo_string(item,n,bOK) {\
308 bOK=TRUE;\
309 for(i=0; (i<n); i++) {\
310 char buf[128];\
311 sprintf(buf,"%s[%d]",#item,i);\
312 bOK = bOK && (bRead ?\
313 do_read ((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__):\
314 do_write((void *)(item)[i],1,eioSTRING,buf,__FILE__,__LINE__));\
318 #endif