A toolkit for working with phylogenetic data.
v0.24.0
strict_fstream.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2020 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
24 /*
25  The code in this file as well as the according header file lib/utils/io/strict_fstream.hpp are
26  adapted from the excellent zstr library (C++ header-only ZLib wrapper" classes) by Matei David,
27  see https://github.com/mateidavid/zstr
28 
29  We adapted the original code by renaming all classes and variables to our standards,
30  moving much of the implementation into a source file, and refining some functionality.
31 
32  For this and the according source file, we need to include the following original license:
33 
34  The MIT License (MIT)
35 
36  Copyright (c) 2015 Matei David, Ontario Institute for Cancer Research
37 
38  Permission is hereby granted, free of charge, to any person obtaining a copy
39  of this software and associated documentation files (the "Software"), to deal
40  in the Software without restriction, including without limitation the rights
41  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42  copies of the Software, and to permit persons to whom the Software is
43  furnished to do so, subject to the following conditions:
44 
45  The above copyright notice and this permission notice shall be included in all
46  copies or substantial portions of the Software.
47 
48  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
54  SOFTWARE.
55  */
56 
65 
67 
68 #include <cassert>
69 #include <cstring>
70 #include <fstream>
71 #include <stdexcept>
72 #include <string>
73 
74 namespace genesis {
75 namespace utils {
76 
77 // ================================================================================================
78 // Error Reporting
79 // ================================================================================================
80 
86 static std::string strerror_()
87 {
88  std::string buff(80, '\0');
89 
90  #ifdef _WIN32
91 
92  if( strerror_s( &buff[0], buff.size(), errno ) != 0 ) {
93  buff = "Unknown error";
94  }
95 
96  #elif ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE) || defined(__APPLE__)
97 
98  // XSI-compliant strerror_r()
99  if( strerror_r( errno, &buff[0], buff.size() ) != 0 ) {
100  buff = "Unknown error";
101  }
102 
103  #else
104 
105  // GNU-specific strerror_r()
106  auto p = strerror_r(errno, &buff[0], buff.size());
107  std::string tmp(p, std::strlen( p ));
108  std::swap(buff, tmp);
109 
110  #endif
111 
112  buff.resize(buff.find('\0'));
113  return buff;
114 }
115 
116 // ================================================================================================
117 // Internal Helper Functions
118 // ================================================================================================
119 
120 static std::string mode_to_string_( std::ios_base::openmode mode )
121 {
122  static const int n_modes = 6;
123  static const std::ios_base::openmode mode_val_v[n_modes] =
124  {
125  std::ios_base::in,
126  std::ios_base::out,
127  std::ios_base::app,
128  std::ios_base::ate,
129  std::ios_base::trunc,
130  std::ios_base::binary
131  };
132 
133  static char const* mode_name_v[n_modes] =
134  {
135  "in",
136  "out",
137  "app",
138  "ate",
139  "trunc",
140  "binary"
141  };
142 
143  std::string res;
144  for (int i = 0; i < n_modes; ++i) {
145  if (mode & mode_val_v[i]) {
146  res += (! res.empty()? "|" : "");
147  res += mode_name_v[i];
148  }
149  }
150 
151  if (res.empty()) {
152  res = "none";
153  }
154  return res;
155 }
156 
157 static void check_mode_( std::string const& filename, std::ios_base::openmode mode )
158 {
159  if ((mode & std::ios_base::trunc) && ! (mode & std::ios_base::out)) {
160  throw except::IOError(
161  std::string("Strict IO File Stream: open('") + filename + "'): mode error: trunc and not out",
162  filename
163  );
164  } else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out)) {
165  throw except::IOError(
166  std::string("Strict IO File Stream: open('") + filename + "'): mode error: app and not out",
167  filename
168  );
169  } else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app)) {
170  throw except::IOError(
171  std::string("Strict IO File Stream: open('") + filename + "'): mode error: trunc and app",
172  filename
173  );
174  }
175  }
176 
177 static void check_open_(std::ios * s_p, std::string const& filename, std::ios_base::openmode mode )
178 {
179  if (s_p->fail()) {
180  throw except::IOError(
181  std::string("Strict IO File Stream: open('") + filename + "'," + mode_to_string_(mode) +
182  "): open failed: " + strerror_(),
183  filename
184  );
185  }
186 }
187 
188 static void check_peek_(std::istream * is_p, std::string const& filename, std::ios_base::openmode mode )
189 {
190  bool peek_failed = true;
191  try {
192  is_p->peek();
193  peek_failed = is_p->fail();
194  } catch( std::ios_base::failure const& e ) {
195  // Nothing to do.
196  }
197  if( peek_failed ) {
198  throw except::IOError(
199  std::string("Strict IO File Stream: open('") + filename + "'," + mode_to_string_(mode) +
200  "): peek failed: " + strerror_(),
201  filename
202  );
203  }
204  is_p->clear();
205 }
206 
207 // ================================================================================================
208 // Strict File Streams
209 // ================================================================================================
210 
211 void StrictIFStream::open( std::string const& filename, std::ios_base::openmode mode )
212 {
213  mode |= std::ios_base::in;
214  exceptions(std::ios_base::badbit);
215  check_mode_(filename, mode);
216  std::ifstream::open(filename, mode);
217  check_open_(this, filename, mode);
218  check_peek_(this, filename, mode);
219 }
220 
221 void StrictOFStream::open( std::string const& filename, std::ios_base::openmode mode )
222 {
223  mode |= std::ios_base::out;
224  exceptions(std::ios_base::badbit);
225  check_mode_(filename, mode);
226  std::ofstream::open(filename, mode);
227  check_open_(this, filename, mode);
228 }
229 
230 void StrictFStream::open( std::string const& filename, std::ios_base::openmode mode )
231 {
232  if (! (mode & std::ios_base::out)) {
233  mode |= std::ios_base::in;
234  }
235  exceptions(std::ios_base::badbit);
236  check_mode_(filename, mode);
237  std::fstream::open(filename, mode);
238  check_open_(this, filename, mode);
239  check_peek_(this, filename, mode);
240 }
241 
242 } // namespace utils
243 } // namespace genesis
static std::string mode_to_string_(std::ios_base::openmode mode)
Exception class for general input/output errors.
Definition: exception.hpp:79
static void check_open_(std::ios *s_p, std::string const &filename, std::ios_base::openmode mode)
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::in)
void swap(SequenceSet &lhs, SequenceSet &rhs)
static void check_peek_(std::istream *is_p, std::string const &filename, std::ios_base::openmode mode)
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
static void check_mode_(std::string const &filename, std::ios_base::openmode mode)
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::in)
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::out)
static std::string strerror_()
Overload of error-reporting function, to enable use with VS.