1
/*=========================================================================
3
Program: Insight Segmentation & Registration Toolkit
4
Module: $RCSfile: cmGeneratedFileStream.h,v $
6
Date: $Date: 2001/06/04 15:34:22 $
7
Version: $Revision: 1.6 $
9
Copyright (c) 2001 Insight Consortium
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are met:
15
* Redistributions of source code must retain the above copyright notice,
16
this list of conditions and the following disclaimer.
18
* Redistributions in binary form must reproduce the above copyright notice,
19
this list of conditions and the following disclaimer in the documentation
20
and/or other materials provided with the distribution.
22
* The name of the Insight Consortium, nor the names of any consortium members,
23
nor of any contributors, may be used to endorse or promote products derived
24
from this software without specific prior written permission.
26
* Modified source versions must be plainly marked as such, and must not be
27
misrepresented as being the original software.
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
30
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
33
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
=========================================================================*/
41
#ifndef cmGeneratedFileStream_h
42
#define cmGeneratedFileStream_h
44
#include "cmStandardIncludes.h"
46
/** \class cmGeneratedFileStream
47
* \brief Output stream for generated files that does copy-if-different.
49
* Many files generated by CMake don't change each time they are generated.
50
* This class can be used in place of std::ofstream to open a file and
51
* write output to it. The class will automatically write output to a
52
* temporary file and copy it over an existing file only if the generated
55
class cmGeneratedFileStream
59
* The constructor takes the name of the file to be generated. It
60
* automatically generates a name for the temporary file.
62
cmGeneratedFileStream(const char* name):
64
m_TempName(m_Name+".tmp"),
65
m_Stream(m_TempName.c_str()),
71
* The destructor ensures that the file has been closed and copied if
74
~cmGeneratedFileStream() { this->DoCopy(); }
77
* Get the real output stream.
79
std::ostream& GetStream() { return m_Stream; }
82
* Allow a test for whether the file is open.
84
operator bool() { return m_Stream.good(); }
87
* Close the file stream. This will cause the copy-if-different to the
88
* real file name to occur.
90
void close() { this->DoCopy(); }
92
* If always copy is true, then copy the file all the time without
93
* checking for differences. The default is false.
95
bool SetAlwaysCopy(bool v) { m_AlwaysCopy = v; return v;}
98
* The name of the real file where output will be copied if it has changed.
103
* The name of the temporary file.
105
std::string m_TempName;
108
* The real output stream used to write to the file.
110
std::ofstream m_Stream;
113
* Whether the temporary file has already been copied to the real file.
118
* If always copy is true, then copy the file all the time without
119
* checking for differences. The default is false.
124
* Closes the temporary file and does the copy-if-different to the
134
cmSystemTools::cmCopyFile(m_TempName.c_str(), m_Name.c_str());
138
cmSystemTools::CopyFileIfDifferent(m_TempName.c_str(),
141
cmSystemTools::RemoveFile(m_TempName.c_str());
149
* Allow a cmGeneratedFileStream to be used just as a real std::ostream
153
std::ostream& operator << (cmGeneratedFileStream& l, const T& r)
155
std::ostream& os = l.GetStream();