CMake/Source/cmGeneratedFileStream.cxx

261 lines
7.2 KiB
C++
Raw Normal View History

2009-10-02 01:21:28 +04:00
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
2009-10-02 01:21:28 +04:00
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
2009-10-02 01:21:28 +04:00
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
2005-01-27 18:14:24 +03:00
#if defined(CMAKE_BUILD_WITH_CMAKE)
# include <cm_zlib.h>
2005-01-27 18:14:24 +03:00
#endif
//----------------------------------------------------------------------------
cmGeneratedFileStream::cmGeneratedFileStream():
cmGeneratedFileStreamBase(), Stream()
{
}
//----------------------------------------------------------------------------
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
cmGeneratedFileStreamBase(name),
2006-03-15 19:02:08 +03:00
Stream(TempName.c_str())
{
// Check if the file opened.
if(!*this && !quiet)
{
2006-05-10 23:56:00 +04:00
cmSystemTools::Error("Cannot open file for write: ",
this->TempName.c_str());
cmSystemTools::ReportLastSystemError("");
}
}
//----------------------------------------------------------------------------
cmGeneratedFileStream::~cmGeneratedFileStream()
{
// This is the first destructor called. Check the status of the
// stream and give the information to the private base. Next the
// stream will be destroyed which will close the temporary file.
// Finally the base destructor will be called to replace the
// destination file.
2006-03-15 19:02:08 +03:00
this->Okay = (*this)?true:false;
}
//----------------------------------------------------------------------------
cmGeneratedFileStream&
2006-01-12 22:21:37 +03:00
cmGeneratedFileStream::Open(const char* name, bool quiet, bool binaryFlag)
{
// Store the file name and construct the temporary file name.
this->cmGeneratedFileStreamBase::Open(name);
// Open the temporary output file.
2006-01-12 22:21:37 +03:00
if ( binaryFlag )
{
2006-05-10 23:56:00 +04:00
this->Stream::open(this->TempName.c_str(),
std::ios::out | std::ios::binary);
}
else
{
2006-03-15 19:02:08 +03:00
this->Stream::open(this->TempName.c_str(), std::ios::out);
}
// Check if the file opened.
if(!*this && !quiet)
{
2006-05-10 23:56:00 +04:00
cmSystemTools::Error("Cannot open file for write: ",
this->TempName.c_str());
cmSystemTools::ReportLastSystemError("");
}
return *this;
}
//----------------------------------------------------------------------------
bool
cmGeneratedFileStream::Close()
{
// Save whether the temporary output file is valid before closing.
2006-03-15 19:02:08 +03:00
this->Okay = (*this)?true:false;
// Close the temporary output file.
this->Stream::close();
// Remove the temporary file (possibly by renaming to the real file).
return this->cmGeneratedFileStreamBase::Close();
}
//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
{
2006-03-15 19:02:08 +03:00
this->CopyIfDifferent = copy_if_different;
}
2005-01-27 18:14:24 +03:00
//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetCompression(bool compression)
{
2006-03-15 19:02:08 +03:00
this->Compress = compression;
2005-01-27 18:14:24 +03:00
}
//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
{
2006-03-15 19:02:08 +03:00
this->CompressExtraExtension = ext;
}
//----------------------------------------------------------------------------
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
2006-03-15 19:02:08 +03:00
Name(),
TempName(),
CopyIfDifferent(false),
Okay(false),
Compress(false),
CompressExtraExtension(true)
{
}
//----------------------------------------------------------------------------
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
2006-03-15 19:02:08 +03:00
Name(),
TempName(),
CopyIfDifferent(false),
Okay(false),
Compress(false),
CompressExtraExtension(true)
{
this->Open(name);
}
//----------------------------------------------------------------------------
cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
{
this->Close();
}
//----------------------------------------------------------------------------
void cmGeneratedFileStreamBase::Open(const char* name)
{
// Save the original name of the file.
2006-03-15 19:02:08 +03:00
this->Name = name;
// Create the name of the temporary file.
2006-03-15 19:02:08 +03:00
this->TempName = name;
#if defined(__VMS)
this->TempName += "_tmp";
#else
2006-03-15 19:02:08 +03:00
this->TempName += ".tmp";
#endif
// Make sure the temporary file that will be used is not present.
2006-03-15 19:02:08 +03:00
cmSystemTools::RemoveFile(this->TempName.c_str());
2006-03-15 19:02:08 +03:00
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
cmSystemTools::MakeDirectory(dir.c_str());
}
//----------------------------------------------------------------------------
bool cmGeneratedFileStreamBase::Close()
{
bool replaced = false;
2006-03-15 19:02:08 +03:00
std::string resname = this->Name;
if ( this->Compress && this->CompressExtraExtension )
2005-01-27 18:14:24 +03:00
{
resname += ".gz";
}
// Only consider replacing the destination file if no error
// occurred.
2006-03-15 19:02:08 +03:00
if(!this->Name.empty() &&
this->Okay &&
(!this->CopyIfDifferent ||
cmSystemTools::FilesDiffer(this->TempName.c_str(), resname.c_str())))
{
// The destination is to be replaced. Rename the temporary to the
// destination atomically.
2006-03-15 19:02:08 +03:00
if ( this->Compress )
2005-01-27 18:14:24 +03:00
{
2006-03-15 19:02:08 +03:00
std::string gzname = this->TempName + ".temp.gz";
if ( this->CompressFile(this->TempName.c_str(), gzname.c_str()) )
2005-01-27 18:14:24 +03:00
{
this->RenameFile(gzname.c_str(), resname.c_str());
}
cmSystemTools::RemoveFile(gzname.c_str());
}
else
{
2006-03-15 19:02:08 +03:00
this->RenameFile(this->TempName.c_str(), resname.c_str());
2005-01-27 18:14:24 +03:00
}
replaced = true;
}
2005-01-27 18:14:24 +03:00
// Else, the destination was not replaced.
//
// Always delete the temporary file. We never want it to stay around.
2006-03-15 19:02:08 +03:00
cmSystemTools::RemoveFile(this->TempName.c_str());
return replaced;
}
2005-01-27 18:14:24 +03:00
//----------------------------------------------------------------------------
#ifdef CMAKE_BUILD_WITH_CMAKE
2005-01-27 18:14:24 +03:00
int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
const char* newname)
{
gzFile gf = gzopen(newname, "w");
2005-01-27 18:14:24 +03:00
if ( !gf )
{
return 0;
}
FILE* ifs = fopen(oldname, "r");
if ( !ifs )
{
return 0;
}
size_t res;
const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
while ( (res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0 )
{
2007-09-27 22:44:11 +04:00
if ( !gzwrite(gf, buffer, static_cast<int>(res)) )
2005-01-27 18:14:24 +03:00
{
fclose(ifs);
gzclose(gf);
2005-01-27 18:14:24 +03:00
return 0;
}
}
fclose(ifs);
gzclose(gf);
2005-01-27 18:14:24 +03:00
return 1;
}
2005-01-27 18:14:24 +03:00
#else
int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
{
2005-01-27 18:14:24 +03:00
return 0;
}
#endif
2005-01-27 18:14:24 +03:00
//----------------------------------------------------------------------------
int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
const char* newname)
{
return cmSystemTools::RenameFile(oldname, newname);
}
2005-05-10 19:11:28 +04:00
//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetName(const char* fname)
{
if ( !fname )
{
2006-03-15 19:02:08 +03:00
this->Name = "";
2005-05-10 19:11:28 +04:00
return;
}
2006-03-15 19:02:08 +03:00
this->Name = fname;
2005-05-10 19:11:28 +04:00
}