Merge "Merge release-5-1 into master"
[alexxy/gromacs.git] / src / gromacs / fileio / gmxfio.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef GMX_FILEIO_GMXFIO_H
39 #define GMX_FILEIO_GMXFIO_H
40
41 #include <stdio.h>
42
43 #include "gromacs/math/vectypes.h"
44 #include "gromacs/utility/cstringutil.h"
45 #include "gromacs/utility/futil.h"
46 #include "gromacs/utility/real.h"
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 typedef struct t_fileio t_fileio;
53
54 /* NOTE ABOUT THREAD SAFETY:
55
56    The functions are all thread-safe, provided that two threads don't
57    do something silly like closing the same file, or one thread
58    accesses a file that has been closed by another.
59  */
60
61 /********************************************************
62  * Open and Close
63  ********************************************************/
64
65 t_fileio *gmx_fio_open(const char *fn, const char *mode);
66 /* Open a new file for reading or writing.
67  * The file type will be deduced from the file name.
68  */
69
70 int gmx_fio_close(t_fileio *fp);
71 /* Close the file corresponding to fp (if not stdio)
72  * The routine will exit when an invalid fio is handled.
73  * Returns 0 on success.
74  */
75
76 int gmx_fio_fp_close(t_fileio *fp);
77 /* Close the file corresponding to fp without closing the FIO entry
78  * Needed e.g. for trxio because the FIO entries are used to store
79  * additional data.
80  * NOTE that the fp still needs to be properly closed with gmx_fio_close().
81  * The routine will exit when an invalid fio is handled.
82  * Returns 0 on success.
83  */
84
85
86 /* Open a file, return a stream, record the entry in internal FIO object */
87 FILE* gmx_fio_fopen(const char *fn, const char *mode);
88
89 /* Close a file previously opened with gmx_fio_fopen.
90  * Do not mix these calls with standard fopen/fclose ones!
91  * Returns 0 on success.  */
92 int gmx_fio_fclose(FILE *fp);
93
94
95
96 /********************************************************
97  * Change properties of the open file
98  ********************************************************/
99
100 void gmx_fio_setprecision(t_fileio *fio, gmx_bool bDouble);
101 /* Select the floating point precision for reading and writing files */
102
103 char *gmx_fio_getname(t_fileio *fio);
104 /* Return the filename corresponding to the fio index */
105
106 int gmx_fio_getftp(t_fileio *fio);
107 /* Return the filetype corresponding to the fio index.
108     There is as of now no corresponding setftp function because the file
109     was opened as a specific file type and changing that midway is most
110     likely an evil hack. */
111
112 void gmx_fio_setdebug(t_fileio *fio, gmx_bool bDebug);
113 /* Set the debug mode */
114
115 gmx_bool gmx_fio_getread(t_fileio *fio);
116 /* Return  whether read mode is on in fio  */
117
118 /***************************************************
119  * FILE Operations
120  ***************************************************/
121
122 void gmx_fio_rewind(t_fileio *fio);
123 /* Rewind the file in fio */
124
125 int gmx_fio_flush(t_fileio *fio);
126 /* Flush the fio, returns 0 on success */
127
128 int gmx_fio_fsync(t_fileio *fio);
129 /* fsync the fio, returns 0 on success.
130    NOTE: don't use fsync function unless you're absolutely sure you need it
131    because it deliberately interferes with the OS's caching mechanisms and
132    can cause dramatically slowed down IO performance. Some OSes (Linux,
133    for example), may implement fsync as a full sync() point. */
134
135 gmx_off_t gmx_fio_ftell(t_fileio *fio);
136 /* Return file position if possible */
137
138 int gmx_fio_seek(t_fileio *fio, gmx_off_t fpos);
139 /* Set file position if possible, quit otherwise */
140
141 FILE *gmx_fio_getfp(t_fileio *fio);
142 /* Return the file pointer itself */
143
144
145 /* Element with information about position in a currently open file.
146  * gmx_off_t should be defined by autoconf if your system does not have it.
147  * If you do not have it on some other platform you do not have largefile
148  * support at all, and you can define it to int (or better, find out how to
149  * enable large files).  */
150 typedef struct gmx_file_position_t
151 {
152     char          filename[STRLEN];
153     gmx_off_t     offset;
154     unsigned char chksum[16];
155     int           chksum_size;
156 }
157 gmx_file_position_t;
158
159 int gmx_fio_get_output_file_positions(gmx_file_position_t ** outputfiles,
160                                       int                   *nfiles );
161 /* Return the name and file pointer positions for all currently open
162  * output files. This is used for saving in the checkpoint files, so we
163  * can truncate output files upon restart-with-appending.
164  *
165  * For the first argument you should use a pointer, which will be set to
166  * point to a list of open files.
167  */
168
169 t_fileio *gmx_fio_all_output_fsync(void);
170 /* fsync all open output files. This is used for checkpointing, where
171    we need to ensure that all output is actually written out to
172    disk.
173    This is most important in the case of some networked file systems,
174    where data is not synced with the file server until close() or
175    fsync(), so data could remain in cache for days.
176    Note the caveats reported with gmx_fio_fsync().
177
178     returns: NULL if no error occurred, or a pointer to the first file that
179              failed if an error occurred
180  */
181
182
183 int gmx_fio_get_file_md5(t_fileio *fio, gmx_off_t offset,
184                          unsigned char digest[]);
185
186
187 int xtc_seek_time(t_fileio *fio, real time, int natoms, gmx_bool bSeekForwardOnly);
188
189
190 #ifdef __cplusplus
191 }
192 #endif
193
194 #endif