Line data Source code
1 : //
2 : // DUNE DAQ modification notice:
3 : // This file has been modified from the original ATLAS config source for the DUNE DAQ project.
4 : // Fork baseline commit: 67a24e731 (2022-10-27).
5 : // Renamed since fork: yes (from bin/config_export_data.cpp to apps/config_export_data.cxx).
6 : //
7 :
8 : #include <stdlib.h>
9 :
10 : #include <iostream>
11 :
12 : #include <boost/program_options.hpp>
13 :
14 : #include <boost/property_tree/info_parser.hpp>
15 : #include <boost/property_tree/json_parser.hpp>
16 : #include <boost/property_tree/xml_parser.hpp>
17 :
18 : #include "conffwk/Configuration.hpp"
19 :
20 : using namespace dunedaq::conffwk;
21 :
22 : int
23 0 : main(int argc, char* argv[])
24 : {
25 0 : std::string output_file, db_name, classes, objects, files, format("json");
26 0 : bool apply_fix(false);
27 :
28 0 : boost::program_options::options_description desc(
29 0 : "Export config data using boost property tree.\n\nOptions/Arguments");
30 :
31 0 : try {
32 0 : desc.add_options()("database,d",
33 0 : boost::program_options::value<std::string>(&db_name)->required(),
34 0 : "database specification in format plugin-name:parameters")(
35 0 : "classes,c", boost::program_options::value<std::string>(&classes), "regex defining class names; ignore if empty")(
36 0 : "objects,i", boost::program_options::value<std::string>(&objects), "regex defining object IDs; ignore if empty")(
37 0 : "files,f", boost::program_options::value<std::string>(&files), "regex defining data files; ignore if empty")(
38 : "output,o",
39 0 : boost::program_options::value<std::string>(&output_file),
40 0 : "output file name; print to standard out, if not defined")(
41 : "format,t",
42 0 : boost::program_options::value<std::string>(&format)->default_value(format),
43 0 : "output format (\"json\", \"xml\" or \"info\")")(
44 0 : "fix,x", "fix arrays output format:\n* enforce empty arrays for json;\n* remove unnamed xml tags")(
45 : "help,h", "Print help message");
46 :
47 0 : boost::program_options::variables_map vm;
48 0 : boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
49 :
50 0 : if (vm.count("help")) {
51 0 : std::cout << desc << std::endl;
52 0 : return EXIT_SUCCESS;
53 : }
54 :
55 0 : boost::program_options::notify(vm);
56 :
57 0 : if (vm.count("fix"))
58 0 : apply_fix = true;
59 :
60 0 : auto valid_formats = { "json", "xml", "info" };
61 0 : if (std::none_of(valid_formats.begin(), valid_formats.end(), [&format](auto p) { return p == format; }))
62 0 : throw std::runtime_error("unsupported format \"" + format + '\"');
63 0 : } catch (std::exception& ex) {
64 0 : std::cerr << "command line error: " << ex.what() << std::endl;
65 0 : return EXIT_FAILURE;
66 0 : }
67 :
68 0 : try {
69 0 : Configuration db(db_name);
70 :
71 0 : boost::property_tree::ptree pt;
72 0 : std::string fix_empty_arrays((apply_fix && format == "json") ? "<-- empty-p3-element -->" : "");
73 :
74 0 : db.export_data(pt, classes, objects, files, fix_empty_arrays);
75 :
76 0 : std::string in, out;
77 :
78 0 : {
79 0 : std::ostringstream buf;
80 0 : if (format == "json")
81 0 : boost::property_tree::json_parser::write_json(buf, pt);
82 0 : else if (format == "xml")
83 0 : boost::property_tree::xml_parser::write_xml(
84 0 : buf, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4));
85 : else
86 0 : boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(' ', 4));
87 :
88 0 : in = buf.str();
89 0 : }
90 :
91 0 : if (!apply_fix || format == "info")
92 0 : out = std::move(in);
93 : else {
94 0 : out.reserve(in.size());
95 0 : std::string::size_type pos = 0, fix_pos;
96 :
97 0 : if (format == "json") {
98 0 : while ((fix_pos = in.find(fix_empty_arrays, pos)) != std::string::npos) {
99 0 : std::string::size_type start = in.rfind('[', fix_pos);
100 0 : std::string::size_type end = in.find(']', fix_pos);
101 :
102 0 : if (start != std::string::npos && end != std::string::npos) {
103 0 : out.append(in, pos, start + 1 - pos);
104 : pos = end;
105 : } else
106 : break;
107 : }
108 : } else {
109 : // remove unnamed xml tags: replace "<>FOO</>" by "FOO"
110 0 : while ((fix_pos = in.find("<>", pos)) != std::string::npos) {
111 0 : std::string::size_type next = in.find("</>", fix_pos);
112 :
113 0 : if (next != std::string::npos) {
114 0 : out.append(in, pos, fix_pos - pos);
115 0 : out.append(in, fix_pos + 2, next - fix_pos - 2);
116 0 : pos = next + 3;
117 : } else
118 : break;
119 : }
120 : }
121 :
122 0 : out.append(in, pos);
123 : }
124 :
125 0 : if (!output_file.empty()) {
126 0 : std::ofstream f(output_file);
127 0 : f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
128 0 : f << out;
129 0 : f.close();
130 0 : } else
131 0 : std::cout << out;
132 :
133 0 : return EXIT_SUCCESS;
134 0 : } catch (const dunedaq::conffwk::Exception& ex) {
135 0 : std::cout << "config error: " << ex << std::endl;
136 0 : } catch (const boost::property_tree::json_parser_error& ex) {
137 0 : std::cout << "ptree json error: " << ex.what() << std::endl;
138 0 : } catch (const std::exception& ex) {
139 0 : std::cout << "error: " << ex.what() << std::endl;
140 0 : }
141 :
142 : return EXIT_FAILURE;
143 0 : }
|