A library for working with phylogenetic and population genetic data.
v0.27.0
output_stream.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_OUTPUT_STREAM_H_
2 #define GENESIS_UTILS_IO_OUTPUT_STREAM_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2021 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
37 
38 #include <fstream>
39 #include <sstream>
40 #include <stdexcept>
41 #include <string>
42 
43 namespace genesis {
44 namespace utils {
45 
46 // =================================================================================================
47 // Output Stream
48 // =================================================================================================
49 
69 inline void file_output_stream(
70  std::string const& filename,
71  std::ofstream& out_stream,
72  std::ios_base::openmode mode = std::ios_base::out,
73  bool create_dirs = true
74 ) {
75  if( ! Options::get().allow_file_overwriting() && utils::file_exists( filename ) ) {
77  "File '" + filename + "' already exists. If you want to allow overwriting of existing "
78  "files, activate genesis::utils::Options::get().allow_file_overwriting() first.",
79  filename
80  );
81  }
82 
83  if( create_dirs ) {
84  auto const path = file_path( filename );
85  dir_create( path );
86  }
87 
88  out_stream.open( filename, mode );
89  if( out_stream.fail() ) {
90  throw std::runtime_error( "Cannot write to file '" + filename + "'." );
91  }
92 }
93 
94 } // namespace utils
95 } // namespace genesis
96 
97 #endif // include guard
fs.hpp
Provides functions for accessing the file system.
genesis::utils::dir_create
void dir_create(std::string const &path, bool with_parents)
Create a directory.
Definition: fs.cpp:192
genesis::utils::file_path
std::string file_path(std::string const &filename)
Return the path leading to a file.
Definition: fs.cpp:675
genesis::tree::path
utils::Range< IteratorPath< true > > path(ElementType const &start, ElementType const &finish)
Definition: path.hpp:337
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
options.hpp
exception.hpp
genesis::utils::file_exists
bool file_exists(std::string const &filename)
Return true iff the file exists.
Definition: fs.cpp:78
genesis::except::ExistingFileError
Exception class that is thrown if trying to write to an existing file.
Definition: exception.hpp:109
genesis::utils::Options::get
static Options & get()
Returns a single instance of this class.
Definition: options.hpp:60
genesis::utils::file_output_stream
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out, bool create_dirs=true)
Helper function to obtain an output stream to a file.
Definition: output_stream.hpp:69