Make some unit tests use mock file output
[alexxy/gromacs.git] / src / gromacs / utility / filestream.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements classes from filestream.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_utility
41  */
42 #include "gmxpre.h"
43
44 #include "filestream.h"
45
46 #include <cerrno>
47 #include <cstdio>
48
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/stringutil.h"
52
53 namespace gmx
54 {
55
56 namespace internal
57 {
58
59 /********************************************************************
60  * FileStreamImpl
61  */
62
63 class FileStreamImpl
64 {
65     public:
66         explicit FileStreamImpl(FILE *fp)
67             : fp_(fp), bClose_(false)
68         {
69         }
70         FileStreamImpl(const char *filename, const char *mode)
71             : fp_(NULL), bClose_(true)
72         {
73             fp_ = std::fopen(filename, mode);
74             if (fp_ == NULL)
75             {
76                 GMX_THROW_WITH_ERRNO(
77                         FileIOError(formatString("Could not open file '%s'", filename)),
78                         "fopen", errno);
79             }
80         }
81         ~FileStreamImpl()
82         {
83             if (fp_ != NULL && bClose_)
84             {
85                 if (std::fclose(fp_) != 0)
86                 {
87                     // TODO: Log the error somewhere
88                 }
89             }
90         }
91
92         FILE *handle()
93         {
94             GMX_RELEASE_ASSERT(fp_ != NULL,
95                                "Attempted to access a file object that is not open");
96             return fp_;
97         }
98
99         void close()
100         {
101             GMX_RELEASE_ASSERT(fp_ != NULL,
102                                "Attempted to close a file object that is not open");
103             GMX_RELEASE_ASSERT(bClose_,
104                                "Attempted to close a file object that should not be");
105             const bool bOk = (std::fclose(fp_) == 0);
106             fp_ = NULL;
107             if (!bOk)
108             {
109                 GMX_THROW_WITH_ERRNO(
110                         FileIOError("Error while closing file"), "fclose", errno);
111             }
112         }
113
114     private:
115         //! File handle for this object (NULL if the stream has been closed).
116         FILE  *fp_;
117         //! Whether \p fp_ should be closed by this object.
118         bool   bClose_;
119 };
120
121 }   // namespace internal
122
123 using internal::FileStreamImpl;
124
125 /********************************************************************
126  * TextOutputFile
127  */
128
129 TextOutputFile::TextOutputFile(const std::string &filename)
130     : impl_(new FileStreamImpl(filename.c_str(), "w"))
131 {
132 }
133
134 TextOutputFile::TextOutputFile(FILE *fp)
135     : impl_(new FileStreamImpl(fp))
136 {
137 }
138
139 TextOutputFile::~TextOutputFile()
140 {
141 }
142
143 void TextOutputFile::write(const char *str)
144 {
145     if (std::fprintf(impl_->handle(), "%s", str) < 0)
146     {
147         GMX_THROW_WITH_ERRNO(FileIOError("Writing to file failed"),
148                              "fprintf", errno);
149     }
150 }
151
152 void TextOutputFile::close()
153 {
154     impl_->close();
155 }
156
157 // static
158 TextOutputFile &TextOutputFile::standardOutput()
159 {
160     static TextOutputFile stdoutObject(stdout);
161     return stdoutObject;
162 }
163
164 // static
165 TextOutputFile &TextOutputFile::standardError()
166 {
167     static TextOutputFile stderrObject(stderr);
168     return stderrObject;
169 }
170
171 } // namespace gmx