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