cmCPackSTGZGenerator.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackSTGZGenerator.h"
  14. #include "cmake.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmSystemTools.h"
  18. #include "cmMakefile.h"
  19. #include "cmCPackLog.h"
  20. #include <cmsys/ios/sstream>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. //----------------------------------------------------------------------
  24. cmCPackSTGZGenerator::cmCPackSTGZGenerator()
  25. {
  26. }
  27. //----------------------------------------------------------------------
  28. cmCPackSTGZGenerator::~cmCPackSTGZGenerator()
  29. {
  30. }
  31. //----------------------------------------------------------------------
  32. int cmCPackSTGZGenerator::InitializeInternal()
  33. {
  34. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
  35. std::string inFile = this->FindTemplate("CPack.STGZ_Header.sh.in");
  36. if ( inFile.empty() )
  37. {
  38. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find template file: "
  39. << inFile.c_str() << std::endl);
  40. return 0;
  41. }
  42. this->SetOptionIfNotSet("CPACK_STGZ_HEADER_FILE", inFile.c_str());
  43. this->SetOptionIfNotSet("CPACK_AT_SIGN", "@");
  44. return this->Superclass::InitializeInternal();
  45. }
  46. //----------------------------------------------------------------------
  47. int cmCPackSTGZGenerator::CompressFiles(const char* outFileName,
  48. const char* toplevel, const std::vector<std::string>& files)
  49. {
  50. if ( !this->Superclass::CompressFiles(outFileName, toplevel, files) )
  51. {
  52. return 0;
  53. }
  54. return cmSystemTools::SetPermissions(outFileName,
  55. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  56. S_IREAD | S_IWRITE | S_IEXEC
  57. #elif defined( __BORLANDC__ )
  58. S_IRUSR | S_IWUSR | S_IXUSR
  59. #else
  60. S_IRUSR | S_IWUSR | S_IXUSR |
  61. S_IRGRP | S_IWGRP | S_IXGRP |
  62. S_IROTH | S_IWOTH | S_IXOTH
  63. #endif
  64. );
  65. }
  66. //----------------------------------------------------------------------
  67. int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
  68. {
  69. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
  70. cmsys_ios::ostringstream str;
  71. int counter = 0;
  72. std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  73. std::string line;
  74. std::ifstream ilfs(inLicFile.c_str());
  75. std::string licenseText;
  76. while ( cmSystemTools::GetLineFromStream(ilfs, line) )
  77. {
  78. licenseText += line + "\n";
  79. }
  80. this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT",
  81. licenseText.c_str());
  82. const char headerLengthTag[] = "###CPACK_HEADER_LENGTH###";
  83. // Create the header
  84. std::string inFile = this->GetOption("CPACK_STGZ_HEADER_FILE");
  85. std::ifstream ifs(inFile.c_str());
  86. std::string packageHeaderText;
  87. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  88. {
  89. packageHeaderText += line + "\n";
  90. }
  91. // Configure in the values
  92. std::string res;
  93. this->ConfigureString(packageHeaderText, res);
  94. // Count the lines
  95. const char* ptr = res.c_str();
  96. while ( *ptr )
  97. {
  98. if ( *ptr == '\n' )
  99. {
  100. counter ++;
  101. }
  102. ++ptr;
  103. }
  104. counter ++;
  105. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  106. "Number of lines: " << counter << std::endl);
  107. char buffer[1024];
  108. sprintf(buffer, "%d", counter);
  109. cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
  110. // Write in file
  111. *os << res.c_str();
  112. return this->Superclass::GenerateHeader(os);
  113. }