DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
config_export_data.cxx
Go to the documentation of this file.
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
19
20using namespace dunedaq::conffwk;
21
22int
23main(int argc, char* argv[])
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(
29 "Export config data using boost property tree.\n\nOptions/Arguments");
30
31 try {
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")(
38 "output,o",
39 boost::program_options::value<std::string>(&output_file),
40 "output file name; print to standard out, if not defined")(
41 "format,t",
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");
46
47 boost::program_options::variables_map vm;
48 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
49
50 if (vm.count("help")) {
51 std::cout << desc << std::endl;
52 return EXIT_SUCCESS;
53 }
54
55 boost::program_options::notify(vm);
56
57 if (vm.count("fix"))
58 apply_fix = true;
59
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;
65 return EXIT_FAILURE;
66 }
67
68 try {
69 Configuration db(db_name);
70
71 boost::property_tree::ptree pt;
72 std::string fix_empty_arrays((apply_fix && format == "json") ? "<-- empty-p3-element -->" : "");
73
74 db.export_data(pt, classes, objects, files, fix_empty_arrays);
75
76 std::string in, out;
77
78 {
79 std::ostringstream buf;
80 if (format == "json")
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));
85 else
86 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(' ', 4));
87
88 in = buf.str();
89 }
90
91 if (!apply_fix || format == "info")
92 out = std::move(in);
93 else {
94 out.reserve(in.size());
95 std::string::size_type pos = 0, fix_pos;
96
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);
101
102 if (start != std::string::npos && end != std::string::npos) {
103 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 while ((fix_pos = in.find("<>", pos)) != std::string::npos) {
111 std::string::size_type next = in.find("</>", fix_pos);
112
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);
116 pos = next + 3;
117 } else
118 break;
119 }
120 }
121
122 out.append(in, pos);
123 }
124
125 if (!output_file.empty()) {
126 std::ofstream f(output_file);
127 f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
128 f << out;
129 f.close();
130 } else
131 std::cout << out;
132
133 return EXIT_SUCCESS;
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;
140 }
141
142 return EXIT_FAILURE;
143}
Defines base class for cache of template objects.
void export_data(boost::property_tree::ptree &tree, const std::string &classes="", const std::string &objects="", const std::string &files="", const std::string &empty_array_item="")
Export configuration data into ptree.
conffwk entry point
int main(int argc, char *argv[])
FELIX Initialization std::string initerror FELIX queue timed out