ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: testXmlParser.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 {
131  nodeMap["config"] = config;
132  nodeMap["range"] = range;
133  nodeMap["step"] = step;
134  nodeMap["size_filter"] = size_filter;
135  nodeMap["name"] = name;
136 
137  m_range = 0.0;
138  m_step = 0;
139  m_size_filter = 0;
140  m_name = "";
141 }
142 
147 vpExampleDataParser::~vpExampleDataParser()
148 {
149 
150 }
151 
160 void
161 vpExampleDataParser::readMainClass (xmlDocPtr doc, xmlNodePtr node)
162 {
163  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
164  if(dataNode->type == XML_ELEMENT_NODE){
165  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
166  if(iter_data != nodeMap.end()){
167  switch (iter_data->second){
168  case range:
169  this->m_range = xmlReadDoubleChild(doc, dataNode);
170  break;
171  case step:
172  this->m_step = xmlReadIntChild(doc, dataNode);
173  break;
174  case size_filter:
175  this->m_size_filter = xmlReadIntChild(doc, dataNode);
176  break;
177  case name:{
178  this->m_name = xmlReadStringChild(doc, dataNode);
179  }break;
180  default:
181  vpTRACE("unknown tag in readConfigNode : %d, %s", iter_data->second, (iter_data->first).c_str());
182  break;
183  }
184  }
185  }
186  }
187 }
188 
196 void
197 vpExampleDataParser::writeMainClass(xmlNodePtr node)
198 {
199  xmlWriteDoubleChild(node, (const char*)"range", m_range);
200  xmlWriteIntChild(node, (const char*)"step", m_step);
201  xmlWriteIntChild(node, (const char*)"size_filter", m_size_filter);
202  xmlWriteCharChild(node, (const char*)"name", m_name.c_str());
203 }
204 
205 
206 #endif // doxygen
207 
208 /* -------------------------------------------------------------------------- */
209 /* COMMAND LINE OPTIONS */
210 /* -------------------------------------------------------------------------- */
211 
212 // List of allowed command line options
213 #define GETOPTARGS "o:h"
214 
225 void usage(const char *name, const char *badparam, const std::string& opath, const std::string& user)
226 {
227  fprintf(stdout, "\n\
228  Write and read data in a xml file.\n\
229  \n\
230  SYNOPSIS\n\
231  %s [-o <output image path>] [-h]\n \
232  ", name);
233 
234  fprintf(stdout, "\n\
235  OPTIONS: Default\n\
236  -o <output data path> %s\n\
237  Set data output path.\n\
238  From this directory, creates the \"%s\"\n\
239  subdirectory depending on the username, where \n\
240  dataTestXml.xml file is written.\n\
241  \n\
242  -h\n\
243  Print the help.\n\n",
244  opath.c_str(), user.c_str());
245 
246  if (badparam) {
247  fprintf(stderr, "ERROR: \n" );
248  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
249  }
250 }
251 
261 bool getOptions(int argc, const char **argv,
262  std::string &opath, const std::string& user)
263 {
264  const char *optarg;
265  int c;
266  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
267 
268  switch (c) {
269  case 'o': opath = optarg; break;
270  case 'h': usage(argv[0], NULL, opath, user); return false; break;
271 
272  default:
273  usage(argv[0], optarg, opath, user); return false; break;
274  }
275  }
276 
277  if ((c == 1) || (c == -1)) {
278  // standalone param or error
279  usage(argv[0], NULL, opath, user);
280  std::cerr << "ERROR: " << std::endl;
281  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
282  return false;
283  }
284 
285  return true;
286 }
287 
288 
289 
290 /* -------------------------------------------------------------------------- */
291 /* MAIN FUNCTION */
292 /* -------------------------------------------------------------------------- */
293 
294 int main(int argc, const char** argv)
295 {
296  std::string opt_opath;
297  std::string opath;
298  std::string filename;
299  std::string username;
300 
301  std::cout << "-------------------------------------------------------" << std::endl ;
302  std::cout << " testXmlParser.cpp" <<std::endl << std::endl ;
303  std::cout << " writing and readind data using a xml parser" << std::endl ;
304  std::cout << "-------------------------------------------------------" << std::endl ;
305  std::cout << std::endl ;
306 
307  // Set the default output path
308 #ifdef UNIX
309  opt_opath = "/tmp";
310 #elif WIN32
311  opt_opath = "C:\\temp";
312 #endif
313 
314  // Get the user login name
315  vpIoTools::getUserName(username);
316 
317  // Read the command line options
318  if (getOptions(argc, argv, opt_opath, username) == false) {
319  exit (-1);
320  }
321 
322  // Get the option values
323  if (!opt_opath.empty())
324  opath = opt_opath;
325 
326  // Append to the output path string, the login name of the user
327  std::string dirname = opath + vpIoTools::path("/") + username;
328 
329  // Test if the output path exist. If no try to create it
330  if (vpIoTools::checkDirectory(dirname) == false) {
331  try {
332  // Create the dirname
333  vpIoTools::makeDirectory(dirname);
334  }
335  catch (...) {
336  usage(argv[0], NULL, opath, username);
337  std::cerr << std::endl
338  << "ERROR:" << std::endl;
339  std::cerr << " Cannot create " << dirname << std::endl;
340  std::cerr << " Check your -o " << opath << " option " << std::endl;
341  exit(-1);
342  }
343  }
344 
345  filename = dirname + vpIoTools::path("/") + "dataTestXml.xml";
346 
347  // Write data using a parser.
348  {
349  vpExampleDataParser parser1;
350 
351  // Acquire data from measurments or tests.
352  parser1.setRange(3.5);
353  parser1.setStep(2);
354  parser1.setSizeFilter(5);
355  parser1.setName("cube");
356 
357  std::cout << "Write data to " << filename << std::endl;
358  parser1.save(filename);
359  }
360 
361  // Read data using another parser.
362  {
363  vpExampleDataParser parser2;
364 
365  parser2.parse(filename);
366 
367  std::cout << "Read from " << filename << std::endl ;
368  std::cout << "Range : " << parser2.getRange() << std::endl;
369  std::cout << "Step : " << parser2.getStep() << std::endl;
370  std::cout << "Filter size : " << parser2.getSizeFilter() << std::endl;
371  std::cout << "name : " << parser2.getName() << std::endl;
372  }
373 
374  // Clean up memory allocated by the xml library
376 
377  return 0;
378 }
379 
380 #else
381 
382 int main()
383 {
384  std::cout << "Xml parser requires libxml2." << std::endl;
385  return 0;
386 }
387 #endif