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);
124 * Reads a single line from the file.
126 * \param[out] line String to receive the line.
127 * \returns false if nothing was read because the file ended.
128 * \throws std::bad_alloc if out of memory.
129 * \throws FileIOError on any I/O error.
131 * On error or when false is returned, \p line will be empty.
132 * Terminating newline will be present in \p line if it was present in
134 * To loop over all lines in the file, use:
137 while (file.readLine(&line))
143 bool readLine(std::string *line);
146 * Writes a string to the file.
148 * \param[in] str String to write.
149 * \throws FileIOError on any I/O error.
151 * The file must be open.
153 void writeString(const char *str);
154 //! \copydoc writeString(const char *)
155 void writeString(const std::string &str) { writeString(str.c_str()); }
157 * Writes a line to the file.
159 * \param[in] line Line to write.
160 * \throws FileIOError on any I/O error.
162 * If \p line does not end in a newline, one newline is appended.
163 * Otherwise, works as writeString().
165 * The file must be open.
167 void writeLine(const char *line);
168 //! \copydoc writeLine(const char *)
169 void writeLine(const std::string &line) { writeLine(line.c_str()); }
171 * Writes a newline to the file.
173 * \throws FileIOError on any I/O error.
178 * Checks whether a file exists.
180 * \param[in] filename Path to the file to check.
181 * \returns true if \p filename exists and is accessible.
185 static bool exists(const char *filename);
186 //! \copydoc exists(const char *)
187 static bool exists(const std::string &filename);
190 * Returns a File object for accessing stdin.
192 * \throws std::bad_alloc if out of memory (only on first call).
194 static File &standardInput();
196 * Returns a File object for accessing stdout.
198 * \throws std::bad_alloc if out of memory (only on first call).
200 static File &standardOutput();
202 * Returns a File object for accessing stderr.
204 * \throws std::bad_alloc if out of memory (only on first call).
206 static File &standardError();
209 * Reads contents of a file to a std::string.
211 * \param[in] filename File to read.
212 * \returns The contents of \p filename.
213 * \throws std::bad_alloc if out of memory.
214 * \throws FileIOError on any I/O error.
216 static std::string readToString(const char *filename);
217 //! \copydoc readToString(const char *)
218 static std::string readToString(const std::string &filename);
220 * Convenience method for writing a file from a string in a single call.
222 * \param[in] filename File to read.
223 * \param[in] text String to write to \p filename.
224 * \throws FileIOError on any I/O error.
226 * If \p filename exists, it is overwritten.
228 static void writeFileFromString(const std::string &filename,
229 const std::string &text);
233 * Initialize file object from an existing file handle.
235 * \param[in] fp File handle to use (may be NULL).
236 * \param[in] bClose Whether this object should close its file handle.
237 * \throws std::bad_alloc if out of memory.
239 * Used internally to implement standardError().
241 File(FILE *fp, bool bClose);
245 PrivateImplPointer<Impl> impl_;