Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File handling changes #667

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/realizations/catchment/Bmi_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ namespace models {
{
// This replicates a lot of Initialize, but it's necessary to be able to do it separately to support
// "initializing" on construction, given using Initialize requires use of virtual functions
errno = 0;
if (!utils::FileChecker::file_is_readable(this->bmi_init_config)) {
init_exception_msg = "Cannot create and initialize " + this->model_name + " using unreadable file '"
+ this->bmi_init_config + "'";
+ this->bmi_init_config + "'. Error: "+std::strerror(errno);
throw std::runtime_error(init_exception_msg);
}
}
Expand Down Expand Up @@ -164,6 +165,7 @@ namespace models {
void Initialize() {
// If there was previous init attempt but w/ failure exception, throw runtime error and include previous
// message
errno = 0;
if (model_initialized && !init_exception_msg.empty()) {
throw std::runtime_error(
"Previous " + model_name + " init attempt had exception: \n\t" + init_exception_msg);
Expand All @@ -174,7 +176,7 @@ namespace models {
}
else if (!utils::FileChecker::file_is_readable(bmi_init_config)) {
init_exception_msg = "Cannot initialize " + model_name + " using unreadable file '"
+ bmi_init_config + "'";
+ bmi_init_config + "'. Error: "+std::strerror(errno);;
throw std::runtime_error(init_exception_msg);
}
else {
Expand Down
19 changes: 16 additions & 3 deletions include/utilities/FileStreamHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ namespace utils
class FileStreamHandler : public StreamHandler
{
public:
FileStreamHandler(const char* path) : StreamHandler()
FileStreamHandler(const char* path) : StreamHandler(), path(path)
{
auto stream = std::make_shared<std::ofstream>();
stream->open(path, std::ios::trunc);
stream = std::make_shared<std::ofstream>();
stream->open(path, std::ios::trunc); //clear any existing data
stream->close(); //avoid too many open files
output_stream = stream;
}
virtual ~FileStreamHandler(){}

template<class DataType> std::ostream& operator<<(const DataType& val)
{
if( !stream->is_open() )
stream->open(path, std::ios_base::app);
put(val);
stream->close();
return *output_stream;
}
private:
std::string path;
std::shared_ptr<std::ofstream> stream;
};

}
Expand Down