ENH: Added default constructor and Open method.

This commit is contained in:
Brad King 2005-01-26 14:25:16 -05:00
parent f6cd83d6f3
commit aaac6f2c3a
2 changed files with 71 additions and 2 deletions

View File

@ -26,6 +26,12 @@
# include <sys/stat.h>
#endif
//----------------------------------------------------------------------------
cmGeneratedFileStream::cmGeneratedFileStream():
cmGeneratedFileStreamBase(), Stream()
{
}
//----------------------------------------------------------------------------
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
cmGeneratedFileStreamBase(name),
@ -50,12 +56,40 @@ cmGeneratedFileStream::~cmGeneratedFileStream()
m_Okay = (*this)?true:false;
}
//----------------------------------------------------------------------------
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),
@ -91,6 +125,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)

View File

@ -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.
@ -78,6 +91,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.
*/