cmGeneratedFileStream.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, bool copy_if_different);
  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. /**
  52. * The constructor takes the name of the file to be generated. It
  53. * automatically generates a name for the temporary file. The
  54. * second argument specifies whether the copy-if-different check
  55. * should be done. If the file cannot be opened an error message is
  56. * produced unless the third argument is set to true.
  57. */
  58. cmGeneratedFileStream(const char* name, bool copy_if_different,
  59. bool quiet = false);
  60. /**
  61. * The destructor checks the stream status to be sure the temporary
  62. * file was successfully written before allowing the original to be
  63. * replaced.
  64. */
  65. ~cmGeneratedFileStream();
  66. };
  67. #endif