23main(
int argc,
char* argv[])
25 std::string output_file, db_name, classes, objects, files, format(
"json");
26 bool apply_fix(
false);
28 boost::program_options::options_description desc(
29 "Export config data using boost property tree.\n\nOptions/Arguments");
32 desc.add_options()(
"database,d",
33 boost::program_options::value<std::string>(&db_name)->required(),
34 "database specification in format plugin-name:parameters")(
35 "classes,c", boost::program_options::value<std::string>(&classes),
"regex defining class names; ignore if empty")(
36 "objects,i", boost::program_options::value<std::string>(&objects),
"regex defining object IDs; ignore if empty")(
37 "files,f", boost::program_options::value<std::string>(&files),
"regex defining data files; ignore if empty")(
39 boost::program_options::value<std::string>(&output_file),
40 "output file name; print to standard out, if not defined")(
42 boost::program_options::value<std::string>(&format)->default_value(format),
43 "output format (\"json\", \"xml\" or \"info\")")(
44 "fix,x",
"fix arrays output format:\n* enforce empty arrays for json;\n* remove unnamed xml tags")(
45 "help,h",
"Print help message");
47 boost::program_options::variables_map vm;
48 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
50 if (vm.count(
"help")) {
51 std::cout << desc << std::endl;
55 boost::program_options::notify(vm);
60 auto valid_formats = {
"json",
"xml",
"info" };
61 if (std::none_of(valid_formats.begin(), valid_formats.end(), [&format](
auto p) { return p == format; }))
62 throw std::runtime_error(
"unsupported format \"" + format +
'\"');
63 }
catch (std::exception& ex) {
64 std::cerr <<
"command line error: " << ex.what() << std::endl;
71 boost::property_tree::ptree pt;
72 std::string fix_empty_arrays((apply_fix && format ==
"json") ?
"<-- empty-p3-element -->" :
"");
74 db.
export_data(pt, classes, objects, files, fix_empty_arrays);
79 std::ostringstream buf;
81 boost::property_tree::json_parser::write_json(buf, pt);
82 else if (format ==
"xml")
83 boost::property_tree::xml_parser::write_xml(
84 buf, pt, boost::property_tree::xml_writer_make_settings<std::string>(
' ', 4));
86 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(
' ', 4));
91 if (!apply_fix || format ==
"info")
94 out.reserve(in.size());
95 std::string::size_type pos = 0, fix_pos;
97 if (format ==
"json") {
98 while ((fix_pos = in.find(fix_empty_arrays, pos)) != std::string::npos) {
99 std::string::size_type start = in.rfind(
'[', fix_pos);
100 std::string::size_type end = in.find(
']', fix_pos);
102 if (start != std::string::npos && end != std::string::npos) {
103 out.append(in, pos, start + 1 - pos);
110 while ((fix_pos = in.find(
"<>", pos)) != std::string::npos) {
111 std::string::size_type next = in.find(
"</>", fix_pos);
113 if (next != std::string::npos) {
114 out.append(in, pos, fix_pos - pos);
115 out.append(in, fix_pos + 2, next - fix_pos - 2);
125 if (!output_file.empty()) {
126 std::ofstream f(output_file);
127 f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
134 }
catch (
const dunedaq::conffwk::Exception& ex) {
135 std::cout <<
"config error: " << ex << std::endl;
136 }
catch (
const boost::property_tree::json_parser_error& ex) {
137 std::cout <<
"ptree json error: " << ex.what() << std::endl;
138 }
catch (
const std::exception& ex) {
139 std::cout <<
"error: " << ex.what() << std::endl;