forked from ismrmrd/siemens_to_ismrmrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXNode.cpp
217 lines (186 loc) · 5.67 KB
/
XNode.cpp
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
#include "XNode.h"
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <stdlib.h>
#include <sstream>
namespace XProtocol
{
const XNode* getChildNodeByName::operator()(const XNodeParamArray& node) const {
unsigned int index = static_cast<unsigned int>(std::atoi(level_.c_str()));
const_cast<XNodeParamArray&>(node).expand_children();
const XNode* ret = 0;
if (index < node.children_.size()) {
ret = &node.children_[index];
}
if (!ret) {
return 0;
}
if (has_sublevels_) {
ret = boost::apply_visitor(getChildNodeByName(sublevel_), *ret);
}
return ret;
}
const XNode* getChildNodeByName::operator()(const XNodeParamMap& node) const {
const XNode* ret = 0;
BOOST_FOREACH(XNode const& cnode, node.children_) {
const std::string& name = boost::apply_visitor(getNodeName(), cnode);
if (boost::iequals(name,level_)) {
//if (name.compare(level_) == 0) {
ret = &cnode;
break;
}
}
if (!ret) {
return 0;
}
if (has_sublevels_) {
ret = boost::apply_visitor(getChildNodeByName(sublevel_), *ret);
}
return ret;
}
const XNode* getChildNodeByName::operator()(const XNodeParamValue& node) const {
//Values don't have children
return 0;
}
const XNode* getChildNodeByIndex::operator()(const XNodeParamMap& node)
{
const XNode* ret = 0;
if (node.children_.size() > index_) {
ret = &node.children_[index_];
}
return ret;
}
const XNode* getChildNodeByIndex::operator()(const XNodeParamArray& node)
{
const XNode* ret = 0;
if (node.children_.size() == 0) { //Children have not yet been expanded
if (!const_cast<XNodeParamArray&>(node).expand_children()) {
std::cout << "Failed to expand children" << std::endl;
return 0;
}
}
if (node.children_.size() > index_) {
ret = &node.children_[index_];
}
return ret;
}
const XNode* getChildNodeByIndex::operator()(const XNodeParamValue& node)
{
return 0; //Param values do not have children
}
std::string getXMLString::operator()(const XNodeParamMap& node) const
{
std::stringstream str;
str << "<" << node.name_ << ">" << std::endl;
BOOST_FOREACH(XNode const& cnode, node.children_) {
str << boost::apply_visitor(getXMLString(), cnode);
}
str << "</" << node.name_ << ">" << std::endl;
return str.str();
}
std::string getXMLString::operator()(const XNodeParamArray& node) const
{
if (node.children_.size() == 0) { //Children have not yet been expanded
if (!const_cast<XNodeParamArray&>(node).expand_children()) {
std::cout << "Failed to expand children" << std::endl;
return 0;
}
}
std::stringstream str;
str << "<" << node.name_ << ">" << std::endl;
BOOST_FOREACH(XNode const& cnode, node.children_) {
str << boost::apply_visitor(getXMLString(), cnode);
}
/*
for (unsigned int i = 0; i < node.values_.size(); i++) {
str << "<" << node.name_ << ">" << node.values_[i] << "</" << node.name_ << ">" << std::endl;
}
*/
str << "</" << node.name_ << ">" << std::endl;
return str.str();
}
std::string getXMLString::operator()(const XNodeParamValue& node) const
{
std::stringstream str;
for (unsigned int i = 0; i < node.values_.size(); i++) {
str << "<" << node.name_ << ">" << node.values_[i] << "</" << node.name_ << ">" << std::endl;
}
return str.str();
}
bool setNodeValues :: operator()(XNodeParamMap& node) {
//std::cout << "Calling param map set values....";
if (node.children_.size() < val_.children_.size()) {
std::cout << "Mismatch between number of values (" << val_.children_.size() << ") and children (" << node.children_.size() << ")" << std::endl;
std::cout << "node name: " << node.name_ << std::endl;
return false;
}
for (unsigned int i = 0; i < val_.children_.size(); i++) {
setNodeValues tmp(val_.children_[i]);
if (!boost::apply_visitor(tmp, node.children_[i])) {
return false;
}
}
//std::cout << "done!" << std::endl;
return true;
}
bool setNodeValues :: operator()(XNodeParamArray& node) {
node.values_.clear();
for (unsigned int i = 0; i < val_.children_.size(); i++) {
node.values_.push_back(val_.children_[i]);
}
node.expand_children();
return true;
}
bool setNodeValues :: operator()(XNodeParamValue& node) {
//std::cout << "Calling param value set values....";
for (unsigned int i = 0; i < val_.values_.size(); i++) {
node.values_.push_back(val_.values_[i]);
}
//std::cout << "done!" << std::endl;
return true;
}
bool XNodeParamArray :: expand_children() {
if (!children_.size()) {
for (unsigned int i = 0; i < values_.size(); i++) {
children_.push_back(default_);
if (values_[i].values_.size() == 0 && values_[i].children_.size()==0) { //Empty values container, occurs sometimes in the files.
//std::cout << "Empty container encountered, " << name_ << std::endl;
continue;
}
setNodeValues tmp(values_[i]);
if (!boost::apply_visitor(tmp, children_[i])) {
return false;
}
}
}
return true;
}
std::vector<std::string> getStringValueArray::operator()(const XNodeParamArray& node) const
{
//std::cout << "Array: " << node.name_ << ", node.values_.size() = " << node.values_.size() << std::endl;
std::vector<std::string> ret;
return ret;
}
std::vector<std::string> getStringValueArray::operator()(const XNodeParamMap& node) const
{
std::vector<std::string> ret;
return ret;
}
std::vector<std::string> getStringValueArray::operator()(const XNodeParamValue& node) const
{
std::vector<std::string> ret;
for (unsigned int i = 0; i < node.values_.size(); i++) {
std::stringstream ss;
std::string svalue;
ss << node.values_[i];
ss >> svalue;
ret.push_back(svalue);
}
return ret;
}
}