1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
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
15 #include "cmStandardIncludes.h"
17 #if defined(__sgi) && !defined(__GNUC__)
18 # pragma set woff 1375 /* base class destructor not virtual */
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
27 // This constructor does not prepare the temporary file. The open
28 // method must be used.
29 cmGeneratedFileStreamBase();
31 // This constructor prepares the temporary output file.
32 cmGeneratedFileStreamBase(const char* name);
34 // The destructor renames the temporary output file to the real name.
35 ~cmGeneratedFileStreamBase();
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);
44 // Internal file replacement implementation.
45 int RenameFile(const char* oldname, const char* newname);
47 // Internal file compression implementation.
48 int CompressFile(const char* oldname, const char* newname);
50 // The name of the final destination file for the output.
53 // The name of the temporary file.
56 // Whether to do a copy-if-different.
59 // Whether the real file stream was valid when it was closed.
62 // Whether the destionation file is compressed
65 // Whether the destionation file is compressed
66 bool CompressExtraExtension;
69 /** \class cmGeneratedFileStream
70 * \brief Output stream for generated files.
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
79 class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
83 typedef std::ofstream Stream;
86 * This constructor prepares a default stream. The open method must
87 * be used before writing to the stream.
89 cmGeneratedFileStream();
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.
97 cmGeneratedFileStream(const char* name, bool quiet=false);
100 * The destructor checks the stream status to be sure the temporary
101 * file was successfully written before allowing the original to be
104 ~cmGeneratedFileStream();
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.
112 cmGeneratedFileStream& Open(const char* name, bool quiet=false,
113 bool binaryFlag=false);
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
124 * Set whether copy-if-different is done.
126 void SetCopyIfDifferent(bool copy_if_different);
129 * Set whether compression is done.
131 void SetCompression(bool compression);
134 * Set whether compression has extra extension
136 void SetCompressionExtraExtension(bool ext);
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.
142 void SetName(const char* fname);
145 cmGeneratedFileStream(cmGeneratedFileStream const&); // not implemented
148 #if defined(__sgi) && !defined(__GNUC__)
149 # pragma reset woff 1375 /* base class destructor not virtual */