Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpIoTools_config_file.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Io tools dedicated to npy.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 #include <visp3/core/vpEndian.h>
36 #include <visp3/core/vpIoTools.h>
37 
38 BEGIN_VISP_NAMESPACE
39 
40 std::string vpIoTools::baseName = "";
41 std::string vpIoTools::baseDir = "";
42 std::string vpIoTools::configFile = "";
43 std::vector<std::string> vpIoTools::configVars = std::vector<std::string>();
44 std::vector<std::string> vpIoTools::configValues = std::vector<std::string>();
45 
46 const char vpIoTools::separator =
47 #if defined(_WIN32)
48 '\\';
49 #else
50 '/';
51 #endif
52 
53 namespace
54 {
55 std::string &ltrim(std::string &s)
56 {
57 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
58  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
59 #else
60  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
61 #endif
62  return s;
63 }
64 
65 std::string &rtrim(std::string &s)
66 {
67 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
68  s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) { return !std::isspace(c); }).base(), s.end());
69 #else
70  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
71 #endif
72  return s;
73 }
74 } // namespace
80 void vpIoTools::setBaseName(const std::string &s) { baseName = s; }
81 
87 void vpIoTools::setBaseDir(const std::string &dir) { baseDir = dir + "/"; }
88 
94 std::string vpIoTools::getBaseName() { return baseName; }
95 
101 std::string vpIoTools::getFullName() { return baseDir + baseName; }
102 
111 bool vpIoTools::loadConfigFile(const std::string &confFile)
112 {
113  configFile = path(confFile);
114  configVars.clear();
115  configValues.clear();
116  std::ifstream confContent(configFile.c_str(), std::ios::in);
117 
118  if (confContent.is_open()) {
119  std::string line, var, val;
120  unsigned long int k;
121  int c;
122  std::string stop[3] = { " ", "\t", "#" };
123  while (std::getline(confContent, line)) {
124  if ((line.compare(0, 1, "#") != 0) && (line.size() > 2)) {
125  // name of the variable
126  k = static_cast<unsigned long>(line.find(" "));
127  var = line.substr(0, k);
128  // look for the end of the actual value
129  c = 200;
130  for (unsigned i = 0; i < 3; ++i) {
131  c = vpMath::minimum(c,
132  static_cast<int>(line.find(stop[i], static_cast<size_t>(k) + static_cast<size_t>(1))));
133  }
134  if (c == -1) {
135  c = static_cast<int>(line.size());
136  }
137  unsigned long int c_ = static_cast<unsigned long int>(c);
138  val = line.substr(static_cast<size_t>(k) + static_cast<size_t>(1),
139  static_cast<size_t>(c_) - static_cast<size_t>(k) - static_cast<size_t>(1));
140  configVars.push_back(var);
141  configValues.push_back(val);
142  }
143  }
144  confContent.close();
145  }
146  else {
147  return false;
148  }
149  return true;
150 }
151 
160 bool vpIoTools::readConfigVar(const std::string &var, float &value)
161 {
162  bool found = false;
163  size_t configvars_size = configVars.size();
164  size_t k = 0;
165  while ((k < configvars_size) && (!found)) {
166  if (configVars[k] == var) {
167  if (configValues[k].compare("PI") == 0) {
168  value = static_cast<float>(M_PI);
169  }
170  else if (configValues[k].compare("PI/2") == 0) {
171  value = static_cast<float>(M_PI / 2.0);
172  }
173  else if (configValues[k].compare("-PI/2") == 0) {
174  value = static_cast<float>(-M_PI / 2.0);
175  }
176  else {
177  value = static_cast<float>(atof(configValues[k].c_str()));
178  }
179  found = true;
180  }
181  ++k;
182  }
183  if (!found) {
184  std::cout << var << " not found in config file" << std::endl;
185  }
186  return found;
187 }
188 
197 bool vpIoTools::readConfigVar(const std::string &var, double &value)
198 {
199  bool found = false;
200  size_t configvars_size = configVars.size();
201  size_t k = 0;
202  while ((k < configvars_size) && (!found)) {
203  if (configVars[k] == var) {
204  if (configValues[k].compare("PI") == 0) {
205  value = M_PI;
206  }
207  else if (configValues[k].compare("PI/2") == 0) {
208  value = M_PI / 2;
209  }
210  else if (configValues[k].compare("-PI/2") == 0) {
211  value = -M_PI / 2;
212  }
213  else {
214  value = atof(configValues[k].c_str());
215  }
216  found = true;
217  }
218  ++k;
219  }
220  if (!found) {
221  std::cout << var << " not found in config file" << std::endl;
222  }
223  return found;
224 }
225 
234 bool vpIoTools::readConfigVar(const std::string &var, int &value)
235 {
236  bool found = false;
237  size_t configvars_size = configVars.size();
238  size_t k = 0;
239  while ((k < configvars_size) && (!found)) {
240  if (configVars[k] == var) {
241  value = atoi(configValues[k].c_str());
242  found = true;
243  }
244  ++k;
245  }
246  if (!found) {
247  std::cout << var << " not found in config file" << std::endl;
248  }
249  return found;
250 }
251 
260 bool vpIoTools::readConfigVar(const std::string &var, unsigned int &value)
261 {
262  int v = 0;
263  bool found = readConfigVar(var, v);
264  value = static_cast<unsigned int>(v);
265  return found;
266 }
267 
276 bool vpIoTools::readConfigVar(const std::string &var, bool &value)
277 {
278  int v = 0;
279  bool found = readConfigVar(var, v);
280  value = (v != 0);
281  return found;
282 }
283 
292 bool vpIoTools::readConfigVar(const std::string &var, vpColor &value)
293 {
294  unsigned int v = 0;
295  bool found = readConfigVar(var, v);
296  value = vpColor::getColor(v);
297  return found;
298 }
299 
308 bool vpIoTools::readConfigVar(const std::string &var, std::string &value)
309 {
310  bool found = false;
311  size_t configvars_size = configVars.size();
312  size_t k = 0;
313  while ((k < configvars_size) && (!found)) {
314  if (configVars[k] == var) {
315  value = configValues[k];
316  found = true;
317  }
318  ++k;
319  }
320  if (!found) {
321  std::cout << var << " not found in config file" << std::endl;
322  }
323  return found;
324 }
325 
339 bool vpIoTools::readConfigVar(const std::string &var, vpArray2D<double> &value, const unsigned int &nCols,
340  const unsigned int &nRows)
341 {
342  bool found = false;
343  std::string nb;
344  size_t configvars_size = configVars.size();
345  size_t k = 0;
346  while ((k < configvars_size) && (!found)) {
347  if (configVars[k] == var) {
348  found = true;
349  // resize or not
350  if ((nCols != 0) && (nRows != 0)) {
351  value.resize(nRows, nCols);
352  }
353  size_t ind = 0, ind2;
354  unsigned int value_rows = value.getRows();
355  unsigned int value_cols = value.getCols();
356  for (unsigned int i = 0; i < value_rows; ++i) {
357  for (unsigned int j = 0; j < value_cols; ++j) {
358  ind2 = configValues[k].find(",", ind);
359  nb = configValues[k].substr(ind, ind2 - ind);
360  if (nb.compare("PI") == 0) {
361  value[i][j] = M_PI;
362  }
363  else if (nb.compare("PI/2") == 0) {
364  value[i][j] = M_PI / 2;
365  }
366  else if (nb.compare("-PI/2") == 0) {
367  value[i][j] = -M_PI / 2;
368  }
369  else {
370  value[i][j] = atof(nb.c_str());
371  }
372  ind = ind2 + 1;
373  }
374  }
375  }
376  ++k;
377  }
378  if (found == false) {
379  std::cout << var << " not found in config file" << std::endl;
380  }
381  return found;
382 }
383 
384 // construct experiment filename & path
385 
394 void vpIoTools::addNameElement(const std::string &strTrue, const bool &cond, const std::string &strFalse)
395 {
396  if (cond) {
397  baseName += "_" + strTrue;
398  }
399  else if (strFalse != "") {
400  baseName += "_" + strFalse;
401  }
402 }
403 
412 void vpIoTools::addNameElement(const std::string &strTrue, const double &val)
413 {
414  // if(val != 0.)
415  if (std::fabs(val) < std::numeric_limits<double>::epsilon()) {
416  std::stringstream valS;
417  valS.precision(4);
418  valS << val;
419  baseName += "_" + strTrue + valS.str();
420  }
421 }
422 
431 void vpIoTools::createBaseNamePath(const bool &empty)
432 {
433  std::string path = baseDir + baseName;
434  if (vpIoTools::checkDirectory(path) == false) {
435  try {
437  std::cout << "Creating directory " << path << std::endl;
438  }
439  catch (...) {
440  throw(vpException(vpException::fatalError, "Cannot create base name directory %s", path.c_str()));
441  }
442  }
443  else {
444  if (empty) {
445  std::cout << "Emptying directory " << path << std::endl;
446  vpIoTools::remove(path + "/*");
447  }
448  }
449 }
450 
457 void vpIoTools::saveConfigFile(const bool &actuallySave)
458 {
459  if (actuallySave) {
460  std::string dest = baseDir + "/" + baseName + "_config.txt";
461  // file copy
463  }
464 }
465 
469 void vpIoTools::readBinaryValueLE(std::ifstream &file, int16_t &short_value)
470 {
471  file.read((char *)(&short_value), sizeof(short_value));
472 
473 #ifdef VISP_BIG_ENDIAN
474  // Swap bytes order from little endian to big endian
475  short_value = vpEndian::swap16bits((uint16_t)short_value);
476 #endif
477 }
478 
482 void vpIoTools::readBinaryValueLE(std::ifstream &file, uint16_t &ushort_value)
483 {
484  file.read((char *)(&ushort_value), sizeof(ushort_value));
485 
486 #ifdef VISP_BIG_ENDIAN
487  // Swap bytes order from little endian to big endian
488  ushort_value = vpEndian::swap16bits(ushort_value);
489 #endif
490 }
491 
495 void vpIoTools::readBinaryValueLE(std::ifstream &file, int32_t &int_value)
496 {
497  file.read((char *)(&int_value), sizeof(int_value));
498 
499 #ifdef VISP_BIG_ENDIAN
500  // Swap bytes order from little endian to big endian
501  int_value = vpEndian::swap32bits((uint32_t)int_value);
502 #endif
503 }
504 
508 void vpIoTools::readBinaryValueLE(std::ifstream &file, uint32_t &uint_value)
509 {
510  file.read((char *)(&uint_value), sizeof(uint_value));
511 
512 #ifdef VISP_BIG_ENDIAN
513  // Swap bytes order from little endian to big endian
514  uint_value = vpEndian::swap32bits(uint_value);
515 #endif
516 }
517 
521 void vpIoTools::readBinaryValueLE(std::ifstream &file, float &float_value)
522 {
523  file.read((char *)(&float_value), sizeof(float_value));
524 
525 #ifdef VISP_BIG_ENDIAN
526  // Swap bytes order from little endian to big endian
527  float_value = vpEndian::swapFloat(float_value);
528 #endif
529 }
530 
534 void vpIoTools::readBinaryValueLE(std::ifstream &file, double &double_value)
535 {
536  file.read((char *)(&double_value), sizeof(double_value));
537 
538 #ifdef VISP_BIG_ENDIAN
539  // Swap bytes order from little endian to big endian
540  double_value = vpEndian::swapDouble(double_value);
541 #endif
542 }
543 
547 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const int16_t short_value)
548 {
549 #ifdef VISP_BIG_ENDIAN
550  // Swap bytes order to little endian
551  uint16_t swap_short = vpEndian::swap16bits((uint16_t)short_value);
552  file.write((char *)(&swap_short), sizeof(swap_short));
553 #else
554  file.write((char *)(&short_value), sizeof(short_value));
555 #endif
556 }
557 
561 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const uint16_t ushort_value)
562 {
563 #ifdef VISP_BIG_ENDIAN
564  // Swap bytes order to little endian
565  uint16_t swap_ushort = vpEndian::swap16bits(ushort_value);
566  file.write((char *)(&swap_ushort), sizeof(swap_ushort));
567 #else
568  file.write((char *)(&ushort_value), sizeof(ushort_value));
569 #endif
570 }
571 
575 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const int32_t int_value)
576 {
577 #ifdef VISP_BIG_ENDIAN
578  // Swap bytes order to little endian
579  uint32_t swap_int = vpEndian::swap32bits((uint32_t)int_value);
580  file.write((char *)(&swap_int), sizeof(swap_int));
581 #else
582  file.write((char *)(&int_value), sizeof(int_value));
583 #endif
584 }
585 
589 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const uint32_t uint_value)
590 {
591 #ifdef VISP_BIG_ENDIAN
592  // Swap bytes order to little endian
593  uint32_t swap_int = vpEndian::swap32bits(uint_value);
594  file.write((char *)(&swap_int), sizeof(swap_int));
595 #else
596  file.write((char *)(&uint_value), sizeof(uint_value));
597 #endif
598 }
599 
603 void vpIoTools::writeBinaryValueLE(std::ofstream &file, float float_value)
604 {
605 #ifdef VISP_BIG_ENDIAN
606  // Swap bytes order to little endian
607  float swap_float = vpEndian::swapFloat(float_value);
608  file.write((char *)(&swap_float), sizeof(swap_float));
609 #else
610  file.write((char *)(&float_value), sizeof(float_value));
611 #endif
612 }
613 
617 void vpIoTools::writeBinaryValueLE(std::ofstream &file, double double_value)
618 {
619 #ifdef VISP_BIG_ENDIAN
620  // Swap bytes order to little endian
621  double swap_double = vpEndian::swapDouble(double_value);
622  file.write((char *)(&swap_double), sizeof(swap_double));
623 #else
624  file.write((char *)(&double_value), sizeof(double_value));
625 #endif
626 }
627 
628 bool vpIoTools::parseBoolean(std::string input)
629 {
630  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
631  std::istringstream is(input);
632  bool b;
633  // Parse string to boolean either in the textual representation
634  // (True/False) or in numeric representation (1/0)
635  is >> (input.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
636  return b;
637 }
638 
642 std::string vpIoTools::trim(std::string s) { return ltrim(rtrim(s)); }
643 
644 END_VISP_NAMESPACE
unsigned int getCols() const
Definition: vpArray2D.h:337
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
unsigned int getRows() const
Definition: vpArray2D.h:347
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static vpColor getColor(const unsigned int &i)
Definition: vpColor.h:311
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
static std::vector< std::string > configVars
Definition: vpIoTools.h:599
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1005
static bool readConfigVar(const std::string &var, float &value)
static void setBaseName(const std::string &s)
static void setBaseDir(const std::string &dir)
static std::string baseDir
Definition: vpIoTools.h:597
static bool loadConfigFile(const std::string &confFile)
static bool copy(const std::string &src, const std::string &dst)
Definition: vpIoTools.cpp:828
static std::string trim(std::string s)
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
static void saveConfigFile(const bool &actuallySave=true)
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static bool parseBoolean(std::string input)
static std::string getFullName()
static void addNameElement(const std::string &strTrue, const bool &cond=true, const std::string &strFalse="")
static std::string getBaseName()
static void createBaseNamePath(const bool &empty=false)
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static std::string configFile
Definition: vpIoTools.h:598
static std::vector< std::string > configValues
Definition: vpIoTools.h:600
static void writeBinaryValueLE(std::ofstream &file, const int16_t short_value)
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:921
static std::string baseName
Definition: vpIoTools.h:596
static const char separator
Definition: vpIoTools.h:532
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:262
VISP_EXPORT float swapFloat(float f)
Definition: vpEndian.cpp:77
VISP_EXPORT uint32_t swap32bits(uint32_t val)
Definition: vpEndian.cpp:61
VISP_EXPORT double swapDouble(double d)
Definition: vpEndian.cpp:101
VISP_EXPORT uint16_t swap16bits(uint16_t val)
Definition: vpEndian.cpp:49