11// Helpers for opening input and output files. If the file cannot be opened, an
2- // exception is thrown. For functions with "pipe", if the filename is "-", the
3- // standard input or output is used instead of actually opening a file.
2+ // exception is thrown. For functions with "pipe" in the name, if the filename
3+ // is "-", the standard input or output is used instead of actually opening a
4+ // file.
45
56#pragma once
67
1415
1516namespace
1617{
17- std::ifstream open_file_input (const std::string & filename)
18+ // We would like to just return a std::ifstream here, but that does not
19+ // work in GCC 4.9.2 for Raspbian/Debian Jessie.
20+ void open_file_input (const std::string & filename, std::ifstream & file)
1821 {
19- std::ifstream file (filename);
22+ file. open (filename);
2023 if (!file)
2124 {
2225 int error_code = errno;
2326 throw std::runtime_error (filename + " : " + strerror (error_code) + " ." );
2427 }
25- return file;
2628 }
2729
2830 std::shared_ptr<std::istream> open_file_or_pipe_input (const std::string & filename)
@@ -35,21 +37,22 @@ namespace
3537 else
3638 {
3739 std::ifstream * concrete_file = new std::ifstream ();
38- *concrete_file = open_file_input (filename);
40+ open_file_input (filename, *concrete_file );
3941 file.reset (concrete_file);
4042 }
4143 return file;
4244 }
4345
44- std::ofstream open_file_output (const std::string & filename)
46+ // We would like to just return a std::ifstream here, but that does not
47+ // work in GCC 4.9.2 for Raspbian/Debian Jessie.
48+ void open_file_output (const std::string & filename, std::ofstream & file)
4549 {
46- std::ofstream file (filename);
50+ file. open (filename);
4751 if (!file)
4852 {
4953 int error_code = errno;
5054 throw std::runtime_error (filename + " : " + strerror (error_code) + " ." );
5155 }
52- return file;
5356 }
5457
5558 std::shared_ptr<std::ostream> open_file_or_pipe_output (const std::string & filename)
@@ -62,7 +65,7 @@ namespace
6265 else
6366 {
6467 std::ofstream * concrete_file = new std::ofstream ();
65- *concrete_file = open_file_output (filename);
68+ open_file_output (filename, *concrete_file );
6669 file.reset (concrete_file);
6770 }
6871 return file;
@@ -71,9 +74,10 @@ namespace
7174 inline std::string write_string_to_file (const std::string & filename,
7275 const std::string & contents)
7376 {
74- auto stream = open_file_output (filename);
75- stream << contents;
76- if (stream.fail ())
77+ std::ofstream file;
78+ open_file_output (filename, file);
79+ file << contents;
80+ if (file.fail ())
7781 {
7882 throw std::runtime_error (" Failed to write to file." );
7983 }
@@ -92,13 +96,14 @@ namespace
9296
9397 inline std::string read_string_from_file (const std::string & filename)
9498 {
95- auto stream = open_file_input (filename);
99+ std::ifstream file;
100+ open_file_input (filename, file);
96101 std::string contents =
97102 std::string (
98- std::istreambuf_iterator<char >(stream ),
103+ std::istreambuf_iterator<char >(file ),
99104 std::istreambuf_iterator<char >()
100105 );
101- if (stream .fail ())
106+ if (file .fail ())
102107 {
103108 throw std::runtime_error (" Failed to read from file." );
104109 }
0 commit comments