cmGeneratedFileStream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // 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 method to setup the instance for a given file name.
  30. void Open(const char* name);
  31. // Internal file replacement implementation.
  32. int RenameFile(const char* oldname, const char* newname);
  33. // Internal file compression implementation.
  34. int CompressFile(const char* oldname, const char* newname);
  35. // The name of the final destination file for the output.
  36. std::string m_Name;
  37. // The name of the temporary file.
  38. std::string m_TempName;
  39. // Whether to do a copy-if-different.
  40. bool m_CopyIfDifferent;
  41. // Whether the destination file should be replaced.
  42. bool m_Okay;
  43. // Whether the destionation file is compressed
  44. bool m_Compress;
  45. };
  46. /** \class cmGeneratedFileStream
  47. * \brief Output stream for generated files.
  48. *
  49. * File generation should be atomic so that if CMake is killed then a
  50. * generated file is either the original version or the complete new
  51. * version. This stream is used to make sure file generation is
  52. * atomic. Optionally the output file is only replaced if its
  53. * contents have changed to prevent the file modification time from
  54. * being updated.
  55. */
  56. class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
  57. public std::ofstream
  58. {
  59. public:
  60. typedef std::ofstream Stream;
  61. /**
  62. * This constructor prepares a default stream. The open method must
  63. * be used before writing to the stream.
  64. */
  65. cmGeneratedFileStream();
  66. /**
  67. * This constructor takes the name of the file to be generated. It
  68. * automatically generates a name for the temporary file. If the
  69. * file cannot be opened an error message is produced unless the
  70. * second argument is set to true.
  71. */
  72. cmGeneratedFileStream(const char* name, bool quiet=false);
  73. /**
  74. * The destructor checks the stream status to be sure the temporary
  75. * file was successfully written before allowing the original to be
  76. * replaced.
  77. */
  78. ~cmGeneratedFileStream();
  79. /**
  80. * Open an output file by name. This should be used only with a
  81. * default-constructed stream. It automatically generates a name
  82. * for the temporary file. If the file cannot be opened an error
  83. * message is produced unless the second argument is set to true.
  84. */
  85. cmGeneratedFileStream& Open(const char* name, bool quiet=false);
  86. /**
  87. * Set whether copy-if-different is done.
  88. */
  89. void SetCopyIfDifferent(bool copy_if_different);
  90. /**
  91. * Set whether compression is done.
  92. */
  93. void SetCompression(bool compression);
  94. };
  95. #endif