2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_FILE_H
44 #define GMX_UTILITY_FILE_H
50 #include "gromacs/utility/classhelpers.h"
58 * This class provides basic file I/O functionality and uses exceptions
59 * (FileIOError) for error reporting.
62 * \ingroup module_utility
68 * Opens a file and returns a `FILE` handle.
70 * \param[in] filename Path of the file to open.
71 * \param[in] mode Mode to open the file in (for fopen()).
72 * \throws FileIOError on any I/O error.
74 * Instead of returning `NULL` on errors, throws an exception with
75 * additional details (including the file name and `errno`).
77 static FILE *openRawHandle(const char *filename, const char *mode);
78 //! \copydoc openRawHandle(const char *, const char *)
79 static FILE *openRawHandle(const std::string &filename, const char *mode);
81 * Creates a file object and opens a file.
83 * \param[in] filename Path of the file to open.
84 * \param[in] mode Mode to open the file in (for fopen()).
85 * \throws std::bad_alloc if out of memory.
86 * \throws FileIOError on any I/O error.
88 * \see open(const char *, const char *)
90 File(const char *filename, const char *mode);
91 //! \copydoc File(const char *, const char *)
92 File(const std::string &filename, const char *mode);
94 * Destroys the file object.
96 * If the file is still open, it is closed.
97 * Any error conditions will be ignored.
104 * \param[in] filename Path of the file to open.
105 * \param[in] mode Mode to open the file in (for fopen()).
106 * \throws FileIOError on any I/O error.
108 * The file object must not be open.
110 void open(const char *filename, const char *mode);
111 //! \copydoc open(const char *, const char *)
112 void open(const std::string &filename, const char *mode);
114 * Closes the file object.
116 * \throws FileIOError on any I/O error.
118 * The file must be open.
123 * Returns a file handle for interfacing with C functions.
125 * The file must be open.
131 * Reads given number of bytes from the file.
133 * \param[out] buffer Pointer to buffer that receives the bytes.
134 * \param[in] bytes Number of bytes to read.
135 * \throws FileIOError on any I/O error.
137 * The file must be open.
139 void readBytes(void *buffer, size_t bytes);
142 * Writes a string to the file.
144 * \param[in] str String to write.
145 * \throws FileIOError on any I/O error.
147 * The file must be open.
149 void writeString(const char *str);
150 //! \copydoc writeString(const char *)
151 void writeString(const std::string &str) { writeString(str.c_str()); }
154 * Checks whether a file exists and is a regular file.
156 * \param[in] filename Path to the file to check.
157 * \returns true if \p filename exists and is accessible.
161 static bool exists(const char *filename);
162 //! \copydoc exists(const char *)
163 static bool exists(const std::string &filename);
166 * Reads contents of a file to a std::string.
168 * \param[in] filename Name of the file to read.
169 * \returns The contents of \p filename.
170 * \throws std::bad_alloc if out of memory.
171 * \throws FileIOError on any I/O error.
173 static std::string readToString(const char *filename);
174 //! \copydoc readToString(const char *)
175 static std::string readToString(const std::string &filename);
177 * Convenience method for writing a file from a string in a single call.
179 * \param[in] filename Name of the file to read.
180 * \param[in] text String to write to \p filename.
181 * \throws FileIOError on any I/O error.
183 * If \p filename exists, it is overwritten.
185 static void writeFileFromString(const std::string &filename,
186 const std::string &text);
191 PrivateImplPointer<Impl> impl_;