A library for working with phylogenetic and population genetic data.
v0.32.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-2023 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 <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
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 <cerrno>
70 #include <cstring>
71 #include <fstream>
72 #include <stdexcept>
73 #include <string>
74 
75 namespace genesis {
76 namespace utils {
77 
78 // ================================================================================================
79 // Error Reporting
80 // ================================================================================================
81 
87 static std::string strerror_()
88 {
89  std::string buff(512, '\0');
90 
91  #ifdef _WIN32
92 
93  if( strerror_s( &buff[0], buff.size(), errno ) != 0 ) {
94  buff = "Unknown error";
95  }
96 
97  #elif ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE) || defined(__APPLE__)
98 
99  // XSI-compliant strerror_r()
100  if( strerror_r( errno, &buff[0], buff.size() ) != 0 ) {
101  buff = "Unknown error";
102  }
103 
104  #else
105 
106  // GNU-specific strerror_r()
107  auto p = strerror_r(errno, &buff[0], buff.size());
108  std::string tmp(p, std::strlen( p ));
109  std::swap(buff, tmp);
110 
111  #endif
112 
113  return std::string( buff.c_str() );
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 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 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 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 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 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  errno = 0;
214  mode |= std::ios_base::in;
215  exceptions(std::ios_base::badbit);
216  check_mode_(filename, mode);
217  std::ifstream::open(filename, mode);
218  check_open_(this, filename, mode);
219  check_peek_(this, filename, mode);
220 }
221 
222 void StrictOFStream::open( std::string const& filename, std::ios_base::openmode mode )
223 {
224  errno = 0;
225  mode |= std::ios_base::out;
226  exceptions(std::ios_base::badbit);
227  check_mode_(filename, mode);
228  std::ofstream::open(filename, mode);
229  check_open_(this, filename, mode);
230 }
231 
232 void StrictFStream::open( std::string const& filename, std::ios_base::openmode mode )
233 {
234  errno = 0;
235  if (! (mode & std::ios_base::out)) {
236  mode |= std::ios_base::in;
237  }
238  exceptions(std::ios_base::badbit);
239  check_mode_(filename, mode);
240  std::fstream::open(filename, mode);
241  check_open_(this, filename, mode);
242  check_peek_(this, filename, mode);
243 }
244 
245 } // namespace utils
246 } // 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:188
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:177
genesis::utils::StrictFStream::open
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::in)
Definition: strict_fstream.cpp:232
genesis::utils::mode_to_string_
static std::string mode_to_string_(std::ios_base::openmode mode)
Definition: strict_fstream.cpp:120
genesis::utils::IOError
Exception class for general input/output errors.
Definition: exception.hpp:79
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:211
genesis::utils::StrictOFStream::open
void open(std::string const &filename, std::ios_base::openmode mode=std::ios_base::out)
Definition: strict_fstream.cpp:222
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:87
genesis::utils::check_mode_
static void check_mode_(std::string const &filename, std::ios_base::openmode mode)
Definition: strict_fstream.cpp:157