1 #ifndef GENESIS_UTILS_IO_FILE_INPUT_SOURCE_H_
2 #define GENESIS_UTILS_IO_FILE_INPUT_SOURCE_H_
71 : file_name_( file_name )
74 throw std::runtime_error(
"File does not exist or is not readable: " + file_name );
84 , file_name_( file_name )
87 std::setvbuf( file_,
nullptr, _IONBF, 0 );
107 size_t read_(
char* buffer,
size_t size )
override
118 if( file_ ==
nullptr ) {
120 file_ = std::fopen( file_name_.c_str(),
"rb" );
122 if( file_ ==
nullptr ) {
123 throw std::runtime_error(
124 "Cannot open file " + file_name_ +
": " + std::string( strerror( errno ))
129 std::setvbuf( file_,
nullptr, _IONBF, 0 );
133 size_t const ret = std::fread( buffer, 1, size, file_ );
134 if( std::ferror( file_ ) ) {
135 throw std::runtime_error(
"Cannot read from file: " + file_name_ );
141 std::fclose( file_ );
152 std::string source_name_()
const override
154 return "input file (" + file_name_ +
")";
160 std::string source_string_()
const override
169 FILE* file_ =
nullptr;
170 std::string file_name_;
171 bool finished_ =
false;
177 #endif // include guard