17{
18 std::string output_file, db_name, classes, objects, files,
format(
"json");
19 bool apply_fix(false);
20
21 boost::program_options::options_description desc("Export config data using boost property tree.\n\nOptions/Arguments");
22
23 try
24 {
25 desc.add_options()
26 (
27 "database,d",
28 boost::program_options::value<std::string>(&db_name)->required(),
29 "database specification in format plugin-name:parameters"
30 )
31 (
32 "classes,c",
33 boost::program_options::value<std::string>(&classes),
34 "regex defining class names; ignore if empty"
35 )
36 (
37 "objects,i",
38 boost::program_options::value<std::string>(&objects),
39 "regex defining object IDs; ignore if empty"
40 )
41 (
42 "files,f",
43 boost::program_options::value<std::string>(&files),
44 "regex defining data files; ignore if empty"
45 )
46 (
47 "output,o",
48 boost::program_options::value<std::string>(&output_file),
49 "output file name; print to standard out, if not defined"
50 )
51 (
52 "format,t",
53 boost::program_options::value<std::string>(&format)->default_value(format),
54 "output format (\"json\", \"xml\" or \"info\")"
55 )
56 (
57 "fix,x",
58 "fix arrays output format:\n* enforce empty arrays for json;\n* remove unnamed xml tags"
59 )
60 (
61 "help,h",
62 "Print help message"
63 );
64
65 boost::program_options::variables_map vm;
66 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
67
68 if (vm.count("help"))
69 {
70 std::cout << desc << std::endl;
71 return EXIT_SUCCESS;
72 }
73
74 boost::program_options::notify(vm);
75
76 if (vm.count("fix"))
77 apply_fix = true;
78
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 + '\"');
82 }
83 catch (std::exception& ex)
84 {
85 std::cerr << "command line error: " << ex.what() << std::endl;
86 return EXIT_FAILURE;
87 }
88
89 try
90 {
92
93 boost::property_tree::ptree pt;
94 std::string fix_empty_arrays((apply_fix && format == "json") ? "<-- empty-p3-element -->" : "");
95
96 db.export_data(pt, classes, objects, files, fix_empty_arrays);
97
99
100 {
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));
106 else
107 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(' ', 4));
108
109 in = buf.str();
110 }
111
112 if (!apply_fix || format == "info")
114 else
115 {
116 out.reserve(in.size());
117 std::string::size_type pos = 0, fix_pos;
118
119 if (format == "json")
120 {
121 while ((fix_pos = in.find(fix_empty_arrays, pos)) != std::string::npos)
122 {
123 std::string::size_type start = in.rfind('[', fix_pos);
124 std::string::size_type end = in.find(']', fix_pos);
125
126 if (start != std::string::npos && end != std::string::npos)
127 {
128 out.append(in, pos, start + 1 - pos);
129 pos = end;
130 }
131 else
132 break;
133 }
134 }
135 else
136 {
137
138 while ((fix_pos = in.find("<>", pos)) != std::string::npos)
139 {
140 std::string::size_type next = in.find("</>", fix_pos);
141
142 if (next != std::string::npos)
143 {
144 out.append(in, pos, fix_pos - pos);
145 out.append(in, fix_pos+2, next - fix_pos - 2);
146 pos = next + 3;
147 }
148 else
149 break;
150 }
151 }
152
154 }
155
156 if (!output_file.empty())
157 {
158 std::ofstream
f(output_file);
159 f.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
162 }
163 else
165
166 return EXIT_SUCCESS;
167 }
168 catch (const dunedaq::conffwk::Exception &ex)
169 {
170 std::cout << "config error: " << ex << std::endl;
171 }
172 catch (const boost::property_tree::json_parser_error &ex)
173 {
174 std::cout << "ptree json error: " << ex.what() << std::endl;
175 }
176 catch (const std::exception &ex)
177 {
178 std::cout << "error: " << ex.what() << std::endl;
179 }
180
181 return EXIT_FAILURE;
182}
Defines base class for cache of template objects.
FELIX Initialization std::string initerror FELIX queue timed out