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 = 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 = strtoul ((
char *)val_char, &control_convert, 10);
199 if (val_char == control_convert){
200 xmlFree((xmlChar*) val_char);
203 xmlFree((xmlChar*) val_char);
222 if(node ->xmlChildrenNode == NULL){
223 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read double";
224 std::cerr << errorMsg << std::endl;
228 char * control_convert;
231 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
232 val_double = strtod ((
char *)val_char, &control_convert);
234 if (val_char == control_convert){
235 xmlFree((xmlChar*) val_char);
238 xmlFree((xmlChar*) val_char);
253 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)value);
254 xmlAddChild(node, tmp);
268 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)value.c_str());
269 xmlAddChild(node, tmp);
284 sprintf(str,
"%d", value);
286 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
287 xmlAddChild(node, tmp);
301 sprintf(str,
"%u", value);
303 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
304 xmlAddChild(node, tmp);
319 sprintf(str,
"%lf", value);
321 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
322 xmlAddChild(node, tmp);
342 xmlNodePtr root_node;
344 doc = xmlParseFile(filename.c_str());
350 root_node = xmlDocGetRootElement(doc);
351 if(root_node == NULL){
375 xmlNodePtr root_node;
377 doc = xmlReadFile(filename.c_str(), NULL, XML_PARSE_NOWARNING + XML_PARSE_NOERROR + XML_PARSE_NOBLANKS);
379 doc = xmlNewDoc ((xmlChar*)
"1.0");
380 root_node = xmlNewNode(NULL, (xmlChar*)
main_tag.c_str());
381 xmlDocSetRootElement(doc, root_node);
386 if (
remove(filename.c_str()) != 0)
389 doc = xmlNewDoc ((xmlChar*)
"1.0");
390 root_node = xmlNewNode(NULL, (xmlChar*)
main_tag.c_str());
391 xmlDocSetRootElement(doc, root_node);
395 root_node = xmlDocGetRootElement(doc);
396 if (root_node == NULL)
404 xmlSaveFormatFile(filename.c_str(), doc, 1);
408 #elif !defined(VISP_BUILD_SHARED_LIBS)
410 void dummy_vpXmlParser() {};
void xmlWriteIntChild(xmlNodePtr node, const char *label, const int 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)
void xmlWriteStringChild(xmlNodePtr node, const char *label, const std::string &value)
std::map< std::string, int > nodeMap
void parse(const std::string &filename)