1
0

cmGeneratedFileStream.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #ifndef cmGeneratedFileStream_h
  33. #define cmGeneratedFileStream_h
  34. #include "cmStandardIncludes.h"
  35. /** \class cmGeneratedFileStream
  36. * \brief Output stream for generated files that does copy-if-different.
  37. *
  38. * Many files generated by CMake don't change each time they are generated.
  39. * This class can be used in place of std::ofstream to open a file and
  40. * write output to it. The class will automatically write output to a
  41. * temporary file and copy it over an existing file only if the generated
  42. * file has changed.
  43. */
  44. class cmGeneratedFileStream
  45. {
  46. public:
  47. /**
  48. * The constructor takes the name of the file to be generated. It
  49. * automatically generates a name for the temporary file.
  50. */
  51. cmGeneratedFileStream(const char* name):
  52. m_Name(name),
  53. m_TempName(m_Name+".tmp"),
  54. m_Stream(m_TempName.c_str()),
  55. m_Copied(false),
  56. m_AlwaysCopy(false)
  57. {}
  58. /**
  59. * The destructor ensures that the file has been closed and copied if
  60. * it has changed.
  61. */
  62. ~cmGeneratedFileStream() { this->DoCopy(); }
  63. /**
  64. * Get the real output stream.
  65. */
  66. std::ostream& GetStream() { return m_Stream; }
  67. /**
  68. * Allow a test for whether the file is open.
  69. */
  70. operator bool() { return m_Stream.good(); }
  71. /**
  72. * Close the file stream. This will cause the copy-if-different to the
  73. * real file name to occur.
  74. */
  75. void close() { this->DoCopy(); }
  76. /**
  77. * If always copy is true, then copy the file all the time without
  78. * checking for differences. The default is false.
  79. */
  80. bool SetAlwaysCopy(bool v) { m_AlwaysCopy = v; return v;}
  81. private:
  82. /**
  83. * The name of the real file where output will be copied if it has changed.
  84. */
  85. std::string m_Name;
  86. /**
  87. * The name of the temporary file.
  88. */
  89. std::string m_TempName;
  90. /**
  91. * The real output stream used to write to the file.
  92. */
  93. std::ofstream m_Stream;
  94. /**
  95. * Whether the temporary file has already been copied to the real file.
  96. */
  97. bool m_Copied;
  98. /**
  99. * If always copy is true, then copy the file all the time without
  100. * checking for differences. The default is false.
  101. */
  102. bool m_AlwaysCopy;
  103. /**
  104. * Closes the temporary file and does the copy-if-different to the
  105. * real file.
  106. */
  107. void DoCopy()
  108. {
  109. if(!m_Copied)
  110. {
  111. m_Stream.close();
  112. if(m_AlwaysCopy)
  113. {
  114. cmSystemTools::cmCopyFile(m_TempName.c_str(), m_Name.c_str());
  115. }
  116. else
  117. {
  118. cmSystemTools::CopyFileIfDifferent(m_TempName.c_str(),
  119. m_Name.c_str());
  120. }
  121. cmSystemTools::RemoveFile(m_TempName.c_str());
  122. m_Copied = true;
  123. }
  124. }
  125. };
  126. /**
  127. * Allow a cmGeneratedFileStream to be used just as a real std::ostream
  128. * would be.
  129. */
  130. template <class T>
  131. std::ostream& operator << (cmGeneratedFileStream& l, const T& r)
  132. {
  133. std::ostream& os = l.GetStream();
  134. os << r;
  135. return os;
  136. }
  137. #endif