cmGeneratedFileStream.h 4.8 KB

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