25 #include <yaml-cpp/yaml.h>
45 static const string class_name;
48 void parse(std::istream& fin,
Parameters& params);
55 int store_map_data(
const YAML::Node&,
Parameters&);
57 int store_vector_data(
const YAML::Node&, vector<string>&);
59 string type_string(
const YAML::Node&)
const;
60 int traverse(
const YAML::Node&,
const string& indent =
"")
const;
66 const string Parser_yamlcpp::class_name =
"ParameterManager_YAML::Parser_yamlcpp";
69 void Parser_yamlcpp::parse(std::istream& fin,
Parameters& params)
71 YAML::Node node = YAML::Load(fin);
73 int result = store_map_data(node, params);
75 if (result != EXIT_SUCCESS) {
76 vout.
crucial(m_vl,
"%s: parse failed.\n", class_name.c_str());
83 int Parser_yamlcpp::store_map_data(
const YAML::Node& node,
Parameters& params)
86 vout.
general(m_vl,
"Error: map expected. found %s\n", type_string(node).c_str());
90 int retv = EXIT_SUCCESS;
92 for (YAML::const_iterator p = node.begin(); p != node.end(); ++p) {
93 string key = p->first.as<
string>();
95 if (p->second.IsScalar()) {
96 string value = p->second.as<
string>();
99 }
else if (p->second.IsSequence()) {
102 if (store_vector_data(p->second, v) != EXIT_SUCCESS) {
107 }
else if (p->second.IsMap()) {
109 store_map_data(p->second, pp);
113 vout.
general(m_vl,
"Error: unexpected type %s\n", type_string(p->second).c_str());
123 int Parser_yamlcpp::store_vector_data(
const YAML::Node& node, vector<string>& v)
125 int retv = EXIT_SUCCESS;
127 if (!node.IsSequence()) {
128 vout.
general(m_vl,
"Error: vector expected. found %s\n", type_string(node).c_str());
132 for (
size_t i = 0; i < node.size(); ++i) {
133 if (!node[i].IsScalar()) {
134 vout.
general(m_vl,
"Error: item[%zu] scalar expected. found %s\n", i, type_string(node).c_str());
137 v.push_back(node[i].as<string>());
146 string Parser_yamlcpp::type_string(
const YAML::Node& node)
const
150 case YAML::NodeType::Undefined:
151 return "UndefinedType";
153 case YAML::NodeType::Null:
156 case YAML::NodeType::Scalar:
159 case YAML::NodeType::Sequence:
162 case YAML::NodeType::Map:
173 int Parser_yamlcpp::traverse(
const YAML::Node& node,
const string& indent)
const
177 case YAML::NodeType::Null:
181 case YAML::NodeType::Scalar:
182 vout.
general(
"%sScalar type \"%s\"\n", indent.c_str(), node.as<
string>().c_str());
185 case YAML::NodeType::Sequence:
187 for (
size_t i = 0; i < node.size(); ++i) {
189 traverse(node[i], indent +
string(
" "));
193 case YAML::NodeType::Map:
195 for (YAML::const_iterator p = node.begin(); p != node.end(); ++p) {
196 vout.
general(
"%s%s:\n", indent.c_str(), p->first.as<
string>().c_str());
197 traverse(p->second, indent +
string(
" "));
227 int parse_line(
char *buf,
string& key,
string& value);
241 if (result != EXIT_SUCCESS) {
252 const char sep =
',';
256 if ((buf[0] !=
'[') || (buf[strlen(buf) - 1] !=
']')) {
260 buf[strlen(buf) - 1] =
'\0';
273 char *q = strchr(p, sep);
286 vec.push_back(
string(p));
295 char *r = p + strlen(p) - 1;
296 while (r >= p && *r ==
' ')
302 vec.push_back(
string(p));
321 const char delim =
':';
324 if (
char *q = strchr(buf,
'#')) { *q =
'\0'; }
327 char *s = buf + strlen(buf) - 1;
328 while (s >= buf && *s ==
' ')
344 char *q = strchr(buf, delim);
358 while (r >= p && *r ==
' ')
382 int retv = EXIT_SUCCESS;
384 const size_t buf_size = 1024;
387 typedef pair<string, Parameters *> env_t;
388 typedef pair<int, env_t> level_t;
389 typedef std::stack<level_t> stack_t;
394 int current_indent = 0;
396 bool expect_map =
false;
398 while (iss.getline(buf, buf_size))
410 if (indent > current_indent) {
419 current_indent = indent;
426 level_t lv = levels.top();
429 string key = lv.second.first;
437 current_params = stored_params;
438 current_indent = lv.first;
443 if (indent < current_indent) {
444 while (indent < current_indent)
446 level_t lv = levels.top();
449 string key = lv.second.first;
453 delete current_params;
456 current_params = stored_params;
457 current_indent = lv.first;
465 if (value.length() > 0) {
466 if (value[0] ==
'[') {
467 memset(buf,
'\0', buf_size);
468 value.copy(buf, buf_size);
490 levels.push(level_t(indent, env_t(key, current_params)));
494 while (current_indent > 0)
496 level_t lv = levels.top();
499 string key = lv.second.first;
503 delete current_params;
506 current_params = stored_params;
507 current_indent = lv.first;
519 #ifdef USE_YAMLCPPLIB
520 Parser_yamlcpp().parse(fin, params);
530 const int io_node = 0;
535 if (Communicator::nodeid() == io_node) {
537 std::ifstream fin(params_file.c_str());
544 fin.seekg(0, std::ios::end);
545 filesize = fin.tellg();
546 fin.seekg(0, std::ios::beg);
548 int padding = 8 - (filesize % 8);
556 buf =
new char [filesize];
557 memset(buf, 0, filesize);
559 fin.read(buf, filesize - padding);
567 buf =
new char [filesize];
568 memset(buf, 0, filesize);
573 std::istringstream iss(buf);