From 389f24f777b790b12759184c96c4690a771f941b Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 5 Apr 2005 08:25:21 -0400 Subject: ENH: Added Close method and updated Open method to allow streams to be reused. --- Source/cmGeneratedFileStream.cxx | 52 +++++++++++++++++++++++++++------------- Source/cmGeneratedFileStream.h | 22 +++++++++++++---- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/Source/cmGeneratedFileStream.cxx b/Source/cmGeneratedFileStream.cxx index 2b2e940..8aa51f7 100644 --- a/Source/cmGeneratedFileStream.cxx +++ b/Source/cmGeneratedFileStream.cxx @@ -80,6 +80,22 @@ cmGeneratedFileStream::Open(const char* name, bool quiet) } //---------------------------------------------------------------------------- +cmGeneratedFileStream& +cmGeneratedFileStream::Close() +{ + // Save whether the temporary output file is valid before closing. + m_Okay = (*this)?true:false; + + // Close the temporary output file. + this->Stream::close(); + + // Remove the temporary file (possibly by renaming to the real file). + this->cmGeneratedFileStreamBase::Close(); + + return *this; +} + +//---------------------------------------------------------------------------- void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different) { m_CopyIfDifferent = copy_if_different; @@ -103,13 +119,29 @@ cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(): //---------------------------------------------------------------------------- cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name): - m_Name(name), - m_TempName(name), + m_Name(), + m_TempName(), m_CopyIfDifferent(false), m_Okay(false), m_Compress(false) { + this->Open(name); +} + +//---------------------------------------------------------------------------- +cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase() +{ + this->Close(); +} + +//---------------------------------------------------------------------------- +void cmGeneratedFileStreamBase::Open(const char* name) +{ + // Save the original name of the file. + m_Name = name; + // Create the name of the temporary file. + m_TempName = name; m_TempName += ".tmp"; // Make sure the temporary file that will be used is not present. @@ -117,7 +149,7 @@ cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name): } //---------------------------------------------------------------------------- -cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase() +void cmGeneratedFileStreamBase::Close() { std::string resname = m_Name; if ( m_Compress ) @@ -155,20 +187,6 @@ cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase() } //---------------------------------------------------------------------------- -void cmGeneratedFileStreamBase::Open(const char* name) -{ - // Save the original name of the file. - m_Name = name; - - // Create the name of the temporary file. - m_TempName = name; - m_TempName += ".tmp"; - - // Make sure the temporary file that will be used is not present. - cmSystemTools::RemoveFile(m_TempName.c_str()); -} - -//---------------------------------------------------------------------------- #ifdef CMAKE_BUILD_WITH_CMAKE int cmGeneratedFileStreamBase::CompressFile(const char* oldname, const char* newname) diff --git a/Source/cmGeneratedFileStream.h b/Source/cmGeneratedFileStream.h index e9b1bb7..90ea51e 100644 --- a/Source/cmGeneratedFileStream.h +++ b/Source/cmGeneratedFileStream.h @@ -35,8 +35,12 @@ protected: // The destructor renames the temporary output file to the real name. ~cmGeneratedFileStreamBase(); - // Internal method to setup the instance for a given file name. + // Internal methods to handle the temporary file. Open is always + // called before the real stream is opened. Close is always called + // after the real stream is closed and m_Okay is set to whether the + // real stream was still valid for writing when it was closed. void Open(const char* name); + void Close(); // Internal file replacement implementation. int RenameFile(const char* oldname, const char* newname); @@ -53,7 +57,7 @@ protected: // Whether to do a copy-if-different. bool m_CopyIfDifferent; - // Whether the destination file should be replaced. + // Whether the real file stream was valid when it was closed. bool m_Okay; // Whether the destionation file is compressed @@ -99,13 +103,21 @@ public: /** * Open an output file by name. This should be used only with a - * default-constructed stream. It automatically generates a name - * for the temporary file. If the file cannot be opened an error - * message is produced unless the second argument is set to true. + * non-open stream. It automatically generates a name for the + * temporary file. If the file cannot be opened an error message is + * produced unless the second argument is set to true. */ cmGeneratedFileStream& Open(const char* name, bool quiet=false); /** + * Close the output file. This should be used only with an open + * stream. The temporary file is atomically renamed to the + * destionation file if the stream is still valid when this method + * is called. + */ + cmGeneratedFileStream& Close(); + + /** * Set whether copy-if-different is done. */ void SetCopyIfDifferent(bool copy_if_different); -- cgit v0.12