3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Declares functions for file handling.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_utility
39 #ifndef GMX_UTILITY_FILE_H
40 #define GMX_UTILITY_FILE_H
54 * This class provides basic file I/O functionality and uses exceptions
55 * (FileIOError) for error reporting.
58 * \ingroup module_utility
64 * Creates a file object and opens a file.
66 * \param[in] filename Path of the file to open.
67 * \param[in] mode Mode to open the file in (for fopen()).
68 * \throws std::bad_alloc if out of memory.
69 * \throws FileIOError on any I/O error.
71 * \see open(const char *, const char *)
73 File(const char *filename, const char *mode);
74 //! \copydoc File(const char *, const char *)
75 File(const std::string &filename, const char *mode);
77 * Destroys the file object.
79 * If the file is still open, it is closed.
80 * Any error conditions will be ignored.
87 * \param[in] filename Path of the file to open.
88 * \param[in] mode Mode to open the file in (for fopen()).
89 * \throws FileIOError on any I/O error.
91 * The file object must not be open.
93 void open(const char *filename, const char *mode);
94 //! \copydoc open(const char *, const char *)
95 void open(const std::string &filename, const char *mode);
97 * Closes the file object.
99 * \throws FileIOError on any I/O error.
101 * The file must be open.
106 * Returns a file handle for interfacing with C functions.
108 * The file must be open.
114 * Reads given number of bytes from the file.
116 * \param[out] buffer Pointer to buffer that receives the bytes.
117 * \param[in] bytes Number of bytes to read.
118 * \throws FileIOError on any I/O error.
120 * The file must be open.
122 void readBytes(void *buffer, size_t bytes);
125 * Writes a string to the file.
127 * \param[in] str String to write.
128 * \throws FileIOError on any I/O error.
130 * The file must be open.
132 void writeString(const char *str);
133 //! \copydoc writeString(const char *)
134 void writeString(const std::string &str) { writeString(str.c_str()); }
136 * Writes a line to the file.
138 * \param[in] line Line to write.
139 * \throws FileIOError on any I/O error.
141 * If \p line does not end in a newline, one newline is appended.
142 * Otherwise, works as writeString().
144 * The file must be open.
146 void writeLine(const char *line);
147 //! \copydoc writeLine(const char *)
148 void writeLine(const std::string &line) { writeLine(line.c_str()); }
150 * Writes a newline to the file.
152 * \throws FileIOError on any I/O error.
157 * Returns a File object for accessing stdout.
159 * \throws std::bad_alloc if out of memory (only on first call).
161 static File &standardOutput();
163 * Returns a File object for accessing stderr.
165 * \throws std::bad_alloc if out of memory (only on first call).
167 static File &standardError();
170 * Reads contents of a file to a std::string.
172 * \param[in] filename File to read.
173 * \returns The contents of \p filename.
174 * \throws std::bad_alloc if out of memory.
175 * \throws FileIOError on any I/O error.
177 static std::string readToString(const char *filename);
178 //! \copydoc readToString(const char *)
179 static std::string readToString(const std::string &filename);
183 * Initialize file object from an existing file handle.
185 * \param[in] fp File handle to use (may be NULL).
186 * \param[in] bClose Whether this object should close its file handle.
187 * \throws std::bad_alloc if out of memory.
189 * Used internally to implement standardError().
191 File(FILE *fp, bool bClose);
195 PrivateImplPointer<Impl> impl_;