1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGeneratedFileStream.cxx,v $
6 Date: $Date: 2009-04-15 13:57:56 $
7 Version: $Revision: 1.20 $
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
;
153 this->TempName
+= ".tmp";
155 // Make sure the temporary file that will be used is not present.
156 cmSystemTools::RemoveFile(this->TempName
.c_str());
158 std::string dir
= cmSystemTools::GetFilenamePath(this->TempName
);
159 cmSystemTools::MakeDirectory(dir
.c_str());
162 //----------------------------------------------------------------------------
163 bool cmGeneratedFileStreamBase::Close()
165 bool replaced
= false;
167 std::string resname
= this->Name
;
168 if ( this->Compress
&& this->CompressExtraExtension
)
173 // Only consider replacing the destination file if no error
175 if(!this->Name
.empty() &&
177 (!this->CopyIfDifferent
||
178 cmSystemTools::FilesDiffer(this->TempName
.c_str(), resname
.c_str())))
180 // The destination is to be replaced. Rename the temporary to the
181 // destination atomically.
182 if ( this->Compress
)
184 std::string gzname
= this->TempName
+ ".temp.gz";
185 if ( this->CompressFile(this->TempName
.c_str(), gzname
.c_str()) )
187 this->RenameFile(gzname
.c_str(), resname
.c_str());
189 cmSystemTools::RemoveFile(gzname
.c_str());
193 this->RenameFile(this->TempName
.c_str(), resname
.c_str());
199 // Else, the destination was not replaced.
201 // Always delete the temporary file. We never want it to stay around.
202 cmSystemTools::RemoveFile(this->TempName
.c_str());
207 //----------------------------------------------------------------------------
208 #ifdef CMAKE_BUILD_WITH_CMAKE
209 int cmGeneratedFileStreamBase::CompressFile(const char* oldname
,
212 gzFile gf
= gzopen(newname
, "w");
217 FILE* ifs
= fopen(oldname
, "r");
223 const size_t BUFFER_SIZE
= 1024;
224 char buffer
[BUFFER_SIZE
];
225 while ( (res
= fread(buffer
, 1, BUFFER_SIZE
, ifs
)) > 0 )
227 if ( !gzwrite(gf
, buffer
, static_cast<int>(res
)) )
239 int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
245 //----------------------------------------------------------------------------
246 int cmGeneratedFileStreamBase::RenameFile(const char* oldname
,
249 return cmSystemTools::RenameFile(oldname
, newname
);
252 //----------------------------------------------------------------------------
253 void cmGeneratedFileStream::SetName(const char* fname
)