Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
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) 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
27
28#pragma once
29#include <iostream>
30#include <fstream>
31#include <sstream>
32#include <boost/filesystem.hpp>
33
34namespace bertini{
35
36 namespace fs = boost::filesystem;
37
38 using Path = fs::path;
39
40 using ifstream = std::ifstream;
44 inline
46 Path const& input_path)
47 {
48 using namespace fs;
49 try{
50 if (exists(input_path))
51 {
52 if (is_directory(input_path))
53 {
54 std::stringstream err_msg;
55 err_msg << "attempting to open file but is a directory, of name '" << input_path.string() << "'";
56 throw std::runtime_error(err_msg.str());
57 }
58 else
59 {
60 in.open(input_path.c_str());
61 if (!in.is_open())
62 {
63 std::stringstream err_msg;
64 err_msg << "file '" << input_path.string() << "' hypothetically exists, but failed to open correctly";
65 throw std::runtime_error(err_msg.str());
66 }
67 }
68 }
69 else
70 {
71 std::stringstream err_msg;
72 err_msg << "attempting to open file which doesn't exist, of name '" << input_path.string() << "'";
73 throw std::runtime_error(err_msg.str());
74 }
75 }
76 catch (const filesystem_error& ex)
77 {
78 std::stringstream err_msg;
79 err_msg << "boost::filesystem throw while attempting to open file '" << input_path.string() << "': '" << ex.what() << "'";
80 throw std::runtime_error(err_msg.str());
81 }
82 }
83
90 inline
91 std::string FileToString(Path const& input_path)
92 {
93 ifstream infile;
94 OpenInFileThrowIfFail(infile, input_path);
95 std::istreambuf_iterator<char> file_begin(infile), file_end;
96 std::string contents(file_begin, file_end);
97
98 // Treat file contents as UTF-8; drop a leading byte-order mark (EF BB BF)
99 // so it is not parsed as a stray leading character.
100 if (contents.size() >= 3 &&
101 static_cast<unsigned char>(contents[0]) == 0xEF &&
102 static_cast<unsigned char>(contents[1]) == 0xBB &&
103 static_cast<unsigned char>(contents[2]) == 0xBF)
104 {
105 contents.erase(0, 3);
106 }
107
108 return contents;
109 }
110}
Definition algorithm_builder.hpp:36
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:45
std::string FileToString(Path const &input_path)
Read an entire file into a string.
Definition file_utilities.hpp:91
fs::path Path
Shorthand for a filesystem path.
Definition file_utilities.hpp:38
std::ifstream ifstream
Shorthand for an input file stream.
Definition file_utilities.hpp:40