ENH: Added Close method and updated Open method to allow streams to be reused.

This commit is contained in:
Brad King 2005-04-05 08:25:21 -04:00
parent 2681215256
commit 389f24f777
2 changed files with 52 additions and 22 deletions

View File

@ -79,6 +79,22 @@ cmGeneratedFileStream::Open(const char* name, bool quiet)
return *this; return *this;
} }
//----------------------------------------------------------------------------
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) void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
{ {
@ -103,13 +119,29 @@ cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name): cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
m_Name(name), m_Name(),
m_TempName(name), m_TempName(),
m_CopyIfDifferent(false), m_CopyIfDifferent(false),
m_Okay(false), m_Okay(false),
m_Compress(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. // Create the name of the temporary file.
m_TempName = name;
m_TempName += ".tmp"; m_TempName += ".tmp";
// Make sure the temporary file that will be used is not present. // 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; std::string resname = m_Name;
if ( m_Compress ) if ( m_Compress )
@ -154,20 +186,6 @@ cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
cmSystemTools::RemoveFile(m_TempName.c_str()); cmSystemTools::RemoveFile(m_TempName.c_str());
} }
//----------------------------------------------------------------------------
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 #ifdef CMAKE_BUILD_WITH_CMAKE
int cmGeneratedFileStreamBase::CompressFile(const char* oldname, int cmGeneratedFileStreamBase::CompressFile(const char* oldname,

View File

@ -35,8 +35,12 @@ protected:
// The destructor renames the temporary output file to the real name. // The destructor renames the temporary output file to the real name.
~cmGeneratedFileStreamBase(); ~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 Open(const char* name);
void Close();
// Internal file replacement implementation. // Internal file replacement implementation.
int RenameFile(const char* oldname, const char* newname); int RenameFile(const char* oldname, const char* newname);
@ -53,7 +57,7 @@ protected:
// Whether to do a copy-if-different. // Whether to do a copy-if-different.
bool m_CopyIfDifferent; bool m_CopyIfDifferent;
// Whether the destination file should be replaced. // Whether the real file stream was valid when it was closed.
bool m_Okay; bool m_Okay;
// Whether the destionation file is compressed // Whether the destionation file is compressed
@ -99,12 +103,20 @@ public:
/** /**
* Open an output file by name. This should be used only with a * Open an output file by name. This should be used only with a
* default-constructed stream. It automatically generates a name * non-open stream. It automatically generates a name for the
* for the temporary file. If the file cannot be opened an error * temporary file. If the file cannot be opened an error message is
* message is produced unless the second argument is set to true. * produced unless the second argument is set to true.
*/ */
cmGeneratedFileStream& Open(const char* name, bool quiet=false); 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. * Set whether copy-if-different is done.
*/ */