36 #include <visp3/core/vpConfig.h>
37 #include <visp3/core/vpXmlParser.h>
41 #include <libxml/parser.h>
42 #include <visp3/core/vpDebug.h>
43 #include <visp3/core/vpException.h>
56 vpXmlParser::vpXmlParser() : nodeMap(), main_tag(
"config") { }
73 vpXmlParser::~vpXmlParser()
83 vpXmlParser::vpXmlParser(
const vpXmlParser &_twin) : nodeMap(_twin.nodeMap), main_tag(_twin.main_tag) { }
98 char *vpXmlParser::xmlReadCharChild(xmlDocPtr doc, xmlNodePtr node)
100 if (node->xmlChildrenNode ==
nullptr) {
101 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read char*";
102 std::cerr << errorMsg << std::endl;
105 return (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
118 std::string vpXmlParser::xmlReadStringChild(xmlDocPtr doc, xmlNodePtr node)
120 if (node->xmlChildrenNode ==
nullptr) {
121 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read std::string";
122 std::cerr << errorMsg << std::endl;
125 char *dataCh = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
126 std::string dataStr = dataCh;
142 int vpXmlParser::xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
144 if (node->xmlChildrenNode ==
nullptr) {
145 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read int";
146 std::cerr << errorMsg << std::endl;
150 char *control_convert;
153 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
154 val_int = (int)strtol((
char *)val_char, &control_convert, 10);
156 if (val_char == control_convert) {
157 xmlFree((xmlChar *)val_char);
160 xmlFree((xmlChar *)val_char);
176 unsigned int vpXmlParser::xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
178 if (node->xmlChildrenNode ==
nullptr) {
179 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read unsigned int";
180 std::cerr << errorMsg << std::endl;
184 char *control_convert;
185 unsigned int val_uint;
187 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
188 val_uint = (
unsigned int)strtoul((
char *)val_char, &control_convert, 10);
190 if (val_char == control_convert) {
191 xmlFree((xmlChar *)val_char);
194 xmlFree((xmlChar *)val_char);
210 double vpXmlParser::xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
212 if (node->xmlChildrenNode ==
nullptr) {
213 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read double";
214 std::cerr << errorMsg << std::endl;
218 char *control_convert;
221 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
222 val_double = strtod((
char *)val_char, &control_convert);
224 if (val_char == control_convert) {
225 xmlFree((xmlChar *)val_char);
228 xmlFree((xmlChar *)val_char);
243 float vpXmlParser::xmlReadFloatChild(xmlDocPtr doc, xmlNodePtr node)
245 if (node->xmlChildrenNode ==
nullptr) {
246 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read float";
247 std::cerr << errorMsg << std::endl;
251 char *control_convert;
254 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
255 #if defined(VISP_HAVE_FUNC_STRTOF)
256 val_float = strtof((
char *)val_char, &control_convert);
258 val_float = (float)strtod((
char *)val_char, &control_convert);
261 if (val_char == control_convert) {
262 xmlFree((xmlChar *)val_char);
265 xmlFree((xmlChar *)val_char);
280 bool vpXmlParser::xmlReadBoolChild(xmlDocPtr doc, xmlNodePtr node)
282 if (node->xmlChildrenNode ==
nullptr) {
283 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read bool";
284 std::cerr << errorMsg << std::endl;
290 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
291 val_bool = val_char[0] !=
'0';
293 xmlFree((xmlChar *)val_char);
304 void vpXmlParser::xmlWriteCharChild(xmlNodePtr node,
const char *label,
const char *value)
307 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)value);
308 xmlAddChild(node, tmp);
318 void vpXmlParser::xmlWriteStringChild(xmlNodePtr node,
const char *label,
const std::string &value)
321 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)value.c_str());
322 xmlAddChild(node, tmp);
332 void vpXmlParser::xmlWriteIntChild(xmlNodePtr node,
const char *label,
int value)
335 snprintf(str, 100,
"%d", value);
337 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)str);
338 xmlAddChild(node, tmp);
348 void vpXmlParser::xmlWriteUnsignedIntChild(xmlNodePtr node,
const char *label,
unsigned int value)
351 snprintf(str, 100,
"%u", value);
353 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)str);
354 xmlAddChild(node, tmp);
364 void vpXmlParser::xmlWriteDoubleChild(xmlNodePtr node,
const char *label,
double value)
367 snprintf(str, 100,
"%lf", value);
369 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)str);
370 xmlAddChild(node, tmp);
380 void vpXmlParser::xmlWriteFloatChild(xmlNodePtr node,
const char *label,
float value)
383 snprintf(str, 100,
"%f", value);
385 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)str);
386 xmlAddChild(node, tmp);
396 void vpXmlParser::xmlWriteBoolChild(xmlNodePtr node,
const char *label,
bool value)
399 snprintf(str, 2,
"%d", (
int)value);
401 tmp = xmlNewChild(node,
nullptr, (xmlChar *)label, (xmlChar *)str);
402 xmlAddChild(node, tmp);
419 void vpXmlParser::parse(
const std::string &filename)
422 xmlNodePtr root_node;
424 doc = xmlParseFile(filename.c_str());
425 if (doc ==
nullptr) {
426 vpERROR_TRACE(
"cannot open file");
430 root_node = xmlDocGetRootElement(doc);
431 if (root_node ==
nullptr) {
432 vpERROR_TRACE(
"cannot get root element");
436 readMainClass(doc, root_node);
451 void vpXmlParser::save(
const std::string &filename,
bool append)
454 xmlNodePtr root_node;
456 doc = xmlReadFile(filename.c_str(),
nullptr, XML_PARSE_NOWARNING + XML_PARSE_NOERROR + XML_PARSE_NOBLANKS);
457 if (doc ==
nullptr) {
458 doc = xmlNewDoc((xmlChar *)
"1.0");
459 root_node = xmlNewNode(
nullptr, (xmlChar *)main_tag.c_str());
460 xmlDocSetRootElement(doc, root_node);
465 if (remove(filename.c_str()) != 0)
468 doc = xmlNewDoc((xmlChar *)
"1.0");
469 root_node = xmlNewNode(
nullptr, (xmlChar *)main_tag.c_str());
470 xmlDocSetRootElement(doc, root_node);
474 root_node = xmlDocGetRootElement(doc);
475 if (root_node ==
nullptr) {
476 vpERROR_TRACE(
"problem to get the root node");
480 writeMainClass(root_node);
482 xmlSaveFormatFile(filename.c_str(), doc, 1);
486 #elif !defined(VISP_BUILD_SHARED_LIBS)
488 void dummy_vpXmlParser() { };
error that can be emitted by ViSP classes.