cmGeneratedFileStream.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // The constructor prepares the temporary output file.
  23. cmGeneratedFileStreamBase(const char* name);
  24. // The destructor renames the temporary output file to the real name.
  25. ~cmGeneratedFileStreamBase();
  26. // Internal file replacement implementation.
  27. int RenameFile(const char* oldname, const char* newname);
  28. // The name of the final destination file for the output.
  29. std::string m_Name;
  30. // The name of the temporary file.
  31. std::string m_TempName;
  32. // Whether to do a copy-if-different.
  33. bool m_CopyIfDifferent;
  34. // Whether the destination file should be replaced.
  35. bool m_Okay;
  36. };
  37. /** \class cmGeneratedFileStream
  38. * \brief Output stream for generated files.
  39. *
  40. * File generation should be atomic so that if CMake is killed then a
  41. * generated file is either the original version or the complete new
  42. * version. This stream is used to make sure file generation is
  43. * atomic. Optionally the output file is only replaced if its
  44. * contents have changed to prevent the file modification time from
  45. * being updated.
  46. */
  47. class cmGeneratedFileStream: private cmGeneratedFileStreamBase,
  48. public std::ofstream
  49. {
  50. public:
  51. typedef std::ofstream Stream;
  52. /**
  53. * The constructor takes the name of the file to be generated. It
  54. * automatically generates a name for the temporary file. If the
  55. * file cannot be opened an error message is produced unless the
  56. * second argument is set to true.
  57. */
  58. cmGeneratedFileStream(const char* name, bool quiet=false);
  59. /**
  60. * The destructor checks the stream status to be sure the temporary
  61. * file was successfully written before allowing the original to be
  62. * replaced.
  63. */
  64. ~cmGeneratedFileStream();
  65. /**
  66. * Set whether copy-if-different is done.
  67. */
  68. void SetCopyIfDifferent(bool copy_if_different);
  69. };
  70. #endif