Skip to content

Commit

Permalink
ord: Redirect writeDb file opening exception to ORD.
Browse files Browse the repository at this point in the history
Also add open method to strean handler so it can be instanciated outside the try
block where file opening is done.

Signed-off-by: Christian Costa <[email protected]>
  • Loading branch information
titan73 committed Nov 10, 2024
1 parent e6917a7 commit a6d624f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/OpenRoad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,12 @@ void OpenRoad::writeDb(std::ostream& stream)

void OpenRoad::writeDb(const char* filename)
{
utl::StreamHandler stream_handler(filename, true);

utl::StreamHandler stream_handler;
try {
stream_handler.open(filename, true);
} catch (const std::ios_base::failure& f) {
logger_->error(ORD, 56, "{}", f.what());
}
db_->write(stream_handler.getStream());
}

Expand Down
3 changes: 3 additions & 0 deletions src/utl/include/utl/ScopedTemporaryFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ class ScopedTemporaryFile
class StreamHandler
{
public:
StreamHandler() = default;
// Set binary to true to open in binary mode
StreamHandler(const char* filename, bool binary = false);
~StreamHandler();
std::ofstream& getStream();
// Set binary to true to open in binary mode
void open(const char* filename, bool binary = false);

private:
std::string filename_;
Expand Down
31 changes: 19 additions & 12 deletions src/utl/src/ScopedTemporaryFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,25 @@ ScopedTemporaryFile::~ScopedTemporaryFile()
}

StreamHandler::StreamHandler(const char* filename, bool binary)
: filename_(filename)
{
tmp_filename_ = generate_unused_filename(filename_);
open(filename, binary);
}

StreamHandler::~StreamHandler()
{
if (!filename_.empty()) {
if (os_.is_open()) {
// Any pending output sequence is written to the file.
os_.close();
}
// If filename_ exists it will be overwritten
fs::rename(tmp_filename_, filename_);
}
}

void StreamHandler::open(const char* filename, bool binary)
{
tmp_filename_ = generate_unused_filename(filename);

os_.exceptions(std::ofstream::failbit | std::ofstream::badbit);
std::ios_base::openmode mode = std::ios_base::out | std::ios::trunc;
Expand All @@ -66,16 +82,7 @@ StreamHandler::StreamHandler(const char* filename, bool binary)
std::throw_with_nested(std::ios_base::failure(error + " (failed to open '"
+ tmp_filename_ + "')"));
}
}

StreamHandler::~StreamHandler()
{
if (os_.is_open()) {
// Any pending output sequence is written to the file.
os_.close();
}
// If filename_ exists it will be overwritten
fs::rename(tmp_filename_, filename_);
filename_ = filename;
}

std::ofstream& StreamHandler::getStream()
Expand Down

0 comments on commit a6d624f

Please sign in to comment.