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