1 #ifndef GENESIS_UTILS_IO_FILE_INPUT_SOURCE_H_
2 #define GENESIS_UTILS_IO_FILE_INPUT_SOURCE_H_
72 : file_name_( file_name )
76 throw std::runtime_error(
77 "Cannot open input file '" + file_name +
"': " + err_str
88 , file_name_( file_name )
91 std::setvbuf( file_,
nullptr, _IONBF, 0 );
105 std::fclose( file_ );
118 size_t read_(
char* buffer,
size_t size )
override
129 if( file_ ==
nullptr ) {
131 file_ = std::fopen( file_name_.c_str(),
"rb" );
133 if( file_ ==
nullptr ) {
134 throw std::runtime_error(
135 "Cannot open input file '" + file_name_ +
"': " +
136 std::string( strerror( errno ))
141 std::setvbuf( file_,
nullptr, _IONBF, 0 );
146 size_t const ret = std::fread( buffer, 1, size, file_ );
147 if( std::ferror( file_ ) ) {
148 assert( ! std::feof( file_ ));
149 throw std::runtime_error(
150 "Cannot read from input file '" + file_name_ +
"': " +
151 std::string( strerror( errno ))
158 assert( std::feof( file_ ));
159 assert( !std::ferror( file_ ));
160 std::fclose( file_ );
171 std::string source_name_()
const override
173 return "input file (" + file_name_ +
")";
179 std::string source_string_()
const override
188 FILE* file_ =
nullptr;
189 std::string file_name_;
190 bool finished_ =
false;
196 #endif // include guard