1 /* $Id: ex_libxml2_conf.cpp,v 1.2 2005-10-25 09:06:44 marc Exp $
2 Copyright (c) 2005, Index Data.
10 #include <boost/program_options.hpp>
11 namespace po = boost::program_options;
13 #include <libxml/xmlreader.h>
18 Configuration(int argc, char **argv)
25 parse_command_line(argc, argv);
44 xmlDoc * m_xml_conf_doc;
47 void parse_command_line(int argc, char **argv)
49 po::options_description generic("Generic options");
51 ("help,h", "produce help message")
52 ("version,v", "version number")
53 ("xinclude,x", "allow Xinclude on XML config files")
56 po::options_description config("Configuration options");
58 ("config,c", po::value<std::string>(&m_config)->default_value("../etc/config1.xml"),
59 "config XML file path (string)")
60 ("duration,d", po::value<int>(&m_duration)->default_value(0),
61 "number of seconds for server to exist (int)")
62 //("commands", po::value< std::vector<std::string> >(),
63 // "listener ports (string)")
66 //po::positional_options_description p;
69 po::options_description cmdline_options;
70 cmdline_options.add(generic).add(config);
75 //po::store(po::command_line_parser(argc, argv).
76 // options(cmdline_options).positional(p).run(), vm);
77 //po::store(po::command_line_parser(argc, argv).
78 // options(cmdline_options).run(), vm);
79 po::store(po::parse_command_line(argc, argv, cmdline_options), vm);
82 catch ( po::invalid_command_line_syntax &e) {
83 std::cerr << "ex_libxml2_conf error: " << e.what() << std::endl;
84 std::cerr << generic << config << std::endl;
87 catch ( po::invalid_option_value &e) {
88 //std::cerr << "ex_libxml2_conf error: " << e.what() << std::endl;
89 std::cerr << "invalid option value" << std::endl;
90 std::cerr << generic << config << std::endl;
93 catch ( po::unknown_option &e) {
94 std::cerr << "ex_libxml2_conf error: " << e.what() << std::endl;
95 std::cerr << generic << config << std::endl;
99 std::cout << "ex_libxml2_conf ";
101 if (vm.count("help")) {
102 std::cout << "--help" << std::endl;
103 std::cout << generic << config << std::endl;
107 if (vm.count("version")) {
108 std::cout << "--version" << std::endl;
112 if (vm.count("xinclude")) {
113 std::cout << "--xinclude" << std::endl;
117 if (vm.count("duration")) {
118 std::cout << "--duration "
119 << vm["duration"].as<int>() << " ";
121 if (vm.count("config")) {
122 std::cout << "--config "
123 << vm["config"].as<std::string>() << " ";
126 std::cout << std::endl;
128 //if (vm.count("port"))
130 // std::vector<std::string> ports =
131 // vm["port"].as< std::vector<std::string> >();
133 // for (size_t i = 0; i<ports.size(); i++)
134 // std::cout << "port " << i << " " << ports[i] << std::endl;
140 void parse_xml_config() {
143 xmlTextReader* reader;
144 //reader->SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1);
146 reader = xmlReaderForFile(m_config.c_str(), NULL, 0);
148 if (reader == NULL) {
149 std::cerr << "failed to open XML config file "
150 << m_config << std::endl;
155 // skipping pi, comments, text nodes ... untilt root element
156 //while((ret = xmlTextReaderRead(reader))
157 //&& 0 == xmlTextReaderDepth(reader)
158 //&& xmlTextReaderNodeType(reader) != XML_ELEMENT_NODE
160 //std::cout << xmlTextReaderConstName(reader) << std::endl;
162 // root element processing
163 if ((ret = xmlTextReaderMoveToElement(reader)))
164 std::cout << xmlTextReaderConstName(reader) << std::endl;
165 //if (xmlTextReaderHasAttributes(reader))
166 // std::cout << "AttributeCount "
167 // << xmlTextReaderAttributeCount(reader) << std::endl;
169 while (xmlTextReaderMoveToNextAttribute(reader))
170 std::cout << xmlTextReaderConstName(reader) << " "
171 << xmlTextReaderConstValue(reader) << " "
172 //<< xmlTextReaderNodeType(reader) << " "
173 //<< xmlTextReaderDepth(reader) << " "
176 // processing all other elements
177 while ((ret = xmlTextReaderRead(reader)))
178 std::cout << xmlTextReaderConstName(reader) << " "
179 << xmlTextReaderDepth(reader) << " "
180 << xmlTextReaderNodeType(reader) << std::endl;
183 xmlFreeTextReader(reader);
185 std::cerr << "Parsing failed of XML config file "
186 << m_config << std::endl;
196 int main(int argc, char **argv)
201 Configuration conf(argc, argv);
203 std::cout << "config " << conf.config() << std::endl;
204 std::cout << "duration " << conf.duration() << std::endl;
211 //std::cerr << "Unknown Exception" << std::endl;
224 * indent-tabs-mode: nil
225 * c-file-style: "stroustrup"
227 * vim: shiftwidth=4 tabstop=8 expandtab