15 #ifdef USE_TINYXML2LIB
27 using namespace tinyxml2;
36 const std::string ParameterManager_XML::class_name =
"ParameterManager_XML";
42 static const string class_name;
45 int parse(
const char *buf,
Parameters& params);
52 int store_map_data(
const XMLElement *,
Parameters&);
53 int store_vector_data(
const XMLElement *, vector<string>&);
55 void traverse(
const XMLElement *,
const string& indent =
"")
const;
62 const string Parser_tinyxml::class_name =
"ParameterManager_XML::Parser_tinyxml";
65 int Parser_tinyxml::parse(
const char *buf,
Parameters& params)
67 if (!buf)
return EXIT_FAILURE;
71 XMLError err = doc.Parse(buf);
73 if (err != XML_NO_ERROR) {
74 vout.
crucial(m_vl,
"Error at %s: parse failed: %s\n", class_name.c_str(), doc.ErrorName());
76 const char *err_str1 = doc.GetErrorStr1();
80 const char *err_str2 = doc.GetErrorStr2();
88 XMLElement *root = doc.FirstChildElement(
"Parameters");
90 vout.
crucial(m_vl,
"Error at %s: tag \"Parameters\" not found.\n", class_name.c_str());
96 for (
const XMLElement *e = root->FirstChildElement(); e; e = e->NextSiblingElement()) {
97 int retv = store_map_data(e, params);
99 if (retv != EXIT_SUCCESS) {
100 vout.
crucial(m_vl,
"Error at %s: parse failed.\n", class_name.c_str());
110 int Parser_tinyxml::store_vector_data(
const XMLElement *elem, vector<string>& v)
112 if (!elem)
return EXIT_FAILURE;
114 const XMLElement *ee = elem->FirstChildElement();
115 if (!ee)
return EXIT_FAILURE;
120 if (strcmp(ee->Name(),
"value") == 0) {
123 for (
const XMLElement *e = elem->FirstChildElement(); e; e = e->NextSiblingElement()) {
126 if (e->QueryIntAttribute(
"id", &
id) == XML_SUCCESS) {
133 fprintf(stderr,
"Error. inappropriate id\n");
137 if (
id > max_index) max_index = id;
139 mm[id] = e->GetText();
143 for (
const XMLElement *e = elem->FirstChildElement(); e; e = e->NextSiblingElement()) {
146 string tag = e->Name();
150 }
else if (tag ==
"y") {
152 }
else if (tag ==
"z") {
154 }
else if (tag ==
"t") {
156 }
else if (tag ==
"w") {
159 fprintf(stderr,
"unknown tag.\n");
163 if (
id > max_index) max_index = id;
165 mm[id] = e->GetText();
169 vector<string> vv(max_index + 1);
171 for (map<int, string>::const_iterator p = mm.begin(); p != mm.end(); ++p) {
172 vv[p->first] = p->second;
182 int Parser_tinyxml::store_map_data(
const XMLElement *elem,
Parameters& params)
184 if (!elem)
return EXIT_FAILURE;
186 const char *elem_name = elem->Name();
187 const char *elem_value = elem->GetText();
189 if (elem_value != NULL) {
196 const char *elem_attr = elem->Attribute(
"type");
198 if (elem_attr && (strcmp(elem_attr,
"sequence") == 0)) {
200 int retv = store_vector_data(elem, v);
202 if (retv == EXIT_SUCCESS) {
212 for (
const XMLElement *e = elem->FirstChildElement(); e; e = e->NextSiblingElement()) {
213 int retv = store_map_data(e, pp);
215 if (retv != EXIT_SUCCESS) {
228 void Parser_tinyxml::traverse(
const XMLElement *elem,
const string& indent)
const
232 const char *elem_name = elem->Name();
233 const char *elem_value = elem->GetText();
235 if (elem_value != NULL) {
236 vout.
general(m_vl,
"%sElementName = %s, value = %s\n", indent.c_str(), elem_name, elem_value);
239 vout.
general(m_vl,
"%sElementName = %s\n", indent.c_str(), elem_name);
241 const char *elem_attr = elem->Attribute(
"type");
242 if (elem_attr && (strcmp(elem_attr,
"sequence") == 0)) {
243 vout.
general(m_vl,
"%s- is a sequence.\n", indent.c_str());
246 for (
const XMLElement *e = elem->FirstChildElement(); e; e = e->NextSiblingElement()) {
247 traverse(e, indent +
" ");
256 void ParameterManager_XML::read_params(
const std::string& params_file,
Parameters& params)
258 const int io_node = 0;
265 std::ifstream fin(params_file.c_str());
267 vout.
crucial(m_vl,
"Error at %s: unable to read parameter file: %s.\n", class_name.c_str(), params_file.c_str());
272 fin.seekg(0, std::ios::end);
273 filesize = fin.tellg();
274 fin.seekg(0, std::ios::beg);
276 int padding = 8 - (filesize % 8);
278 vout.
paranoiac(m_vl,
"%s::%s: filesize = %d, padding = %d\n", class_name.c_str(), __func__, filesize, padding);
284 buf =
new char [filesize];
285 memset(buf, 0, filesize);
287 fin.read(buf, filesize - padding);
295 buf =
new char [filesize];
296 memset(buf, 0, filesize);
301 Parser_tinyxml().parse(buf, params);
void set_Parameters(const string &key, const Parameters &value)
static void abort()
terminate communicator
void general(const char *format,...)
static int broadcast(size_t size, void *data, int sender)
void set_string(const string &key, const string &value)
Common parameter class: provides parameters as singleton.
void paranoiac(const char *format,...)
void crucial(const char *format,...)
static int broadcast(int count, double *data, int sender)
broadcast array of double from sender.
static int nodeid()
alternative name for self().
void set_string_vector(const string &key, const vector< string > &value)