Fix Xcode project references to the source tree
[cmake.git] / Source / cmGeneratedFileStream.cxx
blob1d6cb739bc83974b4217e3fc5436e674881e0ca7
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGeneratedFileStream.cxx,v $
5 Language: C++
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)
22 # include <cm_zlib.h>
23 #endif
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.
37 if(!*this && !quiet)
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
52 // destination file.
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.
64 if ( binaryFlag )
66 this->Stream::open(this->TempName.c_str(),
67 std::ios::out | std::ios::binary);
69 else
71 this->Stream::open(this->TempName.c_str(), std::ios::out);
74 // Check if the file opened.
75 if(!*this && !quiet)
77 cmSystemTools::Error("Cannot open file for write: ",
78 this->TempName.c_str());
79 cmSystemTools::ReportLastSystemError("");
81 return *this;
84 //----------------------------------------------------------------------------
85 bool
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():
118 Name(),
119 TempName(),
120 CopyIfDifferent(false),
121 Okay(false),
122 Compress(false),
123 CompressExtraExtension(true)
127 //----------------------------------------------------------------------------
128 cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
129 Name(),
130 TempName(),
131 CopyIfDifferent(false),
132 Okay(false),
133 Compress(false),
134 CompressExtraExtension(true)
136 this->Open(name);
139 //----------------------------------------------------------------------------
140 cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
142 this->Close();
145 //----------------------------------------------------------------------------
146 void cmGeneratedFileStreamBase::Open(const char* name)
148 // Save the original name of the file.
149 this->Name = name;
151 // Create the name of the temporary file.
152 this->TempName = name;
153 #if defined(__VMS)
154 this->TempName += "_tmp";
155 #else
156 this->TempName += ".tmp";
157 #endif
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 )
174 resname += ".gz";
177 // Only consider replacing the destination file if no error
178 // occurred.
179 if(!this->Name.empty() &&
180 this->Okay &&
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());
195 else
197 this->RenameFile(this->TempName.c_str(), resname.c_str());
200 replaced = true;
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());
208 return replaced;
211 //----------------------------------------------------------------------------
212 #ifdef CMAKE_BUILD_WITH_CMAKE
213 int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
214 const char* newname)
216 gzFile gf = gzopen(newname, "w");
217 if ( !gf )
219 return 0;
221 FILE* ifs = fopen(oldname, "r");
222 if ( !ifs )
224 return 0;
226 size_t res;
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)) )
233 fclose(ifs);
234 gzclose(gf);
235 return 0;
238 fclose(ifs);
239 gzclose(gf);
240 return 1;
242 #else
243 int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
245 return 0;
247 #endif
249 //----------------------------------------------------------------------------
250 int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
251 const char* newname)
253 return cmSystemTools::RenameFile(oldname, newname);
256 //----------------------------------------------------------------------------
257 void cmGeneratedFileStream::SetName(const char* fname)
259 if ( !fname )
261 this->Name = "";
262 return;
264 this->Name = fname;