1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGeneratedFileStream.h,v $
6 Date: $Date: 2007/11/16 12:01:58 $
7 Version: $Revision: 1.23 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #ifndef cmGeneratedFileStream_h
18 #define cmGeneratedFileStream_h
20 #include "cmStandardIncludes.h"
22 #if defined(__sgi) && !defined(__GNUC__)
23 # pragma set woff 1375 /* base class destructor not virtual */
26 // This is the first base class of cmGeneratedFileStream. It will be
27 // created before and destroyed after the ofstream portion and can
28 // therefore be used to manage the temporary file.
29 class cmGeneratedFileStreamBase
32 // This constructor does not prepare the temporary file. The open
33 // method must be used.
34 cmGeneratedFileStreamBase();
36 // This constructor prepares the temporary output file.
37 cmGeneratedFileStreamBase(const char* name
);
39 // The destructor renames the temporary output file to the real name.
40 ~cmGeneratedFileStreamBase();
42 // Internal methods to handle the temporary file. Open is always
43 // called before the real stream is opened. Close is always called
44 // after the real stream is closed and Okay is set to whether the
45 // real stream was still valid for writing when it was closed.
46 void Open(const char* name
);
49 // Internal file replacement implementation.
50 int RenameFile(const char* oldname
, const char* newname
);
52 // Internal file compression implementation.
53 int CompressFile(const char* oldname
, const char* newname
);
55 // The name of the final destination file for the output.
58 // The name of the temporary file.
61 // Whether to do a copy-if-different.
64 // Whether the real file stream was valid when it was closed.
67 // Whether the destionation file is compressed
70 // Whether the destionation file is compressed
71 bool CompressExtraExtension
;
74 /** \class cmGeneratedFileStream
75 * \brief Output stream for generated files.
77 * File generation should be atomic so that if CMake is killed then a
78 * generated file is either the original version or the complete new
79 * version. This stream is used to make sure file generation is
80 * atomic. Optionally the output file is only replaced if its
81 * contents have changed to prevent the file modification time from
84 class cmGeneratedFileStream
: private cmGeneratedFileStreamBase
,
88 typedef std::ofstream Stream
;
91 * This constructor prepares a default stream. The open method must
92 * be used before writing to the stream.
94 cmGeneratedFileStream();
97 * This constructor takes the name of the file to be generated. It
98 * automatically generates a name for the temporary file. If the
99 * file cannot be opened an error message is produced unless the
100 * second argument is set to true.
102 cmGeneratedFileStream(const char* name
, bool quiet
=false);
105 * The destructor checks the stream status to be sure the temporary
106 * file was successfully written before allowing the original to be
109 ~cmGeneratedFileStream();
112 * Open an output file by name. This should be used only with a
113 * non-open stream. It automatically generates a name for the
114 * temporary file. If the file cannot be opened an error message is
115 * produced unless the second argument is set to true.
117 cmGeneratedFileStream
& Open(const char* name
, bool quiet
=false,
118 bool binaryFlag
=false);
121 * Close the output file. This should be used only with an open
122 * stream. The temporary file is atomically renamed to the
123 * destionation file if the stream is still valid when this method
129 * Set whether copy-if-different is done.
131 void SetCopyIfDifferent(bool copy_if_different
);
134 * Set whether compression is done.
136 void SetCompression(bool compression
);
139 * Set whether compression has extra extension
141 void SetCompressionExtraExtension(bool ext
);
144 * Set name of the file that will hold the actual output. This method allows
145 * the output file to be changed during the use of cmGeneratedFileStream.
147 void SetName(const char* fname
);
150 cmGeneratedFileStream(cmGeneratedFileStream
const&); // not implemented
153 #if defined(__sgi) && !defined(__GNUC__)
154 # pragma reset woff 1375 /* base class destructor not virtual */