summaryrefslogtreecommitdiff
path: root/Source/cmGeneratedFileStream.cxx
diff options
context:
space:
mode:
authorMyungJoo Ham <myungjoo.ham@samsung.com>2017-10-11 15:16:57 +0900
committerMyungJoo Ham <myungjoo.ham@samsung.com>2017-10-11 15:16:57 +0900
commit915c76ded744c0f5f151402b9fa69f3fd8452573 (patch)
treeca6a387466543248890f346847acaa8343989b22 /Source/cmGeneratedFileStream.cxx
parent317dbdb79761ef65e45c7358cfc7571c6afa54ad (diff)
downloadcmake-915c76ded744c0f5f151402b9fa69f3fd8452573.tar.gz
cmake-915c76ded744c0f5f151402b9fa69f3fd8452573.tar.bz2
cmake-915c76ded744c0f5f151402b9fa69f3fd8452573.zip
Imported Upstream version 3.9.4upstream/3.9.4
Diffstat (limited to 'Source/cmGeneratedFileStream.cxx')
-rw-r--r--Source/cmGeneratedFileStream.cxx180
1 files changed, 76 insertions, 104 deletions
diff --git a/Source/cmGeneratedFileStream.cxx b/Source/cmGeneratedFileStream.cxx
index 0af07532a..6aa593cc0 100644
--- a/Source/cmGeneratedFileStream.cxx
+++ b/Source/cmGeneratedFileStream.cxx
@@ -1,43 +1,49 @@
-/*============================================================================
- CMake - Cross Platform Makefile Generator
- Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
-
- Distributed under the OSI-approved BSD License (the "License");
- see accompanying file Copyright.txt for details.
-
- 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.
-============================================================================*/
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmGeneratedFileStream.h"
+#include <stdio.h>
+
#include "cmSystemTools.h"
#if defined(CMAKE_BUILD_WITH_CMAKE)
-# include <cm_zlib.h>
+#include "cm_codecvt.hxx"
+#include "cm_zlib.h"
#endif
-//----------------------------------------------------------------------------
-cmGeneratedFileStream::cmGeneratedFileStream():
- cmGeneratedFileStreamBase(), Stream()
+cmGeneratedFileStream::cmGeneratedFileStream(Encoding encoding)
+ : cmGeneratedFileStreamBase()
+ , Stream()
{
+#ifdef CMAKE_BUILD_WITH_CMAKE
+ if (encoding != codecvt::None) {
+ imbue(std::locale(getloc(), new codecvt(encoding)));
+ }
+#else
+ static_cast<void>(encoding);
+#endif
}
-//----------------------------------------------------------------------------
-cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
- cmGeneratedFileStreamBase(name),
- Stream(TempName.c_str())
+cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet,
+ Encoding encoding)
+ : cmGeneratedFileStreamBase(name)
+ , Stream(TempName.c_str())
{
// Check if the file opened.
- if(!*this && !quiet)
- {
+ if (!*this && !quiet) {
cmSystemTools::Error("Cannot open file for write: ",
this->TempName.c_str());
cmSystemTools::ReportLastSystemError("");
- }
+ }
+#ifdef CMAKE_BUILD_WITH_CMAKE
+ if (encoding != codecvt::None) {
+ imbue(std::locale(getloc(), new codecvt(encoding)));
+ }
+#else
+ static_cast<void>(encoding);
+#endif
}
-//----------------------------------------------------------------------------
cmGeneratedFileStream::~cmGeneratedFileStream()
{
// This is the first destructor called. Check the status of the
@@ -45,43 +51,36 @@ cmGeneratedFileStream::~cmGeneratedFileStream()
// stream will be destroyed which will close the temporary file.
// Finally the base destructor will be called to replace the
// destination file.
- this->Okay = (*this)?true:false;
+ this->Okay = !this->fail();
}
-//----------------------------------------------------------------------------
-cmGeneratedFileStream&
-cmGeneratedFileStream::Open(const char* name, bool quiet, bool binaryFlag)
+cmGeneratedFileStream& 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.
- if ( binaryFlag )
- {
+ if (binaryFlag) {
this->Stream::open(this->TempName.c_str(),
std::ios::out | std::ios::binary);
- }
- else
- {
- this->Stream::open(this->TempName.c_str(), std::ios::out);
- }
+ } else {
+ this->Stream::open(this->TempName.c_str());
+ }
// Check if the file opened.
- if(!*this && !quiet)
- {
+ if (!*this && !quiet) {
cmSystemTools::Error("Cannot open file for write: ",
this->TempName.c_str());
cmSystemTools::ReportLastSystemError("");
- }
+ }
return *this;
}
-//----------------------------------------------------------------------------
-bool
-cmGeneratedFileStream::Close()
+bool cmGeneratedFileStream::Close()
{
// Save whether the temporary output file is valid before closing.
- this->Okay = (*this)?true:false;
+ this->Okay = !this->fail();
// Close the temporary output file.
this->Stream::close();
@@ -90,54 +89,47 @@ cmGeneratedFileStream::Close()
return this->cmGeneratedFileStreamBase::Close();
}
-//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
{
this->CopyIfDifferent = copy_if_different;
}
-//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetCompression(bool compression)
{
this->Compress = compression;
}
-//----------------------------------------------------------------------------
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
{
this->CompressExtraExtension = ext;
}
-//----------------------------------------------------------------------------
-cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
- Name(),
- TempName(),
- CopyIfDifferent(false),
- Okay(false),
- Compress(false),
- CompressExtraExtension(true)
+cmGeneratedFileStreamBase::cmGeneratedFileStreamBase()
+ : Name()
+ , TempName()
+ , CopyIfDifferent(false)
+ , Okay(false)
+ , Compress(false)
+ , CompressExtraExtension(true)
{
}
-//----------------------------------------------------------------------------
-cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
- Name(),
- TempName(),
- CopyIfDifferent(false),
- Okay(false),
- Compress(false),
- CompressExtraExtension(true)
+cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name)
+ : 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.
@@ -152,84 +144,71 @@ void cmGeneratedFileStreamBase::Open(const char* name)
#endif
// Make sure the temporary file that will be used is not present.
- cmSystemTools::RemoveFile(this->TempName.c_str());
+ cmSystemTools::RemoveFile(this->TempName);
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
cmSystemTools::MakeDirectory(dir.c_str());
}
-//----------------------------------------------------------------------------
bool cmGeneratedFileStreamBase::Close()
{
bool replaced = false;
std::string resname = this->Name;
- if ( this->Compress && this->CompressExtraExtension )
- {
+ if (this->Compress && this->CompressExtraExtension) {
resname += ".gz";
- }
+ }
// Only consider replacing the destination file if no error
// occurred.
- if(!this->Name.empty() &&
- this->Okay &&
- (!this->CopyIfDifferent ||
- cmSystemTools::FilesDiffer(this->TempName.c_str(), resname.c_str())))
- {
+ if (!this->Name.empty() && this->Okay &&
+ (!this->CopyIfDifferent ||
+ cmSystemTools::FilesDiffer(this->TempName, resname))) {
// The destination is to be replaced. Rename the temporary to the
// destination atomically.
- if ( this->Compress )
- {
+ if (this->Compress) {
std::string gzname = this->TempName + ".temp.gz";
- if ( this->CompressFile(this->TempName.c_str(), gzname.c_str()) )
- {
+ if (this->CompressFile(this->TempName.c_str(), gzname.c_str())) {
this->RenameFile(gzname.c_str(), resname.c_str());
- }
- cmSystemTools::RemoveFile(gzname.c_str());
}
- else
- {
+ cmSystemTools::RemoveFile(gzname);
+ } else {
this->RenameFile(this->TempName.c_str(), resname.c_str());
- }
+ }
replaced = true;
- }
+ }
// Else, the destination was not replaced.
//
// Always delete the temporary file. We never want it to stay around.
- cmSystemTools::RemoveFile(this->TempName.c_str());
+ cmSystemTools::RemoveFile(this->TempName);
return replaced;
}
-//----------------------------------------------------------------------------
#ifdef CMAKE_BUILD_WITH_CMAKE
int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
const char* newname)
{
gzFile gf = gzopen(newname, "w");
- if ( !gf )
- {
+ if (!gf) {
return 0;
- }
- FILE* ifs = fopen(oldname, "r");
- if ( !ifs )
- {
+ }
+ FILE* ifs = cmsys::SystemTools::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 )
- {
- if ( !gzwrite(gf, buffer, static_cast<int>(res)) )
- {
+ while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
+ if (!gzwrite(gf, buffer, static_cast<int>(res))) {
fclose(ifs);
gzclose(gf);
return 0;
- }
}
+ }
fclose(ifs);
gzclose(gf);
return 1;
@@ -241,20 +220,13 @@ int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
}
#endif
-//----------------------------------------------------------------------------
int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
const char* newname)
{
return cmSystemTools::RenameFile(oldname, newname);
}
-//----------------------------------------------------------------------------
-void cmGeneratedFileStream::SetName(const char* fname)
+void cmGeneratedFileStream::SetName(const std::string& fname)
{
- if ( !fname )
- {
- this->Name = "";
- return;
- }
this->Name = fname;
}