STYLE: Fix typo in GetFilenameLastExtension docs
[cmake.git] / Source / cmGeneratedFileStream.h
bloba058c82ab2470322d5d0be743c64d55d882ff841
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGeneratedFileStream.h,v $
5 Language: C++
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 */
24 #endif
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
31 protected:
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);
47 bool Close();
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.
56 std::string Name;
58 // The name of the temporary file.
59 std::string TempName;
61 // Whether to do a copy-if-different.
62 bool CopyIfDifferent;
64 // Whether the real file stream was valid when it was closed.
65 bool Okay;
67 // Whether the destionation file is compressed
68 bool Compress;
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
82 * being updated.
84 class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
85 public std::ofstream
87 public:
88 typedef std::ofstream Stream;
90 /**
91 * This constructor prepares a default stream. The open method must
92 * be used before writing to the stream.
94 cmGeneratedFileStream();
96 /**
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
107 * replaced.
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
124 * is called.
126 bool Close();
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);
149 private:
150 cmGeneratedFileStream(cmGeneratedFileStream const&); // not implemented
153 #if defined(__sgi) && !defined(__GNUC__)
154 # pragma reset woff 1375 /* base class destructor not virtual */
155 #endif
157 #endif