From b8fd8290709a9f8245286ab93ae0f4e5a635f010 Mon Sep 17 00:00:00 2001 From: Christian Costa Date: Sun, 10 Nov 2024 14:00:48 +0100 Subject: [PATCH] ord: Redirect writeDb file opening exception to ORD. 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 --- src/OpenRoad.cc | 8 ++++-- src/utl/include/utl/ScopedTemporaryFile.h | 3 +++ src/utl/src/ScopedTemporaryFile.cpp | 31 ++++++++++++++--------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/OpenRoad.cc b/src/OpenRoad.cc index fc46d46587f..d942824c53c 100644 --- a/src/OpenRoad.cc +++ b/src/OpenRoad.cc @@ -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()); } diff --git a/src/utl/include/utl/ScopedTemporaryFile.h b/src/utl/include/utl/ScopedTemporaryFile.h index c2231538b26..661426fef94 100644 --- a/src/utl/include/utl/ScopedTemporaryFile.h +++ b/src/utl/include/utl/ScopedTemporaryFile.h @@ -70,10 +70,13 @@ class ScopedTemporaryFile class StreamHandler { public: + StreamHandler(void) {} // 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_; diff --git a/src/utl/src/ScopedTemporaryFile.cpp b/src/utl/src/ScopedTemporaryFile.cpp index e7791b6bead..0432c1166bd 100644 --- a/src/utl/src/ScopedTemporaryFile.cpp +++ b/src/utl/src/ScopedTemporaryFile.cpp @@ -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; @@ -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()