-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathConfigParse.cpp
More file actions
210 lines (180 loc) · 6.19 KB
/
ConfigParse.cpp
File metadata and controls
210 lines (180 loc) · 6.19 KB
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
/*!
* @file
*
* @section LICENSE
*
* Copyright (C) 2017 by the Georgia Tech Research Institute (GTRI)
*
* This file is part of SCRIMMAGE.
*
* SCRIMMAGE is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* SCRIMMAGE is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with SCRIMMAGE. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kevin DeMarco <kevin.demarco@gtri.gatech.edu>
* @author Eric Squires <eric.squires@gtri.gatech.edu>
* @date 31 July 2017
* @version 0.1.0
* @brief Brief file description.
* @section DESCRIPTION
* A Long description goes here.
*
*/
#include <scrimmage/common/Utilities.h>
#include <scrimmage/common/FileSearch.h>
#include <scrimmage/parse/ConfigParse.h>
#include <iostream>
#include <fstream>
#include <vector>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/algorithm/string.hpp>
#include <rapidxml/rapidxml.hpp>
namespace fs = boost::filesystem;
namespace rx = rapidxml;
using std::cout;
using std::endl;
namespace scrimmage {
ConfigParse::ConfigParse() {}
void ConfigParse::set_required(std::string node_name) {
required_.push_back(node_name);
}
void ConfigParse::recursive_params(rx::xml_node<char> *root,
const std::map<std::string, std::string> &overrides,
std::map<std::string, std::string> ¶ms,
const std::string &prev) {
// End condition
if (root == 0 || root->name() == std::string("")
|| root->name() == std::string(" ")) {
return;
}
// Process this node
std::string name = root->name();
if (prev != "") {
name = prev + "/" + name;
}
if (params.count(name) > 0) {
// Name already exists, consider this a list. Does the size
// parameter exist yet?
std::string name_size = name + ":size";
int size = 2;
if (params.count(name_size) > 0) {
// It does exist, grab the current size and increment.
size = std::stoi(params[name_size]) + 1;
}
name = name + "_" + std::to_string(size-1);
params[name_size] = std::to_string(size);
}
if (overrides.count(name)) {
params[name] = overrides.at(name);
} else {
params[name] = root->value();
}
// Recurse (depth-first)
recursive_params(root->first_node(), overrides, params, name);
// Recurse (sibling node)
recursive_params(root->next_sibling(), overrides, params, prev);
}
bool ConfigParse::parse(const std::map<std::string, std::string> &overrides,
std::string filename, std::string env_var,
FileSearch &file_search, bool verbose) {
std::string result = "";
bool status = file_search.find_file(filename, "xml", env_var, result, verbose);
if (!status) {
if (boost::algorithm::to_lower_copy(filename) != "sphere") {
// sphere does not have a filename so we do not
// need a warning message for this
cout << "Failed to find configuration: " << filename << endl;
}
return false;
} else if (verbose) {
cout << "ConfigParse: found " << result << std::endl;
}
filename_ = result;
rx::xml_document<> doc;
std::ifstream file(filename_.c_str());
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
std::string content(buffer.str());
try {
// Note: This parse function can hard fail (seg fault, no exception) on
// badly formatted xml data. Sometimes it'll except, sometimes not.
// doc.parse<0>(mission_file_content_vec.data());
doc.parse<0>(&content[0]);
}
catch (const rapidxml::parse_error& e)
{
std::cout << e.what() << std::endl;
throw std::runtime_error("Error parsing config file " + filename);
// cout << "scrimmage::MissionParse::parse: Exception during rapidxml::xml_document<>.parse<>()." << endl;
return false;
}
rx::xml_node<> *config_node = doc.first_node("params");
if (config_node == 0) {
cout << "Missing tag: params" << endl;
return false;
}
params_.clear();
params_["XML_DIR"] = this->directory() + "/";
params_["XML_FILENAME"] = filename_;
recursive_params(config_node->first_node(), overrides, params_, "");
// Determine if there were any overrides (XML attributes) specified in the
// mission file that weren't declared in the Plugin's XML
// file. Automatically add these overrides to the params block.
for (auto &kv : overrides) {
if (params_.count(kv.first) == 0) {
params_[kv.first] = kv.second;
}
}
for (std::string &node_name : required_) {
if (params_.count(node_name) == 0) {
cout << "Config file is missing XML tag: " << node_name << endl;
return false;
}
}
return true;
}
std::map<std::string, std::string> &ConfigParse::params() { return params_; }
std::string ConfigParse::filename() { return filename_; }
std::string ConfigParse::directory() {
if (fs::exists(filename_)) {
return fs::path(filename_).parent_path().string();
}
return "";
}
std::string ConfigParse::extension() {
if (fs::exists(filename_)) {
return fs::path(filename_).extension().string();
}
return "";
}
std::string ConfigParse::stem() {
if (fs::exists(filename_)) {
return fs::path(filename_).stem().string();
}
return "";
}
void ConfigParse::print_params() {
for (auto &kv : params_) {
cout << kv.first << "=" << kv.second << endl;
}
}
std::ostream& operator<<(std::ostream& os, ConfigParse& cp) {
for (auto &kv : cp.params()) {
os << kv.first << "=" << kv.second << endl;
}
return os;
}
} // namespace scrimmage