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
|
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/ Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef YAML_READER_H
#define YAML_READER_H
#include "yaml.h"
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
template <typename ConsumerClass> class YamlReader {
private:
enum StateValue {
START,
ACCEPT_KEY,
ACCEPT_VALUE,
STOP,
ERROR,
};
StateValue state;
bool accepted;
std::string key;
std::string basename;
protected:
typedef void (ConsumerClass::*EventConsumer)(const yaml_event_t &event);
std::map<std::string, EventConsumer> consumers;
public:
YamlReader() {}
virtual ~YamlReader() {}
std::string get_basename() const { return basename; }
int parse_file(const std::string &infile)
{
basename = infile;
std::size_t found = basename.rfind(".yaml");
if (found > 0) basename = basename.substr(0, found);
found = basename.find_last_of("/\\");
if (found != std::string::npos) basename = basename.substr(found + 1);
FILE *fp = fopen(infile.c_str(), "r");
yaml_parser_t parser;
yaml_event_t event;
if (!fp) {
std::cerr << "Cannot open yaml file '" << infile << "': " << strerror(errno)
<< std::endl;
return 1;
}
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, fp);
state = START;
do {
if (!yaml_parser_parse(&parser, &event)) {
state = STOP;
}
if (!consume_event(event)) {
state = STOP;
}
if (accepted) {
if (!consume_key_value(key, event)) {
std::cerr << "Ignoring unknown key/value pair: " << key << " = "
<< event.data.scalar.value << std::endl;
}
}
yaml_event_delete(&event);
} while (state != STOP);
yaml_parser_delete(&parser);
fclose(fp);
return 0;
}
protected:
bool consume_key_value(const std::string &key, const yaml_event_t &event)
{
auto it = consumers.find(key);
ConsumerClass *consumer = dynamic_cast<ConsumerClass *>(this);
if (consumer) {
if (it != consumers.end()) {
// std::cerr << "Loading: " << key << std::endl;
(consumer->*(it->second))(event);
return true;
}
std::cerr << "UNKNOWN" << std::endl;
} else {
std::cerr << "ConsumerClass is not valid" << std::endl;
}
return false;
}
bool consume_event(yaml_event_t &event)
{
accepted = false;
switch (state) {
case START:
switch (event.type) {
case YAML_MAPPING_START_EVENT:
state = ACCEPT_KEY;
break;
case YAML_SCALAR_EVENT:
case YAML_SEQUENCE_START_EVENT:
state = ERROR;
break;
case YAML_STREAM_END_EVENT:
state = STOP;
break;
case YAML_STREAM_START_EVENT:
case YAML_DOCUMENT_START_EVENT:
case YAML_DOCUMENT_END_EVENT:
// ignore
break;
default:
std::cerr << "UNHANDLED YAML EVENT: " << event.type << std::endl;
state = ERROR;
break;
}
break;
case ACCEPT_KEY:
switch (event.type) {
case YAML_SCALAR_EVENT:
key = (char *)event.data.scalar.value;
state = ACCEPT_VALUE;
break;
case YAML_MAPPING_END_EVENT:
state = STOP;
break;
default:
std::cerr << "UNHANDLED YAML EVENT (key): " << event.type
<< "\nVALUE: " << event.data.scalar.value << std::endl;
state = ERROR;
break;
}
break;
case ACCEPT_VALUE:
switch (event.type) {
case YAML_SCALAR_EVENT:
accepted = true;
state = ACCEPT_KEY;
break;
default:
std::cerr << "UNHANDLED YAML EVENT (value): " << event.type
<< "\nVALUE: " << event.data.scalar.value << std::endl;
state = ERROR;
break;
}
break;
case ERROR:
case STOP:
break;
}
return (state != ERROR);
}
};
#endif
|