From aaac6f2c3ac9e9f95f0fdad7d77b4de21568ed52 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 26 Jan 2005 14:25:16 -0500 Subject: ENH: Added default constructor and Open method. --- Source/cmGeneratedFileStream.cxx | 48 ++++++++++++++++++++++++++++++++++++++++ Source/cmGeneratedFileStream.h | 25 +++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/Source/cmGeneratedFileStream.cxx b/Source/cmGeneratedFileStream.cxx index 254b6bf..53f3077 100644 --- a/Source/cmGeneratedFileStream.cxx +++ b/Source/cmGeneratedFileStream.cxx @@ -27,6 +27,12 @@ #endif //---------------------------------------------------------------------------- +cmGeneratedFileStream::cmGeneratedFileStream(): + cmGeneratedFileStreamBase(), Stream() +{ +} + +//---------------------------------------------------------------------------- cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet): cmGeneratedFileStreamBase(name), Stream(m_TempName.c_str()) @@ -51,12 +57,40 @@ cmGeneratedFileStream::~cmGeneratedFileStream() } //---------------------------------------------------------------------------- +cmGeneratedFileStream& +cmGeneratedFileStream::Open(const char* name, bool quiet) +{ + // Store the file name and construct the temporary file name. + this->cmGeneratedFileStreamBase::Open(name); + + // Open the temporary output file. + this->Stream::open(m_TempName.c_str()); + + // Check if the file opened. + if(!*this && !quiet) + { + cmSystemTools::Error("Cannot open file for write: ", m_TempName.c_str()); + cmSystemTools::ReportLastSystemError(""); + } + return *this; +} + +//---------------------------------------------------------------------------- void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different) { m_CopyIfDifferent = copy_if_different; } //---------------------------------------------------------------------------- +cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(): + m_Name(), + m_TempName(), + m_CopyIfDifferent(false), + m_Okay(false) +{ +} + +//---------------------------------------------------------------------------- cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name): m_Name(name), m_TempName(name), @@ -92,6 +126,20 @@ 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()); +} + +//---------------------------------------------------------------------------- int cmGeneratedFileStreamBase::RenameFile(const char* oldname, const char* newname) { diff --git a/Source/cmGeneratedFileStream.h b/Source/cmGeneratedFileStream.h index 7430ef9..ff5f228 100644 --- a/Source/cmGeneratedFileStream.h +++ b/Source/cmGeneratedFileStream.h @@ -25,12 +25,19 @@ class cmGeneratedFileStreamBase { protected: - // The constructor prepares the temporary output file. + // This constructor does not prepare the temporary file. The open + // method must be used. + cmGeneratedFileStreamBase(); + + // This constructor prepares the temporary output file. cmGeneratedFileStreamBase(const char* name); // The destructor renames the temporary output file to the real name. ~cmGeneratedFileStreamBase(); + // Internal method to setup the instance for a given file name. + void Open(const char* name); + // Internal file replacement implementation. int RenameFile(const char* oldname, const char* newname); @@ -64,7 +71,13 @@ public: typedef std::ofstream Stream; /** - * The constructor takes the name of the file to be generated. It + * This constructor prepares a default stream. The open method must + * be used before writing to the stream. + */ + cmGeneratedFileStream(); + + /** + * This constructor takes the name of the file to be generated. 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. @@ -79,6 +92,14 @@ public: ~cmGeneratedFileStream(); /** + * 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. + */ + cmGeneratedFileStream& Open(const char* name, bool quiet=false); + + /** * Set whether copy-if-different is done. */ void SetCopyIfDifferent(bool copy_if_different); -- cgit v0.12