summaryrefslogtreecommitdiffstats
path: root/Source/cmCableData.cxx
blob: 0c0058166ca7690c26b503b87ef400e4e555751a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) 2000 National Library of Medicine
  All rights reserved.

  See COPYRIGHT.txt for copyright details.

=========================================================================*/
#include "cmCableData.h"
#include "cmCacheManager.h"

#include "cmCablePackageCommand.h"

/**
 * The cmCableData instance is owned by one cmCableCommand, which is given
 * to this constructor.
 */
cmCableData::cmCableData(const cmCableCommand* owner,
                         const std::string& configurationFile):
  m_Owner(owner),
  m_OutputFileName(configurationFile),
  m_OutputFile(configurationFile.c_str()),
  m_Indentation(0),
  m_Package(NULL),
  m_PackageNamespaceDepth(0),
  m_PackageClassIndex(-1)
{
  this->InitializeOutputFile();
}


/**
 * Free all data that was stored here.  Also close the output file.
 */
cmCableData::~cmCableData()
{
  // End last package, if any.
  this->EndPackage();
  
  // Finish up the output file.
  this->CloseOutputFile();
}


/**
 * Write the configuration header to the output file.
 */
void cmCableData::InitializeOutputFile()
{
  if(m_OutputFile)
    {
    this->WriteConfigurationHeader();
    }
  else
    {
    cmSystemTools::Error("Unable to open CABLE config file: ",
                         m_OutputFileName.c_str());
    }
}


/**
 * Close the configuration output file.  This writes the configuration
 * footer.
 */
void cmCableData::CloseOutputFile()
{
  if(m_OutputFile)
    {
    this->WriteConfigurationFooter();
    m_OutputFile.close();
    }
}


/**
 * Write a CABLE configuration file header.
 */
void cmCableData::WriteConfigurationHeader()
{
  m_OutputFile << m_Indentation << "<?xml version=\"1.0\"?>" << std::endl
               << m_Indentation << "<CableConfiguration>" << std::endl;  
  this->Indent();
}


/**
 * Write a CABLE configuration file footer.
 */
void cmCableData::WriteConfigurationFooter()
{
  this->Unindent();
  m_OutputFile << m_Indentation << "</CableConfiguration>" << std::endl;
}


/**
 * Print indentation spaces.
 */
void
cmCableData::Indentation
::Print(std::ostream& os) const
{
  if(m_Indent <= 0)
    { return; }
  
  // Use blocks of 8 spaces to speed up big indents.
  unsigned int blockCount = m_Indent >> 3;
  unsigned int singleCount = m_Indent & 7;
  while(blockCount-- > 0)
    {
    os << "        ";
    }
  while(singleCount-- > 0)
    {
    os << " ";
    }
}


/**
 * Open a namespace with the given name.
 */
void cmCableData::OpenNamespace(const std::string& name)
{
  m_NamespaceStack.push_back(name);
}


/**
 * Close the current namespace, checking whether it has the given name.
 */
void cmCableData::CloseNamespace(const std::string& name)
{
  if(m_NamespaceStack.empty())
    {
    cmSystemTools::Error("Unbalanced close-namespace = ", name.c_str());
    return;
    }
  if(m_NamespaceStack.back() != name)
    {
    cmSystemTools::Error("Wrong name on close-namespace = ", name.c_str());
    }
  
  // If this closes the namespace where the current package was opened,
  // the package must end as well.
  if(m_Package && (m_PackageNamespaceDepth == m_NamespaceStack.size()))
    {
    this->EndPackage();
    }
  
  m_NamespaceStack.pop_back();
}


/**
 * Begin a new package definition.  If there is a current one, it
 * will be ended.
 */
void cmCableData::BeginPackage(cmCablePackageCommand* command)
{
  // Close the current package, if any.
  this->EndPackage();
  
  // Open this package.
  m_Package = command;
  
  // Write out the package's header.
  m_Package->WritePackageHeader();
  
  // Save the package's opening namespace depth for later verification
  // on the end of the package.
  m_PackageNamespaceDepth = m_NamespaceStack.size();
}


/**
 * End a package definition.
 */
void cmCableData::EndPackage()
{
  // Make sure we have an open package.
  if(!m_Package)
    {
    return;
    }
  
  // Make sure the namespace nesting depth matches the opening depth
  // of the package.
  if(m_PackageNamespaceDepth != m_NamespaceStack.size())
    {
    cmSystemTools::Error("Package ended at different namespace depth than"
                         "it was created!", "");
    }
  // Write out the package's footer.
  m_Package->WritePackageFooter();
  
  // Done with the package.
  m_Package = NULL;
}


/**
 * Simplify indentation printing by allowing Indentation objects to be added
 * to streams.
 */
std::ostream& operator<<(std::ostream& os,
                         const cmCableData::Indentation& indent)
{  
  indent.Print(os);
  return os;
}