*
* Copyright (c) 1991-2000, University of Groningen, The Netherlands.
* Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2018,2019,2021, by the GROMACS development team, led by
* Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
* and including many others, as listed in the AUTHORS file in the
* top-level source directory and at http://www.gromacs.org.
#ifndef GMX_UTILITY_FUTIL_H
#define GMX_UTILITY_FUTIL_H
-#include <limits.h>
-#include <stdio.h>
+#include <climits>
+#include <cstdio>
-#include "gromacs/utility/basedefinitions.h"
+#include <string>
-#ifdef __cplusplus
-extern "C" {
-#endif
-#if 0
-}
-#endif
+#include "gromacs/utility/basedefinitions.h"
+#include "gromacs/utility/fileptr.h"
/*! \def GMX_PATH_MAX
* \brief
* Maximum path length, if the OS provides one, otherwise a fixed constant.
*/
#ifdef PATH_MAX
-# define GMX_PATH_MAX PATH_MAX
+# define GMX_PATH_MAX PATH_MAX
#elif defined MAX_PATH
-# define GMX_PATH_MAX MAX_PATH
+# define GMX_PATH_MAX MAX_PATH
#else
-# define GMX_PATH_MAX 4096
+# define GMX_PATH_MAX 4096
#endif
/** \Gromacs definition to use instead of `off_t`. */
-typedef gmx_int64_t gmx_off_t;
+typedef int64_t gmx_off_t;
/*! \brief
* Turn off buffering for output files (which is default) for debugging
*
* This only has effect on files opened with gmx_ffopen().
*/
-void gmx_disable_file_buffering(void);
+void gmx_disable_file_buffering();
/*! \brief
* Enables backups with the specified number of maximum backups.
* Note that this returns `TRUE` even if \p fname is a directory instead of a
* file.
*/
-gmx_bool gmx_fexist(const char *fname);
+gmx_bool gmx_fexist(const std::string& fname);
/*! \brief
* Makes a backup of file if the file exists.
*/
-void make_backup(const char *file);
+void make_backup(const std::string& file);
/*! \brief
* Opens a file, with \Gromacs-specific additions.
* overwriting it.
* A fatal error results if the file cannot be opened, for whatever reason.
*/
-FILE *gmx_ffopen(const char *file, const char *mode);
+FILE* gmx_ffopen(const std::string& file, const char* mode);
/** Closes a file opened with gmx_ffopen(). */
-int gmx_ffclose(FILE *fp);
+int gmx_ffclose(FILE* fp);
/*! \brief
* Wraps rewind() for files opened with gmx_ffopen().
* A fatal error results if this function is called for a pipe (a compressed
* input file).
*/
-void frewind(FILE *fp);
-
-/** OS-independent 64-bit fseek(). */
-int gmx_fseek(FILE *stream, gmx_off_t offset, int whence);
+void frewind(FILE* fp);
-/** OS-independent 64-bit ftell(). */
-gmx_off_t gmx_ftell(FILE *stream);
-
-/** OS-independent truncate(). */
-int gmx_truncate(const char *filename, gmx_off_t length);
-
-/*! \brief
- * Finds full path for a library file.
+/** OS-independent 64-bit fseek().
*
- * Searches first in the current directory, and then in the configured library
- * directories.
- * Fatal error results if the file is not found in any location.
- * The caller is responsible of freeing the returned string.
+ * \return 0 when successful, or -1 (and set errno) in case of error.
*/
-char *gmxlibfn(const char *file);
+int gmx_fseek(FILE* stream, gmx_off_t offset, int whence);
-/*! \brief
- * Opens a library file for reading.
+/** OS-independent 64-bit ftell().
*
- * Works as gmxlibfn(), except that it opens the file and returns a file
- * handle.
+ * \return The current offset when successful, or -1 (and set errno) in case of error.
*/
-FILE *libopen(const char *file);
+gmx_off_t gmx_ftell(FILE* stream);
+
+/** OS-independent truncate(). */
+int gmx_truncate(const std::string& filename, gmx_off_t length);
+
+namespace gmx
+{
/*! \brief
- * More flexible gmxlibfn().
+ * Finds full path for a library file.
*
- * Works as gmxlibfn(), but provides control whether the current working
- * directory is searched or not, and whether a missing file is a fatal error or
- * not.
+ * Searches in the configured library directories for \c filename. If
+ * \c bAddCWD is true, searches first in the current directory. Fatal
+ * error results if the file is not found in any location and \c
+ * bFatal is true.
*/
-char *low_gmxlibfn(const char *file, gmx_bool bAddCWD, gmx_bool bFatal);
+std::string findLibraryFile(const std::string& filename, bool bAddCWD = true, bool bFatal = true);
+//! \copydoc findLibraryFile(const std::string &, bool, bool)
+std::string findLibraryFile(const char* filename, bool bAddCWD = true, bool bFatal = true);
/*! \brief
- * Alternative for libopen() that optionally does not exit.
+ * Opens a library file for reading in an RAII-style `FILE` handle.
*
- * Works as libopen(), but provides control whether a missing file is a fatal
- * error or not.
+ * Works as findLibraryFile(), except that it opens the file and
+ * returns a file handle.
*/
-FILE *low_libopen(const char *file, gmx_bool bFatal);
+FilePtr openLibraryFile(const std::string& filename, bool bAddCWD = true, bool bFatal = true);
+//! \copydoc openLibraryFile(const std::string &, bool, bool)
+FilePtr openLibraryFile(const char* filename, bool bAddCWD = true, bool bFatal = true);
+} // namespace gmx
/*! \brief
* Creates unique name for temp file (wrapper around mkstemp) and opens it.
*
* \p buf should be at least 7 bytes long
*/
-FILE *gmx_fopen_temporary(char *buf);
+FILE* gmx_fopen_temporary(char* buf);
/*! \brief
* Creates unique name for temp file (wrapper around mkstemp).
*
* \p buf should be at least 7 bytes long
*/
-void gmx_tmpnam(char *buf);
+void gmx_tmpnam(char* buf);
/*! \brief
* OS-independent rename().
*
* Renames/moves a file atomically, if the OS makes that available.
*/
-int gmx_file_rename(const char *oldname, const char *newname);
+void gmx_file_rename(const char* oldname, const char* newname);
/*! \brief
* Copies a file (data only) oldname to newname.
*
* If \p copy_if_empty is `FALSE`, the file won't be copied if it's empty.
*/
-int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty);
+int gmx_file_copy(const char* oldname, const char* newname, gmx_bool copy_if_empty);
/*! \brief
* OS-independent fsync().
*
* Only use this during checkpointing!
*/
-int gmx_fsync(FILE *fp);
+int gmx_fsync(FILE* fp);
/*! \brief
* OS-independent chdir().
*
* Exits with a fatal error if changing the directory fails.
*/
-void gmx_chdir(const char *directory);
+void gmx_chdir(const char* directory);
/*! \brief
* OS-independent getcwd().
*
* Exits with a fatal error if the call fails.
*/
-void gmx_getcwd(char *buffer, size_t size);
-
-#ifdef __cplusplus
-}
+void gmx_getcwd(char* buffer, size_t size);
namespace gmx
{
*
* \ingroup module_utility
*/
-const DataFileFinder &getLibraryFileFinder();
+const DataFileFinder& getLibraryFileFinder();
/*! \brief
* Sets a finder for location data files from share/top/.
*
* The provided object must remain valid until the global instance is changed
* by another call to setLibraryFileFinder().
*
- * The global instance is used by gmxlibfn() and libopen().
+ * The global instance is used by findLibraryFile() and openLibraryFile().
*
* This method is not thread-safe. See setProgramContext(); the same
* constraints apply here as well.
*
* Does not throw.
*/
-void setLibraryFileFinder(const DataFileFinder *finder);
+void setLibraryFileFinder(const DataFileFinder* finder);
} // namespace gmx
-#endif
#endif