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 FileIOError on any I/O error.
70 * \see open(const char *, const char *)
72 File(const char *filename, const char *mode);
73 //! \copydoc File(const char *, const char *)
74 File(const std::string &filename, const char *mode);
76 * Destroys the file object.
78 * If the file is still open, it is closed.
79 * Any error conditions will be ignored.
86 * \param[in] filename Path of the file to open.
87 * \param[in] mode Mode to open the file in (for fopen()).
88 * \throws FileIOError on any I/O error.
90 * The file object must not be open.
92 void open(const char *filename, const char *mode);
93 //! \copydoc open(const char *, const char *)
94 void open(const std::string &filename, const char *mode);
96 * Closes the file object.
98 * \throws FileIOError on any I/O error.
100 * The file must be open.
105 * Returns a file handle for interfacing with C functions.
107 * The file must be open.
113 * Reads given number of bytes from the file.
115 * \param[out] buffer Pointer to buffer that receives the bytes.
116 * \param[in] bytes Number of bytes to read.
117 * \throws FileIOError on any I/O error.
119 * The file must be open.
121 void readBytes(void *buffer, size_t bytes);
124 * Reads contents of a file to a std::string.
126 * \param[in] filename File to read.
127 * \returns The contents of \p filename.
128 * \throws std::bad_alloc if out of memory.
129 * \throws FileIOError on any I/O error.
131 static std::string readToString(const char *filename);
132 //! \copydoc readToString(const char *)
133 static std::string readToString(const std::string &filename);
138 GMX_DISALLOW_COPY_AND_ASSIGN(File);