Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmGeneratedFileStream.h
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #ifndef cmGeneratedFileStream_h
13 #define cmGeneratedFileStream_h
14
15 #include "cmStandardIncludes.h"
16
17 #if defined(__sgi) && !defined(__GNUC__)
18 # pragma set woff 1375 /* base class destructor not virtual */
19 #endif
20
21 // This is the first base class of cmGeneratedFileStream.  It will be
22 // created before and destroyed after the ofstream portion and can
23 // therefore be used to manage the temporary file.
24 class cmGeneratedFileStreamBase
25 {
26 protected:
27   // This constructor does not prepare the temporary file.  The open
28   // method must be used.
29   cmGeneratedFileStreamBase();
30
31   // This constructor prepares the temporary output file.
32   cmGeneratedFileStreamBase(const char* name);
33
34   // The destructor renames the temporary output file to the real name.
35   ~cmGeneratedFileStreamBase();
36
37   // Internal methods to handle the temporary file.  Open is always
38   // called before the real stream is opened.  Close is always called
39   // after the real stream is closed and Okay is set to whether the
40   // real stream was still valid for writing when it was closed.
41   void Open(const char* name);
42   bool Close();
43
44   // Internal file replacement implementation.
45   int RenameFile(const char* oldname, const char* newname);
46
47   // Internal file compression implementation.
48   int CompressFile(const char* oldname, const char* newname);
49
50   // The name of the final destination file for the output.
51   std::string Name;
52
53   // The name of the temporary file.
54   std::string TempName;
55
56   // Whether to do a copy-if-different.
57   bool CopyIfDifferent;
58
59   // Whether the real file stream was valid when it was closed.
60   bool Okay;
61
62   // Whether the destionation file is compressed
63   bool Compress;
64
65   // Whether the destionation file is compressed
66   bool CompressExtraExtension;
67 };
68
69 /** \class cmGeneratedFileStream
70  * \brief Output stream for generated files.
71  *
72  * File generation should be atomic so that if CMake is killed then a
73  * generated file is either the original version or the complete new
74  * version.  This stream is used to make sure file generation is
75  * atomic.  Optionally the output file is only replaced if its
76  * contents have changed to prevent the file modification time from
77  * being updated.
78  */
79 class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
80                              public std::ofstream
81 {
82 public:
83   typedef std::ofstream Stream;
84
85   /**
86    * This constructor prepares a default stream.  The open method must
87    * be used before writing to the stream.
88    */
89   cmGeneratedFileStream();
90
91   /**
92    * This constructor takes the name of the file to be generated.  It
93    * automatically generates a name for the temporary file.  If the
94    * file cannot be opened an error message is produced unless the
95    * second argument is set to true.
96    */
97   cmGeneratedFileStream(const char* name, bool quiet=false);
98
99   /**
100    * The destructor checks the stream status to be sure the temporary
101    * file was successfully written before allowing the original to be
102    * replaced.
103    */
104   ~cmGeneratedFileStream();
105
106   /**
107    * Open an output file by name.  This should be used only with a
108    * non-open stream.  It automatically generates a name for the
109    * temporary file.  If the file cannot be opened an error message is
110    * produced unless the second argument is set to true.
111    */
112   cmGeneratedFileStream& Open(const char* name, bool quiet=false,
113     bool binaryFlag=false);
114
115   /**
116    * Close the output file.  This should be used only with an open
117    * stream.  The temporary file is atomically renamed to the
118    * destionation file if the stream is still valid when this method
119    * is called.
120    */
121   bool Close();
122
123   /**
124    * Set whether copy-if-different is done.
125    */
126   void SetCopyIfDifferent(bool copy_if_different);
127
128   /**
129    * Set whether compression is done.
130    */
131   void SetCompression(bool compression);
132
133   /**
134    * Set whether compression has extra extension
135    */
136   void SetCompressionExtraExtension(bool ext);
137
138   /**
139    * Set name of the file that will hold the actual output. This method allows
140    * the output file to be changed during the use of cmGeneratedFileStream.
141    */
142   void SetName(const char* fname);
143
144 private:
145   cmGeneratedFileStream(cmGeneratedFileStream const&); // not implemented
146 };
147
148 #if defined(__sgi) && !defined(__GNUC__)
149 # pragma reset woff 1375 /* base class destructor not virtual */
150 #endif
151
152 #endif