A library for working with phylogenetic and population genetic data.
v0.27.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(512, '\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  return std::string( buff.c_str() );
113 }
114 
115 // ================================================================================================
116 // Internal Helper Functions
117 // ================================================================================================
118 
119 static std::string mode_to_string_( std::ios_base::openmode mode )
120 {
121  static const int n_modes = 6;
122  static const std::ios_base::openmode mode_val_v[n_modes] =
123  {
124  std::ios_base::in,
125  std::ios_base::out,
126  std::ios_base::app,
127  std::ios_base::ate,
128  std::ios_base::trunc,
129  std::ios_base::binary
130  };
131 
132  static char const* mode_name_v[n_modes] =
133  {
134  "in",
135  "out",
136  "app",
137  "ate",
138  "trunc",
139  "binary"
140  };
141 
142  std::string res;
143  for (int i = 0; i < n_modes; ++i) {
144  if (mode & mode_val_v[i]) {
145  res += (! res.empty()? "|" : "");
146  res += mode_name_v[i];
147  }
148  }
149 
150  if (res.empty()) {
151  res = "none";
152  }
153  return res;
154 }
155 
156 static void check_mode_( std::string const& filename, std::ios_base::openmode mode )
157 {
158  if ((mode & std::ios_base::trunc) && ! (mode & std::ios_base::out)) {
159  throw except::IOError(
160  std::string("Strict IO File Stream: open('") + filename + "'): mode error: trunc and not out",
161  filename
162  );
163  } else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out)) {
164  throw except::IOError(
165  std::string("Strict IO File Stream: open('") + filename + "'): mode error: app and not out",
166  filename
167  );
168  } else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app)) {
169  throw except::IOError(
170  std::string("Strict IO File Stream: open('") + filename + "'): mode error: trunc and app",
171  filename
172  );
173  }
174  }
175 
176 static void check_open_(std::ios * s_p, std::string const& filename, std::ios_base::openmode mode )
177 {
178  if (s_p->fail()) {
179  throw except::IOError(
180  std::string("Strict IO File Stream: open('") + filename + "'," + mode_to_string_(mode) +
181  "): open failed: " + strerror_(),
182  filename
183  );
184  }
185 }
186 
187 static void check_peek_(std::istream * is_p, std::string const& filename, std::ios_base::openmode mode )
188 {
189  bool peek_failed = true;
190  try {
191  is_p->peek();
192  peek_failed = is_p->fail();
193  } catch( std::ios_base::failure const& e ) {
194  // Nothing to do.
195  }
196  if( peek_failed ) {
197  throw except::IOError(
198  std::string("Strict IO File Stream: open('") + filename + "'," + mode_to_string_(mode) +
199  "): peek failed: " + strerror_(),
200  filename
201  );
202  }
203  is_p->clear();
204 }
205 
206 // ================================================================================================
207 // Strict File Streams
208 // ================================================================================================
209 
210 void StrictIFStream::open( std::string const& filename, std::ios_base::openmode mode )
211 {
212  mode |= std::ios_base::in;
213  exceptions(std::ios_base::badbit);
214  check_mode_(filename, mode);
215  std::ifstream::open(filename, mode);
216  check_open_(this, filename, mode);
217  check_peek_(this, filename, mode);
218 }
219 
220 void StrictOFStream::open( std::string const& filename, std::ios_base::openmode mode )
221 {
222  mode |= std::ios_base::out;
223  exceptions(std::ios_base::badbit);
224  check_mode_(filename, mode);
225  std::ofstream::open(filename, mode);
226  check_open_(this, filename, mode);
227 }
228 
229 void StrictFStream::open( std::string const& filename, std::ios_base::openmode mode )
230 {
231  if (! (mode & std::ios_base::out)) {
232  mode |= std::ios_base::in;
233  }
234  exceptions(std::ios_base::badbit);
235  check_mode_(filename, mode);
236  std::fstream::open(filename, mode);
237  check_open_(this, filename, mode);
238  check_peek_(this, filename, mode);
239 }
240 
241 } // namespace utils
242 } // namespace genesis
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::utils::check_peek_
static void check_peek_(std::istream *is_p, std::string const &filename, std::ios_base::openmode mode)
Definition: strict_fstream.cpp:187
genesis::utils::check_open_
static void check_open_(std::ios *s_p, std::string const &filename, std::ios_base::openmode mode)
Definition: strict_fstream.cpp:176
genesis::utils::StrictFStream::open
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::in)
Definition: strict_fstream.cpp:229
genesis::utils::mode_to_string_
static std::string mode_to_string_(std::ios_base::openmode mode)
Definition: strict_fstream.cpp:119
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::StrictIFStream::open
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::in)
Definition: strict_fstream.cpp:210
genesis::utils::StrictOFStream::open
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::out)
Definition: strict_fstream.cpp:220
strict_fstream.hpp
exception.hpp
genesis::utils::strerror_
static std::string strerror_()
Overload of error-reporting function, to enable use with VS.
Definition: strict_fstream.cpp:86
genesis::utils::check_mode_
static void check_mode_(std::string const &filename, std::ios_base::openmode mode)
Definition: strict_fstream.cpp:156
genesis::except::IOError
Exception class for general input/output errors.
Definition: exception.hpp:79