1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGeneratedFileStream.cxx,v $
6 Date: $Date: 2009-06-10 17:03:11 $
7 Version: $Revision: 1.21 $
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 #include "cmGeneratedFileStream.h"
19 #include "cmSystemTools.h"
21 #if defined(CMAKE_BUILD_WITH_CMAKE)
25 //----------------------------------------------------------------------------
26 cmGeneratedFileStream::cmGeneratedFileStream():
27 cmGeneratedFileStreamBase(), Stream()
31 //----------------------------------------------------------------------------
32 cmGeneratedFileStream::cmGeneratedFileStream(const char* name
, bool quiet
):
33 cmGeneratedFileStreamBase(name
),
34 Stream(TempName
.c_str())
36 // Check if the file opened.
39 cmSystemTools::Error("Cannot open file for write: ",
40 this->TempName
.c_str());
41 cmSystemTools::ReportLastSystemError("");
45 //----------------------------------------------------------------------------
46 cmGeneratedFileStream::~cmGeneratedFileStream()
48 // This is the first destructor called. Check the status of the
49 // stream and give the information to the private base. Next the
50 // stream will be destroyed which will close the temporary file.
51 // Finally the base destructor will be called to replace the
53 this->Okay
= (*this)?true:false;
56 //----------------------------------------------------------------------------
57 cmGeneratedFileStream
&
58 cmGeneratedFileStream::Open(const char* name
, bool quiet
, bool binaryFlag
)
60 // Store the file name and construct the temporary file name.
61 this->cmGeneratedFileStreamBase::Open(name
);
63 // Open the temporary output file.
66 this->Stream::open(this->TempName
.c_str(),
67 std::ios::out
| std::ios::binary
);
71 this->Stream::open(this->TempName
.c_str(), std::ios::out
);
74 // Check if the file opened.
77 cmSystemTools::Error("Cannot open file for write: ",
78 this->TempName
.c_str());
79 cmSystemTools::ReportLastSystemError("");
84 //----------------------------------------------------------------------------
86 cmGeneratedFileStream::Close()
88 // Save whether the temporary output file is valid before closing.
89 this->Okay
= (*this)?true:false;
91 // Close the temporary output file.
92 this->Stream::close();
94 // Remove the temporary file (possibly by renaming to the real file).
95 return this->cmGeneratedFileStreamBase::Close();
98 //----------------------------------------------------------------------------
99 void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different
)
101 this->CopyIfDifferent
= copy_if_different
;
104 //----------------------------------------------------------------------------
105 void cmGeneratedFileStream::SetCompression(bool compression
)
107 this->Compress
= compression
;
110 //----------------------------------------------------------------------------
111 void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext
)
113 this->CompressExtraExtension
= ext
;
116 //----------------------------------------------------------------------------
117 cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
120 CopyIfDifferent(false),
123 CompressExtraExtension(true)
127 //----------------------------------------------------------------------------
128 cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name
):
131 CopyIfDifferent(false),
134 CompressExtraExtension(true)
139 //----------------------------------------------------------------------------
140 cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
145 //----------------------------------------------------------------------------
146 void cmGeneratedFileStreamBase::Open(const char* name
)
148 // Save the original name of the file.
151 // Create the name of the temporary file.
152 this->TempName
= name
;
154 this->TempName
+= "_tmp";
156 this->TempName
+= ".tmp";
159 // Make sure the temporary file that will be used is not present.
160 cmSystemTools::RemoveFile(this->TempName
.c_str());
162 std::string dir
= cmSystemTools::GetFilenamePath(this->TempName
);
163 cmSystemTools::MakeDirectory(dir
.c_str());
166 //----------------------------------------------------------------------------
167 bool cmGeneratedFileStreamBase::Close()
169 bool replaced
= false;
171 std::string resname
= this->Name
;
172 if ( this->Compress
&& this->CompressExtraExtension
)
177 // Only consider replacing the destination file if no error
179 if(!this->Name
.empty() &&
181 (!this->CopyIfDifferent
||
182 cmSystemTools::FilesDiffer(this->TempName
.c_str(), resname
.c_str())))
184 // The destination is to be replaced. Rename the temporary to the
185 // destination atomically.
186 if ( this->Compress
)
188 std::string gzname
= this->TempName
+ ".temp.gz";
189 if ( this->CompressFile(this->TempName
.c_str(), gzname
.c_str()) )
191 this->RenameFile(gzname
.c_str(), resname
.c_str());
193 cmSystemTools::RemoveFile(gzname
.c_str());
197 this->RenameFile(this->TempName
.c_str(), resname
.c_str());
203 // Else, the destination was not replaced.
205 // Always delete the temporary file. We never want it to stay around.
206 cmSystemTools::RemoveFile(this->TempName
.c_str());
211 //----------------------------------------------------------------------------
212 #ifdef CMAKE_BUILD_WITH_CMAKE
213 int cmGeneratedFileStreamBase::CompressFile(const char* oldname
,
216 gzFile gf
= gzopen(newname
, "w");
221 FILE* ifs
= fopen(oldname
, "r");
227 const size_t BUFFER_SIZE
= 1024;
228 char buffer
[BUFFER_SIZE
];
229 while ( (res
= fread(buffer
, 1, BUFFER_SIZE
, ifs
)) > 0 )
231 if ( !gzwrite(gf
, buffer
, static_cast<int>(res
)) )
243 int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
249 //----------------------------------------------------------------------------
250 int cmGeneratedFileStreamBase::RenameFile(const char* oldname
,
253 return cmSystemTools::RenameFile(oldname
, newname
);
256 //----------------------------------------------------------------------------
257 void cmGeneratedFileStream::SetName(const char* fname
)