57 #include <visp3/core/vpConfig.h> 60 #if defined(VISP_HAVE_XML2) 62 #include <visp3/core/vpDebug.h> 63 #include <visp3/core/vpIoTools.h> 64 #include <visp3/core/vpXmlParser.h> 65 #include <visp3/io/vpParseArgv.h> 69 #ifndef DOXYGEN_SHOULD_SKIP_THIS 90 typedef enum { config, range, step, size_filter, name } dataToParse;
93 vpExampleDataParser();
94 virtual ~vpExampleDataParser();
97 double getRange()
const {
return m_range; }
98 int getStep()
const {
return m_step; }
99 int getSizeFilter()
const {
return m_size_filter; }
100 std::string getName()
const {
return m_name; }
102 void setRange(
const double _range) { m_range = _range; }
103 void setStep(
const int _step) { m_step = _step; }
104 void setSizeFilter(
const int _size_filter) { m_size_filter = _size_filter; }
105 void setName(
const std::string &_name) { m_name = _name; }
118 vpExampleDataParser::vpExampleDataParser() : m_range(0.), m_step(0), m_size_filter(0), m_name(
"")
120 nodeMap[
"config"] = config;
121 nodeMap[
"range"] = range;
122 nodeMap[
"step"] = step;
123 nodeMap[
"size_filter"] = size_filter;
124 nodeMap[
"name"] = name;
131 vpExampleDataParser::~vpExampleDataParser() {}
141 void vpExampleDataParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
143 for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
144 if (dataNode->type == XML_ELEMENT_NODE) {
145 std::map<std::string, int>::iterator iter_data = this->nodeMap.find((
char *)dataNode->name);
146 if (iter_data != nodeMap.end()) {
147 switch (iter_data->second) {
149 this->m_range = xmlReadDoubleChild(doc, dataNode);
152 this->m_step = xmlReadIntChild(doc, dataNode);
155 this->m_size_filter = xmlReadIntChild(doc, dataNode);
158 this->m_name = xmlReadStringChild(doc, dataNode);
161 vpTRACE(
"unknown tag in readConfigNode : %d, %s", iter_data->second, (iter_data->first).c_str());
176 void vpExampleDataParser::writeMainClass(xmlNodePtr node)
178 xmlWriteDoubleChild(node, (
const char *)
"range", m_range);
179 xmlWriteIntChild(node, (
const char *)
"step", m_step);
180 xmlWriteIntChild(node, (
const char *)
"size_filter", m_size_filter);
181 xmlWriteCharChild(node, (
const char *)
"name", m_name.c_str());
193 #define GETOPTARGS "cdo:h" 195 void usage(
const char *name,
const char *badparam,
const std::string &opath,
const std::string &user);
196 bool getOptions(
int argc,
const char **argv, std::string &opath,
const std::string &user);
208 void usage(
const char *name,
const char *badparam,
const std::string &opath,
const std::string &user)
211 Write and read data in a xml file.\n\ 214 %s [-o <output image path>] [-h]\n", name);
218 -o <output data path> %s\n\ 219 Set data output path.\n\ 220 From this directory, creates the \"%s\"\n\ 221 subdirectory depending on the username, where \n\ 222 dataTestXml.xml file is written.\n\ 225 Print the help.\n\n", opath.c_str(), user.c_str());
228 fprintf(stderr,
"ERROR: \n");
229 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
242 bool getOptions(
int argc,
const char **argv, std::string &opath,
const std::string &user)
253 usage(argv[0], NULL, opath, user);
262 usage(argv[0], optarg_, opath, user);
268 if ((c == 1) || (c == -1)) {
270 usage(argv[0], NULL, opath, user);
271 std::cerr <<
"ERROR: " << std::endl;
272 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
285 int main(
int argc,
const char **argv)
288 std::string opt_opath;
290 std::string filename;
291 std::string username;
293 std::cout <<
"-------------------------------------------------------" << std::endl;
294 std::cout <<
" testXmlParser.cpp" << std::endl << std::endl;
295 std::cout <<
" writing and readind data using a xml parser" << std::endl;
296 std::cout <<
"-------------------------------------------------------" << std::endl;
297 std::cout << std::endl;
300 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 302 #elif defined(_WIN32) 303 opt_opath =
"C:\\temp";
310 if (getOptions(argc, argv, opt_opath, username) ==
false) {
315 if (!opt_opath.empty())
327 usage(argv[0], NULL, opath, username);
328 std::cerr << std::endl <<
"ERROR:" << std::endl;
329 std::cerr <<
" Cannot create " << dirname << std::endl;
330 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
339 vpExampleDataParser parser1;
342 parser1.setRange(3.5);
344 parser1.setSizeFilter(5);
345 parser1.setName(
"cube");
347 std::cout <<
"Write data to " << filename << std::endl;
348 parser1.save(filename);
353 vpExampleDataParser parser2;
355 parser2.parse(filename);
357 std::cout <<
"Read from " << filename << std::endl;
358 std::cout <<
"Range : " << parser2.getRange() << std::endl;
359 std::cout <<
"Step : " << parser2.getStep() << std::endl;
360 std::cout <<
"Filter size : " << parser2.getSizeFilter() << std::endl;
361 std::cout <<
"name : " << parser2.getName() << std::endl;
368 std::cout <<
"Catch an exception: " << e << std::endl;
377 std::cout <<
"Xml parser requires libxml2." << std::endl;
error that can be emited by ViSP classes.
virtual void writeMainClass(xmlNodePtr node)=0
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)=0