summaryrefslogtreecommitdiffstats
path: root/Source/cmGeneratedFileStream.cxx
diff options
context:
space:
mode:
authorAndy Cedilnik <andy.cedilnik@kitware.com>2005-01-27 15:14:24 (GMT)
committerAndy Cedilnik <andy.cedilnik@kitware.com>2005-01-27 15:14:24 (GMT)
commit14477cee6e8a380e74f4cbf70afe5f3c3bfbd8a2 (patch)
tree4bcf591495aed6f04124fc1a7039f9a4dcd45fe8 /Source/cmGeneratedFileStream.cxx
parent197ba0bdd6a331af212e997595190372e06b55c5 (diff)
downloadCMake-14477cee6e8a380e74f4cbf70afe5f3c3bfbd8a2.zip
CMake-14477cee6e8a380e74f4cbf70afe5f3c3bfbd8a2.tar.gz
CMake-14477cee6e8a380e74f4cbf70afe5f3c3bfbd8a2.tar.bz2
ENH: Add compression support
Diffstat (limited to 'Source/cmGeneratedFileStream.cxx')
-rw-r--r--Source/cmGeneratedFileStream.cxx86
1 files changed, 75 insertions, 11 deletions
diff --git a/Source/cmGeneratedFileStream.cxx b/Source/cmGeneratedFileStream.cxx
index 53f3077..e891971 100644
--- a/Source/cmGeneratedFileStream.cxx
+++ b/Source/cmGeneratedFileStream.cxx
@@ -26,6 +26,10 @@
# include <sys/stat.h>
#endif
+#if defined(CMAKE_BUILD_WITH_CMAKE)
+# include <cmzlib/zlib.h>
+#endif
+
//----------------------------------------------------------------------------
cmGeneratedFileStream::cmGeneratedFileStream():
cmGeneratedFileStreamBase(), Stream()
@@ -82,11 +86,18 @@ void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
}
//----------------------------------------------------------------------------
+void cmGeneratedFileStream::SetCompression(bool compression)
+{
+ m_Compress = compression;
+}
+
+//----------------------------------------------------------------------------
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
m_Name(),
m_TempName(),
m_CopyIfDifferent(false),
- m_Okay(false)
+ m_Okay(false),
+ m_Compress(false)
{
}
@@ -95,7 +106,8 @@ cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
m_Name(name),
m_TempName(name),
m_CopyIfDifferent(false),
- m_Okay(false)
+ m_Okay(false),
+ m_Compress(false)
{
// Create the name of the temporary file.
m_TempName += ".tmp";
@@ -107,22 +119,39 @@ cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
//----------------------------------------------------------------------------
cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
{
+ std::string resname = m_Name;
+ if ( m_Compress )
+ {
+ resname += ".gz";
+ }
+
// Only consider replacing the destination file if no error
// occurred.
if(m_Okay &&
- (!m_CopyIfDifferent ||
- cmSystemTools::FilesDiffer(m_TempName.c_str(), m_Name.c_str())))
+ (!m_CopyIfDifferent ||
+ cmSystemTools::FilesDiffer(m_TempName.c_str(), resname.c_str())))
{
// The destination is to be replaced. Rename the temporary to the
// destination atomically.
- this->RenameFile(m_TempName.c_str(), m_Name.c_str());
- }
- else
- {
- // The destination was not replaced. Just delete the temporary
- // file.
- cmSystemTools::RemoveFile(m_TempName.c_str());
+ if ( m_Compress )
+ {
+ std::string gzname = m_TempName + ".temp.gz";
+ if ( this->CompressFile(m_TempName.c_str(), gzname.c_str()) )
+ {
+ this->RenameFile(gzname.c_str(), resname.c_str());
+ }
+ cmSystemTools::RemoveFile(gzname.c_str());
+ }
+ else
+ {
+ this->RenameFile(m_TempName.c_str(), resname.c_str());
+ }
}
+
+ // Else, the destination was not replaced.
+ //
+ // Always delete the temporary file. We never want it to stay around.
+ cmSystemTools::RemoveFile(m_TempName.c_str());
}
//----------------------------------------------------------------------------
@@ -140,6 +169,41 @@ void cmGeneratedFileStreamBase::Open(const char* name)
}
//----------------------------------------------------------------------------
+int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
+ const char* newname)
+{
+#ifdef CMAKE_BUILD_WITH_CMAKE
+ gzFile gf = cm_zlib_gzopen(newname, "w");
+ 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 )
+ {
+ if ( !cm_zlib_gzwrite(gf, buffer, res) )
+ {
+ fclose(ifs);
+ cm_zlib_gzclose(gf);
+ return 0;
+ }
+ }
+ fclose(ifs);
+ cm_zlib_gzclose(gf);
+ return 1;
+#else
+ return 0;
+#endif
+}
+
+//----------------------------------------------------------------------------
int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
const char* newname)
{