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;
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;
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);
void set_Parameters(const string &key, const Parameters &value)
int parse_stream(std::istream &iss, Parameters ¶ms)
static void abort()
terminate communicator
void general(const char *format,...)
int parse_line(char *buf, string &key, string &value)
void parse(std::istream &iss, Parameters ¶ms)
static int broadcast(size_t size, void *data, int sender)
int parse_vector(char *buf, vector< string > &vec)
static const std::string class_name
void set_string(const string &key, const string &value)
Common parameter class: provides parameters as singleton.
void paranoiac(const char *format,...)
static const string class_name
void crucial(const char *format,...)
void read_params(const std::string ¶ms_file, Parameters ¶ms)
read parameters from file.
Bridge::VerboseLevel m_vl
Bridge::VerboseLevel m_vl
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)