~ubuntu-branches/ubuntu/intrepid/cmake/intrepid-backports

« back to all changes in this revision

Viewing changes to Source/cmGeneratedFileStream.h

  • Committer: Bazaar Package Importer
  • Author(s): Maitland Bottoms
  • Date: 2002-02-14 18:36:25 UTC
  • Revision ID: james.westby@ubuntu.com-20020214183625-8m44isdas2k4l0f7
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
 
 
3
  Program:   Insight Segmentation & Registration Toolkit
 
4
  Module:    $RCSfile: cmGeneratedFileStream.h,v $
 
5
  Language:  C++
 
6
  Date:      $Date: 2001/06/04 15:34:22 $
 
7
  Version:   $Revision: 1.6 $
 
8
 
 
9
Copyright (c) 2001 Insight Consortium
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or without
 
13
modification, are permitted provided that the following conditions are met:
 
14
 
 
15
 * Redistributions of source code must retain the above copyright notice,
 
16
   this list of conditions and the following disclaimer.
 
17
 
 
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.
 
21
 
 
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.
 
25
 
 
26
  * Modified source versions must be plainly marked as such, and must not be
 
27
    misrepresented as being the original software.
 
28
 
 
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.
 
39
 
 
40
=========================================================================*/
 
41
#ifndef cmGeneratedFileStream_h
 
42
#define cmGeneratedFileStream_h
 
43
 
 
44
#include "cmStandardIncludes.h"
 
45
 
 
46
/** \class cmGeneratedFileStream
 
47
 * \brief Output stream for generated files that does copy-if-different.
 
48
 *
 
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
 
53
 * file has changed.
 
54
 */
 
55
class cmGeneratedFileStream
 
56
{
 
57
public:
 
58
  /**
 
59
   * The constructor takes the name of the file to be generated.  It
 
60
   * automatically generates a name for the temporary file.
 
61
   */
 
62
  cmGeneratedFileStream(const char* name):
 
63
    m_Name(name),
 
64
    m_TempName(m_Name+".tmp"),
 
65
    m_Stream(m_TempName.c_str()),
 
66
    m_Copied(false),
 
67
    m_AlwaysCopy(false)
 
68
    {}
 
69
  
 
70
  /**
 
71
   * The destructor ensures that the file has been closed and copied if
 
72
   * it has changed.
 
73
   */
 
74
  ~cmGeneratedFileStream() { this->DoCopy(); }
 
75
  
 
76
  /**
 
77
   * Get the real output stream.
 
78
   */
 
79
  std::ostream& GetStream() { return m_Stream; }
 
80
  
 
81
  /**
 
82
   * Allow a test for whether the file is open.
 
83
   */
 
84
  operator bool() { return m_Stream.good(); }
 
85
  
 
86
  /**
 
87
   * Close the file stream.  This will cause the copy-if-different to the
 
88
   * real file name to occur.
 
89
   */
 
90
  void close() { this->DoCopy(); }
 
91
  /**
 
92
   * If always copy is true, then copy the file all the time without
 
93
   *  checking for differences.  The default is false.
 
94
   */
 
95
  bool SetAlwaysCopy(bool v) { m_AlwaysCopy = v; return v;}
 
96
private:
 
97
  /**
 
98
   * The name of the real file where output will be copied if it has changed.
 
99
   */
 
100
  std::string m_Name;
 
101
  
 
102
  /**
 
103
   * The name of the temporary file.
 
104
   */
 
105
  std::string m_TempName;
 
106
  
 
107
  /**
 
108
   * The real output stream used to write to the file.
 
109
   */
 
110
  std::ofstream m_Stream;
 
111
  
 
112
  /**
 
113
   * Whether the temporary file has already been copied to the real file.
 
114
   */
 
115
  bool m_Copied;
 
116
 
 
117
  /**
 
118
   *  If always copy is true, then copy the file all the time without
 
119
   *  checking for differences.  The default is false.
 
120
   */
 
121
  bool m_AlwaysCopy;
 
122
  
 
123
  /**
 
124
   * Closes the temporary file and does the copy-if-different to the
 
125
   * real file.
 
126
   */
 
127
  void DoCopy()
 
128
    {
 
129
    if(!m_Copied)
 
130
      {
 
131
      m_Stream.close();
 
132
      if(m_AlwaysCopy)
 
133
        {
 
134
        cmSystemTools::cmCopyFile(m_TempName.c_str(), m_Name.c_str());
 
135
        }
 
136
      else
 
137
        {
 
138
        cmSystemTools::CopyFileIfDifferent(m_TempName.c_str(),
 
139
                                           m_Name.c_str());
 
140
        }
 
141
      cmSystemTools::RemoveFile(m_TempName.c_str());
 
142
      m_Copied = true;
 
143
      }
 
144
    }
 
145
};
 
146
 
 
147
 
 
148
/**
 
149
 * Allow a cmGeneratedFileStream to be used just as a real std::ostream
 
150
 * would be.
 
151
 */
 
152
template <class T>
 
153
std::ostream& operator << (cmGeneratedFileStream& l, const T& r)
 
154
{
 
155
  std::ostream& os = l.GetStream();
 
156
  os << r;
 
157
  return os;
 
158
}
 
159
 
 
160
 
 
161
 
 
162
#endif