Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / utility / futil.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, 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 /*! \file
38  * \brief
39  * Low-level wrappers for OS-specific file handling with some \Gromacs
40  * customizations.
41  *
42  * \inpublicapi
43  * \ingroup module_utility
44  */
45 #ifndef GMX_UTILITY_FUTIL_H
46 #define GMX_UTILITY_FUTIL_H
47
48 #include <limits.h>
49 #include <stdio.h>
50
51 #include "basedefinitions.h"
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 #if 0
57 }
58 #endif
59
60 #include "gmx_header_config.h"
61 /*! \def DIR_SEPARATOR
62  * \brief
63  * Directory separator on this OS.
64  *
65  * Native Windows uses backslash path separators (but accepts also slashes).
66  * Cygwin and most other systems use slash.
67  *
68  * \todo
69  * Get rid of this (Redmine #950), or at least remove this from an installed
70  * header.  It is not necessary for constructing paths on the systems that it
71  * currently supports, and is not reliable in parsing input paths either, since
72  * Windows needs to accept both instead of only DIR_SEPARATOR.
73  */
74 #ifdef GMX_NATIVE_WINDOWS
75 #define DIR_SEPARATOR '\\'
76 #else
77 #define DIR_SEPARATOR '/'
78 #endif
79
80 /*! \def GMX_PATH_MAX
81  * \brief
82  * Maximum path length, if the OS provides one, otherwise a fixed constant.
83  */
84 #ifdef PATH_MAX
85 #  define GMX_PATH_MAX PATH_MAX
86 #elif defined MAX_PATH
87 #  define GMX_PATH_MAX MAX_PATH
88 #else
89 #  define GMX_PATH_MAX 4096
90 #endif
91
92 /** \Gromacs definition to use instead of `off_t`. */
93 typedef gmx_int64_t    gmx_off_t;
94
95 /*! \brief
96  * Turn off buffering of files (which is default) for debugging purposes.
97  *
98  * This only has effect on files opened with gmx_ffopen().
99  */
100 void no_buffers(void);
101
102 /*! \brief
103  * Check whether a path exists.
104  *
105  * \returns `TRUE` when \p fname exists.
106  *
107  * Note that this returns `TRUE` even if \p fname is a directory instead of a
108  * file.
109  */
110 gmx_bool gmx_fexist(const char *fname);
111
112 /*! \brief
113  * Checks for end of file.
114  *
115  * \returns `TRUE` on end-of-file
116  *
117  * \todo
118  * There are only two callers for this function, while there are ~20 direct
119  * calls to feof().  Probably this is unnecessary.
120  */
121 gmx_bool gmx_eof(FILE *fp);
122
123 /*! \brief
124  * Makes a backup of file if the file exists.
125  *
126  * \returns `FALSE` if there was a problem.
127  */
128 gmx_bool make_backup(const char *file);
129
130 /*! \brief
131  * Opens a file, with \Gromacs-specific additions.
132  *
133  * If the file is in compressed format, opens a pipe which uncompresses the
134  * file on the fly.  For this to work, gmx_ffclose() and frewind() should
135  * always be used for files opened with gmx_ffopen() instead of fclose() and
136  * rewind().  For compressed files, the \p file parameter should be passed
137  * without the compressed extension; if that file is not found, then a few
138  * compression extensions are tried.
139  * Creates a backup if a file opened for writing already exists before
140  * overwriting it.
141  * A fatal error results if the file cannot be opened, for whatever reason.
142  */
143 FILE *gmx_ffopen(const char *file, const char *mode);
144
145 /** Closes a file opened with gmx_ffopen(). */
146 int gmx_ffclose(FILE *fp);
147
148 /*! \brief
149  * Wraps rewind() for files opened with gmx_ffopen().
150  *
151  * A fatal error results if this function is called for a pipe (a compressed
152  * input file).
153  */
154 void frewind(FILE *fp);
155
156 /** OS-independent 64-bit fseek(). */
157 int gmx_fseek(FILE *stream, gmx_off_t offset, int whence);
158
159 /** OS-independent 64-bit ftell(). */
160 gmx_off_t gmx_ftell(FILE *stream);
161
162 /*! \brief
163  * Finds full path for a library file.
164  *
165  * Searches first in the current directory, and then in the configured library
166  * directories.
167  * Fatal error results if the file is not found in any location.
168  * The caller is responsible of freeing the returned string.
169  */
170 char *gmxlibfn(const char *file);
171
172 /*! \brief
173  * Opens a library file for reading.
174  *
175  * Works as gmxlibfn(), except that it opens the file and returns a file
176  * handle.
177  */
178 FILE *libopen(const char *file);
179
180 /*! \brief
181  * More flexible gmxlibfn().
182  *
183  * Works as gmxlibfn(), but provides control whether the current working
184  * directory is searched or not, and whether a missing file is a fatal error or
185  * not.
186  */
187 char *low_gmxlibfn(const char *file, gmx_bool bAddCWD, gmx_bool bFatal);
188
189 /*! \brief
190  * Alternative for libopen() that optionally does not exit.
191  *
192  * Works as libopen(), but provides control whether a missing file is a fatal
193  * error or not.
194  */
195 FILE *low_libopen(const char *file, gmx_bool bFatal);
196
197 /** Opaque data type to list directories. */
198 typedef struct gmx_directory *gmx_directory_t;
199
200 /*! \brief
201  * Opens a directory for reading.
202  *
203  * \param[out] p_gmxdir Handle to the opened directory.
204  * \param[in]  dirname  Path to directory to open.
205  * \returns  0 on success.
206  */
207 int
208 gmx_directory_open(gmx_directory_t *p_gmxdir, const char *dirname);
209
210 /*! \brief
211  * Gets next file in a directory.
212  *
213  * Given an initialized gmx_directory_t, if there are more files in
214  * the directory this routine returns 0 and write the next name
215  * into the USER-PROVIDED buffer \p name.  The last argument is the max
216  * number of characters that will be written.  Just as strncpy(), the
217  * string will NOT be terminated it it is longer than \p maxlength_name.
218  */
219 int
220 gmx_directory_nextfile(gmx_directory_t gmxdir, char *name, int maxlength_name);
221
222 /** Releases all data for a directory structure. */
223 int
224 gmx_directory_close(gmx_directory_t gmxdir);
225
226
227 /*! \brief
228  * Creates unique name for temp file (wrapper around mkstemp).
229  *
230  * \p buf should be at least 7 bytes long
231  */
232 void gmx_tmpnam(char *buf);
233
234 /*! \brief
235  * OS-independent rename().
236  *
237  * Renames/moves a file atomically, if the OS makes that available.
238  */
239 int gmx_file_rename(const char *oldname, const char *newname);
240
241 /*! \brief
242  * Copies a file (data only) oldname to newname.
243  *
244  * If \p copy_if_empty is `FALSE`, the file won't be copied if it's empty.
245  */
246 int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty);
247
248 /*! \brief
249  * OS-independent fsync().
250  *
251  * Only use this during checkpointing!
252  */
253 int gmx_fsync(FILE *fp);
254
255 /*! \brief
256  * OS-independent chdir().
257  *
258  * Exits with a fatal error if changing the directory fails.
259  */
260 void gmx_chdir(const char *directory);
261 /*! \brief
262  * OS-independent getcwd().
263  *
264  * Exits with a fatal error if the call fails.
265  */
266 void gmx_getcwd(char *buffer, size_t size);
267
268 #ifdef __cplusplus
269 }
270 #endif
271
272 #endif