Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
file_utilities.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/io/file_utilities.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/io/file_utilities.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/io/file_utilities.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2021 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// silviana amethyst, university of wisconsin-eau claire
24
30
31#pragma once
32#include <iostream>
33#include <fstream>
34#include <boost/filesystem.hpp>
35
36namespace bertini{
37
38 namespace fs = boost::filesystem;
39
40 using Path = fs::path;
41
42 using ifstream = std::ifstream;
46 inline
47 void OpenInFileThrowIfFail(ifstream & in,
48 Path const& input_path)
49 {
50 using namespace fs;
51 try{
52 if (exists(input_path))
53 {
54 if (is_directory(input_path))
55 {
56 std::stringstream err_msg;
57 err_msg << "attempting to open file but is a directory, of name '" << input_path.string() << "'";
58 throw std::runtime_error(err_msg.str());
59 }
60 else
61 {
62 in.open(input_path.c_str());
63 if (!in.is_open())
64 {
65 std::stringstream err_msg;
66 err_msg << "file '" << input_path.string() << "' hypothetically exists, but failed to open correctly";
67 throw std::runtime_error(err_msg.str());
68 }
69 }
70 }
71 else
72 {
73 std::stringstream err_msg;
74 err_msg << "attempting to open file which doesn't exist, of name '" << input_path.string() << "'";
75 throw std::runtime_error(err_msg.str());
76 }
77 }
78 catch (const filesystem_error& ex)
79 {
80 std::stringstream err_msg;
81 err_msg << "boost::filesystem throw while attempting to open file '" << input_path.string() << "': '" << ex.what() << "'";
82 throw std::runtime_error(err_msg.str());
83 }
84 }
85
92 inline
93 std::string FileToString(Path const& input_path)
94 {
95 ifstream infile;
96 OpenInFileThrowIfFail(infile, input_path);
97 return std::string ( std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>() );
98 }
99}
100
void OpenInFileThrowIfFail(ifstream &in, Path const &input_path)
Try to open a file, and throw if it doesn't exist, or is a directory.
Definition file_utilities.hpp:47
std::string FileToString(Path const &input_path)
Read an entire file into a string.
Definition file_utilities.hpp:93