cmGeneratedFileStream.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 m_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. void 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 m_Name;
  44. // The name of the temporary file.
  45. std::string m_TempName;
  46. // Whether to do a copy-if-different.
  47. bool m_CopyIfDifferent;
  48. // Whether the real file stream was valid when it was closed.
  49. bool m_Okay;
  50. // Whether the destionation file is compressed
  51. bool m_Compress;
  52. };
  53. /** \class cmGeneratedFileStream
  54. * \brief Output stream for generated files.
  55. *
  56. * File generation should be atomic so that if CMake is killed then a
  57. * generated file is either the original version or the complete new
  58. * version. This stream is used to make sure file generation is
  59. * atomic. Optionally the output file is only replaced if its
  60. * contents have changed to prevent the file modification time from
  61. * being updated.
  62. */
  63. class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
  64. public std::ofstream
  65. {
  66. public:
  67. typedef std::ofstream Stream;
  68. /**
  69. * This constructor prepares a default stream. The open method must
  70. * be used before writing to the stream.
  71. */
  72. cmGeneratedFileStream();
  73. /**
  74. * This constructor takes the name of the file to be generated. It
  75. * automatically generates a name for the temporary file. If the
  76. * file cannot be opened an error message is produced unless the
  77. * second argument is set to true.
  78. */
  79. cmGeneratedFileStream(const char* name, bool quiet=false);
  80. /**
  81. * The destructor checks the stream status to be sure the temporary
  82. * file was successfully written before allowing the original to be
  83. * replaced.
  84. */
  85. ~cmGeneratedFileStream();
  86. /**
  87. * Open an output file by name. This should be used only with a
  88. * non-open stream. It automatically generates a name for the
  89. * temporary file. If the file cannot be opened an error message is
  90. * produced unless the second argument is set to true.
  91. */
  92. cmGeneratedFileStream& Open(const char* name, bool quiet=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. cmGeneratedFileStream& 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 name of the file that will hold the actual output. This method allows
  110. * the output file to be changed during the use of cmGeneratedFileStream.
  111. */
  112. void SetName(const char* fname);
  113. };
  114. #if defined(__sgi) && !defined(__GNUC__)
  115. # pragma reset woff 1375 /* base class destructor not virtual */
  116. #endif
  117. #endif