ViSP
testXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: testXmlParser.cpp 5126 2015-01-05 22:07:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Example which describes how to use the xml parser class.
36  *
37  * Author:
38  * Romain Tallonneau
39  *
40  *****************************************************************************/
41 
42 
62 #include <visp/vpConfig.h>
63 
64 #include <iostream>
65 #if defined(VISP_HAVE_XML2)
66 
67 #include <visp/vpXmlParser.h>
68 #include <visp/vpDebug.h>
69 #include <visp/vpIoTools.h>
70 #include <visp/vpParseArgv.h>
71 
72 #include <string>
73 
74 
75 #ifndef DOXYGEN_SHOULD_SKIP_THIS
76 
77 /* -------------------------------------------------------------------------- */
78 /* CLASS EXAMPLE */
79 /* -------------------------------------------------------------------------- */
80 
86 class vpExampleDataParser: public vpXmlParser
87 {
88 protected:
89  double m_range;
90  int m_step;
91  int m_size_filter;
92  std::string m_name;
93 
94  typedef enum{
95  config,
96  range,
97  step,
98  size_filter,
99  name
100  }dataToParse;
101 
102 
103 public:
104  vpExampleDataParser();
105  virtual ~vpExampleDataParser();
106 
107  // Data accessors.
108  double getRange() const {return m_range;}
109  int getStep() const {return m_step;}
110  int getSizeFilter() const {return m_size_filter;}
111  std::string getName() const {return m_name;}
112 
113  void setRange(const double _range) {m_range = _range;}
114  void setStep(const int _step) {m_step = _step;}
115  void setSizeFilter(const int _size_filter) {m_size_filter = _size_filter;}
116  void setName(const std::string& _name) { m_name = _name;}
117 
118 protected:
119  virtual void readMainClass (xmlDocPtr doc, xmlNodePtr node);
120  virtual void writeMainClass (xmlNodePtr node);
121 };
122 
129 vpExampleDataParser::vpExampleDataParser()
130  : m_range(0.), m_step(0), m_size_filter(0), m_name("")
131 {
132  nodeMap["config"] = config;
133  nodeMap["range"] = range;
134  nodeMap["step"] = step;
135  nodeMap["size_filter"] = size_filter;
136  nodeMap["name"] = name;
137 }
138 
143 vpExampleDataParser::~vpExampleDataParser()
144 {
145 
146 }
147 
156 void
157 vpExampleDataParser::readMainClass (xmlDocPtr doc, xmlNodePtr node)
158 {
159  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
160  if(dataNode->type == XML_ELEMENT_NODE){
161  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
162  if(iter_data != nodeMap.end()){
163  switch (iter_data->second){
164  case range:
165  this->m_range = xmlReadDoubleChild(doc, dataNode);
166  break;
167  case step:
168  this->m_step = xmlReadIntChild(doc, dataNode);
169  break;
170  case size_filter:
171  this->m_size_filter = xmlReadIntChild(doc, dataNode);
172  break;
173  case name:{
174  this->m_name = xmlReadStringChild(doc, dataNode);
175  }break;
176  default:
177  vpTRACE("unknown tag in readConfigNode : %d, %s", iter_data->second, (iter_data->first).c_str());
178  break;
179  }
180  }
181  }
182  }
183 }
184 
192 void
193 vpExampleDataParser::writeMainClass(xmlNodePtr node)
194 {
195  xmlWriteDoubleChild(node, (const char*)"range", m_range);
196  xmlWriteIntChild(node, (const char*)"step", m_step);
197  xmlWriteIntChild(node, (const char*)"size_filter", m_size_filter);
198  xmlWriteCharChild(node, (const char*)"name", m_name.c_str());
199 }
200 
201 
202 #endif // doxygen
203 
204 /* -------------------------------------------------------------------------- */
205 /* COMMAND LINE OPTIONS */
206 /* -------------------------------------------------------------------------- */
207 
208 // List of allowed command line options
209 #define GETOPTARGS "o:h"
210 
211 void usage(const char *name, const char *badparam, const std::string& opath, const std::string& user);
212 bool getOptions(int argc, const char **argv, std::string &opath, const std::string& user);
213 
224 void usage(const char *name, const char *badparam, const std::string& opath, const std::string& user)
225 {
226  fprintf(stdout, "\n\
227 Write and read data in a xml file.\n\
228  \n\
229 SYNOPSIS\n\
230  %s [-o <output image path>] [-h]\n", name);
231 
232  fprintf(stdout, "\n\
233 OPTIONS: Default\n\
234  -o <output data path> %s\n\
235  Set data output path.\n\
236  From this directory, creates the \"%s\"\n\
237  subdirectory depending on the username, where \n\
238  dataTestXml.xml file is written.\n\
239  \n\
240  -h\n\
241  Print the help.\n\n", opath.c_str(), user.c_str());
242 
243  if (badparam) {
244  fprintf(stderr, "ERROR: \n" );
245  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
246  }
247 }
248 
258 bool getOptions(int argc, const char **argv, std::string &opath, const std::string& user)
259 {
260  const char *optarg_;
261  int c;
262  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
263 
264  switch (c) {
265  case 'o': opath = optarg_; break;
266  case 'h': usage(argv[0], NULL, opath, user); return false; 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:315
#define vpTRACE
Definition: vpDebug.h:418
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:695
virtual void writeMainClass(xmlNodePtr node)=0
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
Definition: vpXmlParser.h:178
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static std::string getUserName()
Definition: vpIoTools.cpp:141
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)=0
static void cleanup()
Definition: vpXmlParser.h:220