3
3
Program: CMake - Cross-Platform Makefile Generator
4
4
Module: $RCSfile: cmGeneratedFileStream.cxx,v $
6
Date: $Date: 2005/05/10 15:11:28 $
7
Version: $Revision: 1.9 $
6
Date: $Date: 2006/05/11 02:15:09 $
7
Version: $Revision: 1.15.2.1 $
9
9
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10
10
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
39
39
//----------------------------------------------------------------------------
40
40
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
41
41
cmGeneratedFileStreamBase(name),
42
Stream(m_TempName.c_str())
42
Stream(TempName.c_str())
44
44
// Check if the file opened.
45
45
if(!*this && !quiet)
47
cmSystemTools::Error("Cannot open file for write: ", m_TempName.c_str());
47
cmSystemTools::Error("Cannot open file for write: ",
48
this->TempName.c_str());
48
49
cmSystemTools::ReportLastSystemError("");
57
58
// stream will be destroyed which will close the temporary file.
58
59
// Finally the base destructor will be called to replace the
59
60
// destination file.
60
m_Okay = (*this)?true:false;
61
this->Okay = (*this)?true:false;
63
64
//----------------------------------------------------------------------------
64
65
cmGeneratedFileStream&
65
cmGeneratedFileStream::Open(const char* name, bool quiet)
66
cmGeneratedFileStream::Open(const char* name, bool quiet, bool binaryFlag)
67
68
// Store the file name and construct the temporary file name.
68
69
this->cmGeneratedFileStreamBase::Open(name);
70
71
// Open the temporary output file.
71
this->Stream::open(m_TempName.c_str());
74
this->Stream::open(this->TempName.c_str(),
75
std::ios::out | std::ios::binary);
79
this->Stream::open(this->TempName.c_str(), std::ios::out);
73
82
// Check if the file opened.
74
83
if(!*this && !quiet)
76
cmSystemTools::Error("Cannot open file for write: ", m_TempName.c_str());
85
cmSystemTools::Error("Cannot open file for write: ",
86
this->TempName.c_str());
77
87
cmSystemTools::ReportLastSystemError("");
84
94
cmGeneratedFileStream::Close()
86
96
// Save whether the temporary output file is valid before closing.
87
m_Okay = (*this)?true:false;
97
this->Okay = (*this)?true:false;
89
99
// Close the temporary output file.
90
100
this->Stream::close();
98
108
//----------------------------------------------------------------------------
99
109
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
101
m_CopyIfDifferent = copy_if_different;
111
this->CopyIfDifferent = copy_if_different;
104
114
//----------------------------------------------------------------------------
105
115
void cmGeneratedFileStream::SetCompression(bool compression)
107
m_Compress = compression;
117
this->Compress = compression;
120
//----------------------------------------------------------------------------
121
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
123
this->CompressExtraExtension = ext;
110
126
//----------------------------------------------------------------------------
111
127
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
114
m_CopyIfDifferent(false),
130
CopyIfDifferent(false),
133
CompressExtraExtension(true)
120
137
//----------------------------------------------------------------------------
121
138
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
124
m_CopyIfDifferent(false),
141
CopyIfDifferent(false),
144
CompressExtraExtension(true)
128
146
this->Open(name);
138
156
void cmGeneratedFileStreamBase::Open(const char* name)
140
158
// Save the original name of the file.
143
161
// Create the name of the temporary file.
145
m_TempName += ".tmp";
162
this->TempName = name;
163
this->TempName += ".tmp";
147
165
// Make sure the temporary file that will be used is not present.
148
cmSystemTools::RemoveFile(m_TempName.c_str());
166
cmSystemTools::RemoveFile(this->TempName.c_str());
150
std::string dir = cmSystemTools::GetFilenamePath(m_TempName);
168
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
151
169
cmSystemTools::MakeDirectory(dir.c_str());
154
172
//----------------------------------------------------------------------------
155
173
void cmGeneratedFileStreamBase::Close()
157
std::string resname = m_Name;
175
std::string resname = this->Name;
176
if ( this->Compress && this->CompressExtraExtension )
160
178
resname += ".gz";
163
181
// Only consider replacing the destination file if no error
165
if(!m_Name.empty() &&
167
(!m_CopyIfDifferent ||
168
cmSystemTools::FilesDiffer(m_TempName.c_str(), resname.c_str())))
183
if(!this->Name.empty() &&
185
(!this->CopyIfDifferent ||
186
cmSystemTools::FilesDiffer(this->TempName.c_str(), resname.c_str())))
170
188
// The destination is to be replaced. Rename the temporary to the
171
189
// destination atomically.
190
if ( this->Compress )
174
std::string gzname = m_TempName + ".temp.gz";
175
if ( this->CompressFile(m_TempName.c_str(), gzname.c_str()) )
192
std::string gzname = this->TempName + ".temp.gz";
193
if ( this->CompressFile(this->TempName.c_str(), gzname.c_str()) )
177
195
this->RenameFile(gzname.c_str(), resname.c_str());
183
this->RenameFile(m_TempName.c_str(), resname.c_str());
201
this->RenameFile(this->TempName.c_str(), resname.c_str());
187
205
// Else, the destination was not replaced.
189
207
// Always delete the temporary file. We never want it to stay around.
190
cmSystemTools::RemoveFile(m_TempName.c_str());
208
cmSystemTools::RemoveFile(this->TempName.c_str());
193
211
//----------------------------------------------------------------------------