Browse Source

ENH: Added default constructor and Open method.

Brad King 21 years ago
parent
commit
aaac6f2c3a
2 changed files with 71 additions and 2 deletions
  1. 48 0
      Source/cmGeneratedFileStream.cxx
  2. 23 2
      Source/cmGeneratedFileStream.h

+ 48 - 0
Source/cmGeneratedFileStream.cxx

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

+ 23 - 2
Source/cmGeneratedFileStream.h

@@ -25,12 +25,19 @@
 class cmGeneratedFileStreamBase
 class cmGeneratedFileStreamBase
 {
 {
 protected:
 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);
   cmGeneratedFileStreamBase(const char* name);
 
 
   // 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.
+  void Open(const char* name);
+
   // Internal file replacement implementation.
   // Internal file replacement implementation.
   int RenameFile(const char* oldname, const char* newname);
   int RenameFile(const char* oldname, const char* newname);
 
 
@@ -64,7 +71,13 @@ public:
   typedef std::ofstream Stream;
   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
    * automatically generates a name for the temporary file.  If the
    * file cannot be opened an error message is produced unless the
    * file cannot be opened an error message is produced unless the
    * second argument is set to true.
    * second argument is set to true.
@@ -78,6 +91,14 @@ public:
    */
    */
   ~cmGeneratedFileStream();
   ~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.
    * Set whether copy-if-different is done.
    */
    */