Visual Servoing Platform  version 3.0.1
testXmlParser.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example which describes how to use the xml parser class.
32  *
33  * Author:
34  * Romain Tallonneau
35  *
36  *****************************************************************************/
37 
38 
58 #include <visp3/core/vpConfig.h>
59 
60 #include <iostream>
61 #if defined(VISP_HAVE_XML2)
62 
63 #include <visp3/core/vpXmlParser.h>
64 #include <visp3/core/vpDebug.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/io/vpParseArgv.h>
67 
68 #include <string>
69 
70 
71 #ifndef DOXYGEN_SHOULD_SKIP_THIS
72 
73 /* -------------------------------------------------------------------------- */
74 /* CLASS EXAMPLE */
75 /* -------------------------------------------------------------------------- */
76 
82 class vpExampleDataParser: public vpXmlParser
83 {
84 protected:
85  double m_range;
86  int m_step;
87  int m_size_filter;
88  std::string m_name;
89 
90  typedef enum{
91  config,
92  range,
93  step,
94  size_filter,
95  name
96  }dataToParse;
97 
98 
99 public:
100  vpExampleDataParser();
101  virtual ~vpExampleDataParser();
102 
103  // Data accessors.
104  double getRange() const {return m_range;}
105  int getStep() const {return m_step;}
106  int getSizeFilter() const {return m_size_filter;}
107  std::string getName() const {return m_name;}
108 
109  void setRange(const double _range) {m_range = _range;}
110  void setStep(const int _step) {m_step = _step;}
111  void setSizeFilter(const int _size_filter) {m_size_filter = _size_filter;}
112  void setName(const std::string& _name) { m_name = _name;}
113 
114 protected:
115  virtual void readMainClass (xmlDocPtr doc, xmlNodePtr node);
116  virtual void writeMainClass (xmlNodePtr node);
117 };
118 
125 vpExampleDataParser::vpExampleDataParser()
126  : m_range(0.), m_step(0), m_size_filter(0), m_name("")
127 {
128  nodeMap["config"] = config;
129  nodeMap["range"] = range;
130  nodeMap["step"] = step;
131  nodeMap["size_filter"] = size_filter;
132  nodeMap["name"] = name;
133 }
134 
139 vpExampleDataParser::~vpExampleDataParser()
140 {
141 
142 }
143 
152 void
153 vpExampleDataParser::readMainClass (xmlDocPtr doc, xmlNodePtr node)
154 {
155  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
156  if(dataNode->type == XML_ELEMENT_NODE){
157  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
158  if(iter_data != nodeMap.end()){
159  switch (iter_data->second){
160  case range:
161  this->m_range = xmlReadDoubleChild(doc, dataNode);
162  break;
163  case step:
164  this->m_step = xmlReadIntChild(doc, dataNode);
165  break;
166  case size_filter:
167  this->m_size_filter = xmlReadIntChild(doc, dataNode);
168  break;
169  case name:{
170  this->m_name = xmlReadStringChild(doc, dataNode);
171  }break;
172  default:
173  vpTRACE("unknown tag in readConfigNode : %d, %s", iter_data->second, (iter_data->first).c_str());
174  break;
175  }
176  }
177  }
178  }
179 }
180 
188 void
189 vpExampleDataParser::writeMainClass(xmlNodePtr node)
190 {
191  xmlWriteDoubleChild(node, (const char*)"range", m_range);
192  xmlWriteIntChild(node, (const char*)"step", m_step);
193  xmlWriteIntChild(node, (const char*)"size_filter", m_size_filter);
194  xmlWriteCharChild(node, (const char*)"name", m_name.c_str());
195 }
196 
197 
198 #endif // doxygen
199 
200 /* -------------------------------------------------------------------------- */
201 /* COMMAND LINE OPTIONS */
202 /* -------------------------------------------------------------------------- */
203 
204 // List of allowed command line options
205 #define GETOPTARGS "cdo:h"
206 
207 void usage(const char *name, const char *badparam, const std::string& opath, const std::string& user);
208 bool getOptions(int argc, const char **argv, std::string &opath, const std::string& user);
209 
220 void usage(const char *name, const char *badparam, const std::string& opath, const std::string& user)
221 {
222  fprintf(stdout, "\n\
223 Write and read data in a xml file.\n\
224  \n\
225 SYNOPSIS\n\
226  %s [-o <output image path>] [-h]\n", name);
227 
228  fprintf(stdout, "\n\
229 OPTIONS: Default\n\
230  -o <output data path> %s\n\
231  Set data output path.\n\
232  From this directory, creates the \"%s\"\n\
233  subdirectory depending on the username, where \n\
234  dataTestXml.xml file is written.\n\
235  \n\
236  -h\n\
237  Print the help.\n\n", opath.c_str(), user.c_str());
238 
239  if (badparam) {
240  fprintf(stderr, "ERROR: \n" );
241  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
242  }
243 }
244 
254 bool getOptions(int argc, const char **argv, std::string &opath, const std::string& user)
255 {
256  const char *optarg_;
257  int c;
258  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
259 
260  switch (c) {
261  case 'o': opath = optarg_; break;
262  case 'h': usage(argv[0], NULL, opath, user); return false; break;
263 
264  case 'c':
265  case 'd':
266  break;
267 
268  default:
269  usage(argv[0], optarg_, opath, user); return false; break;
270  }
271  }
272 
273  if ((c == 1) || (c == -1)) {
274  // standalone param or error
275  usage(argv[0], NULL, opath, user);
276  std::cerr << "ERROR: " << std::endl;
277  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
278  return false;
279  }
280 
281  return true;
282 }
283 
284 
285 
286 /* -------------------------------------------------------------------------- */
287 /* MAIN FUNCTION */
288 /* -------------------------------------------------------------------------- */
289 
290 int main(int argc, const char** argv)
291 {
292  try {
293  std::string opt_opath;
294  std::string opath;
295  std::string filename;
296  std::string username;
297 
298  std::cout << "-------------------------------------------------------" << std::endl ;
299  std::cout << " testXmlParser.cpp" <<std::endl << std::endl ;
300  std::cout << " writing and readind data using a xml parser" << std::endl ;
301  std::cout << "-------------------------------------------------------" << std::endl ;
302  std::cout << std::endl ;
303 
304  // Set the default output path
305 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
306  opt_opath = "/tmp";
307 #elif defined(_WIN32)
308  opt_opath = "C:\\temp";
309 #endif
310 
311  // Get the user login name
312  vpIoTools::getUserName(username);
313 
314  // Read the command line options
315  if (getOptions(argc, argv, opt_opath, username) == false) {
316  exit (-1);
317  }
318 
319  // Get the option values
320  if (!opt_opath.empty())
321  opath = opt_opath;
322 
323  // Append to the output path string, the login name of the user
324  std::string dirname = vpIoTools::createFilePath(opath, username);
325 
326  // Test if the output path exist. If no try to create it
327  if (vpIoTools::checkDirectory(dirname) == false) {
328  try {
329  // Create the dirname
330  vpIoTools::makeDirectory(dirname);
331  }
332  catch (...) {
333  usage(argv[0], NULL, opath, username);
334  std::cerr << std::endl
335  << "ERROR:" << std::endl;
336  std::cerr << " Cannot create " << dirname << std::endl;
337  std::cerr << " Check your -o " << opath << " option " << std::endl;
338  exit(-1);
339  }
340  }
341 
342  filename = dirname + vpIoTools::path("/") + "dataTestXml.xml";
343 
344  // Write data using a parser.
345  {
346  vpExampleDataParser parser1;
347 
348  // Acquire data from measurments or tests.
349  parser1.setRange(3.5);
350  parser1.setStep(2);
351  parser1.setSizeFilter(5);
352  parser1.setName("cube");
353 
354  std::cout << "Write data to " << filename << std::endl;
355  parser1.save(filename);
356  }
357 
358  // Read data using another parser.
359  {
360  vpExampleDataParser parser2;
361 
362  parser2.parse(filename);
363 
364  std::cout << "Read from " << filename << std::endl ;
365  std::cout << "Range : " << parser2.getRange() << std::endl;
366  std::cout << "Step : " << parser2.getStep() << std::endl;
367  std::cout << "Filter size : " << parser2.getSizeFilter() << std::endl;
368  std::cout << "name : " << parser2.getName() << std::endl;
369  }
370 
371  // Clean up memory allocated by the xml library
373  return 0;
374  }
375  catch(vpException &e) {
376  std::cout << "Catch an exception: " << e << std::endl;
377  return 1;
378  }
379 }
380 
381 #else
382 
383 int main()
384 {
385  std::cout << "Xml parser requires libxml2." << std::endl;
386  return 0;
387 }
388 #endif
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
error that can be emited by ViSP classes.
Definition: vpException.h:73
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:770
virtual void writeMainClass(xmlNodePtr node)=0
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
Definition: vpXmlParser.h:175
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
#define vpTRACE
Definition: vpDebug.h:414
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
static std::string getUserName()
Definition: vpIoTools.cpp:177
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)=0
static void cleanup()
Definition: vpXmlParser.h:308