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