38 #include <visp3/core/vpXmlParser.h>
39 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpException.h>
44 #include <visp3/core/vpDebug.h>
45 #include <libxml/parser.h>
86 : nodeMap(), main_tag(
"config")
108 if(node ->xmlChildrenNode == NULL){
109 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read char*";
110 std::cerr << errorMsg << std::endl;
113 return (
char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
129 if(node ->xmlChildrenNode == NULL){
130 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read std::string";
131 std::cerr << errorMsg << std::endl;
134 char* dataCh = (
char*)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
135 std::string dataStr = dataCh;
153 if(node ->xmlChildrenNode == NULL){
154 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read int";
155 std::cerr << errorMsg << std::endl;
159 char * control_convert;
162 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
163 val_int = (int)strtol ((
char *)val_char, &control_convert, 10);
165 if (val_char == control_convert){
166 xmlFree((xmlChar*) val_char);
169 xmlFree((xmlChar*) val_char);
187 if(node ->xmlChildrenNode == NULL){
188 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read unsigned int";
189 std::cerr << errorMsg << std::endl;
193 char * control_convert;
194 unsigned int val_uint;
196 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
197 val_uint = (
unsigned int)strtoul ((
char *)val_char, &control_convert, 10);
199 if (val_char == control_convert){
200 xmlFree((xmlChar*) val_char);
203 xmlFree((xmlChar*) val_char);
221 if(node ->xmlChildrenNode == NULL){
222 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read double";
223 std::cerr << errorMsg << std::endl;
227 char * control_convert;
230 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
231 val_double = strtod ((
char *)val_char, &control_convert);
233 if (val_char == control_convert){
234 xmlFree((xmlChar*) val_char);
237 xmlFree((xmlChar*) val_char);
254 if(node ->xmlChildrenNode == NULL){
255 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read float";
256 std::cerr << errorMsg << std::endl;
260 char * control_convert;
263 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
264 #if defined(VISP_HAVE_FUNC_STRTOF)
265 val_float = strtof ((
char *)val_char, &control_convert);
267 val_float = (float) strtod ((
char *)val_char, &control_convert);
270 if (val_char == control_convert){
271 xmlFree((xmlChar*) val_char);
274 xmlFree((xmlChar*) val_char);
291 if(node ->xmlChildrenNode == NULL){
292 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read bool";
293 std::cerr << errorMsg << std::endl;
299 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
300 val_bool = val_char[0] !=
'0';
302 xmlFree((xmlChar*) val_char);
317 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)value);
318 xmlAddChild(node, tmp);
332 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)value.c_str());
333 xmlAddChild(node, tmp);
348 sprintf(str,
"%d", value);
350 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
351 xmlAddChild(node, tmp);
365 sprintf(str,
"%u", value);
367 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
368 xmlAddChild(node, tmp);
383 sprintf(str,
"%lf", value);
385 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
386 xmlAddChild(node, tmp);
400 sprintf(str,
"%f", value);
402 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
403 xmlAddChild(node, tmp);
417 sprintf(str,
"%d", (
int)value);
419 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
420 xmlAddChild(node, tmp);
439 xmlNodePtr root_node;
441 doc = xmlParseFile(filename.c_str());
447 root_node = xmlDocGetRootElement(doc);
448 if(root_node == NULL){
472 xmlNodePtr root_node;
474 doc = xmlReadFile(filename.c_str(), NULL, XML_PARSE_NOWARNING + XML_PARSE_NOERROR + XML_PARSE_NOBLANKS);
476 doc = xmlNewDoc ((xmlChar*)
"1.0");
477 root_node = xmlNewNode(NULL, (xmlChar*)
main_tag.c_str());
478 xmlDocSetRootElement(doc, root_node);
483 if (
remove(filename.c_str()) != 0)
486 doc = xmlNewDoc ((xmlChar*)
"1.0");
487 root_node = xmlNewNode(NULL, (xmlChar*)
main_tag.c_str());
488 xmlDocSetRootElement(doc, root_node);
492 root_node = xmlDocGetRootElement(doc);
493 if (root_node == NULL)
501 xmlSaveFormatFile(filename.c_str(), doc, 1);
505 #elif !defined(VISP_BUILD_SHARED_LIBS)
507 void dummy_vpXmlParser() {};
void xmlWriteIntChild(xmlNodePtr node, const char *label, const int value)
void xmlWriteFloatChild(xmlNodePtr node, const char *label, const float value)
void xmlWriteBoolChild(xmlNodePtr node, const char *label, const bool value)
error that can be emited by ViSP classes.
virtual void writeMainClass(xmlNodePtr node)=0
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
char * xmlReadCharChild(xmlDocPtr doc, xmlNodePtr node)
void save(const std::string &filename, const bool append=false)
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
void xmlWriteUnsignedIntChild(xmlNodePtr node, const char *label, const unsigned int value)
void xmlWriteDoubleChild(xmlNodePtr node, const char *label, const double value)
void xmlWriteCharChild(xmlNodePtr node, const char *label, const char *value)
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)=0
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
std::string xmlReadStringChild(xmlDocPtr doc, xmlNodePtr node)
float xmlReadFloatChild(xmlDocPtr doc, xmlNodePtr node)
void xmlWriteStringChild(xmlNodePtr node, const char *label, const std::string &value)
bool xmlReadBoolChild(xmlDocPtr doc, xmlNodePtr node)
std::map< std::string, int > nodeMap
void parse(const std::string &filename)