cmCableData.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "cmCableData.h"
  33. #include "cmCacheManager.h"
  34. #include "cmCablePackageCommand.h"
  35. /**
  36. * The cmCableData instance is owned by one cmCableCommand, which is given
  37. * to this constructor.
  38. */
  39. cmCableData::cmCableData(const cmCableCommand* owner,
  40. const std::string& configurationFile):
  41. m_Owner(owner),
  42. m_OutputFileName(configurationFile),
  43. m_OutputFile(configurationFile.c_str()),
  44. m_Indentation(0),
  45. m_Package(NULL),
  46. m_PackageNamespaceDepth(0)
  47. {
  48. this->InitializeOutputFile();
  49. }
  50. /**
  51. * Free all data that was stored here. Also close the output file.
  52. */
  53. cmCableData::~cmCableData()
  54. {
  55. // End last package, if any.
  56. this->EndPackage();
  57. // Finish up the output file.
  58. this->CloseOutputFile();
  59. }
  60. /**
  61. * Write the configuration header to the output file.
  62. */
  63. void cmCableData::InitializeOutputFile()
  64. {
  65. if(m_OutputFile)
  66. {
  67. this->WriteConfigurationHeader();
  68. }
  69. else
  70. {
  71. cmSystemTools::Error("Unable to open CABLE config file: ",
  72. m_OutputFileName.c_str());
  73. }
  74. }
  75. /**
  76. * Close the configuration output file. This writes the configuration
  77. * footer.
  78. */
  79. void cmCableData::CloseOutputFile()
  80. {
  81. if(m_OutputFile)
  82. {
  83. this->WriteConfigurationFooter();
  84. m_OutputFile.close();
  85. }
  86. }
  87. /**
  88. * Write a CABLE configuration file header.
  89. */
  90. void cmCableData::WriteConfigurationHeader()
  91. {
  92. m_OutputFile << m_Indentation << "<?xml version=\"1.0\"?>" << std::endl
  93. << m_Indentation << "<CableConfiguration>" << std::endl;
  94. this->Indent();
  95. }
  96. /**
  97. * Write a CABLE configuration file footer.
  98. */
  99. void cmCableData::WriteConfigurationFooter()
  100. {
  101. this->Unindent();
  102. m_OutputFile << m_Indentation << "</CableConfiguration>" << std::endl;
  103. }
  104. /**
  105. * Print indentation spaces.
  106. */
  107. void
  108. cmCableData::Indentation
  109. ::Print(std::ostream& os) const
  110. {
  111. if(m_Indent <= 0)
  112. { return; }
  113. // Use blocks of 8 spaces to speed up big indents.
  114. unsigned int blockCount = m_Indent >> 3;
  115. unsigned int singleCount = m_Indent & 7;
  116. while(blockCount-- > 0)
  117. {
  118. os << " ";
  119. }
  120. while(singleCount-- > 0)
  121. {
  122. os << " ";
  123. }
  124. }
  125. /**
  126. * Open a namespace with the given name.
  127. */
  128. void cmCableData::OpenNamespace(const std::string& name)
  129. {
  130. m_NamespaceStack.push_back(name);
  131. }
  132. /**
  133. * Close the current namespace, checking whether it has the given name.
  134. */
  135. void cmCableData::CloseNamespace(const std::string& name)
  136. {
  137. if(m_NamespaceStack.empty())
  138. {
  139. cmSystemTools::Error("Unbalanced close-namespace = ", name.c_str());
  140. return;
  141. }
  142. if(m_NamespaceStack.back() != name)
  143. {
  144. cmSystemTools::Error("Wrong name on close-namespace = ", name.c_str());
  145. }
  146. // If this closes the namespace where the current package was opened,
  147. // the package must end as well.
  148. if(m_Package && (m_PackageNamespaceDepth == m_NamespaceStack.size()))
  149. {
  150. this->EndPackage();
  151. }
  152. m_NamespaceStack.pop_back();
  153. }
  154. /**
  155. * Begin a new package definition. If there is a current one, it
  156. * will be ended.
  157. */
  158. void cmCableData::BeginPackage(cmCablePackageCommand* command)
  159. {
  160. // Close the current package, if any.
  161. this->EndPackage();
  162. // Open this package.
  163. m_Package = command;
  164. // Write out the package's header.
  165. m_Package->WritePackageHeader();
  166. // Save the package's opening namespace depth for later verification
  167. // on the end of the package.
  168. m_PackageNamespaceDepth = m_NamespaceStack.size();
  169. }
  170. /**
  171. * End a package definition.
  172. */
  173. void cmCableData::EndPackage()
  174. {
  175. // Make sure we have an open package.
  176. if(!m_Package)
  177. {
  178. return;
  179. }
  180. // Make sure the namespace nesting depth matches the opening depth
  181. // of the package.
  182. if(m_PackageNamespaceDepth != m_NamespaceStack.size())
  183. {
  184. cmSystemTools::Error("Package ended at different namespace depth than"
  185. "it was created!", "");
  186. }
  187. // Write out the package's footer.
  188. m_Package->WritePackageFooter();
  189. // Done with the package.
  190. m_Package = NULL;
  191. }
  192. /**
  193. * Simplify indentation printing by allowing Indentation objects to be added
  194. * to streams.
  195. */
  196. std::ostream& operator<<(std::ostream& os,
  197. const cmCableData::Indentation& indent)
  198. {
  199. indent.Print(os);
  200. return os;
  201. }