cmGeneratedFileStream.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmGeneratedFileStream_h
  11. #define cmGeneratedFileStream_h
  12. #include <cmConfigure.h>
  13. #include <cmsys/FStream.hxx>
  14. #include <string>
  15. // This is the first base class of cmGeneratedFileStream. It will be
  16. // created before and destroyed after the ofstream portion and can
  17. // therefore be used to manage the temporary file.
  18. class cmGeneratedFileStreamBase
  19. {
  20. protected:
  21. // This constructor does not prepare the temporary file. The open
  22. // method must be used.
  23. cmGeneratedFileStreamBase();
  24. // This constructor prepares the temporary output file.
  25. cmGeneratedFileStreamBase(const char* name);
  26. // The destructor renames the temporary output file to the real name.
  27. ~cmGeneratedFileStreamBase();
  28. // Internal methods to handle the temporary file. Open is always
  29. // called before the real stream is opened. Close is always called
  30. // after the real stream is closed and Okay is set to whether the
  31. // real stream was still valid for writing when it was closed.
  32. void Open(const char* name);
  33. bool Close();
  34. // Internal file replacement implementation.
  35. int RenameFile(const char* oldname, const char* newname);
  36. // Internal file compression implementation.
  37. int CompressFile(const char* oldname, const char* newname);
  38. // The name of the final destination file for the output.
  39. std::string Name;
  40. // The name of the temporary file.
  41. std::string TempName;
  42. // Whether to do a copy-if-different.
  43. bool CopyIfDifferent;
  44. // Whether the real file stream was valid when it was closed.
  45. bool Okay;
  46. // Whether the destination file is compressed
  47. bool Compress;
  48. // Whether the destination file is compressed
  49. bool CompressExtraExtension;
  50. };
  51. /** \class cmGeneratedFileStream
  52. * \brief Output stream for generated files.
  53. *
  54. * File generation should be atomic so that if CMake is killed then a
  55. * generated file is either the original version or the complete new
  56. * version. This stream is used to make sure file generation is
  57. * atomic. Optionally the output file is only replaced if its
  58. * contents have changed to prevent the file modification time from
  59. * being updated.
  60. */
  61. class cmGeneratedFileStream : private cmGeneratedFileStreamBase,
  62. public cmsys::ofstream
  63. {
  64. public:
  65. typedef cmsys::ofstream Stream;
  66. /**
  67. * This constructor prepares a default stream. The open method must
  68. * be used before writing to the stream.
  69. */
  70. cmGeneratedFileStream();
  71. /**
  72. * This constructor takes the name of the file to be generated. It
  73. * automatically generates a name for the temporary file. If the
  74. * file cannot be opened an error message is produced unless the
  75. * second argument is set to true.
  76. */
  77. cmGeneratedFileStream(const char* name, bool quiet = false);
  78. /**
  79. * The destructor checks the stream status to be sure the temporary
  80. * file was successfully written before allowing the original to be
  81. * replaced.
  82. */
  83. ~cmGeneratedFileStream() CM_OVERRIDE;
  84. /**
  85. * Open an output file by name. This should be used only with a
  86. * non-open stream. It automatically generates a name for the
  87. * temporary file. If the file cannot be opened an error message is
  88. * produced unless the second argument is set to true.
  89. */
  90. cmGeneratedFileStream& Open(const char* name, bool quiet = false,
  91. bool binaryFlag = false);
  92. /**
  93. * Close the output file. This should be used only with an open
  94. * stream. The temporary file is atomically renamed to the
  95. * destionation file if the stream is still valid when this method
  96. * is called.
  97. */
  98. bool Close();
  99. /**
  100. * Set whether copy-if-different is done.
  101. */
  102. void SetCopyIfDifferent(bool copy_if_different);
  103. /**
  104. * Set whether compression is done.
  105. */
  106. void SetCompression(bool compression);
  107. /**
  108. * Set whether compression has extra extension
  109. */
  110. void SetCompressionExtraExtension(bool ext);
  111. /**
  112. * Set name of the file that will hold the actual output. This method allows
  113. * the output file to be changed during the use of cmGeneratedFileStream.
  114. */
  115. void SetName(const std::string& fname);
  116. private:
  117. cmGeneratedFileStream(cmGeneratedFileStream const&); // not implemented
  118. };
  119. #endif