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(
"Export config data using boost property tree.\n\nOptions/Arguments");
35 boost::program_options::value<std::string>(&db_name)->required(),
36 "database specification in format plugin-name:parameters"
40 boost::program_options::value<std::string>(&classes),
41 "regex defining class names; ignore if empty"
45 boost::program_options::value<std::string>(&objects),
46 "regex defining object IDs; ignore if empty"
50 boost::program_options::value<std::string>(&files),
51 "regex defining data files; ignore if empty"
55 boost::program_options::value<std::string>(&output_file),
56 "output file name; print to standard out, if not defined"
60 boost::program_options::value<std::string>(&format)->default_value(format),
61 "output format (\"json\", \"xml\" or \"info\")"
65 "fix arrays output format:\n* enforce empty arrays for json;\n* remove unnamed xml tags"
72 boost::program_options::variables_map vm;
73 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
77 std::cout << desc << std::endl;
81 boost::program_options::notify(vm);
86 auto valid_formats = {
"json",
"xml",
"info"};
87 if (std::none_of(valid_formats.begin(), valid_formats.end(), [&format](
auto p){ return p == format; }))
88 throw std::runtime_error(
"unsupported format \"" + format +
'\"');
90 catch (std::exception& ex)
92 std::cerr <<
"command line error: " << ex.what() << std::endl;
100 boost::property_tree::ptree pt;
101 std::string fix_empty_arrays((apply_fix && format ==
"json") ?
"<-- empty-p3-element -->" :
"");
103 db.
export_data(pt, classes, objects, files, fix_empty_arrays);
108 std::ostringstream buf;
109 if (format ==
"json")
110 boost::property_tree::json_parser::write_json(buf, pt);
111 else if (format ==
"xml")
112 boost::property_tree::xml_parser::write_xml(buf, pt, boost::property_tree::xml_writer_make_settings<std::string>(
' ', 4));
114 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(
' ', 4));
119 if (!apply_fix || format ==
"info")
123 out.reserve(in.size());
124 std::string::size_type pos = 0, fix_pos;
126 if (format ==
"json")
128 while ((fix_pos = in.find(fix_empty_arrays, pos)) != std::string::npos)
130 std::string::size_type start = in.rfind(
'[', fix_pos);
131 std::string::size_type end = in.find(
']', fix_pos);
133 if (start != std::string::npos && end != std::string::npos)
135 out.append(in, pos, start + 1 - pos);
145 while ((fix_pos = in.find(
"<>", pos)) != std::string::npos)
147 std::string::size_type next = in.find(
"</>", fix_pos);
149 if (next != std::string::npos)
151 out.append(in, pos, fix_pos - pos);
152 out.append(in, fix_pos+2, next - fix_pos - 2);
163 if (!output_file.empty())
165 std::ofstream f(output_file);
166 f.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
175 catch (
const dunedaq::conffwk::Exception &ex)
177 std::cout <<
"config error: " << ex << std::endl;
179 catch (
const boost::property_tree::json_parser_error &ex)
181 std::cout <<
"ptree json error: " << ex.what() << std::endl;
183 catch (
const std::exception &ex)
185 std::cout <<
"error: " << ex.what() << std::endl;