You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.9 KiB

  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. #ifndef cmGeneratedFileStream_h
  14. #define cmGeneratedFileStream_h
  15. #include "cmStandardIncludes.h"
  16. #if defined(__sgi) && !defined(__GNUC__)
  17. # pragma set woff 1375 /* base class destructor not virtual */
  18. #endif
  19. // This is the first base class of cmGeneratedFileStream. It will be
  20. // created before and destroyed after the ofstream portion and can
  21. // therefore be used to manage the temporary file.
  22. class cmGeneratedFileStreamBase
  23. {
  24. protected:
  25. // This constructor does not prepare the temporary file. The open
  26. // method must be used.
  27. cmGeneratedFileStreamBase();
  28. // This constructor prepares the temporary output file.
  29. cmGeneratedFileStreamBase(const char* name);
  30. // The destructor renames the temporary output file to the real name.
  31. ~cmGeneratedFileStreamBase();
  32. // Internal methods to handle the temporary file. Open is always
  33. // called before the real stream is opened. Close is always called
  34. // after the real stream is closed and Okay is set to whether the
  35. // real stream was still valid for writing when it was closed.
  36. void Open(const char* name);
  37. bool Close();
  38. // Internal file replacement implementation.
  39. int RenameFile(const char* oldname, const char* newname);
  40. // Internal file compression implementation.
  41. int CompressFile(const char* oldname, const char* newname);
  42. // The name of the final destination file for the output.
  43. std::string Name;
  44. // The name of the temporary file.
  45. std::string TempName;
  46. // Whether to do a copy-if-different.
  47. bool CopyIfDifferent;
  48. // Whether the real file stream was valid when it was closed.
  49. bool Okay;
  50. // Whether the destionation file is compressed
  51. bool Compress;
  52. // Whether the destionation file is compressed
  53. bool CompressExtraExtension;
  54. };
  55. /** \class cmGeneratedFileStream
  56. * \brief Output stream for generated files.
  57. *
  58. * File generation should be atomic so that if CMake is killed then a
  59. * generated file is either the original version or the complete new
  60. * version. This stream is used to make sure file generation is
  61. * atomic. Optionally the output file is only replaced if its
  62. * contents have changed to prevent the file modification time from
  63. * being updated.
  64. */
  65. class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
  66. public std::ofstream
  67. {
  68. public:
  69. typedef std::ofstream Stream;
  70. /**
  71. * This constructor prepares a default stream. The open method must
  72. * be used before writing to the stream.
  73. */
  74. cmGeneratedFileStream();
  75. /**
  76. * This constructor takes the name of the file to be generated. It
  77. * automatically generates a name for the temporary file. If the
  78. * file cannot be opened an error message is produced unless the
  79. * second argument is set to true.
  80. */
  81. cmGeneratedFileStream(const char* name, bool quiet=false);
  82. /**
  83. * The destructor checks the stream status to be sure the temporary
  84. * file was successfully written before allowing the original to be
  85. * replaced.
  86. */
  87. ~cmGeneratedFileStream();
  88. /**
  89. * Open an output file by name. This should be used only with a
  90. * non-open stream. It automatically generates a name for the
  91. * temporary file. If the file cannot be opened an error message is
  92. * produced unless the second argument is set to true.
  93. */
  94. cmGeneratedFileStream& Open(const char* name, bool quiet=false,
  95. bool binaryFlag=false);
  96. /**
  97. * Close the output file. This should be used only with an open
  98. * stream. The temporary file is atomically renamed to the
  99. * destionation file if the stream is still valid when this method
  100. * is called.
  101. */
  102. bool Close();
  103. /**
  104. * Set whether copy-if-different is done.
  105. */
  106. void SetCopyIfDifferent(bool copy_if_different);
  107. /**
  108. * Set whether compression is done.
  109. */
  110. void SetCompression(bool compression);
  111. /**
  112. * Set whether compression has extra extension
  113. */
  114. void SetCompressionExtraExtension(bool ext);
  115. /**
  116. * Set name of the file that will hold the actual output. This method allows
  117. * the output file to be changed during the use of cmGeneratedFileStream.
  118. */
  119. void SetName(const char* fname);
  120. private:
  121. cmGeneratedFileStream(cmGeneratedFileStream const&); // not implemented
  122. };
  123. #if defined(__sgi) && !defined(__GNUC__)
  124. # pragma reset woff 1375 /* base class destructor not virtual */
  125. #endif
  126. #endif