16main(
int argc,
char *argv[])
18 std::string output_file, db_name, classes, objects, files, format(
"json");
19 bool apply_fix(
false);
21 boost::program_options::options_description desc(
"Export config data using boost property tree.\n\nOptions/Arguments");
28 boost::program_options::value<std::string>(&db_name)->required(),
29 "database specification in format plugin-name:parameters"
33 boost::program_options::value<std::string>(&classes),
34 "regex defining class names; ignore if empty"
38 boost::program_options::value<std::string>(&objects),
39 "regex defining object IDs; ignore if empty"
43 boost::program_options::value<std::string>(&files),
44 "regex defining data files; ignore if empty"
48 boost::program_options::value<std::string>(&output_file),
49 "output file name; print to standard out, if not defined"
53 boost::program_options::value<std::string>(&format)->default_value(format),
54 "output format (\"json\", \"xml\" or \"info\")"
58 "fix arrays output format:\n* enforce empty arrays for json;\n* remove unnamed xml tags"
65 boost::program_options::variables_map vm;
66 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
70 std::cout << desc << std::endl;
74 boost::program_options::notify(vm);
79 auto valid_formats = {
"json",
"xml",
"info"};
80 if (std::none_of(valid_formats.begin(), valid_formats.end(), [&format](
auto p){ return p == format; }))
81 throw std::runtime_error(
"unsupported format \"" + format +
'\"');
83 catch (std::exception& ex)
85 std::cerr <<
"command line error: " << ex.what() << std::endl;
93 boost::property_tree::ptree pt;
94 std::string fix_empty_arrays((apply_fix && format ==
"json") ?
"<-- empty-p3-element -->" :
"");
96 db.
export_data(pt, classes, objects, files, fix_empty_arrays);
101 std::ostringstream buf;
102 if (format ==
"json")
103 boost::property_tree::json_parser::write_json(buf, pt);
104 else if (format ==
"xml")
105 boost::property_tree::xml_parser::write_xml(buf, pt, boost::property_tree::xml_writer_make_settings<std::string>(
' ', 4));
107 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(
' ', 4));
112 if (!apply_fix || format ==
"info")
116 out.reserve(in.size());
117 std::string::size_type pos = 0, fix_pos;
119 if (format ==
"json")
121 while ((fix_pos = in.find(fix_empty_arrays, pos)) != std::string::npos)
123 std::string::size_type start = in.rfind(
'[', fix_pos);
124 std::string::size_type end = in.find(
']', fix_pos);
126 if (start != std::string::npos && end != std::string::npos)
128 out.append(in, pos, start + 1 - pos);
138 while ((fix_pos = in.find(
"<>", pos)) != std::string::npos)
140 std::string::size_type next = in.find(
"</>", fix_pos);
142 if (next != std::string::npos)
144 out.append(in, pos, fix_pos - pos);
145 out.append(in, fix_pos+2, next - fix_pos - 2);
156 if (!output_file.empty())
158 std::ofstream f(output_file);
159 f.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
168 catch (
const dunedaq::conffwk::Exception &ex)
170 std::cout <<
"config error: " << ex << std::endl;
172 catch (
const boost::property_tree::json_parser_error &ex)
174 std::cout <<
"ptree json error: " << ex.what() << std::endl;
176 catch (
const std::exception &ex)
178 std::cout <<
"error: " << ex.what() << std::endl;