2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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.
37 * Declares functions for file handling.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_FILE_H
44 #define GMX_UTILITY_FILE_H
58 * This class provides basic file I/O functionality and uses exceptions
59 * (FileIOError) for error reporting.
62 * \ingroup module_utility
68 * Creates a file object and opens a file.
70 * \param[in] filename Path of the file to open.
71 * \param[in] mode Mode to open the file in (for fopen()).
72 * \throws std::bad_alloc if out of memory.
73 * \throws FileIOError on any I/O error.
75 * \see open(const char *, const char *)
77 File(const char *filename, const char *mode);
78 //! \copydoc File(const char *, const char *)
79 File(const std::string &filename, const char *mode);
81 * Destroys the file object.
83 * If the file is still open, it is closed.
84 * Any error conditions will be ignored.
91 * \param[in] filename Path of the file to open.
92 * \param[in] mode Mode to open the file in (for fopen()).
93 * \throws FileIOError on any I/O error.
95 * The file object must not be open.
97 void open(const char *filename, const char *mode);
98 //! \copydoc open(const char *, const char *)
99 void open(const std::string &filename, const char *mode);
101 * Closes the file object.
103 * \throws FileIOError on any I/O error.
105 * The file must be open.
110 * Returns a file handle for interfacing with C functions.
112 * The file must be open.
118 * Reads given number of bytes from the file.
120 * \param[out] buffer Pointer to buffer that receives the bytes.
121 * \param[in] bytes Number of bytes to read.
122 * \throws FileIOError on any I/O error.
124 * The file must be open.
126 void readBytes(void *buffer, size_t bytes);
128 * Reads a single line from the file.
130 * \param[out] line String to receive the line.
131 * \returns false if nothing was read because the file ended.
132 * \throws std::bad_alloc if out of memory.
133 * \throws FileIOError on any I/O error.
135 * On error or when false is returned, \p line will be empty.
136 * Terminating newline will be present in \p line if it was present in
138 * To loop over all lines in the file, use:
141 while (file.readLine(&line))
147 bool readLine(std::string *line);
150 * Writes a string to the file.
152 * \param[in] str String to write.
153 * \throws FileIOError on any I/O error.
155 * The file must be open.
157 void writeString(const char *str);
158 //! \copydoc writeString(const char *)
159 void writeString(const std::string &str) { writeString(str.c_str()); }
161 * Writes a line to the file.
163 * \param[in] line Line to write.
164 * \throws FileIOError on any I/O error.
166 * If \p line does not end in a newline, one newline is appended.
167 * Otherwise, works as writeString().
169 * The file must be open.
171 void writeLine(const char *line);
172 //! \copydoc writeLine(const char *)
173 void writeLine(const std::string &line) { writeLine(line.c_str()); }
175 * Writes a newline to the file.
177 * \throws FileIOError on any I/O error.
182 * Checks whether a file exists.
184 * \param[in] filename Path to the file to check.
185 * \returns true if \p filename exists and is accessible.
189 static bool exists(const char *filename);
190 //! \copydoc exists(const char *)
191 static bool exists(const std::string &filename);
194 * Returns a File object for accessing stdin.
196 * \throws std::bad_alloc if out of memory (only on first call).
198 static File &standardInput();
200 * Returns a File object for accessing stdout.
202 * \throws std::bad_alloc if out of memory (only on first call).
204 static File &standardOutput();
206 * Returns a File object for accessing stderr.
208 * \throws std::bad_alloc if out of memory (only on first call).
210 static File &standardError();
213 * Reads contents of a file to a std::string.
215 * \param[in] filename File to read.
216 * \returns The contents of \p filename.
217 * \throws std::bad_alloc if out of memory.
218 * \throws FileIOError on any I/O error.
220 static std::string readToString(const char *filename);
221 //! \copydoc readToString(const char *)
222 static std::string readToString(const std::string &filename);
224 * Convenience method for writing a file from a string in a single call.
226 * \param[in] filename File to read.
227 * \param[in] text String to write to \p filename.
228 * \throws FileIOError on any I/O error.
230 * If \p filename exists, it is overwritten.
232 static void writeFileFromString(const std::string &filename,
233 const std::string &text);
237 * Initialize file object from an existing file handle.
239 * \param[in] fp File handle to use (may be NULL).
240 * \param[in] bClose Whether this object should close its file handle.
241 * \throws std::bad_alloc if out of memory.
243 * Used internally to implement standardError().
245 File(FILE *fp, bool bClose);
249 PrivateImplPointer<Impl> impl_;