ENH: check in almost building VMS stuff with VMSBuild directory since the bootstrap...
[cmake.git] / Source / cmGeneratedFileStream.cxx
blobcca1aa1d1c94933b04b8ebcd5686cef07604b2aa
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGeneratedFileStream.cxx,v $
5 Language: C++
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)
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 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 )
170 resname += ".gz";
173 // Only consider replacing the destination file if no error
174 // occurred.
175 if(!this->Name.empty() &&
176 this->Okay &&
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());
191 else
193 this->RenameFile(this->TempName.c_str(), resname.c_str());
196 replaced = true;
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());
204 return replaced;
207 //----------------------------------------------------------------------------
208 #ifdef CMAKE_BUILD_WITH_CMAKE
209 int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
210 const char* newname)
212 gzFile gf = gzopen(newname, "w");
213 if ( !gf )
215 return 0;
217 FILE* ifs = fopen(oldname, "r");
218 if ( !ifs )
220 return 0;
222 size_t res;
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)) )
229 fclose(ifs);
230 gzclose(gf);
231 return 0;
234 fclose(ifs);
235 gzclose(gf);
236 return 1;
238 #else
239 int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
241 return 0;
243 #endif
245 //----------------------------------------------------------------------------
246 int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
247 const char* newname)
249 return cmSystemTools::RenameFile(oldname, newname);
252 //----------------------------------------------------------------------------
253 void cmGeneratedFileStream::SetName(const char* fname)
255 if ( !fname )
257 this->Name = "";
258 return;
260 this->Name = fname;