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