Visual Servoing Platform  version 3.6.1 under development (2024-05-16)
testXmlParser.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
51 #include <visp3/core/vpConfig.h>
52 
53 #include <iostream>
54 #if defined(VISP_HAVE_XML2)
55 
56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpIoTools.h>
58 #include <visp3/core/vpXmlParser.h>
59 #include <visp3/io/vpParseArgv.h>
60 
61 #include <string>
62 
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 
70 class vpExampleDataParser : public vpXmlParser
71 {
72 protected:
73  double m_range;
74  int m_step;
75  int m_size_filter;
76  std::string m_name;
77 
78  typedef enum { config, range, step, size_filter, name } dataToParse;
79 
80 public:
81  vpExampleDataParser();
82 
83  // Data accessors.
84  double getRange() const { return m_range; }
85  int getStep() const { return m_step; }
86  int getSizeFilter() const { return m_size_filter; }
87  std::string getName() const { return m_name; }
88 
89  void setRange(double _range) { m_range = _range; }
90  void setStep(int _step) { m_step = _step; }
91  void setSizeFilter(int _size_filter) { m_size_filter = _size_filter; }
92  void setName(const std::string &_name) { m_name = _name; }
93 
94 protected:
95  virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node);
96  virtual void writeMainClass(xmlNodePtr node);
97 };
98 
104 vpExampleDataParser::vpExampleDataParser() : m_range(0.), m_step(0), m_size_filter(0), m_name("")
105 {
106  nodeMap["config"] = config;
107  nodeMap["range"] = range;
108  nodeMap["step"] = step;
109  nodeMap["size_filter"] = size_filter;
110  nodeMap["name"] = name;
111 }
112 
121 void vpExampleDataParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
122 {
123  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != nullptr; dataNode = dataNode->next) {
124  if (dataNode->type == XML_ELEMENT_NODE) {
125  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
126  if (iter_data != nodeMap.end()) {
127  switch (iter_data->second) {
128  case range:
129  this->m_range = xmlReadDoubleChild(doc, dataNode);
130  break;
131  case step:
132  this->m_step = xmlReadIntChild(doc, dataNode);
133  break;
134  case size_filter:
135  this->m_size_filter = xmlReadIntChild(doc, dataNode);
136  break;
137  case name: {
138  this->m_name = xmlReadStringChild(doc, dataNode);
139  } break;
140  default:
141  vpTRACE("unknown tag in readConfigNode : %d, %s", iter_data->second, (iter_data->first).c_str());
142  break;
143  }
144  }
145  }
146  }
147 }
148 
156 void vpExampleDataParser::writeMainClass(xmlNodePtr node)
157 {
158  xmlWriteDoubleChild(node, (const char *)"range", m_range);
159  xmlWriteIntChild(node, (const char *)"step", m_step);
160  xmlWriteIntChild(node, (const char *)"size_filter", m_size_filter);
161  xmlWriteCharChild(node, (const char *)"name", m_name.c_str());
162 }
163 
164 #endif // doxygen
165 
166 /* -------------------------------------------------------------------------- */
167 /* COMMAND LINE OPTIONS */
168 /* -------------------------------------------------------------------------- */
169 
170  // List of allowed command line options
171 #define GETOPTARGS "cdo:h"
172 
173 void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user);
174 bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user);
175 
184 void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user)
185 {
186  fprintf(stdout, "\n\
187 Write and read data in a xml file.\n\
188  \n\
189 SYNOPSIS\n\
190  %s [-o <output image path>] [-h]\n",
191  name);
192 
193  fprintf(stdout, "\n\
194 OPTIONS: Default\n\
195  -o <output data path> %s\n\
196  Set data output path.\n\
197  From this directory, creates the \"%s\"\n\
198  subdirectory depending on the username, where \n\
199  dataTestXml.xml file is written.\n\
200  \n\
201  -h\n\
202  Print the help.\n\n",
203  opath.c_str(), user.c_str());
204 
205  if (badparam) {
206  fprintf(stderr, "ERROR: \n");
207  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
208  }
209 }
210 
220 bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user)
221 {
222  const char *optarg_;
223  int c;
224  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
225 
226  switch (c) {
227  case 'o':
228  opath = optarg_;
229  break;
230  case 'h':
231  usage(argv[0], nullptr, opath, user);
232  return false;
233  break;
234 
235  case 'c':
236  case 'd':
237  break;
238 
239  default:
240  usage(argv[0], optarg_, opath, user);
241  return false;
242  break;
243  }
244  }
245 
246  if ((c == 1) || (c == -1)) {
247  // standalone param or error
248  usage(argv[0], nullptr, opath, user);
249  std::cerr << "ERROR: " << std::endl;
250  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
251  return false;
252  }
253 
254  return true;
255 }
256 
257 /* -------------------------------------------------------------------------- */
258 /* MAIN FUNCTION */
259 /* -------------------------------------------------------------------------- */
260 
261 int main(int argc, const char **argv)
262 {
263  try {
264  std::string opt_opath;
265  std::string opath;
266  std::string filename;
267  std::string username;
268 
269  std::cout << "-------------------------------------------------------" << std::endl;
270  std::cout << " testXmlParser.cpp" << std::endl << std::endl;
271  std::cout << " writing and readind data using a xml parser" << std::endl;
272  std::cout << "-------------------------------------------------------" << std::endl;
273  std::cout << std::endl;
274 
275  // Set the default output path
276 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
277  opt_opath = "/tmp";
278 #elif defined(_WIN32)
279  opt_opath = "C:\\temp";
280 #endif
281 
282  // Get the user login name
283  vpIoTools::getUserName(username);
284 
285  // Read the command line options
286  if (getOptions(argc, argv, opt_opath, username) == false) {
287  return EXIT_FAILURE;
288  }
289 
290  // Get the option values
291  if (!opt_opath.empty())
292  opath = opt_opath;
293 
294  // Append to the output path string, the login name of the user
295  std::string dirname = vpIoTools::createFilePath(opath, username);
296 
297  // Test if the output path exist. If no try to create it
298  if (vpIoTools::checkDirectory(dirname) == false) {
299  try {
300  // Create the dirname
301  vpIoTools::makeDirectory(dirname);
302  }
303  catch (...) {
304  usage(argv[0], nullptr, opath, username);
305  std::cerr << std::endl << "ERROR:" << std::endl;
306  std::cerr << " Cannot create " << dirname << std::endl;
307  std::cerr << " Check your -o " << opath << " option " << std::endl;
308  return EXIT_FAILURE;
309  }
310  }
311 
312  filename = dirname + vpIoTools::path("/") + "dataTestXml.xml";
313 
314  // Write data using a parser.
315  {
316  vpExampleDataParser parser1;
317 
318  // Acquire data from measurments or tests.
319  parser1.setRange(3.5);
320  parser1.setStep(2);
321  parser1.setSizeFilter(5);
322  parser1.setName("cube");
323 
324  std::cout << "Write data to " << filename << std::endl;
325  parser1.save(filename);
326  }
327 
328  // Read data using another parser.
329  {
330  vpExampleDataParser parser2;
331 
332  parser2.parse(filename);
333 
334  std::cout << "Read from " << filename << std::endl;
335  std::cout << "Range : " << parser2.getRange() << std::endl;
336  std::cout << "Step : " << parser2.getStep() << std::endl;
337  std::cout << "Filter size : " << parser2.getSizeFilter() << std::endl;
338  std::cout << "name : " << parser2.getName() << std::endl;
339  }
340 
341  // Clean up memory allocated by the xml library
343  return EXIT_SUCCESS;
344  }
345  catch (const vpException &e) {
346  std::cout << "Catch an exception: " << e << std::endl;
347  return EXIT_FAILURE;
348  }
349 }
350 
351 #else
352 
353 int main()
354 {
355  std::cout << "Xml parser requires libxml2." << std::endl;
356  return EXIT_SUCCESS;
357 }
358 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1434
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:834
static std::string getUserName()
Definition: vpIoTools.cpp:727
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2197
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:983
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)=0
static void cleanup()
Definition: vpXmlParser.h:309
virtual void writeMainClass(xmlNodePtr node)=0
#define vpTRACE
Definition: vpDebug.h:407