Merge branch release-2016 into release-2018
[gromacs.git] / src / gromacs / utility / futil.h
blob1a964bf87f6854e506b29512132a78111ea17f0c
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 /*! \file
38 * \brief
39 * Low-level wrappers for OS-specific file handling with some \Gromacs
40 * customizations.
42 * \inpublicapi
43 * \ingroup module_utility
45 #ifndef GMX_UTILITY_FUTIL_H
46 #define GMX_UTILITY_FUTIL_H
48 #include <limits.h>
49 #include <stdio.h>
51 #include "gromacs/utility/basedefinitions.h"
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 #if 0
58 #endif
60 /*! \def GMX_PATH_MAX
61 * \brief
62 * Maximum path length, if the OS provides one, otherwise a fixed constant.
64 #ifdef PATH_MAX
65 # define GMX_PATH_MAX PATH_MAX
66 #elif defined MAX_PATH
67 # define GMX_PATH_MAX MAX_PATH
68 #else
69 # define GMX_PATH_MAX 4096
70 #endif
72 /** \Gromacs definition to use instead of `off_t`. */
73 typedef gmx_int64_t gmx_off_t;
75 /*! \brief
76 * Turn off buffering for output files (which is default) for debugging
77 * purposes.
79 * This only has effect on files opened with gmx_ffopen().
81 void gmx_disable_file_buffering(void);
83 /*! \brief
84 * Enables backups with the specified number of maximum backups.
86 * If \p count == 0, disables backups. If not called, this is the default.
87 * If \p count == -1, reads the count from an environment variable.
89 * This is not currently thread-safe, as it is only called during
90 * initialization code.
92 void gmx_set_max_backup_count(int count);
94 /*! \brief
95 * Check whether a path exists.
97 * \returns `TRUE` when \p fname exists.
99 * Note that this returns `TRUE` even if \p fname is a directory instead of a
100 * file.
102 gmx_bool gmx_fexist(const char *fname);
104 /*! \brief
105 * Makes a backup of file if the file exists.
107 void make_backup(const char *file);
109 /*! \brief
110 * Opens a file, with \Gromacs-specific additions.
112 * If the file is in compressed format, opens a pipe which uncompresses the
113 * file on the fly. For this to work, gmx_ffclose() and frewind() should
114 * always be used for files opened with gmx_ffopen() instead of fclose() and
115 * rewind(). For compressed files, the \p file parameter should be passed
116 * without the compressed extension; if that file is not found, then a few
117 * compression extensions are tried.
118 * Creates a backup if a file opened for writing already exists before
119 * overwriting it.
120 * A fatal error results if the file cannot be opened, for whatever reason.
122 FILE *gmx_ffopen(const char *file, const char *mode);
124 /** Closes a file opened with gmx_ffopen(). */
125 int gmx_ffclose(FILE *fp);
127 /*! \brief
128 * Wraps rewind() for files opened with gmx_ffopen().
130 * A fatal error results if this function is called for a pipe (a compressed
131 * input file).
133 void frewind(FILE *fp);
135 /** OS-independent 64-bit fseek(). */
136 int gmx_fseek(FILE *stream, gmx_off_t offset, int whence);
138 /** OS-independent 64-bit ftell(). */
139 gmx_off_t gmx_ftell(FILE *stream);
141 /** OS-independent truncate(). */
142 int gmx_truncate(const char *filename, gmx_off_t length);
144 /*! \brief
145 * Finds full path for a library file.
147 * Searches first in the current directory, and then in the configured library
148 * directories.
149 * Fatal error results if the file is not found in any location.
150 * The caller is responsible of freeing the returned string.
152 char *gmxlibfn(const char *file);
154 /*! \brief
155 * Opens a library file for reading.
157 * Works as gmxlibfn(), except that it opens the file and returns a file
158 * handle.
160 FILE *libopen(const char *file);
162 /*! \brief
163 * More flexible gmxlibfn().
165 * Works as gmxlibfn(), but provides control whether the current working
166 * directory is searched or not, and whether a missing file is a fatal error or
167 * not.
169 char *low_gmxlibfn(const char *file, gmx_bool bAddCWD, gmx_bool bFatal);
171 /*! \brief
172 * Alternative for libopen() that optionally does not exit.
174 * Works as libopen(), but provides control whether a missing file is a fatal
175 * error or not.
177 FILE *low_libopen(const char *file, gmx_bool bFatal);
180 /*! \brief
181 * Creates unique name for temp file (wrapper around mkstemp) and opens it.
183 * \p buf should be at least 7 bytes long
185 FILE *gmx_fopen_temporary(char *buf);
187 /*! \brief
188 * Creates unique name for temp file (wrapper around mkstemp).
190 * \p buf should be at least 7 bytes long
192 void gmx_tmpnam(char *buf);
194 /*! \brief
195 * OS-independent rename().
197 * Renames/moves a file atomically, if the OS makes that available.
199 int gmx_file_rename(const char *oldname, const char *newname);
201 /*! \brief
202 * Copies a file (data only) oldname to newname.
204 * If \p copy_if_empty is `FALSE`, the file won't be copied if it's empty.
206 int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty);
208 /*! \brief
209 * OS-independent fsync().
211 * Only use this during checkpointing!
213 int gmx_fsync(FILE *fp);
215 /*! \brief
216 * OS-independent chdir().
218 * Exits with a fatal error if changing the directory fails.
220 void gmx_chdir(const char *directory);
221 /*! \brief
222 * OS-independent getcwd().
224 * Exits with a fatal error if the call fails.
226 void gmx_getcwd(char *buffer, size_t size);
228 #ifdef __cplusplus
231 namespace gmx
234 class DataFileFinder;
236 /*! \brief
237 * Gets a finder for locating data files from share/top/.
239 * \returns Finder set with setLibraryFileFinder(), or a default finder.
241 * If setLibraryFileFinder() has not been called (or a `NULL` finder has been
242 * set), a default finder is returned.
243 * The default finder searches data files from the directory identified by the
244 * global program context; it does not respect GMXLIB environment variable.
245 * Calling initForCommandLine() sets a finder that respects GMXLIB.
247 * Does not throw.
249 * See setLibraryFileFinder() for thread safety.
251 * \ingroup module_utility
253 const DataFileFinder &getLibraryFileFinder();
254 /*! \brief
255 * Sets a finder for location data files from share/top/.
257 * \param[in] finder finder to set
258 * (can be NULL to restore the default finder).
260 * The library does not take ownership of \p finder.
261 * The provided object must remain valid until the global instance is changed
262 * by another call to setLibraryFileFinder().
264 * The global instance is used by gmxlibfn() and libopen().
266 * This method is not thread-safe. See setProgramContext(); the same
267 * constraints apply here as well.
269 * Does not throw.
271 void setLibraryFileFinder(const DataFileFinder *finder);
273 } // namespace gmx
274 #endif
276 #endif