2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2018, 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.
35 /*! \libinternal \file
37 * Declares implementations for textstream.h interfaces for file input/output.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_FILESTREAM_H
44 #define GMX_UTILITY_FILESTREAM_H
50 #include "gromacs/utility/classhelpers.h"
51 #include "gromacs/utility/fileptr.h"
52 #include "gromacs/utility/textstream.h"
62 /*! \libinternal \brief
63 * Text input stream implementation for reading from `stdin`.
65 * Implementations for the TextInputStream methods throw FileIOError on any
69 * \ingroup module_utility
71 class StandardInputStream
: public TextInputStream
75 * Returns whether `stdin` is an interactive terminal.
77 * Only works on Unix, otherwise always returns true.
81 bool isInteractive() const;
83 // From TextInputStream
84 bool readLine(std::string
*line
) override
;
85 void close() override
{}
88 * Returns a stream for accessing `stdin`.
92 static StandardInputStream
&instance();
95 /*! \libinternal \brief
96 * Text input stream implementation for reading from a file.
98 * Implementations for the TextInputStream methods throw FileIOError on any
102 * \ingroup module_utility
104 class TextInputFile
: public TextInputStream
108 * Opens a file and returns an RAII-style `FILE` handle.
110 * \param[in] filename Path of the file to open.
111 * \throws FileIOError on any I/O error.
113 * Instead of returning `NULL` on errors, throws an exception with
114 * additional details (including the file name and `errno`).
116 static FilePtr
openRawHandle(const char *filename
);
117 //! \copydoc openRawHandle(const char *)
118 static FilePtr
openRawHandle(const std::string
&filename
);
121 * Opens a text file as a stream.
123 * \param[in] filename Path to the file to open.
124 * \throws std::bad_alloc if out of memory.
125 * \throws FileIOError on any I/O error.
127 explicit TextInputFile(const std::string
&filename
);
129 * Initializes file object from an existing file handle.
131 * \param[in] fp File handle to use.
132 * \throws std::bad_alloc if out of memory.
134 * The caller is responsible of closing the file; close() does nothing
135 * for an object constructed this way.
137 explicit TextInputFile(FILE *fp
);
138 ~TextInputFile() override
;
141 * Returns a raw handle to the input file.
143 * This is provided for interoperability with older C-like code.
147 // From TextInputStream
148 bool readLine(std::string
*line
) override
;
149 void close() override
;
152 PrivateImplPointer
<internal::FileStreamImpl
> impl_
;
155 /*! \libinternal \brief
156 * Text output stream implementation for writing to a file.
158 * Implementations for the TextOutputStream methods throw FileIOError on any
162 * \ingroup module_utility
164 class TextOutputFile
: public TextOutputStream
167 //! \copydoc TextInputFile::TextInputFile(const std::string &)
168 explicit TextOutputFile(const std::string
&filename
);
169 //! \copydoc TextInputFile::TextInputFile(FILE *)
170 explicit TextOutputFile(FILE *fp
);
171 ~TextOutputFile() override
;
173 // From TextOutputStream
174 void write(const char *text
) override
;
175 void close() override
;
178 * Returns a stream for accessing `stdout`.
180 * \throws std::bad_alloc if out of memory (only on first call).
182 static TextOutputFile
&standardOutput();
184 * Returns a stream for accessing `stderr`.
186 * \throws std::bad_alloc if out of memory (only on first call).
188 static TextOutputFile
&standardError();
191 PrivateImplPointer
<internal::FileStreamImpl
> impl_
;