Visual Servoing Platform  version 3.2.0
vpIoTools.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Directory management.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
43 #include <algorithm>
44 #include <cmath>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <fstream>
48 #include <limits>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include <visp3/core/vpDebug.h>
55 #include <visp3/core/vpIoException.h>
56 #include <visp3/core/vpIoTools.h>
57 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
58 #include <dirent.h>
59 #include <unistd.h>
60 #elif defined(_WIN32)
61 #include <direct.h>
62 #include <windows.h>
63 #endif
64 #if !defined(_WIN32)
65  #ifdef __ANDROID__
66  // Like IOS, wordexp.cpp is not defined for Android
67  #else
68  #include <wordexp.h>
69  #endif
70 #endif
71 
72 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
73 #include <TargetConditionals.h> // To detect OSX or IOS using TARGET_OS_IOS macro
74 #endif
75 
76 #ifndef PATH_MAX
77 # ifdef _MAX_PATH
78 # define PATH_MAX _MAX_PATH
79 # else
80 # define PATH_MAX 1024
81 # endif
82 #endif
83 
84 // Detect endianness of the host machine
85 // Reference: http://www.boost.org/doc/libs/1_36_0/boost/detail/endian.hpp
86 #if defined(__GLIBC__)
87 #include <endian.h>
88 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
89 #define VISP_LITTLE_ENDIAN
90 #elif (__BYTE_ORDER == __BIG_ENDIAN)
91 #define VISP_BIG_ENDIAN
92 #elif (__BYTE_ORDER == __PDP_ENDIAN)
93 // Currently not supported when reading / writing binary file
94 #define VISP_PDP_ENDIAN
95 //#error PDP endian is not supported. //Uncomment if needed/happens
96 #else
97 #error Unknown machine endianness detected.
98 #endif
99 #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) || defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
100 #define VISP_BIG_ENDIAN
101 #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) || defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
102 #define VISP_LITTLE_ENDIAN
103 #elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || \
104  defined(__hpux) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
105 
106 #define VISP_BIG_ENDIAN
107 #elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || \
108  defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || \
109  defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__ANDROID__)
110  // It appears that all Android systems are little endian.
111  // Refer https://stackoverflow.com/questions/6212951/endianness-of-android-ndk
112 #define VISP_LITTLE_ENDIAN
113 #elif defined(WINRT) // For UWP
114 // Refer https://social.msdn.microsoft.com/Forums/en-US/04c92ef9-e38e-415f-8958-ec9f7c196fd3/arm-endianess-under-windows-mobile?forum=windowsmobiledev
115 #define VISP_LITTLE_ENDIAN
116 #else
117 #error Cannot detect host machine endianness.
118 #endif
119 
120 std::string vpIoTools::baseName = "";
121 std::string vpIoTools::baseDir = "";
122 std::string vpIoTools::configFile = "";
123 std::vector<std::string> vpIoTools::configVars = std::vector<std::string>();
124 std::vector<std::string> vpIoTools::configValues = std::vector<std::string>();
125 
126 namespace
127 {
128 // The following code is not working on iOS since wordexp() is not available
129 // The function is not used on Android
130 #if (TARGET_OS_IOS == 0) && !defined(__ANDROID__)
131 void replaceAll(std::string &str, const std::string &search, const std::string &replace)
132 {
133  size_t start_pos = 0;
134  while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
135  str.replace(start_pos, search.length(), replace);
136  start_pos += replace.length(); // Handles case where 'replace' is a
137  // substring of 'search'
138  }
139 }
140 #endif
141 
142 #ifdef VISP_BIG_ENDIAN
143 // Swap 16 bits by shifting to the right the first byte and by shifting to the
144 // left the second byte
145 uint16_t swap16bits(const uint16_t val)
146 {
147  return (((val >> 8) & 0x00FF) | ((val << 8) & 0xFF00));
148 }
149 
150 // Swap 32 bits by shifting to the right the first 2 bytes and by shifting to
151 // the left the last 2 bytes
152 uint32_t swap32bits(const uint32_t val)
153 {
154  return (((val >> 24) & 0x000000FF) | ((val >> 8) & 0x0000FF00) | ((val << 8) & 0x00FF0000) |
155  ((val << 24) & 0xFF000000));
156 }
157 
158 // Swap a float, the union is necessary because of the representation of a
159 // float in memory in IEEE 754.
160 float swapFloat(const float f)
161 {
162  union {
163  float f;
164  unsigned char b[4];
165  } dat1, dat2;
166 
167  dat1.f = f;
168  dat2.b[0] = dat1.b[3];
169  dat2.b[1] = dat1.b[2];
170  dat2.b[2] = dat1.b[1];
171  dat2.b[3] = dat1.b[0];
172  return dat2.f;
173 }
174 
175 // Swap a double, the union is necessary because of the representation of a
176 // double in memory in IEEE 754.
177 double swapDouble(const double d)
178 {
179  union {
180  double d;
181  unsigned char b[8];
182  } dat1, dat2;
183 
184  dat1.d = d;
185  dat2.b[0] = dat1.b[7];
186  dat2.b[1] = dat1.b[6];
187  dat2.b[2] = dat1.b[5];
188  dat2.b[3] = dat1.b[4];
189  dat2.b[4] = dat1.b[3];
190  dat2.b[5] = dat1.b[2];
191  dat2.b[6] = dat1.b[1];
192  dat2.b[7] = dat1.b[0];
193  return dat2.d;
194 }
195 #endif
196 }
197 
201 const std::string &vpIoTools::getBuildInformation()
202 {
203  static std::string build_info =
204 #include "version_string.inc"
205  ;
206  return build_info;
207 }
208 
214 void vpIoTools::setBaseName(const std::string &s) { baseName = s; }
220 void vpIoTools::setBaseDir(const std::string &dir) { baseDir = dir + "/"; }
226 std::string vpIoTools::getBaseName() { return baseName; }
232 std::string vpIoTools::getFullName() { return baseDir + baseName; }
233 
249 void vpIoTools::getUserName(std::string &username)
250 {
251 // With MinGW, UNIX and _WIN32 are defined
252 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
253  // Get the user name.
254  char *_username = NULL;
255  _username = ::getenv("LOGNAME");
256  if (_username == NULL) {
257  username = "unknown";
258  }
259  else {
260  username = _username;
261  }
262 #elif defined(_WIN32)
263 #if (!defined(WINRT))
264  unsigned int info_buffer_size = 1024;
265  TCHAR *infoBuf = new TCHAR[info_buffer_size];
266  DWORD bufCharCount = (DWORD)info_buffer_size;
267  // Get the user name.
268  if (!GetUserName(infoBuf, &bufCharCount)) {
269  delete[] infoBuf;
270  throw(vpIoException(vpIoException::cantGetUserName, "Cannot get the username"));
271  }
272  username = infoBuf;
273  delete[] infoBuf;
274 #else
275  // Universal platform
276  username = "unknown";
277 #endif
278 #else
279  username = "unknown";
280 #endif
281 }
300 {
301  std::string username;
302 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
303  // Get the user name.
304  char *_username = NULL;
305  _username = ::getenv("LOGNAME");
306  if (_username == NULL) {
307  vpERROR_TRACE("Cannot get the username. Check your LOGNAME environment variable");
308  throw(vpIoException(vpIoException::cantGetUserName, "Cannot get the username"));
309  }
310  username = _username;
311 #elif defined(_WIN32)
312 #if (!defined(WINRT))
313  unsigned int info_buffer_size = 1024;
314  TCHAR *infoBuf = new TCHAR[info_buffer_size];
315  DWORD bufCharCount = (DWORD)info_buffer_size;
316  // Get the user name.
317  if (!GetUserName(infoBuf, &bufCharCount)) {
318  delete[] infoBuf;
319  vpERROR_TRACE("Cannot get the username");
320  throw(vpIoException(vpIoException::cantGetUserName, "Cannot get the username"));
321  }
322  username = infoBuf;
323  delete[] infoBuf;
324 #else
325  throw(vpIoException(vpIoException::cantGetUserName, "Cannot get the username: not implemented on Universal "
326  "Windows Platform"));
327 #endif
328 #endif
329  return username;
330 }
331 
363 std::string vpIoTools::getenv(const char *env)
364 {
365 #if defined(_WIN32) && defined(WINRT)
366  throw(vpIoException(vpIoException::cantGetenv, "Cannot get the environment variable value: not "
367  "implemented on Universal Windows Platform"));
368 #else
369  std::string value;
370  // Get the environment variable value.
371  char *_value = NULL;
372  _value = ::getenv(env);
373  if (_value == NULL) {
374  throw(vpIoException(vpIoException::cantGetenv, "Cannot get the environment variable value"));
375  }
376  value = _value;
377 
378  return value;
379 #endif
380 }
381 
414 std::string vpIoTools::getenv(const std::string &env) { return (vpIoTools::getenv(env.c_str())); }
415 
425 void vpIoTools::getVersion(const std::string &version, unsigned int &major, unsigned int &minor, unsigned int &patch)
426 {
427  if (version.size() == 0) {
428  major = 0;
429  minor = 0;
430  patch = 0;
431  } else {
432  size_t major_pos = version.find('.');
433  std::string major_str = version.substr(0, major_pos);
434  major = (unsigned)atoi(major_str.c_str());
435 
436  if (major_pos != std::string::npos) {
437  size_t minor_pos = version.find('.', major_pos + 1);
438  std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
439  minor = (unsigned)atoi(minor_str.c_str());
440 
441  if (minor_pos != std::string::npos) {
442  std::string patch_str = version.substr(minor_pos + 1);
443  patch = (unsigned)atoi(patch_str.c_str());
444  } else {
445  patch = 0;
446  }
447  } else {
448  minor = 0;
449  patch = 0;
450  }
451  }
452 }
453 
468 bool vpIoTools::checkDirectory(const char *dirname)
469 {
470 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
471  struct stat stbuf;
472 #elif defined(_WIN32) && defined(__MINGW32__)
473  struct stat stbuf;
474 #elif defined(_WIN32)
475  struct _stat stbuf;
476 #endif
477 
478  if (dirname == NULL || dirname[0] == '\0') {
479  return false;
480  }
481 
482  std::string _dirname = path(dirname);
483 
484 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
485  if (stat(_dirname.c_str(), &stbuf) != 0)
486 #elif defined(_WIN32) && defined(__MINGW32__)
487  // Remove trailing separator character if any
488  // AppVeyor: Windows 6.3.9600 AMD64 ; C:/MinGW/bin/g++.exe (ver 5.3.0) ;
489  // GNU Make 3.82.90 Built for i686-pc-mingw32
490  if (!_dirname.empty() && _dirname.at(_dirname.size() - 1) == vpIoTools::separator)
491  _dirname = _dirname.substr(0, _dirname.size() - 1);
492  if (stat(_dirname.c_str(), &stbuf) != 0)
493 #elif defined(_WIN32)
494  if (_stat(_dirname.c_str(), &stbuf) != 0)
495 #endif
496  {
497  return false;
498  }
499 #if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
500  if ((stbuf.st_mode & S_IFDIR) == 0)
501 #endif
502  {
503  return false;
504  }
505 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
506  if ((stbuf.st_mode & S_IWUSR) == 0)
507 #elif defined(_WIN32)
508  if ((stbuf.st_mode & S_IWRITE) == 0)
509 #endif
510  {
511  return false;
512  }
513  return true;
514 }
515 
529 bool vpIoTools::checkDirectory(const std::string &dirname) { return vpIoTools::checkDirectory(dirname.c_str()); }
530 
531 // See:
532 // https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
533 int vpIoTools::mkdir_p(const char *path, const int mode)
534 {
535  /* Adapted from http://stackoverflow.com/a/2336245/119527 */
536  const size_t len = strlen(path);
537  char _path[PATH_MAX];
538  char *p = NULL;
539  const char sep = vpIoTools::separator;
540 
541  std::fill(_path, _path + PATH_MAX, 0);
542 
543  errno = 0;
544  if (len > sizeof(_path) - 1) {
545  errno = ENAMETOOLONG;
546  return -1;
547  }
548  /* Copy string so its mutable */
549  strcpy(_path, path);
550 
551  /* Iterate over the string */
552  for (p = _path + 1; *p; p++) { // path cannot be empty
553  if (*p == sep) {
554  /* Temporarily truncate */
555  *p = '\0';
556 
557 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
558  if (mkdir(_path, (mode_t)mode) != 0)
559 #elif defined(_WIN32)
560  (void)mode; // var not used
561  if (!checkDirectory(_path) && _mkdir(_path) != 0)
562 #endif
563  {
564  if (errno != EEXIST)
565  return -1;
566  }
567  *p = sep;
568  }
569  }
570 
571 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
572  if (mkdir(_path, (mode_t)mode) != 0)
573 #elif defined(_WIN32)
574  if (_mkdir(_path) != 0)
575 #endif
576  {
577  if (errno != EEXIST)
578  return -1;
579  }
580 
581  return 0;
582 }
583 
598 void vpIoTools::makeDirectory(const char *dirname)
599 {
600 #if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32)
601  std::cerr << "Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
602  return;
603 #endif
604 
605 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
606  struct stat stbuf;
607 #elif defined(_WIN32) && defined(__MINGW32__)
608  struct stat stbuf;
609 #elif defined(_WIN32)
610  struct _stat stbuf;
611 #endif
612 
613  if (dirname == NULL || dirname[0] == '\0') {
614  vpERROR_TRACE("invalid directory name\n");
615  throw(vpIoException(vpIoException::invalidDirectoryName, "invalid directory name"));
616  }
617 
618  std::string _dirname = path(dirname);
619 
620 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
621  if (stat(_dirname.c_str(), &stbuf) != 0)
622 #elif defined(_WIN32) && defined(__MINGW32__)
623  if (stat(_dirname.c_str(), &stbuf) != 0)
624 #elif defined(_WIN32)
625  if (_stat(_dirname.c_str(), &stbuf) != 0)
626 #endif
627  {
628  if (vpIoTools::mkdir_p(_dirname.c_str(), 0755) != 0) {
629  vpERROR_TRACE("unable to create directory '%s'\n", dirname);
630  throw(vpIoException(vpIoException::cantCreateDirectory, "unable to create directory"));
631  }
632 
633  vpDEBUG_TRACE(2, "has created directory '%s'\n", dirname);
634  }
635 
636  if (checkDirectory(dirname) == false) {
637  vpERROR_TRACE("unable to create directory '%s'\n", dirname);
638  throw(vpIoException(vpIoException::cantCreateDirectory, "unable to create directory"));
639  }
640 }
641 
654 void vpIoTools::makeDirectory(const std::string &dirname)
655 {
656  try {
657  vpIoTools::makeDirectory(dirname.c_str());
658  } catch (...) {
659  vpERROR_TRACE("unable to create directory '%s'\n", dirname.c_str());
660  throw(vpIoException(vpIoException::cantCreateDirectory, "unable to create directory"));
661  }
662 }
663 
676 bool vpIoTools::checkFilename(const char *filename)
677 {
678 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
679  struct stat stbuf;
680 #elif defined(_WIN32)
681  struct _stat stbuf;
682 #endif
683 
684  if (filename == NULL || filename[0] == '\0') {
685  return false;
686  }
687 
688  std::string _filename = path(filename);
689 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
690  if (stat(_filename.c_str(), &stbuf) != 0)
691 #elif defined(_WIN32)
692  if (_stat(_filename.c_str(), &stbuf) != 0)
693 #endif
694  {
695  return false;
696  }
697  if ((stbuf.st_mode & S_IFREG) == 0) {
698  return false;
699  }
700 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
701  if ((stbuf.st_mode & S_IRUSR) == 0)
702 #elif defined(_WIN32)
703  if ((stbuf.st_mode & S_IREAD) == 0)
704 #endif
705  {
706  return false;
707  }
708  return true;
709 }
710 
723 bool vpIoTools::checkFilename(const std::string &filename) { return vpIoTools::checkFilename(filename.c_str()); }
724 
737 bool vpIoTools::copy(const char *src, const char *dst)
738 {
739  // Check if we have to consider a file or a directory
740  if (vpIoTools::checkFilename(src)) {
741 // std::cout << "copy file: " << src << " in " << dst << std::endl;
742 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
743 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since
744  // wordexp() is not available
745  char cmd[FILENAME_MAX];
746  int ret;
747  sprintf(cmd, "cp -p %s %s", src, dst);
748  ret = system(cmd);
749  if (ret) {
750  }; // to avoid a warning
751  // std::cout << cmd << " return value: " << ret << std::endl;
752  return true;
753 #else
754  throw(vpIoException(vpException::fatalError, "Cannot copy %s in %s: not implemented on iOS Platform", src, dst));
755 #endif
756 #elif defined(_WIN32)
757 #if (!defined(WINRT))
758  char cmd[FILENAME_MAX];
759  int ret;
760  std::string src_ = vpIoTools::path(src);
761  std::string dst_ = vpIoTools::path(dst);
762  sprintf(cmd, "copy %s %s", src_.c_str(), dst_.c_str());
763  ret = system(cmd);
764  if (ret) {
765  }; // to avoid a warning
766  // std::cout << cmd << " return value: " << ret << std::endl;
767  return true;
768 #else
769  throw(vpIoException(vpException::fatalError, "Cannot copy %s in %s: not implemented on Universal Windows Platform",
770  src, dst));
771 #endif
772 #endif
773  } else if (vpIoTools::checkDirectory(src)) {
774 // std::cout << "copy directory: " << src << " in " << dst << std::endl;
775 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
776 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since
777  // wordexp() is not available
778  char cmd[FILENAME_MAX];
779  int ret;
780  sprintf(cmd, "cp -p -r %s %s", src, dst);
781  ret = system(cmd);
782  if (ret) {
783  }; // to avoid a warning
784  // std::cout << cmd << " return value: " << ret << std::endl;
785  return true;
786 #else
787  throw(vpIoException(vpException::fatalError, "Cannot copy %s in %s: not implemented on iOS Platform", src, dst));
788 #endif
789 #elif defined(_WIN32)
790 #if (!defined(WINRT))
791  char cmd[FILENAME_MAX];
792  int ret;
793  std::string src_ = vpIoTools::path(src);
794  std::string dst_ = vpIoTools::path(dst);
795  sprintf(cmd, "copy %s %s", src_.c_str(), dst_.c_str());
796  ret = system(cmd);
797  if (ret) {
798  }; // to avoid a warning
799  // std::cout << cmd << " return value: " << ret << std::endl;
800  return true;
801 #else
802  throw(vpIoException(vpException::fatalError, "Cannot copy %s in %s: not implemented on Universal Windows Platform",
803  src, dst));
804 #endif
805 #endif
806  } else {
807  std::cout << "Cannot copy: " << src << " in " << dst << std::endl;
808  return false;
809  }
810 }
823 bool vpIoTools::copy(const std::string &src, const std::string &dst)
824 {
825  return vpIoTools::copy(src.c_str(), dst.c_str());
826 }
827 
838 bool vpIoTools::remove(const char *file_or_dir)
839 {
840  // Check if we have to consider a file or a directory
841  if (vpIoTools::checkFilename(file_or_dir)) {
842  // std::cout << "remove file: " << file_or_dir << std::endl;
843  if (::remove(file_or_dir) != 0)
844  return false;
845  else
846  return true;
847  } else if (vpIoTools::checkDirectory(file_or_dir)) {
848 // std::cout << "remove directory: " << file_or_dir << std::endl;
849 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
850 #if TARGET_OS_IOS == 0 // The following code is not working on iOS since
851  // wordexp() is not available
852  char cmd[FILENAME_MAX];
853  sprintf(cmd, "rm -rf \"%s\"", file_or_dir);
854  int ret = system(cmd);
855  if (ret) {
856  }; // to avoid a warning
857  // std::cout << cmd << " return value: " << ret << std::endl;
858  return true;
859 #else
860  throw(vpIoException(vpException::fatalError, "Cannot remove %s: not implemented on iOS Platform", file_or_dir));
861 #endif
862 #elif defined(_WIN32)
863 #if (!defined(WINRT))
864  char cmd[FILENAME_MAX];
865  std::string file_or_dir_ = vpIoTools::path(file_or_dir);
866  sprintf(cmd, "rmdir /S /Q %s", file_or_dir_.c_str());
867  int ret = system(cmd);
868  if (ret) {
869  }; // to avoid a warning
870  // std::cout << cmd << " return value: " << ret << std::endl;
871  return true;
872 #else
873  throw(vpIoException(vpException::fatalError, "Cannot remove %s: not implemented on Universal Windows Platform",
874  file_or_dir));
875 #endif
876 #endif
877  } else {
878  std::cout << "Cannot remove: " << file_or_dir << std::endl;
879  return false;
880  }
881 }
893 bool vpIoTools::remove(const std::string &file_or_dir) { return vpIoTools::remove(file_or_dir.c_str()); }
894 
906 bool vpIoTools::rename(const char *oldfilename, const char *newfilename)
907 {
908  if (::rename(oldfilename, newfilename) != 0)
909  return false;
910  else
911  return true;
912 }
913 
925 bool vpIoTools::rename(const std::string &oldfilename, const std::string &newfilename)
926 {
927  return vpIoTools::rename(oldfilename.c_str(), newfilename.c_str());
928 }
929 
942 std::string vpIoTools::path(const char *pathname)
943 {
944  std::string path(pathname);
945 
946 #if defined(_WIN32)
947  for (unsigned int i = 0; i < path.length(); i++)
948  if (path[i] == '/')
949  path[i] = '\\';
950 #elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
951  for (unsigned int i = 0; i < path.length(); i++)
952  if (path[i] == '\\')
953  path[i] = '/';
954 #if TARGET_OS_IOS == 0 // The following code is not working on iOS and android since
955  // wordexp() is not available
956  #ifdef __ANDROID__
957  // Do nothing
958  #else
959  wordexp_t exp_result;
960 
961  // escape quote character
962  replaceAll(path, "'", "'\\''");
963  // add quotes to handle special characters like parentheses and spaces
964  wordexp(std::string("'" + path + "'").c_str(), &exp_result, 0);
965  path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] : "";
966  wordfree(&exp_result);
967  #endif
968 #endif
969 #endif
970 
971  return path;
972 }
973 
986 std::string vpIoTools::path(const std::string &pathname) { return path(pathname.c_str()); }
987 
996 bool vpIoTools::loadConfigFile(const std::string &confFile)
997 {
998  configFile = path(confFile);
999  configVars.clear();
1000  configValues.clear();
1001  std::ifstream confContent(configFile.c_str(), std::ios::in);
1002 
1003  if (confContent.is_open()) {
1004  std::string line, var, val;
1005  long unsigned int k;
1006  int c;
1007  std::string stop[3] = {" ", "\t", "#"};
1008  while (std::getline(confContent, line)) {
1009  if ((line.compare(0, 1, "#") != 0) && (line.size() > 2)) {
1010  try {
1011  // name of the variable
1012  k = (unsigned long)line.find(" ");
1013  var = line.substr(0, k);
1014  // look for the end of the actual value
1015  c = 200;
1016  for (unsigned i = 0; i < 3; ++i)
1017  c = vpMath::minimum(c, (int)line.find(stop[i], k + 1));
1018  if (c == -1)
1019  c = (int)line.size();
1020  long unsigned int c_ = (long unsigned int)c;
1021  val = line.substr(k + 1, c_ - k - 1);
1022  configVars.push_back(var);
1023  configValues.push_back(val);
1024  } catch (...) {
1025  }
1026  }
1027  }
1028  confContent.close();
1029  } else {
1030  return false;
1031  }
1032  return true;
1033 }
1034 
1043 bool vpIoTools::readConfigVar(const std::string &var, float &value)
1044 {
1045  bool found = false;
1046  for (unsigned int k = 0; k < configVars.size() && found == false; ++k) {
1047  if (configVars[k] == var) {
1048  if (configValues[k].compare("PI") == 0)
1049  value = (float)M_PI;
1050  else if (configValues[k].compare("PI/2") == 0)
1051  value = (float)(M_PI / 2.0);
1052  else if (configValues[k].compare("-PI/2") == 0)
1053  value = (float)(-M_PI / 2.0);
1054  else
1055  value = (float)atof(configValues[k].c_str());
1056  found = true;
1057  }
1058  }
1059  if (found == false)
1060  std::cout << var << " not found in config file" << std::endl;
1061  return found;
1062 }
1071 bool vpIoTools::readConfigVar(const std::string &var, double &value)
1072 {
1073  bool found = false;
1074  for (unsigned int k = 0; k < configVars.size() && found == false; ++k) {
1075  if (configVars[k] == var) {
1076  if (configValues[k].compare("PI") == 0)
1077  value = M_PI;
1078  else if (configValues[k].compare("PI/2") == 0)
1079  value = M_PI / 2;
1080  else if (configValues[k].compare("-PI/2") == 0)
1081  value = -M_PI / 2;
1082  else
1083  value = atof(configValues[k].c_str());
1084  found = true;
1085  }
1086  }
1087  if (found == false)
1088  std::cout << var << " not found in config file" << std::endl;
1089  return found;
1090 }
1091 
1100 bool vpIoTools::readConfigVar(const std::string &var, int &value)
1101 {
1102  bool found = false;
1103  for (unsigned int k = 0; k < configVars.size() && found == false; ++k) {
1104  if (configVars[k] == var) {
1105  value = atoi(configValues[k].c_str());
1106  found = true;
1107  }
1108  }
1109  if (found == false)
1110  std::cout << var << " not found in config file" << std::endl;
1111  return found;
1112 }
1113 
1122 bool vpIoTools::readConfigVar(const std::string &var, unsigned int &value)
1123 {
1124  int v = 0;
1125  bool found = readConfigVar(var, v);
1126  value = (unsigned int)v;
1127  return found;
1128 }
1129 
1138 bool vpIoTools::readConfigVar(const std::string &var, bool &value)
1139 {
1140  int v = 0;
1141  bool found = readConfigVar(var, v);
1142  value = (v != 0);
1143  return found;
1144 }
1145 
1154 bool vpIoTools::readConfigVar(const std::string &var, vpColor &value)
1155 {
1156  unsigned int v = 0;
1157  bool found = readConfigVar(var, v);
1158  value = vpColor::getColor(v);
1159  return found;
1160 }
1161 
1170 bool vpIoTools::readConfigVar(const std::string &var, std::string &value)
1171 {
1172  bool found = false;
1173  for (unsigned int k = 0; k < configVars.size() && found == false; ++k) {
1174  if (configVars[k] == var) {
1175  value = configValues[k];
1176  found = true;
1177  }
1178  }
1179  if (found == false)
1180  std::cout << var << " not found in config file" << std::endl;
1181  return found;
1182 }
1183 
1197 bool vpIoTools::readConfigVar(const std::string &var, vpArray2D<double> &value, const unsigned int &nCols,
1198  const unsigned int &nRows)
1199 {
1200  bool found = false;
1201  std::string nb;
1202  for (unsigned int k = 0; k < configVars.size() && found == false; ++k) {
1203  if (configVars[k] == var) {
1204  found = true;
1205  // resize or not
1206  if (nCols != 0 && nRows != 0)
1207  value.resize(nRows, nCols);
1208  size_t ind = 0, ind2;
1209  for (unsigned int i = 0; i < value.getRows(); ++i)
1210  for (unsigned int j = 0; j < value.getCols(); ++j) {
1211  ind2 = configValues[k].find(",", ind);
1212  nb = configValues[k].substr(ind, ind2 - ind);
1213  if (nb.compare("PI") == 0)
1214  value[i][j] = M_PI;
1215  else if (nb.compare("PI/2") == 0)
1216  value[i][j] = M_PI / 2;
1217  else if (nb.compare("-PI/2") == 0)
1218  value[i][j] = -M_PI / 2;
1219  else
1220  value[i][j] = atof(nb.c_str());
1221  ind = ind2 + 1;
1222  }
1223  }
1224  }
1225  if (found == false)
1226  std::cout << var << " not found in config file" << std::endl;
1227  return found;
1228 }
1229 
1230 // construct experiment filename & path
1231 
1240 void vpIoTools::addNameElement(const std::string &strTrue, const bool &cond, const std::string &strFalse)
1241 {
1242  if (cond)
1243  baseName += "_" + strTrue;
1244  else if (strFalse != "")
1245  baseName += "_" + strFalse;
1246 }
1247 
1256 void vpIoTools::addNameElement(const std::string &strTrue, const double &val)
1257 {
1258  // if(val != 0.)
1259  if (std::fabs(val) < std::numeric_limits<double>::epsilon()) {
1260  char valC[256];
1261  sprintf(valC, "%.3f", val);
1262  std::string valS(valC);
1263  baseName += "_" + strTrue + valS;
1264  }
1265 }
1266 
1275 void vpIoTools::createBaseNamePath(const bool &empty)
1276 {
1277  if (vpIoTools::checkDirectory(baseDir + baseName) == false) {
1279  std::cout << "creating directory " + baseDir + baseName << std::endl;
1280  } else {
1281  if (empty) {
1282  std::cout << "emptying directory " + baseDir + baseName << std::endl;
1284  }
1285  }
1286 }
1287 
1294 void vpIoTools::saveConfigFile(const bool &actuallySave)
1295 {
1296  if (actuallySave) {
1297  std::string dest = baseDir + "/" + baseName + "_config.txt";
1298  // file copy
1299  vpIoTools::copy(configFile, dest);
1300  }
1301 }
1302 
1318 {
1319  std::string data_path;
1320  std::string file_to_test("mbt/cube.cao");
1321  std::string filename;
1322 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
1323  // Test if visp-images-data package is u-installed (Ubuntu and Debian)
1324  data_path = "/usr/share/visp-images-data/ViSP-images";
1325  filename = data_path + "/" + file_to_test;
1326  if (vpIoTools::checkFilename(filename))
1327  return data_path;
1328  data_path = "/usr/share/visp-images-data/visp-images";
1329  filename = data_path + "/" + file_to_test;
1330  if (vpIoTools::checkFilename(filename))
1331  return data_path;
1332 #endif
1333  // Test if VISP_INPUT_IMAGE_PATH env var is set
1334  try {
1335  data_path = vpIoTools::getenv("VISP_INPUT_IMAGE_PATH");
1336  filename = data_path + "/" + file_to_test;
1337  if (vpIoTools::checkFilename(filename))
1338  return data_path;
1339  data_path = vpIoTools::getenv("VISP_INPUT_IMAGE_PATH") + "/ViSP-images";
1340  filename = data_path + "/" + file_to_test;
1341  if (vpIoTools::checkFilename(filename))
1342  return data_path;
1343  data_path = vpIoTools::getenv("VISP_INPUT_IMAGE_PATH") + "/visp-images";
1344  filename = data_path + "/" + file_to_test;
1345  if (vpIoTools::checkFilename(filename))
1346  return data_path;
1347  } catch (...) {
1348  }
1349  data_path = "";
1350  return data_path;
1351 }
1352 
1364 std::string vpIoTools::getFileExtension(const std::string &pathname, const bool checkFile)
1365 {
1366  if (checkFile && (vpIoTools::checkDirectory(pathname) || !vpIoTools::checkFilename(pathname))) {
1367  return "";
1368  }
1369 
1370 #if defined(_WIN32)
1371  std::string sep = "\\";
1372  std::string altsep = "/";
1373  std::string extsep = ".";
1374 #else
1375  // On Unix, or on the Mac
1376  std::string sep = "/";
1377  std::string altsep = "";
1378  std::string extsep = ".";
1379 #endif
1380 
1381  // Python 2.7.8 module.
1382  //# Split a path in root and extension.
1383  //# The extension is everything starting at the last dot in the last
1384  //# pathname component; the root is everything before that.
1385  //# It is always true that root + ext == p.
1386  //
1387  //# Generic implementation of splitext, to be parametrized with
1388  //# the separators
1389  // def _splitext(p, sep, altsep, extsep):
1390  // """Split the extension from a pathname.
1391  //
1392  // Extension is everything from the last dot to the end, ignoring
1393  // leading dots. Returns "(root, ext)"; ext may be empty."""
1394  //
1395  // sepIndex = p.rfind(sep)
1396  // if altsep:
1397  // altsepIndex = p.rfind(altsep)
1398  // sepIndex = max(sepIndex, altsepIndex)
1399  //
1400  // dotIndex = p.rfind(extsep)
1401  // if dotIndex > sepIndex:
1402  // # skip all leading dots
1403  // filenameIndex = sepIndex + 1
1404  // while filenameIndex < dotIndex:
1405  // if p[filenameIndex] != extsep:
1406  // return p[:dotIndex], p[dotIndex:]
1407  // filenameIndex += 1
1408  //
1409  // return p, ''
1410 
1411  int sepIndex = (int)pathname.rfind(sep);
1412  if (!altsep.empty()) {
1413  int altsepIndex = (int)pathname.rfind(altsep);
1414  sepIndex = ((std::max))(sepIndex, altsepIndex);
1415  }
1416 
1417  size_t dotIndex = pathname.rfind(extsep);
1418  if (dotIndex != std::string::npos) {
1419  // The extsep character exists
1420  if ((sepIndex != (int)std::string::npos && (int)dotIndex > sepIndex) || sepIndex == (int)std::string::npos) {
1421  if (sepIndex == (int)std::string::npos) {
1422  sepIndex = -1;
1423  }
1424  size_t filenameIndex = (size_t)(sepIndex + 1);
1425 
1426  while (filenameIndex < dotIndex) {
1427  if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1428  return pathname.substr(dotIndex);
1429  }
1430  filenameIndex++;
1431  }
1432  }
1433  }
1434 
1435  return "";
1436 }
1437 
1443 std::string vpIoTools::getName(const std::string &pathname)
1444 {
1445  if (pathname.size() > 0) {
1446  std::string convertedPathname = vpIoTools::path(pathname);
1447 
1448  size_t index = convertedPathname.find_last_of(vpIoTools::separator);
1449  if (index != std::string::npos) {
1450  return convertedPathname.substr(index + 1);
1451  }
1452 
1453  return convertedPathname;
1454  }
1455 
1456  return "";
1457 }
1458 
1465 std::string vpIoTools::getNameWE(const std::string &pathname)
1466 {
1467  std::string name = vpIoTools::getName(pathname);
1468  size_t found = name.find_last_of(".");
1469  std::string name_we = name.substr(0, found);
1470  return name_we;
1471 }
1472 
1478 std::string vpIoTools::getParent(const std::string &pathname)
1479 {
1480  if (pathname.size() > 0) {
1481  std::string convertedPathname = vpIoTools::path(pathname);
1482 
1483  size_t index = convertedPathname.find_last_of(vpIoTools::separator);
1484  if (index != std::string::npos) {
1485  return convertedPathname.substr(0, index);
1486  }
1487  }
1488 
1489  return "";
1490 }
1491 
1500 std::string vpIoTools::getAbsolutePathname(const std::string &pathname)
1501 {
1502 
1503 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
1504  std::string real_path_str = pathname;
1505  char *real_path = realpath(pathname.c_str(), NULL);
1506 
1507  if (real_path != NULL) {
1508  real_path_str = real_path;
1509  free(real_path);
1510  }
1511  return real_path_str;
1512 #elif defined(_WIN32)
1513 #if (!defined(WINRT))
1514  std::string real_path_str = pathname;
1515  DWORD retval = 0;
1516  TCHAR buffer[4096] = TEXT("");
1517 
1518  retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1519  if (retval != 0) {
1520  real_path_str = buffer;
1521  }
1522  return real_path_str;
1523 #else
1525  "Cannot get absolute path of %s: not implemented on "
1526  "Universal Windows Platform",
1527  pathname.c_str()));
1528 #endif
1529 #endif
1530 }
1531 
1542 std::string vpIoTools::createFilePath(const std::string &parent, const std::string &child)
1543 {
1544  if (child.size() == 0 && parent.size() == 0) {
1545  return "";
1546  }
1547 
1548  if (child.size() == 0) {
1549  return vpIoTools::path(parent);
1550  }
1551 
1552  if (parent.size() == 0) {
1553  return vpIoTools::path(child);
1554  }
1555 
1556  std::string convertedParent = vpIoTools::path(parent);
1557  std::string convertedChild = vpIoTools::path(child);
1558 
1559  std::stringstream ss;
1560  ss << vpIoTools::separator;
1561  std::string stringSeparator;
1562  ss >> stringSeparator;
1563 
1564  std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1565  std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1566 
1567  if (lastConvertedParentChar == stringSeparator) {
1568  convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1569  }
1570 
1571  if (firstConvertedChildChar == stringSeparator) {
1572  convertedChild = convertedChild.substr(1);
1573  }
1574 
1575  return std::string(convertedParent + vpIoTools::separator + convertedChild);
1576 }
1577 
1583 bool vpIoTools::isAbsolutePathname(const std::string &pathname)
1584 {
1585  //# Inspired by the Python 2.7.8 module.
1586  //# Return whether a path is absolute.
1587  //# Trivial in Posix, harder on the Mac or MS-DOS.
1588  //# For DOS it is absolute if it starts with a slash or backslash (current
1589  //# volume), or if a pathname after the volume letter and colon / UNC
1590  // resource # starts with a slash or backslash.
1591  //
1592  // def isabs(s):
1593  // """Test whether a path is absolute"""
1594  // s = splitdrive(s)[1]
1595  // return s != '' and s[:1] in '/\\'
1596  std::string path = splitDrive(pathname).second;
1597  return path.size() > 0 && (path.substr(0, 1) == "/" || path.substr(0, 1) == "\\");
1598 }
1599 
1607 bool vpIoTools::isSamePathname(const std::string &pathname1, const std::string &pathname2)
1608 {
1609  // Normalize path
1610  std::string path1_normalize = vpIoTools::path(pathname1);
1611  std::string path2_normalize = vpIoTools::path(pathname2);
1612 
1613  // Get absolute path
1614  path1_normalize = vpIoTools::getAbsolutePathname(path1_normalize);
1615  path2_normalize = vpIoTools::getAbsolutePathname(path2_normalize);
1616 
1617  return (path1_normalize == path2_normalize);
1618 }
1619 
1627 std::pair<std::string, std::string> vpIoTools::splitDrive(const std::string &pathname)
1628 {
1629 //# Split a path in a drive specification (a drive letter followed by a
1630 //# colon) and the path specification.
1631 //# It is always true that drivespec + pathspec == p
1632 // def splitdrive(p):
1633 // """Split a pathname into drive/UNC sharepoint and relative path
1634 // specifiers. Returns a 2-tuple (drive_or_unc, path); either part may be
1635 // empty.
1636 //
1637 // If you assign
1638 // result = splitdrive(p)
1639 // It is always true that:
1640 // result[0] + result[1] == p
1641 //
1642 // If the path contained a drive letter, drive_or_unc will contain
1643 // everything up to and including the colon. e.g. splitdrive("c:/dir")
1644 // returns ("c:", "/dir")
1645 //
1646 // If the path contained a UNC path, the drive_or_unc will contain the host
1647 // name and share up to but not including the fourth directory separator
1648 // character. e.g. splitdrive("//host/computer/dir") returns
1649 // ("//host/computer", "/dir")
1650 //
1651 // Paths cannot contain both a drive letter and a UNC path.
1652 //
1653 // """
1654 // if len(p) > 1:
1655 // normp = p.replace(altsep, sep)
1656 // if (normp[0:2] == sep*2) and (normp[2] != sep):
1657 // # is a UNC path:
1658 // # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
1659 // # \\machine\mountpoint\directory\etc\...
1660 // # directory ^^^^^^^^^^^^^^^
1661 // index = normp.find(sep, 2)
1662 // if index == -1:
1663 // return '', p
1664 // index2 = normp.find(sep, index + 1)
1665 // # a UNC path can't have two slashes in a row
1666 // # (after the initial two)
1667 // if index2 == index + 1:
1668 // return '', p
1669 // if index2 == -1:
1670 // index2 = len(p)
1671 // return p[:index2], p[index2:]
1672 // if normp[1] == ':':
1673 // return p[:2], p[2:]
1674 // return '', p
1675 
1676 // On Unix, the drive is always empty.
1677 // On the Mac, the drive is always empty (don't use the volume name -- it
1678 // doesn't have the same syntactic and semantic oddities as DOS drive
1679 // letters, such as there being a separate current directory per drive).
1680 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1681  return std::pair<std::string, std::string>("", pathname);
1682 #else
1683  const std::string sep = "\\";
1684  const std::string sepsep = "\\\\";
1685  const std::string altsep = "/";
1686 
1687  if (pathname.size() > 1) {
1688  std::string normPathname = pathname;
1689  std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1690 
1691  if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1692  // is a UNC path:
1693  // vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
1694  // \\machine\mountpoint\directory\etc\...
1695  // directory ^^^^^^^^^^^^^^^
1696  size_t index = normPathname.find(sep, 2);
1697  if (index == std::string::npos) {
1698  return std::pair<std::string, std::string>("", pathname);
1699  }
1700 
1701  size_t index2 = normPathname.find(sep, index + 1);
1702  //# a UNC path can't have two slashes in a row
1703  //# (after the initial two)
1704  if (index2 == index + 1) {
1705  return std::pair<std::string, std::string>("", pathname);
1706  }
1707 
1708  if (index2 == std::string::npos) {
1709  index2 = pathname.size();
1710  }
1711 
1712  return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1713  }
1714 
1715  if (normPathname[1] == ':') {
1716  return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1717  }
1718  }
1719 
1720  return std::pair<std::string, std::string>("", pathname);
1721 #endif
1722 }
1723 
1772 std::vector<std::string> vpIoTools::splitChain(const std::string &chain, const std::string &sep)
1773 {
1774  size_t startIndex = 0;
1775 
1776  std::string chainToSplit = chain;
1777  std::vector<std::string> subChain;
1778  size_t sepIndex = chainToSplit.find(sep);
1779 
1780  while (sepIndex != std::string::npos) {
1781  std::string sub = chainToSplit.substr(startIndex, sepIndex);
1782  if (!sub.empty())
1783  subChain.push_back(sub);
1784  chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1785 
1786  sepIndex = chainToSplit.find(sep);
1787  }
1788  if (!chainToSplit.empty())
1789  subChain.push_back(chainToSplit);
1790 
1791  return subChain;
1792 }
1793 
1801 std::vector<std::string> vpIoTools::getDirFiles(const std::string &pathname)
1802 {
1803 
1804  if (!checkDirectory(pathname)) {
1805  throw(vpIoException(vpException::fatalError, "Directory %s doesn't exist'", pathname.c_str()));
1806  }
1807  std::string dirName = path(pathname);
1808 
1809 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
1810 
1811  std::vector<std::string> files;
1812  struct dirent **list = NULL;
1813  int filesCount = scandir(dirName.c_str(), &list, NULL, NULL);
1814  if (filesCount == -1) {
1815  throw(vpIoException(vpException::fatalError, "Cannot read files of directory %s", dirName.c_str()));
1816  }
1817  for (int i = 0; i < filesCount; i++) {
1818  std::string fileName = list[i]->d_name;
1819  if (fileName != "." && fileName != "..") {
1820  files.push_back(fileName);
1821  }
1822  free(list[i]);
1823  }
1824  free(list);
1825  std::sort(files.begin(), files.end());
1826  return files;
1827 
1828 #elif defined(_WIN32)
1829 #if (!defined(WINRT))
1830 
1831  std::vector<std::string> files;
1832  std::string fileMask = dirName;
1833  fileMask.append("\\*");
1834  WIN32_FIND_DATA FindFileData;
1835  HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1836  // Directory is empty
1837  if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1838  return files;
1839  }
1840  if (hFind == INVALID_HANDLE_VALUE) {
1841  throw(vpIoException(vpException::fatalError, "Cannot read files of directory %s", dirName.c_str()));
1842  }
1843  do {
1844  std::string fileName = FindFileData.cFileName;
1845  if (fileName != "." && fileName != "..") {
1846  files.push_back(fileName);
1847  }
1848  } while (FindNextFile(hFind, &FindFileData));
1849  FindClose(hFind);
1850  std::sort(files.begin(), files.end());
1851  return files;
1852 
1853 #else
1855  "Cannot read files of directory %s: not implemented on "
1856  "Universal Windows Platform",
1857  dirName.c_str()));
1858 #endif
1859 #endif
1860 }
1861 
1865 void vpIoTools::readBinaryValueLE(std::ifstream &file, int16_t &short_value)
1866 {
1867  file.read((char *)(&short_value), sizeof(short_value));
1868 
1869 #ifdef VISP_BIG_ENDIAN
1870  // Swap bytes order from little endian to big endian
1871  short_value = swap16bits((uint16_t)short_value);
1872 #endif
1873 }
1874 
1878 void vpIoTools::readBinaryValueLE(std::ifstream &file, uint16_t &ushort_value)
1879 {
1880  file.read((char *)(&ushort_value), sizeof(ushort_value));
1881 
1882 #ifdef VISP_BIG_ENDIAN
1883  // Swap bytes order from little endian to big endian
1884  ushort_value = swap16bits(ushort_value);
1885 #endif
1886 }
1887 
1891 void vpIoTools::readBinaryValueLE(std::ifstream &file, int32_t &int_value)
1892 {
1893  file.read((char *)(&int_value), sizeof(int_value));
1894 
1895 #ifdef VISP_BIG_ENDIAN
1896  // Swap bytes order from little endian to big endian
1897  int_value = swap32bits((uint32_t)int_value);
1898 #endif
1899 }
1900 
1904 void vpIoTools::readBinaryValueLE(std::ifstream &file, uint32_t &uint_value)
1905 {
1906  file.read((char *)(&uint_value), sizeof(uint_value));
1907 
1908 #ifdef VISP_BIG_ENDIAN
1909  // Swap bytes order from little endian to big endian
1910  uint_value = swap32bits(uint_value);
1911 #endif
1912 }
1913 
1917 void vpIoTools::readBinaryValueLE(std::ifstream &file, float &float_value)
1918 {
1919  file.read((char *)(&float_value), sizeof(float_value));
1920 
1921 #ifdef VISP_BIG_ENDIAN
1922  // Swap bytes order from little endian to big endian
1923  float_value = swapFloat(float_value);
1924 #endif
1925 }
1926 
1930 void vpIoTools::readBinaryValueLE(std::ifstream &file, double &double_value)
1931 {
1932  file.read((char *)(&double_value), sizeof(double_value));
1933 
1934 #ifdef VISP_BIG_ENDIAN
1935  // Swap bytes order from little endian to big endian
1936  double_value = swapDouble(double_value);
1937 #endif
1938 }
1939 
1943 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const int16_t short_value)
1944 {
1945 #ifdef VISP_BIG_ENDIAN
1946  // Swap bytes order to little endian
1947  uint16_t swap_short = swap16bits((uint16_t)short_value);
1948  file.write((char *)(&swap_short), sizeof(swap_short));
1949 #else
1950  file.write((char *)(&short_value), sizeof(short_value));
1951 #endif
1952 }
1953 
1957 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const uint16_t ushort_value)
1958 {
1959 #ifdef VISP_BIG_ENDIAN
1960  // Swap bytes order to little endian
1961  uint16_t swap_ushort = swap16bits(ushort_value);
1962  file.write((char *)(&swap_ushort), sizeof(swap_ushort));
1963 #else
1964  file.write((char *)(&ushort_value), sizeof(ushort_value));
1965 #endif
1966 }
1967 
1971 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const int32_t int_value)
1972 {
1973 #ifdef VISP_BIG_ENDIAN
1974  // Swap bytes order to little endian
1975  uint32_t swap_int = swap32bits((uint32_t)int_value);
1976  file.write((char *)(&swap_int), sizeof(swap_int));
1977 #else
1978  file.write((char *)(&int_value), sizeof(int_value));
1979 #endif
1980 }
1981 
1985 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const uint32_t uint_value)
1986 {
1987 #ifdef VISP_BIG_ENDIAN
1988  // Swap bytes order to little endian
1989  uint32_t swap_int = swap32bits(uint_value);
1990  file.write((char *)(&swap_int), sizeof(swap_int));
1991 #else
1992  file.write((char *)(&uint_value), sizeof(uint_value));
1993 #endif
1994 }
1995 
1999 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const float float_value)
2000 {
2001 #ifdef VISP_BIG_ENDIAN
2002  // Swap bytes order to little endian
2003  float swap_float = swapFloat(float_value);
2004  file.write((char *)(&swap_float), sizeof(swap_float));
2005 #else
2006  file.write((char *)(&float_value), sizeof(float_value));
2007 #endif
2008 }
2009 
2013 void vpIoTools::writeBinaryValueLE(std::ofstream &file, const double double_value)
2014 {
2015 #ifdef VISP_BIG_ENDIAN
2016  // Swap bytes order to little endian
2017  double swap_double = swapDouble(double_value);
2018  file.write((char *)(&swap_double), sizeof(swap_double));
2019 #else
2020  file.write((char *)(&double_value), sizeof(double_value));
2021 #endif
2022 }
vpIoTools::getenv
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:363
vpIoTools::getUserName
static std::string getUserName()
Definition: vpIoTools.cpp:299
vpIoException::cantCreateDirectory
Definition: vpIoException.h:80
vpIoException::invalidDirectoryName
Definition: vpIoException.h:79
vpIoTools::addNameElement
static void addNameElement(const std::string &strTrue, const bool &cond=true, const std::string &strFalse="")
Definition: vpIoTools.cpp:1240
vpIoTools::getViSPImagesDataPath
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1317
vpIoTools::copy
static bool copy(const char *src, const char *dst)
Definition: vpIoTools.cpp:737
vpIoTools::getVersion
static void getVersion(const std::string &version, unsigned int &major, unsigned int &minor, unsigned int &patch)
Definition: vpIoTools.cpp:425
vpColor::getColor
static vpColor getColor(const unsigned int &i)
Definition: vpColor.h:248
vpIoTools::readConfigVar
static bool readConfigVar(const std::string &var, float &value)
Definition: vpIoTools.cpp:1043
vpIoTools::separator
static const char separator
Definition: vpIoTools.h:186
vpIoTools::rename
static bool rename(const char *oldfilename, const char *newfilename)
Definition: vpIoTools.cpp:906
vpIoTools::getNameWE
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1465
vpIoTools::getName
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1443
vpException::fatalError
Fatal error.
Definition: vpException.h:95
vpIoTools::checkFilename
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:676
vpIoTools::setBaseDir
static void setBaseDir(const std::string &dir)
Definition: vpIoTools.cpp:220
vpIoTools::getBuildInformation
static const std::string & getBuildInformation()
Definition: vpIoTools.cpp:200
vpIoException::cantGetenv
Definition: vpIoException.h:82
vpIoTools::createBaseNamePath
static void createBaseNamePath(const bool &empty=false)
Definition: vpIoTools.cpp:1275
vpIoTools::makeDirectory
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:598
vpIoTools::saveConfigFile
static void saveConfigFile(const bool &actuallySave=true)
Definition: vpIoTools.cpp:1294
vpIoTools::configFile
static std::string configFile
Definition: vpIoTools.h:251
vpIoTools::getDirFiles
static std::vector< std::string > getDirFiles(const std::string &dirname)
Definition: vpIoTools.cpp:1801
vpArray2D< double >
vpIoTools::writeBinaryValueLE
static void writeBinaryValueLE(std::ofstream &file, const int16_t short_value)
Definition: vpIoTools.cpp:1943
vpIoTools::remove
static bool remove(const char *filename)
Definition: vpIoTools.cpp:838
vpIoTools::readBinaryValueLE
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
Definition: vpIoTools.cpp:1865
vpArray2D::getCols
unsigned int getCols() const
Definition: vpArray2D.h:145
vpIoTools::setBaseName
static void setBaseName(const std::string &s)
Definition: vpIoTools.cpp:214
vpIoTools::configVars
static std::vector< std::string > configVars
Definition: vpIoTools.h:252
vpIoTools::isAbsolutePathname
static bool isAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1583
vpIoTools::loadConfigFile
static bool loadConfigFile(const std::string &confFile)
Definition: vpIoTools.cpp:996
vpIoTools::splitChain
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1772
vpIoTools::getFileExtension
static std::string getFileExtension(const std::string &pathname, const bool checkFile=false)
Definition: vpIoTools.cpp:1364
vpIoTools::baseName
static std::string baseName
Definition: vpIoTools.h:249
vpIoTools::mkdir_p
static int mkdir_p(const char *path, const int mode)
Definition: vpIoTools.cpp:533
vpMath::minimum
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:144
vpIoTools::createFilePath
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1542
vpIoException::cantGetUserName
Definition: vpIoException.h:81
vpIoTools::checkDirectory
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:468
vpERROR_TRACE
#define vpERROR_TRACE
Definition: vpDebug.h:392
vpIoTools::splitDrive
static std::pair< std::string, std::string > splitDrive(const std::string &pathname)
Definition: vpIoTools.cpp:1627
vpIoTools::getBaseName
static std::string getBaseName()
Definition: vpIoTools.cpp:226
vpIoException
Error that can be emited by the vpIoTools class and its derivates.
Definition: vpIoException.h:71
vpIoTools::configValues
static std::vector< std::string > configValues
Definition: vpIoTools.h:253
vpIoTools::baseDir
static std::string baseDir
Definition: vpIoTools.h:250
vpIoTools::getFullName
static std::string getFullName()
Definition: vpIoTools.cpp:232
vpArray2D::resize
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:170
vpColor
Class to define colors available for display functionnalities.
Definition: vpColor.h:119
vpIoTools::path
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:942
vpIoTools::getAbsolutePathname
static std::string getAbsolutePathname(const std::string &pathname)
Definition: vpIoTools.cpp:1500
vpIoTools::getParent
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1478
vpDEBUG_TRACE
#define vpDEBUG_TRACE
Definition: vpDebug.h:486
vpIoTools::isSamePathname
static bool isSamePathname(const std::string &pathname1, const std::string &pathname2)
Definition: vpIoTools.cpp:1607
vpArray2D::getRows
unsigned int getRows() const
Definition: vpArray2D.h:155